From dfd2660a85e4f0891c63009ee8425b2796586409 Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 6 Jan 2018 01:21:59 +0100 Subject: Core/Misc: Added compatibility layer for boost 1.66 and future std:: networking stuff * Based on work done by @dimiandre in PR #21173 Closes #21171 Closes #21173 --- src/common/Asio/AsioHacksFwd.h | 94 ++++++++++++++++++++++++++++++++++++++++++ src/common/Asio/IoContext.h | 65 +++++++++++++++++++++++++++++ src/common/Asio/IpAddress.h | 46 +++++++++++++++++++++ src/common/Asio/IpNetwork.h | 71 +++++++++++++++++++++++++++++++ src/common/Asio/Resolver.h | 52 +++++++++++++++++++++++ src/common/Asio/Strand.h | 53 ++++++++++++++++++++++++ 6 files changed, 381 insertions(+) create mode 100644 src/common/Asio/AsioHacksFwd.h create mode 100644 src/common/Asio/IoContext.h create mode 100644 src/common/Asio/IpAddress.h create mode 100644 src/common/Asio/IpNetwork.h create mode 100644 src/common/Asio/Resolver.h create mode 100644 src/common/Asio/Strand.h (limited to 'src/common/Asio') diff --git a/src/common/Asio/AsioHacksFwd.h b/src/common/Asio/AsioHacksFwd.h new file mode 100644 index 00000000000..457f53f39b1 --- /dev/null +++ b/src/common/Asio/AsioHacksFwd.h @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2008-2018 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef AsioHacksFwd_h__ +#define AsioHacksFwd_h__ + +#include + +/** + Collection of forward declarations to improve compile time + */ +namespace boost +{ + namespace posix_time + { + class ptime; + } + + namespace asio + { + template + struct time_traits; + + namespace ip + { + class address; + + class tcp; + + template + class basic_endpoint; + + typedef basic_endpoint tcp_endpoint; + } + +#if BOOST_VERSION >= 106600 + template + class basic_deadline_timer; + + typedef basic_deadline_timer> deadline_timer; + + namespace ip + { + template + class basic_resolver; + + typedef basic_resolver tcp_resolver; + } +#else + template + class deadline_timer_service; + + template + class basic_deadline_timer; + + typedef basic_deadline_timer, deadline_timer_service>> deadline_timer; + + namespace ip + { + template + class resolver_service; + + template + class basic_resolver; + + typedef basic_resolver> tcp_resolver; + } +#endif + } +} + +namespace Trinity +{ + namespace Asio + { + class Strand; + } +} + +#endif // AsioHacksFwd_h__ diff --git a/src/common/Asio/IoContext.h b/src/common/Asio/IoContext.h new file mode 100644 index 00000000000..50411b76957 --- /dev/null +++ b/src/common/Asio/IoContext.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2008-2018 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef IoContext_h__ +#define IoContext_h__ + +#include + +#if BOOST_VERSION >= 106600 +#include +#include +#define IoContextBaseNamespace boost::asio +#define IoContextBase io_context +#else +#include +#define IoContextBaseNamespace boost::asio +#define IoContextBase io_service +#endif + +namespace Trinity +{ + namespace Asio + { + class IoContext : public IoContextBaseNamespace::IoContextBase + { + using IoContextBaseNamespace::IoContextBase::IoContextBase; + }; + + template + inline decltype(auto) post(IoContextBaseNamespace::IoContextBase& ioContext, T&& t) + { +#if BOOST_VERSION >= 106600 + return boost::asio::post(ioContext, std::forward(t)); +#else + return ioContext.post(std::forward(t)); +#endif + } + + template + inline decltype(auto) get_io_context(T&& ioObject) + { +#if BOOST_VERSION >= 106600 + return ioObject.get_executor().context(); +#else + return ioObject.get_io_service(); +#endif + } + } +} + +#endif // IoContext_h__ diff --git a/src/common/Asio/IpAddress.h b/src/common/Asio/IpAddress.h new file mode 100644 index 00000000000..a4994012036 --- /dev/null +++ b/src/common/Asio/IpAddress.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008-2018 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef IpAddress_h__ +#define IpAddress_h__ + +#include "Define.h" +#include + +namespace Trinity +{ + namespace Net + { +#if BOOST_VERSION >= 106600 + using boost::asio::ip::make_address; + using boost::asio::ip::make_address_v4; + inline uint32 address_to_uint(boost::asio::ip::address_v4 const& address) { return address.to_uint(); } +#else + inline boost::asio::ip::address make_address(char const* str) { return boost::asio::ip::address::from_string(str); } + inline boost::asio::ip::address make_address(char const* str, boost::system::error_code& ec) { return boost::asio::ip::address::from_string(str, ec); } + inline boost::asio::ip::address make_address(std::string const& str) { return boost::asio::ip::address::from_string(str); } + inline boost::asio::ip::address make_address(std::string const& str, boost::system::error_code& ec) { return boost::asio::ip::address::from_string(str, ec); } + inline boost::asio::ip::address_v4 make_address_v4(char const* str) { return boost::asio::ip::address_v4::from_string(str); } + inline boost::asio::ip::address_v4 make_address_v4(char const* str, boost::system::error_code& ec) { return boost::asio::ip::address_v4::from_string(str, ec); } + inline boost::asio::ip::address_v4 make_address_v4(std::string const& str) { return boost::asio::ip::address_v4::from_string(str); } + inline boost::asio::ip::address_v4 make_address_v4(std::string const& str, boost::system::error_code& ec) { return boost::asio::ip::address_v4::from_string(str, ec); } + inline uint32 address_to_uint(boost::asio::ip::address_v4 const& address) { return address.to_ulong(); } +#endif + } +} + +#endif // IpAddress_h__ diff --git a/src/common/Asio/IpNetwork.h b/src/common/Asio/IpNetwork.h new file mode 100644 index 00000000000..bdd3c1dc683 --- /dev/null +++ b/src/common/Asio/IpNetwork.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2008-2018 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef IpNetwork_h__ +#define IpNetwork_h__ + +#include "Define.h" +#include "IpAddress.h" +#include +#if BOOST_VERSION >= 106600 +#include +#include +#endif + +namespace Trinity +{ + namespace Net + { + inline bool IsInNetwork(boost::asio::ip::address_v4 const& networkAddress, boost::asio::ip::address_v4 const& mask, boost::asio::ip::address_v4 const& clientAddress) + { +#if BOOST_VERSION >= 106600 + boost::asio::ip::network_v4 network = boost::asio::ip::make_network_v4(networkAddress, mask); + boost::asio::ip::address_v4_range hosts = network.hosts(); + return hosts.find(clientAddress) != hosts.end(); +#else + return (clientAddress.to_ulong() & mask.to_ulong()) == (networkAddress.to_ulong() & mask.to_ulong()); +#endif + } + + inline boost::asio::ip::address_v4 GetDefaultNetmaskV4(boost::asio::ip::address_v4 const& networkAddress) + { + if ((address_to_uint(networkAddress) & 0x80000000) == 0) + return boost::asio::ip::address_v4(0xFF000000); + if ((address_to_uint(networkAddress) & 0xC0000000) == 0x80000000) + return boost::asio::ip::address_v4(0xFFFF0000); + if ((address_to_uint(networkAddress) & 0xE0000000) == 0xC0000000) + return boost::asio::ip::address_v4(0xFFFFFF00); + return boost::asio::ip::address_v4(0xFFFFFFFF); + } + + inline bool IsInNetwork(boost::asio::ip::address_v6 const& networkAddress, uint16 prefixLength, boost::asio::ip::address_v6 const& clientAddress) + { +#if BOOST_VERSION >= 106600 + boost::asio::ip::network_v6 network = boost::asio::ip::make_network_v6(networkAddress, prefixLength); + boost::asio::ip::address_v6_range hosts = network.hosts(); + return hosts.find(clientAddress) != hosts.end(); +#else + (void)networkAddress; + (void)prefixLength; + (void)clientAddress; + return false; +#endif + } + } +} + +#endif // IpNetwork_h__ diff --git a/src/common/Asio/Resolver.h b/src/common/Asio/Resolver.h new file mode 100644 index 00000000000..5106421ee5e --- /dev/null +++ b/src/common/Asio/Resolver.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2008-2018 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef Resolver_h__ +#define Resolver_h__ + +#include "Optional.h" +#include +#include + +namespace Trinity +{ + namespace Net + { + inline Optional Resolve(boost::asio::ip::tcp::resolver& resolver, boost::asio::ip::tcp const& protocol, + std::string const& host, std::string const& service) + { + boost::system::error_code ec; +#if BOOST_VERSION >= 106600 + boost::asio::ip::tcp::resolver::results_type results = resolver.resolve(protocol, host, service, ec); + if (results.empty() || ec) + return {}; + + return results.begin()->endpoint(); +#else + boost::asio::ip::tcp::resolver::query query(std::move(protocol), std::move(host), std::move(service)); + boost::asio::ip::tcp::resolver::iterator itr = resolver.resolve(query, ec); + boost::asio::ip::tcp::resolver::iterator end; + if (itr == end || ec) + return {}; + + return itr->endpoint(); +#endif + } + } +} + +#endif // Resolver_h__ diff --git a/src/common/Asio/Strand.h b/src/common/Asio/Strand.h new file mode 100644 index 00000000000..2b69c02f7bd --- /dev/null +++ b/src/common/Asio/Strand.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2008-2018 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef Strand_h__ +#define Strand_h__ + +#include "IoContext.h" +#include + +#if BOOST_VERSION >= 106600 +#include +#endif + +namespace Trinity +{ + namespace Asio + { + /** + Hack to make it possible to forward declare strand (which is a inner class) + */ + class Strand : public IoContextBaseNamespace::IoContextBase::strand + { + public: + Strand(IoContext& ioContext) : IoContextBaseNamespace::IoContextBase::strand(ioContext) { } + }; + +#if BOOST_VERSION >= 106600 + using boost::asio::bind_executor; +#else + template + inline decltype(auto) bind_executor(Strand& strand, T&& t) + { + return strand.wrap(std::forward(t)); + } +#endif + } +} + +#endif // Strand_h__ -- cgit v1.2.3