AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
WorldSocketMgr Class Reference

Manages all sockets connected to peers and network threads. More...

#include "WorldSocketMgr.h"

Inheritance diagram for WorldSocketMgr:
SocketMgr< WorldSocket >

Public Member Functions

bool StartWorldNetwork (Acore::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int networkThreads)
 Start network, listen at address:port . More...
 
void StopNetwork () override
 Stops all network threads, It will wait for all running threads . More...
 
void OnSocketOpen (tcp::socket &&sock, uint32 threadIndex) override
 
std::size_t GetApplicationSendBufferSize () const
 
- Public Member Functions inherited from SocketMgr< WorldSocket >
virtual ~SocketMgr ()
 
virtual bool StartNetwork (Acore::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int threadCount)
 
virtual void StopNetwork ()
 
void Wait ()
 
virtual void OnSocketOpen (tcp::socket &&sock, uint32 threadIndex)
 
int32 GetNetworkThreadCount () const
 
uint32 SelectThreadWithMinConnections () const
 
std::pair< tcp::socket *, uint32GetSocketForAccept ()
 

Static Public Member Functions

static WorldSocketMgrInstance ()
 

Protected Member Functions

 WorldSocketMgr ()
 
NetworkThread< WorldSocket > * CreateThreads () const override
 
- Protected Member Functions inherited from SocketMgr< WorldSocket >
 SocketMgr ()=default
 
virtual NetworkThread< WorldSocket > * CreateThreads () const=0
 

Static Protected Member Functions

static void OnSocketAccept (tcp::socket &&sock, uint32 threadIndex)
 

Private Types

typedef SocketMgr< WorldSocketBaseSocketMgr
 

Private Attributes

int32 _socketSystemSendBufferSize
 
int32 _socketApplicationSendBufferSize
 
bool _tcpNoDelay
 

Additional Inherited Members

- Protected Attributes inherited from SocketMgr< WorldSocket >
std::unique_ptr< AsyncAcceptor_acceptor
 
std::unique_ptr< NetworkThread< WorldSocket >[]> _threads
 
int32 _threadCount
 

Detailed Description

Manages all sockets connected to peers and network threads.

Member Typedef Documentation

◆ BaseSocketMgr

Constructor & Destructor Documentation

◆ WorldSocketMgr()

WorldSocketMgr::WorldSocketMgr ( )
protected
40 :
42{
43}
int32 _socketSystemSendBufferSize
Definition: WorldSocketMgr.h:59
bool _tcpNoDelay
Definition: WorldSocketMgr.h:61
SocketMgr< WorldSocket > BaseSocketMgr
Definition: WorldSocketMgr.h:33
int32 _socketApplicationSendBufferSize
Definition: WorldSocketMgr.h:60

Member Function Documentation

◆ CreateThreads()

NetworkThread< WorldSocket > * WorldSocketMgr::CreateThreads ( ) const
overrideprotectedvirtual

Implements SocketMgr< WorldSocket >.

116{
117
119
120 bool proxyProtocolEnabled = sConfigMgr->GetOption<bool>("Network.EnableProxyProtocol", false, true);
121 if (proxyProtocolEnabled)
122 for (int i = 0; i < GetNetworkThreadCount(); i++)
123 threads[i].EnableProxyProtocol();
124
125 return threads;
126}
#define sConfigMgr
Definition: Config.h:74
Definition: WorldSocketMgr.cpp:26
Definition: NetworkThread.h:38
int32 GetNetworkThreadCount() const
Definition: SocketMgr.h:105

References SocketMgr< WorldSocket >::GetNetworkThreadCount(), and sConfigMgr.

◆ GetApplicationSendBufferSize()

std::size_t WorldSocketMgr::GetApplicationSendBufferSize ( ) const
inline

◆ Instance()

WorldSocketMgr & WorldSocketMgr::Instance ( )
static
46{
47 static WorldSocketMgr instance;
48 return instance;
49}
Manages all sockets connected to peers and network threads.
Definition: WorldSocketMgr.h:32

◆ OnSocketAccept()

static void WorldSocketMgr::OnSocketAccept ( tcp::socket &&  sock,
uint32  threadIndex 
)
inlinestaticprotected
54 {
55 Instance().OnSocketOpen(std::forward<tcp::socket>(sock), threadIndex);
56 }
void OnSocketOpen(tcp::socket &&sock, uint32 threadIndex) override
Definition: WorldSocketMgr.cpp:84
static WorldSocketMgr & Instance()
Definition: WorldSocketMgr.cpp:45

References Instance.

