18#ifndef __ASYNCACCEPT_H_
19#define __ASYNCACCEPT_H_
24#include <boost/asio/ip/tcp.hpp>
28#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_listen_connections
44 template<AcceptCallback acceptCallback>
49 boost::asio::ip::tcp::socket* socket = tmpSocket;
50 uint32 threadIndex = tmpThreadIndex;
51 _acceptor.async_accept(*socket, [
this, socket, threadIndex](boost::system::error_code error)
57 socket->non_blocking(
true);
59 acceptCallback(std::move(*socket), threadIndex);
61 catch (boost::system::system_error
const& err)
63 TC_LOG_INFO(
"network",
"Failed to initialize client's socket {}", err.what());
68 this->AsyncAcceptWithCallback<acceptCallback>();
74 boost::system::error_code errorCode;
78 TC_LOG_INFO(
"network",
"Failed to open acceptor {}", errorCode.message());
82#if TRINITY_PLATFORM != TRINITY_PLATFORM_WINDOWS
83 _acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(
true), errorCode);
86 TC_LOG_INFO(
"network",
"Failed to set reuse_address option on acceptor {}", errorCode.message());
113 boost::system::error_code err;
126 std::function<std::pair<boost::asio::ip::tcp::socket*, uint32>()>
_socketFactory;
139 std::make_shared<T>(std::move(this->
_socket))->Start();
141 catch (boost::system::system_error
const& err)
143 TC_LOG_INFO(
"network",
"Failed to retrieve client's remote address {}", err.what());
149 this->AsyncAccept<T>();
#define TRINITY_MAX_LISTEN_CONNECTIONS
#define TC_LOG_INFO(filterType__,...)
std::atomic< bool > _closed
boost::asio::ip::tcp::endpoint _endpoint
AsyncAcceptor(Trinity::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port)
boost::asio::ip::tcp::socket _socket
void SetSocketFactory(std::function< std::pair< boost::asio::ip::tcp::socket *, uint32 >()> func)
boost::asio::ip::tcp::acceptor _acceptor
void(* AcceptCallback)(boost::asio::ip::tcp::socket &&newSocket, uint32 threadIndex)
std::pair< boost::asio::ip::tcp::socket *, uint32 > DefeaultSocketFactory()
void AsyncAcceptWithCallback()
std::function< std::pair< boost::asio::ip::tcp::socket *, uint32 >()> _socketFactory