mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-20 17:27:36 +01:00
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:
@@ -18,22 +18,29 @@
|
||||
#ifndef __ASYNCACCEPT_H_
|
||||
#define __ASYNCACCEPT_H_
|
||||
|
||||
#include "IoContext.h"
|
||||
#include "IpAddress.h"
|
||||
#include "Log.h"
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
#if BOOST_VERSION >= 106600
|
||||
#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_listen_connections
|
||||
#else
|
||||
#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_connections
|
||||
#endif
|
||||
|
||||
class AsyncAcceptor
|
||||
{
|
||||
public:
|
||||
typedef void(*AcceptCallback)(tcp::socket&& newSocket, uint32 threadIndex);
|
||||
|
||||
AsyncAcceptor(boost::asio::io_service& ioService, std::string const& bindIp, uint16 port) :
|
||||
_acceptor(ioService), _endpoint(boost::asio::ip::address::from_string(bindIp), port),
|
||||
_socket(ioService), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this))
|
||||
AsyncAcceptor(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port) :
|
||||
_acceptor(ioContext), _endpoint(Trinity::Net::make_address(bindIp), port),
|
||||
_socket(ioContext), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -84,7 +91,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
_acceptor.listen(boost::asio::socket_base::max_connections, errorCode);
|
||||
_acceptor.listen(TRINITY_MAX_LISTEN_CONNECTIONS, errorCode);
|
||||
if (errorCode)
|
||||
{
|
||||
TC_LOG_INFO("network", "Failed to start listening on %s:%u %s", _endpoint.address().to_string().c_str(), _endpoint.port(), errorCode.message().c_str());
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "Define.h"
|
||||
#include "Errors.h"
|
||||
#include "IoContext.h"
|
||||
#include "Log.h"
|
||||
#include "Timer.h"
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
@@ -37,8 +38,8 @@ template<class SocketType>
|
||||
class NetworkThread
|
||||
{
|
||||
public:
|
||||
NetworkThread() : _connections(0), _stopped(false), _thread(nullptr), _io_service(1),
|
||||
_acceptSocket(_io_service), _updateTimer(_io_service)
|
||||
NetworkThread() : _connections(0), _stopped(false), _thread(nullptr), _ioContext(1),
|
||||
_acceptSocket(_ioContext), _updateTimer(_ioContext)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -55,7 +56,7 @@ public:
|
||||
void Stop()
|
||||
{
|
||||
_stopped = true;
|
||||
_io_service.stop();
|
||||
_ioContext.stop();
|
||||
}
|
||||
|
||||
bool Start()
|
||||
@@ -123,7 +124,7 @@ protected:
|
||||
|
||||
_updateTimer.expires_from_now(boost::posix_time::milliseconds(10));
|
||||
_updateTimer.async_wait(std::bind(&NetworkThread<SocketType>::Update, this));
|
||||
_io_service.run();
|
||||
_ioContext.run();
|
||||
|
||||
TC_LOG_DEBUG("misc", "Network Thread exits");
|
||||
_newSockets.clear();
|
||||
@@ -170,7 +171,7 @@ private:
|
||||
std::mutex _newSocketsLock;
|
||||
SocketContainer _newSockets;
|
||||
|
||||
boost::asio::io_service _io_service;
|
||||
Trinity::Asio::IoContext _ioContext;
|
||||
tcp::socket _acceptSocket;
|
||||
boost::asio::deadline_timer _updateTimer;
|
||||
};
|
||||
|
||||
@@ -35,14 +35,14 @@ public:
|
||||
ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction");
|
||||
}
|
||||
|
||||
virtual bool StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port, int threadCount)
|
||||
virtual bool StartNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount)
|
||||
{
|
||||
ASSERT(threadCount > 0);
|
||||
|
||||
AsyncAcceptor* acceptor = nullptr;
|
||||
try
|
||||
{
|
||||
acceptor = new AsyncAcceptor(service, bindIp, port);
|
||||
acceptor = new AsyncAcceptor(ioContext, bindIp, port);
|
||||
}
|
||||
catch (boost::system::system_error const& err)
|
||||
{
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
*/
|
||||
|
||||
#include "Realm.h"
|
||||
#include "IpAddress.h"
|
||||
#include "IpNetwork.h"
|
||||
#include "StringFormat.h"
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
@@ -47,12 +48,8 @@ boost::asio::ip::address Realm::GetAddressForClient(boost::asio::ip::address con
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clientAddr.is_v4() &&
|
||||
(clientAddr.to_v4().to_ulong() & LocalSubnetMask->to_v4().to_ulong()) ==
|
||||
(LocalAddress->to_v4().to_ulong() & LocalSubnetMask->to_v4().to_ulong()))
|
||||
{
|
||||
if (clientAddr.is_v4() && Trinity::Net::IsInNetwork(LocalAddress->to_v4(), LocalSubnetMask->to_v4(), clientAddr.to_v4()))
|
||||
realmIp = *LocalAddress;
|
||||
}
|
||||
else
|
||||
realmIp = *ExternalAddress;
|
||||
}
|
||||
|
||||
@@ -21,8 +21,10 @@
|
||||
#include "BigNumber.h"
|
||||
#include "DatabaseEnv.h"
|
||||
#include "Errors.h"
|
||||
#include "IoContext.h"
|
||||
#include "Log.h"
|
||||
#include "ProtobufJSON.h"
|
||||
#include "Resolver.h"
|
||||
#include "Util.h"
|
||||
#include "game_utilities_service.pb.h"
|
||||
#include "RealmList.pb.h"
|
||||
@@ -48,11 +50,11 @@ RealmList* RealmList::Instance()
|
||||
}
|
||||
|
||||
// Load the realm list from the database
|
||||
void RealmList::Initialize(boost::asio::io_service& ioService, uint32 updateInterval)
|
||||
void RealmList::Initialize(Trinity::Asio::IoContext& ioContext, uint32 updateInterval)
|
||||
{
|
||||
_updateInterval = updateInterval;
|
||||
_updateTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioService);
|
||||
_resolver = Trinity::make_unique<boost::asio::ip::tcp::resolver>(ioService);
|
||||
_updateTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioContext);
|
||||
_resolver = Trinity::make_unique<boost::asio::ip::tcp::resolver>(ioContext);
|
||||
|
||||
// Get the content of the realmlist table in the database
|
||||
UpdateRealms(boost::system::error_code());
|
||||
@@ -64,7 +66,7 @@ void RealmList::Close()
|
||||
}
|
||||
|
||||
void RealmList::UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint32 build, std::string const& name,
|
||||
boost::asio::ip::address const& address, boost::asio::ip::address const& localAddr, boost::asio::ip::address const& localSubmask,
|
||||
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr, boost::asio::ip::address&& localSubmask,
|
||||
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel,
|
||||
float population)
|
||||
{
|
||||
@@ -78,11 +80,11 @@ void RealmList::UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint
|
||||
realm.AllowedSecurityLevel = allowedSecurityLevel;
|
||||
realm.PopulationLevel = population;
|
||||
if (!realm.ExternalAddress || *realm.ExternalAddress != address)
|
||||
realm.ExternalAddress = Trinity::make_unique<boost::asio::ip::address>(address);
|
||||
realm.ExternalAddress = Trinity::make_unique<boost::asio::ip::address>(std::move(address));
|
||||
if (!realm.LocalAddress || *realm.LocalAddress != localAddr)
|
||||
realm.LocalAddress = Trinity::make_unique<boost::asio::ip::address>(localAddr);
|
||||
realm.LocalAddress = Trinity::make_unique<boost::asio::ip::address>(std::move(localAddr));
|
||||
if (!realm.LocalSubnetMask || *realm.LocalSubnetMask != localSubmask)
|
||||
realm.LocalSubnetMask = Trinity::make_unique<boost::asio::ip::address>(localSubmask);
|
||||
realm.LocalSubnetMask = Trinity::make_unique<boost::asio::ip::address>(std::move(localSubmask));
|
||||
realm.Port = port;
|
||||
}
|
||||
|
||||
@@ -110,43 +112,34 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::ip::tcp::resolver::iterator end;
|
||||
|
||||
Field* fields = result->Fetch();
|
||||
uint32 realmId = fields[0].GetUInt32();
|
||||
std::string name = fields[1].GetString();
|
||||
boost::asio::ip::tcp::resolver::query externalAddressQuery(boost::asio::ip::tcp::v4(), fields[2].GetString(), "");
|
||||
std::string externalAddressString = fields[2].GetString();
|
||||
std::string localAddressString = fields[3].GetString();
|
||||
std::string localSubmaskString = fields[4].GetString();
|
||||
|
||||
boost::system::error_code ec;
|
||||
boost::asio::ip::tcp::resolver::iterator endPoint = _resolver->resolve(externalAddressQuery, ec);
|
||||
if (endPoint == end || ec)
|
||||
Optional<boost::asio::ip::tcp::endpoint> externalAddress = Trinity::Net::Resolve(*_resolver, boost::asio::ip::tcp::v4(), externalAddressString, "");
|
||||
if (!externalAddress)
|
||||
{
|
||||
TC_LOG_ERROR("realmlist", "Could not resolve address %s for realm \"%s\" id %u", fields[2].GetString().c_str(), name.c_str(), realmId);
|
||||
TC_LOG_ERROR("realmlist", "Could not resolve address %s for realm \"%s\" id %u", externalAddressString.c_str(), name.c_str(), realmId);
|
||||
continue;
|
||||
}
|
||||
|
||||
boost::asio::ip::address externalAddress = endPoint->endpoint().address();
|
||||
|
||||
boost::asio::ip::tcp::resolver::query localAddressQuery(boost::asio::ip::tcp::v4(), fields[3].GetString(), "");
|
||||
endPoint = _resolver->resolve(localAddressQuery, ec);
|
||||
if (endPoint == end || ec)
|
||||
Optional<boost::asio::ip::tcp::endpoint> localAddress = Trinity::Net::Resolve(*_resolver, boost::asio::ip::tcp::v4(), localAddressString, "");
|
||||
if (!localAddress)
|
||||
{
|
||||
TC_LOG_ERROR("realmlist", "Could not resolve localAddress %s for realm \"%s\" id %u", fields[3].GetString().c_str(), name.c_str(), realmId);
|
||||
TC_LOG_ERROR("realmlist", "Could not resolve localAddress %s for realm \"%s\" id %u", localAddressString.c_str(), name.c_str(), realmId);
|
||||
continue;
|
||||
}
|
||||
|
||||
boost::asio::ip::address localAddress = endPoint->endpoint().address();
|
||||
|
||||
boost::asio::ip::tcp::resolver::query localSubmaskQuery(boost::asio::ip::tcp::v4(), fields[4].GetString(), "");
|
||||
endPoint = _resolver->resolve(localSubmaskQuery, ec);
|
||||
if (endPoint == end || ec)
|
||||
Optional<boost::asio::ip::tcp::endpoint> localSubmask = Trinity::Net::Resolve(*_resolver, boost::asio::ip::tcp::v4(), localSubmaskString, "");
|
||||
if (!localSubmask)
|
||||
{
|
||||
TC_LOG_ERROR("realmlist", "Could not resolve localSubnetMask %s for realm \"%s\" id %u", fields[4].GetString().c_str(), name.c_str(), realmId);
|
||||
TC_LOG_ERROR("realmlist", "Could not resolve localSubnetMask %s for realm \"%s\" id %u", localSubmaskString.c_str(), name.c_str(), realmId);
|
||||
continue;
|
||||
}
|
||||
|
||||
boost::asio::ip::address localSubmask = endPoint->endpoint().address();
|
||||
|
||||
uint16 port = fields[5].GetUInt16();
|
||||
uint8 icon = fields[6].GetUInt8();
|
||||
if (icon == REALM_TYPE_FFA_PVP)
|
||||
@@ -163,15 +156,15 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
|
||||
|
||||
Battlenet::RealmHandle id{ region, battlegroup, realmId };
|
||||
|
||||
UpdateRealm(newRealms[id], id, build, name, externalAddress, localAddress, localSubmask, port, icon,
|
||||
UpdateRealm(newRealms[id], id, build, name, externalAddress->address(), localAddress->address(), localSubmask->address(), port, icon,
|
||||
flag, timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);
|
||||
|
||||
newSubRegions.insert(Battlenet::RealmHandle{ region, battlegroup, 0 }.GetAddressString());
|
||||
|
||||
if (!existingRealms.count(id))
|
||||
TC_LOG_INFO("realmlist", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
|
||||
TC_LOG_INFO("realmlist", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddressString.c_str(), port);
|
||||
else
|
||||
TC_LOG_DEBUG("realmlist", "Updating realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
|
||||
TC_LOG_DEBUG("realmlist", "Updating realm \"%s\" at %s:%u.", name.c_str(), externalAddressString.c_str(), port);
|
||||
|
||||
existingRealms.erase(id);
|
||||
}
|
||||
|
||||
@@ -38,11 +38,6 @@ namespace boost
|
||||
{
|
||||
class shared_mutex;
|
||||
|
||||
namespace asio
|
||||
{
|
||||
class io_service;
|
||||
}
|
||||
|
||||
namespace system
|
||||
{
|
||||
class error_code;
|
||||
@@ -72,6 +67,14 @@ namespace JSON
|
||||
}
|
||||
}
|
||||
|
||||
namespace Trinity
|
||||
{
|
||||
namespace Asio
|
||||
{
|
||||
class IoContext;
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage object for the list of realms on the server
|
||||
class TC_SHARED_API RealmList
|
||||
{
|
||||
@@ -82,7 +85,7 @@ public:
|
||||
|
||||
~RealmList();
|
||||
|
||||
void Initialize(boost::asio::io_service& ioService, uint32 updateInterval);
|
||||
void Initialize(Trinity::Asio::IoContext& ioContext, uint32 updateInterval);
|
||||
void Close();
|
||||
|
||||
Realm const* GetRealm(Battlenet::RealmHandle const& id) const;
|
||||
@@ -99,7 +102,7 @@ private:
|
||||
|
||||
void UpdateRealms(boost::system::error_code const& error);
|
||||
void UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint32 build, std::string const& name,
|
||||
boost::asio::ip::address const& address, boost::asio::ip::address const& localAddr, boost::asio::ip::address const& localSubmask,
|
||||
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr, boost::asio::ip::address&& localSubmask,
|
||||
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population);
|
||||
|
||||
std::unique_ptr<boost::shared_mutex> _realmsMutex;
|
||||
|
||||
Reference in New Issue
Block a user