AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
AsyncAcceptor Class Reference

#include "AsyncAcceptor.h"

Public Types

typedef void(* AcceptCallback) (tcp::socket &&newSocket, uint32 threadIndex)
 

Public Member Functions

 AsyncAcceptor (Acore::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port)
 
template<class T >
void AsyncAccept ()
 
template<AcceptCallback acceptCallback>
void AsyncAcceptWithCallback ()
 
bool Bind ()
 
void Close ()
 
void SetSocketFactory (std::function< std::pair< tcp::socket *, uint32 >()> func)
 

Private Member Functions

std::pair< tcp::socket *, uint32DefeaultSocketFactory ()
 

Private Attributes

tcp::acceptor _acceptor
 
tcp::endpoint _endpoint
 
tcp::socket _socket
 
std::atomic< bool > _closed
 
std::function< std::pair< tcp::socket *, uint32 >()> _socketFactory
 

Detailed Description

Member Typedef Documentation

◆ AcceptCallback

typedef void(* AsyncAcceptor::AcceptCallback) (tcp::socket &&newSocket, uint32 threadIndex)

Constructor & Destructor Documentation

◆ AsyncAcceptor()

AsyncAcceptor::AsyncAcceptor ( Acore::Asio::IoContext ioContext,
std::string const &  bindIp,
uint16  port 
)
inline
41 :
42 _acceptor(ioContext), _endpoint(Acore::Net::make_address(bindIp), port),
43 _socket(ioContext), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this))
44 {
45 }
boost::asio::ip::address make_address(char const *str)
Definition: IpAddress.h:31
tcp::endpoint _endpoint
Definition: AsyncAcceptor.h:128
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:130
std::pair< tcp::socket *, uint32 > DefeaultSocketFactory()
Definition: AsyncAcceptor.h:125
std::function< std::pair< tcp::socket *, uint32 >()> _socketFactory
Definition: AsyncAcceptor.h:131
tcp::socket _socket
Definition: AsyncAcceptor.h:129
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:127

Member Function Documentation

◆ AsyncAccept()

template<class T >
void AsyncAcceptor::AsyncAccept
136{
137 _acceptor.async_accept(_socket, [this](boost::system::error_code error)
138 {
139 if (!error)
140 {
141 try
142 {
143 // this-> is required here to fix an segmentation fault in gcc 4.7.2 - reason is lambdas in a templated class
144 std::make_shared<T>(std::move(this->_socket))->Start();
145 }
146 catch (boost::system::system_error const& err)
147 {
148 LOG_INFO("network", "Failed to retrieve client's remote address {}", err.what());
149 }
150 }
151
152 // lets slap some more this-> on this so we can fix this bug with gcc 4.7.2 throwing internals in yo face
153 if (!_closed)
154 this->AsyncAccept<T>();
155 });
156}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165

References _acceptor, _closed, _socket, and LOG_INFO.

◆ AsyncAcceptWithCallback()

template<AcceptCallback acceptCallback>
void AsyncAcceptor::AsyncAcceptWithCallback ( )
inline
52 {
53 tcp::socket* socket;
54 uint32 threadIndex;
55 std::tie(socket, threadIndex) = _socketFactory();
56 _acceptor.async_accept(*socket, [this, socket, threadIndex](boost::system::error_code error)
57 {
58 if (!error)
59 {
60 try
61 {
62 socket->non_blocking(true);
63
64 acceptCallback(std::move(*socket), threadIndex);
65 }
66 catch (boost::system::system_error const& err)
67 {
68 LOG_INFO("network", "Failed to initialize client's socket {}", err.what());
69 }
70 }
71
72 if (!_closed)
73 this->AsyncAcceptWithCallback<acceptCallback>();
74 });
75 }
std::uint32_t uint32
Definition: Define.h:108

References _acceptor, _closed, _socketFactory, and LOG_INFO.

◆ Bind()

bool AsyncAcceptor::Bind ( )
inline
78 {
79 boost::system::error_code errorCode;
80 _acceptor.open(_endpoint.protocol(), errorCode);
81 if (errorCode)
82 {
83 LOG_INFO("network", "Failed to open acceptor {}", errorCode.message());
84 return false;
85 }
86
87#if AC_PLATFORM != AC_PLATFORM_WINDOWS
88 _acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true), errorCode);
89 if (errorCode)
90 {
91 LOG_INFO("network", "Failed to set reuse_address option on acceptor {}", errorCode.message());
92 return false;
93 }
94#endif
95
96 _acceptor.bind(_endpoint, errorCode);
97 if (errorCode)
98 {
99 LOG_INFO("network", "Could not bind to {}:{} {}", _endpoint.address().to_string(), _endpoint.port(), errorCode.message());
100 return false;
101 }
102
103 _acceptor.listen(ACORE_MAX_LISTEN_CONNECTIONS, errorCode);
104 if (errorCode)
105 {
106 LOG_INFO("network", "Failed to start listening on {}:{} {}", _endpoint.address().to_string(), _endpoint.port(), errorCode.message());
107 return false;
108 }
109
110 return true;
111 }
#define ACORE_MAX_LISTEN_CONNECTIONS
Definition: AsyncAcceptor.h:33

References _acceptor, _endpoint, ACORE_MAX_LISTEN_CONNECTIONS, and LOG_INFO.

◆ Close()

void AsyncAcceptor::Close ( )
inline
114 {
115 if (_closed.exchange(true))
116 return;
117
118 boost::system::error_code err;
119 _acceptor.close(err);
120 }

References _acceptor, and _closed.

◆ DefeaultSocketFactory()

std::pair< tcp::socket *, uint32 > AsyncAcceptor::DefeaultSocketFactory ( )
inlineprivate
125{ return std::make_pair(&_socket, 0); }

References _socket.

◆ SetSocketFactory()

void AsyncAcceptor::SetSocketFactory ( std::function< std::pair< tcp::socket *, uint32 >()>  func)
inline
122{ _socketFactory = func; }

References _socketFactory.

Member Data Documentation

◆ _acceptor

tcp::acceptor AsyncAcceptor::_acceptor
private

◆ _closed

std::atomic<bool> AsyncAcceptor::_closed
private

◆ _endpoint

tcp::endpoint AsyncAcceptor::_endpoint
private

Referenced by Bind().

◆ _socket

tcp::socket AsyncAcceptor::_socket
private

◆ _socketFactory

std::function<std::pair<tcp::socket*, uint32>()> AsyncAcceptor::_socketFactory
private