AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
SocketMgr< SocketType > Class Template Referenceabstract

#include "SocketMgr.h"

Public Member Functions

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 ()
 

Protected Member Functions

 SocketMgr ()
 
virtual NetworkThread< SocketType > * CreateThreads () const =0
 

Protected Attributes

AsyncAcceptor_acceptor
 
NetworkThread< SocketType > * _threads
 
int32 _threadCount
 

Detailed Description

template<class SocketType>
class SocketMgr< SocketType >

Constructor & Destructor Documentation

◆ ~SocketMgr()

template<class SocketType >
virtual SocketMgr< SocketType >::~SocketMgr ( )
inlinevirtual
34 {
35 ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction");
36 }
#define ASSERT
Definition: Errors.h:68
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:138
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:139
int32 _threadCount
Definition: SocketMgr.h:140

References SocketMgr< SocketType >::_acceptor, SocketMgr< SocketType >::_threadCount, SocketMgr< SocketType >::_threads, and ASSERT.

◆ SocketMgr()

template<class SocketType >
SocketMgr< SocketType >::SocketMgr ( )
inlineprotected
133 :
134 _acceptor(nullptr), _threads(nullptr), _threadCount(0) { }

Member Function Documentation

◆ CreateThreads()

template<class SocketType >
virtual NetworkThread< SocketType > * SocketMgr< SocketType >::CreateThreads ( ) const
protectedpure virtual

◆ GetNetworkThreadCount()

template<class SocketType >
int32 SocketMgr< SocketType >::GetNetworkThreadCount ( ) const
inline

◆ GetSocketForAccept()

template<class SocketType >
std::pair< tcp::socket *, uint32 > SocketMgr< SocketType >::GetSocketForAccept ( )
inline
127 {
129 return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex);
130 }
std::uint32_t uint32
Definition: Define.h:108
std::pair< tcp::socket *, uint32 > GetSocketForAccept()
Definition: SocketMgr.h:126
uint32 SelectThreadWithMinConnections() const
Definition: SocketMgr.h:115

References SocketMgr< SocketType >::_threads, SocketMgr< SocketType >::GetSocketForAccept(), and SocketMgr< SocketType >::SelectThreadWithMinConnections().

Referenced by SocketMgr< SocketType >::GetSocketForAccept(), and SocketMgr< SocketType >::StartNetwork().

◆ OnSocketOpen()

template<class SocketType >
virtual void SocketMgr< SocketType >::OnSocketOpen ( tcp::socket &&  sock,
uint32  threadIndex 
)
inlinevirtual

Reimplemented in WorldSocketMgr.

99 {
100 try
101 {
102 std::shared_ptr<SocketType> newSocket = std::make_shared<SocketType>(std::move(sock));
103 newSocket->Start();
104
105 _threads[threadIndex].AddSocket(newSocket);
106 }
107 catch (boost::system::system_error const& err)
108 {
109 LOG_WARN("network", "Failed to retrieve client's remote address {}", err.what());
110 }
111 }
#define LOG_WARN(filterType__,...)
Definition: Log.h:163

References SocketMgr< SocketType >::_threads, and LOG_WARN.

Referenced by AuthSocketMgr::OnSocketAccept().

◆ SelectThreadWithMinConnections()

template<class SocketType >
uint32 SocketMgr< SocketType >::SelectThreadWithMinConnections ( ) const
inline
116 {
117 uint32 min = 0;
118
119 for (int32 i = 1; i < _threadCount; ++i)
120 if (_threads[i].GetConnectionCount() < _threads[min].GetConnectionCount())
121 min = i;
122
123 return min;
124 }
std::int32_t int32
Definition: Define.h:104

References SocketMgr< SocketType >::_threadCount, and SocketMgr< SocketType >::_threads.

Referenced by SocketMgr< SocketType >::GetSocketForAccept().

◆ StartNetwork()

template<class SocketType >
virtual bool SocketMgr< SocketType >::StartNetwork ( Acore::Asio::IoContext ioContext,
std::string const &  bindIp,
uint16  port,
int  threadCount 
)
inlinevirtual

Reimplemented in AuthSocketMgr.

39 {
40 ASSERT(threadCount > 0);
41
42 AsyncAcceptor* acceptor = nullptr;
43 try
44 {
45 acceptor = new AsyncAcceptor(ioContext, bindIp, port);
46 }
47 catch (boost::system::system_error const& err)
48 {
49 LOG_ERROR("network", "Exception caught in SocketMgr.StartNetwork ({}:{}): {}", bindIp, port, err.what());
50 return false;
51 }
52
53 if (!acceptor->Bind())
54 {
55 LOG_ERROR("network", "StartNetwork failed to bind socket acceptor");
56 delete acceptor;
57 return false;
58 }
59
60 _acceptor = acceptor;
61 _threadCount = threadCount;
63
65
66 for (int32 i = 0; i < _threadCount; ++i)
67 _threads[i].Start();
68
69 _acceptor->SetSocketFactory([this]() { return GetSocketForAccept(); });
70
71 return true;
72 }
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
Definition: AsyncAcceptor.h:37
void SetSocketFactory(std::function< std::pair< tcp::socket *, uint32 >()> func)
Definition: AsyncAcceptor.h:122
bool Bind()
Definition: AsyncAcceptor.h:77
virtual NetworkThread< SocketType > * CreateThreads() const =0

References SocketMgr< SocketType >::_acceptor, SocketMgr< SocketType >::_threadCount, SocketMgr< SocketType >::_threads, ASSERT, AsyncAcceptor::Bind(), SocketMgr< SocketType >::CreateThreads(), SocketMgr< SocketType >::GetSocketForAccept(), LOG_ERROR, and AsyncAcceptor::SetSocketFactory().

◆ StopNetwork()

template<class SocketType >
virtual void SocketMgr< SocketType >::StopNetwork ( )
inlinevirtual

Reimplemented in WorldSocketMgr.

75 {
77
78 if (_threadCount != 0)
79 for (int32 i = 0; i < _threadCount; ++i)
80 _threads[i].Stop();
81
82 Wait();
83
84 delete _acceptor;
85 _acceptor = nullptr;
86 delete[] _threads;
87 _threads = nullptr;
88 _threadCount = 0;
89 }
void Close()
Definition: AsyncAcceptor.h:113
void Wait()
Definition: SocketMgr.h:91

References SocketMgr< SocketType >::_acceptor, SocketMgr< SocketType >::_threadCount, SocketMgr< SocketType >::_threads, AsyncAcceptor::Close(), and SocketMgr< SocketType >::Wait().

◆ Wait()

template<class SocketType >
void SocketMgr< SocketType >::Wait ( )
inline

Member Data Documentation

◆ _acceptor

template<class SocketType >
AsyncAcceptor* SocketMgr< SocketType >::_acceptor
protected

◆ _threadCount

◆ _threads