Referenced by StartWorldNetwork().

◆ OnSocketOpen()

void WorldSocketMgr::OnSocketOpen ( tcp::socket &&  sock,
uint32  threadIndex 
)
overridevirtual

Reimplemented from SocketMgr< WorldSocket >.

85{
86 // set some options here
88 {
89 boost::system::error_code err;
90 sock.set_option(boost::asio::socket_base::send_buffer_size(_socketSystemSendBufferSize), err);
91
92 if (err && err != boost::system::errc::not_supported)
93 {
94 LOG_ERROR("network", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::socket_base::send_buffer_size) err = {}", err.message());
95 return;
96 }
97 }
98
99 // Set TCP_NODELAY.
100 if (_tcpNoDelay)
101 {
102 boost::system::error_code err;
103 sock.set_option(boost::asio::ip::tcp::no_delay(true), err);
104
105 if (err)
106 {
107 LOG_ERROR("network", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::ip::tcp::no_delay) err = {}", err.message());
108 return;
109 }
110 }
111
112 BaseSocketMgr::OnSocketOpen(std::forward<tcp::socket>(sock), threadIndex);
113}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
virtual void OnSocketOpen(tcp::socket &&sock, uint32 threadIndex)
Definition: SocketMgr.h:92

References _socketSystemSendBufferSize, _tcpNoDelay, LOG_ERROR, and SocketMgr< WorldSocket >::OnSocketOpen().

◆ StartWorldNetwork()

bool WorldSocketMgr::StartWorldNetwork ( Acore::Asio::IoContext ioContext,
std::string const &  bindIp,
uint16  port,
int  networkThreads 
)

Start network, listen at address:port .

52{
53 _tcpNoDelay = sConfigMgr->GetOption<bool>("Network.TcpNodelay", true);
54
55 int const max_connections = ACORE_MAX_LISTEN_CONNECTIONS;
56 LOG_DEBUG("network", "Max allowed socket connections {}", max_connections);
57
58 // -1 means use default
59 _socketSystemSendBufferSize = sConfigMgr->GetOption<int32>("Network.OutKBuff", -1);
60 _socketApplicationSendBufferSize = sConfigMgr->GetOption<int32>("Network.OutUBuff", 4096);
61
63 {
64 LOG_ERROR("network", "Network.OutUBuff is wrong in your config file");
65 return false;
66 }
67
68 if (!BaseSocketMgr::StartNetwork(ioContext, bindIp, port, threadCount))
69 return false;
70
71 _acceptor->AsyncAcceptWithCallback<&WorldSocketMgr::OnSocketAccept>();
72
73 sScriptMgr->OnNetworkStart();
74 return true;
75}
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:169
std::int32_t int32
Definition: Define.h:103
constexpr auto ACORE_MAX_LISTEN_CONNECTIONS
Definition: AsyncAcceptor.h:29
#define sScriptMgr
Definition: ScriptMgr.h:709
static void OnSocketAccept(tcp::socket &&sock, uint32 threadIndex)
Definition: WorldSocketMgr.h:53
virtual bool StartNetwork(Acore::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int threadCount)
Definition: SocketMgr.h:38
std::unique_ptr< AsyncAcceptor > _acceptor
Definition: SocketMgr.h:129

References SocketMgr< WorldSocket >::_acceptor, _socketApplicationSendBufferSize, _socketSystemSendBufferSize, _tcpNoDelay, ACORE_MAX_LISTEN_CONNECTIONS, LOG_DEBUG, LOG_ERROR, OnSocketAccept(), sConfigMgr, sScriptMgr, and SocketMgr< WorldSocket >::StartNetwork().

◆ StopNetwork()

void WorldSocketMgr::StopNetwork ( )
overridevirtual

Stops all network threads, It will wait for all running threads .

Reimplemented from SocketMgr< WorldSocket >.

78{
80
81 sScriptMgr->OnNetworkStop();
82}
virtual void StopNetwork()
Definition: SocketMgr.h:72

References sScriptMgr, and SocketMgr< WorldSocket >::StopNetwork().

Member Data Documentation

◆ _socketApplicationSendBufferSize

int32 WorldSocketMgr::_socketApplicationSendBufferSize
private

Referenced by StartWorldNetwork().

◆ _socketSystemSendBufferSize

int32 WorldSocketMgr::_socketSystemSendBufferSize
private

Referenced by OnSocketOpen(), and StartWorldNetwork().

◆ _tcpNoDelay

bool WorldSocketMgr::_tcpNoDelay
private

Referenced by OnSocketOpen(), and StartWorldNetwork().