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 *, uint32DefaultSocketFactory ()
 

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
37 :
38 _acceptor(ioContext), _endpoint(Acore::Net::make_address(bindIp), port),
39 _socket(ioContext), _closed(false), _socketFactory([this](){ return DefaultSocketFactory(); })
40 {
41 }
boost::asio::ip::address make_address(char const *str)
Definition: IpAddress.h:31
tcp::endpoint _endpoint
Definition: AsyncAcceptor.h:124
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:126
std::function< std::pair< tcp::socket *, uint32 >()> _socketFactory
Definition: AsyncAcceptor.h:127
std::pair< tcp::socket *, uint32 > DefaultSocketFactory()
Definition: AsyncAcceptor.h:121
tcp::socket _socket
Definition: AsyncAcceptor.h:125
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:123

References DefaultSocketFactory().

Member Function Documentation

◆ AsyncAccept()

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

References _acceptor, _closed, _socket, and LOG_INFO.

◆ AsyncAcceptWithCallback()

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

References _acceptor, _closed, _socketFactory, and LOG_INFO.

◆ Bind()

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

References _acceptor, _endpoint, ACORE_MAX_LISTEN_CONNECTIONS, and LOG_INFO.

◆ Close()

void AsyncAcceptor::Close ( )
inline
110 {
111 if (_closed.exchange(true))
112 return;
113
114 boost::system::error_code err;
115 _acceptor.close(err);
116 }

References _acceptor, and _closed.

◆ DefaultSocketFactory()

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

References _socket.

Referenced by AsyncAcceptor().

◆ SetSocketFactory()

void AsyncAcceptor::SetSocketFactory ( std::function< std::pair< tcp::socket *, uint32 >()>  func)
inline
118{ _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