18#ifndef TRINITYCORE_ASYNC_ACCEPTOR_H
19#define TRINITYCORE_ASYNC_ACCEPTOR_H
25#include <boost/asio/ip/tcp.hpp>
29using boost::asio::ip::tcp;
31#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_listen_connections
35template <
typename Callable>
36concept AcceptCallback = std::invocable<Callable, IoContextTcpSocket&&, uint32>;
47 template <AcceptCallback Callback>
53 uint32 threadIndex = tmpThreadIndex;
54 _acceptor.async_accept(*socket, [
this, socket, threadIndex, acceptCallback = std::forward<Callback>(acceptCallback)](boost::system::error_code
const& error)
mutable
60 socket->non_blocking(
true);
62 acceptCallback(std::move(*socket), threadIndex);
64 catch (boost::system::system_error
const& err)
66 TC_LOG_INFO(
"network",
"Failed to initialize client's socket {}", err.what());
77 boost::system::error_code errorCode;
81 TC_LOG_INFO(
"network",
"Failed to open acceptor {}", errorCode.message());
85#if TRINITY_PLATFORM != TRINITY_PLATFORM_WINDOWS
86 _acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(
true), errorCode);
89 TC_LOG_INFO(
"network",
"Failed to set reuse_address option on acceptor {}", errorCode.message());
116 boost::system::error_code err;
125 boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, IoContextTcpSocket::executor_type>
_acceptor;
#define TRINITY_MAX_LISTEN_CONNECTIONS
#define TC_LOG_INFO(filterType__,...)
std::atomic< bool > _closed
AsyncAcceptor(Asio::IoContext &ioContext, std::string const &bindIp, uint16 port)
void SetSocketFactory(std::function< std::pair< IoContextTcpSocket *, uint32 >()> func)
boost::asio::basic_socket_acceptor< boost::asio::ip::tcp, IoContextTcpSocket::executor_type > _acceptor
boost::asio::ip::tcp::endpoint _endpoint
std::pair< IoContextTcpSocket *, uint32 > DefeaultSocketFactory()
IoContextTcpSocket _socket
std::function< std::pair< IoContextTcpSocket *, uint32 >()> _socketFactory
void AsyncAccept(Callback &&acceptCallback)
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket