#include "AsyncAcceptor.h"
◆ AcceptCallback
typedef void(* AsyncAcceptor::AcceptCallback) (tcp::socket &&newSocket, uint32 threadIndex) |
◆ AsyncAcceptor()
41 :
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
◆ AsyncAccept()
template<class T >
void AsyncAcceptor::AsyncAccept |
136{
138 {
139 if (!error)
140 {
141 try
142 {
143
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
154 this->AsyncAccept<T>();
155 });
156}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
References _acceptor, _closed, _socket, and LOG_INFO.
◆ AsyncAcceptWithCallback()
void AsyncAcceptor::AsyncAcceptWithCallback |
( |
| ) |
|
|
inline |
52 {
53 tcp::socket* socket;
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
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;
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
97 if (errorCode)
98 {
100 return false;
101 }
102
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 {
116 return;
117
118 boost::system::error_code err;
120 }
References _acceptor, and _closed.
◆ DefeaultSocketFactory()
std::pair< tcp::socket *, uint32 > AsyncAcceptor::DefeaultSocketFactory |
( |
| ) |
|
|
inlineprivate |
◆ SetSocketFactory()
void AsyncAcceptor::SetSocketFactory |
( |
std::function< std::pair< tcp::socket *, uint32 >()> |
func | ) |
|
|
inline |
◆ _acceptor
tcp::acceptor AsyncAcceptor::_acceptor |
|
private |
◆ _closed
std::atomic<bool> AsyncAcceptor::_closed |
|
private |
◆ _endpoint
tcp::endpoint AsyncAcceptor::_endpoint |
|
private |
◆ _socket
tcp::socket AsyncAcceptor::_socket |
|
private |
◆ _socketFactory
std::function<std::pair<tcp::socket*, uint32>()> AsyncAcceptor::_socketFactory |
|
private |