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
36 :
37 _acceptor(ioContext), _endpoint(Acore::Net::make_address(bindIp), port),
38 _socket(ioContext), _closed(false), _socketFactory([this](){ return DefaultSocketFactory(); })
39 {
40 }
tcp::endpoint _endpoint
Definition: AsyncAcceptor.h:123
std::atomic< bool > _closed
Definition: AsyncAcceptor.h:125
std::function< std::pair< tcp::socket *, uint32 >()> _socketFactory
Definition: AsyncAcceptor.h:126
std::pair< tcp::socket *, uint32 > DefaultSocketFactory()
Definition: AsyncAcceptor.h:120
tcp::socket _socket
Definition: AsyncAcceptor.h:124
tcp::acceptor _acceptor
Definition: AsyncAcceptor.h:122

References DefaultSocketFactory().

Member Function Documentation

◆ AsyncAccept()

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

References _acceptor, _closed, _socket, and LOG_INFO.

◆ AsyncAcceptWithCallback()

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

References _acceptor, _closed, _socketFactory, and LOG_INFO.

◆ Bind()

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

References _acceptor, _endpoint, ACORE_MAX_LISTEN_CONNECTIONS, and LOG_INFO.

◆ Close()

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

References _acceptor, and _closed.

◆ DefaultSocketFactory()

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

References _socket.

Referenced by AsyncAcceptor().

◆ SetSocketFactory()

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