diff options
| author | Shauren <shauren.trinity@gmail.com> | 2018-01-06 01:21:59 +0100 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2018-01-06 01:21:59 +0100 |
| commit | dfd2660a85e4f0891c63009ee8425b2796586409 (patch) | |
| tree | 26704dff3840402765ada5e6e4549a48b95ed82b /src/common/Asio | |
| parent | 76577ddc3ca4edd5943777443d9cf5a4c5314e10 (diff) | |
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
Diffstat (limited to 'src/common/Asio')
| -rw-r--r-- | src/common/Asio/AsioHacksFwd.h | 94 | ||||
| -rw-r--r-- | src/common/Asio/IoContext.h | 65 | ||||
| -rw-r--r-- | src/common/Asio/IpAddress.h | 46 | ||||
| -rw-r--r-- | src/common/Asio/IpNetwork.h | 71 | ||||
| -rw-r--r-- | src/common/Asio/Resolver.h | 52 | ||||
| -rw-r--r-- | src/common/Asio/Strand.h | 53 |
6 files changed, 381 insertions, 0 deletions
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 <https://www.trinitycore.org/> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef AsioHacksFwd_h__ +#define AsioHacksFwd_h__ + +#include <boost/version.hpp> + +/** + Collection of forward declarations to improve compile time + */ +namespace boost +{ + namespace posix_time + { + class ptime; + } + + namespace asio + { + template <typename Time> + struct time_traits; + + namespace ip + { + class address; + + class tcp; + + template <typename InternetProtocol> + class basic_endpoint; + + typedef basic_endpoint<tcp> tcp_endpoint; + } + +#if BOOST_VERSION >= 106600 + template <typename Time, typename TimeTraits> + class basic_deadline_timer; + + typedef basic_deadline_timer<posix_time::ptime, time_traits<posix_time::ptime>> deadline_timer; + + namespace ip + { + template <typename InternetProtocol> + class basic_resolver; + + typedef basic_resolver<tcp> tcp_resolver; + } +#else + template <typename TimeType, typename TimeTraits> + class deadline_timer_service; + + template <typename Time, typename TimeTraits, typename TimerService> + class basic_deadline_timer; + + typedef basic_deadline_timer<posix_time::ptime, time_traits<posix_time::ptime>, deadline_timer_service<posix_time::ptime, time_traits<posix_time::ptime>>> deadline_timer; + + namespace ip + { + template <typename InternetProtocol> + class resolver_service; + + template <typename InternetProtocol, typename ResolverService> + class basic_resolver; + + typedef basic_resolver<tcp, resolver_service<tcp>> 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 <https://www.trinitycore.org/> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef IoContext_h__ +#define IoContext_h__ + +#include <boost/version.hpp> + +#if BOOST_VERSION >= 106600 +#include <boost/asio/io_context.hpp> +#include <boost/asio/post.hpp> +#define IoContextBaseNamespace boost::asio +#define IoContextBase io_context +#else +#include <boost/asio/io_service.hpp> +#define IoContextBaseNamespace boost::asio +#define IoContextBase io_service +#endif + +namespace Trinity +{ + namespace Asio + { + class IoContext : public IoContextBaseNamespace::IoContextBase + { + using IoContextBaseNamespace::IoContextBase::IoContextBase; + }; + + template<typename T> + inline decltype(auto) post(IoContextBaseNamespace::IoContextBase& ioContext, T&& t) + { +#if BOOST_VERSION >= 106600 + return boost::asio::post(ioContext, std::forward<T>(t)); +#else + return ioContext.post(std::forward<T>(t)); +#endif + } + + template<typename T> + 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 <https://www.trinitycore.org/> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef IpAddress_h__ +#define IpAddress_h__ + +#include "Define.h" +#include <boost/asio/ip/address.hpp> + +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 <https://www.trinitycore.org/> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef IpNetwork_h__ +#define IpNetwork_h__ + +#include "Define.h" +#include "IpAddress.h" +#include <boost/version.hpp> +#if BOOST_VERSION >= 106600 +#include <boost/asio/ip/network_v4.hpp> +#include <boost/asio/ip/network_v6.hpp> +#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 <https://www.trinitycore.org/> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef Resolver_h__ +#define Resolver_h__ + +#include "Optional.h" +#include <boost/asio/ip/tcp.hpp> +#include <string> + +namespace Trinity +{ + namespace Net + { + inline Optional<boost::asio::ip::tcp::endpoint> 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 <https://www.trinitycore.org/> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef Strand_h__ +#define Strand_h__ + +#include "IoContext.h" +#include <boost/asio/strand.hpp> + +#if BOOST_VERSION >= 106600 +#include <boost/asio/bind_executor.hpp> +#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<typename T> + inline decltype(auto) bind_executor(Strand& strand, T&& t) + { + return strand.wrap(std::forward<T>(t)); + } +#endif + } +} + +#endif // Strand_h__ |
