#include "AsyncAcceptor.h"
◆ AcceptCallback
typedef void(* AsyncAcceptor::AcceptCallback) (tcp::socket &&newSocket, uint32 threadIndex) |
◆ AsyncAcceptor()
37 :
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().
◆ AsyncAccept()
template<class T >
void AsyncAcceptor::AsyncAccept |
132{
134 {
135 if (!error)
136 {
137 try
138 {
139
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
150 this->AsyncAccept<T>();
151 });
152}
#define LOG_INFO(filterType__,...)
Definition: Log.h:164
References _acceptor, _closed, _socket, and LOG_INFO.
◆ AsyncAcceptWithCallback()
void AsyncAcceptor::AsyncAcceptWithCallback |
( |
| ) |
|
|
inline |
48 {
49 tcp::socket* socket;
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
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;
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
93 if (errorCode)
94 {
96 return false;
97 }
98
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 {
112 return;
113
114 boost::system::error_code err;
116 }
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 |