18#ifndef TRINITYCORE_SOCKET_MGR_H
19#define TRINITYCORE_SOCKET_MGR_H
25#include <boost/asio/ip/tcp.hpp>
30template <
typename Traits>
34 using Self =
typename Traits::Self;
52 std::unique_ptr<AsyncAcceptor> acceptor =
nullptr;
55 acceptor = std::make_unique<AsyncAcceptor>(ioContext, bindIp, port);
57 catch (boost::system::system_error
const& err)
59 TC_LOG_ERROR(
"network",
"Exception caught in SocketMgr.StartNetwork ({}:{}): {}", bindIp, port, err.what());
63 if (!acceptor->Bind())
65 TC_LOG_ERROR(
"network",
"StartNetwork failed to bind socket acceptor");
109 int32 threadIndex = 0;
111 if (
_threads[threadIndex].GetIoContext()->get_executor() == sock.get_executor())
114 std::shared_ptr<SocketType> newSocket = std::make_shared<SocketType>(std::move(sock));
117 _threads[threadIndex].AddSocket(std::move(newSocket));
119 catch (boost::system::system_error
const& err)
121 TC_LOG_WARN(
"network",
"Failed to retrieve client's remote address {}", err.what());
132 if (i->GetConnectionCount() < min->GetConnectionCount())
135 return min->GetIoContext();
#define TC_LOG_ERROR(filterType__, message__,...)
#define TC_LOG_WARN(filterType__, message__,...)
int32 GetNetworkThreadCount() const
SocketMgr & operator=(SocketMgr &&)=delete
virtual bool StartNetwork(Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int threadCount)
typename Traits::SocketType SocketType
virtual std::unique_ptr< ThreadType[]> CreateThreads() const
typename Traits::ThreadType ThreadType
virtual void StopNetwork()
virtual void OnSocketOpen(IoContextTcpSocket &&sock)
Asio::IoContext * SelectThreadWithMinConnections() const
std::unique_ptr< ThreadType[]> _threads
typename Traits::Self Self
std::unique_ptr< AsyncAcceptor > _acceptor
SocketMgr & operator=(SocketMgr const &)=delete
SocketMgr(SocketMgr &&)=delete
SocketMgr(SocketMgr const &)=delete
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket