#include "AsyncAcceptor.h"
◆ AcceptCallback
typedef void(* AsyncAcceptor::AcceptCallback) (tcp::socket &&newSocket, uint32 threadIndex) |
◆ AsyncAcceptor()
36 :
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().
◆ AsyncAccept()
template<class T >
void AsyncAcceptor::AsyncAccept |
131{
133 {
134 if (!error)
135 {
136 try
137 {
138
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
149 this->AsyncAccept<T>();
150 });
151}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
References _acceptor, _closed, _socket, and LOG_INFO.
◆ AsyncAcceptWithCallback()
void AsyncAcceptor::AsyncAcceptWithCallback |
( |
| ) |
|
|
inline |
47 {
48 tcp::socket* socket;
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
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;
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
92 if (errorCode)
93 {
95 return false;
96 }
97
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 {
111 return;
112
113 boost::system::error_code err;
115 }
References _acceptor, and _closed.
◆ DefaultSocketFactory()
std::pair< tcp::socket *, uint32 > AsyncAcceptor::DefaultSocketFactory |
( |
| ) |
|
|
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 |