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
This commit is contained in:
Shauren
2018-01-06 01:21:59 +01:00
parent 76577ddc3c
commit dfd2660a85
33 changed files with 474 additions and 190 deletions

View File

@@ -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__

View File

@@ -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__

View File

@@ -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__

View File

@@ -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__

View File

@@ -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__

53
src/common/Asio/Strand.h Normal file
View File

@@ -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__