mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user