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 ()=default
 
virtual NetworkThread< SocketType > * CreateThreads () const =0
 

Protected Attributes

std::unique_ptr< AsyncAcceptor_acceptor
 
std::unique_ptr< 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
std::unique_ptr< NetworkThread< SocketType >[]> _threads
Definition: SocketMgr.h:130
int32 _threadCount
Definition: SocketMgr.h:131
std::unique_ptr< AsyncAcceptor > _acceptor
Definition: SocketMgr.h:129

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

◆ SocketMgr()

template<class SocketType >
SocketMgr< SocketType >::SocketMgr ( )
protecteddefault

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
119 {
121 return { _threads[threadIndex].GetSocketForAccept(), threadIndex };
122 }
std::uint32_t uint32
Definition: Define.h:107
uint32 SelectThreadWithMinConnections() const
Definition: SocketMgr.h:107

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

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

◆ OnSocketOpen()

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

Reimplemented in WorldSocketMgr.

93 {
94 try
95 {
96 std::shared_ptr<SocketType> newSocket = std::make_shared<SocketType>(std::move(sock));
97 _threads[threadIndex].AddSocket(newSocket);
98 }
99 catch (boost::system::system_error const& err)
100 {
101 LOG_WARN("network", "Failed to retrieve client's remote address {}", err.what());
102 }
103 }
#define LOG_WARN(filterType__,...)
Definition: Log.h:161

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

Referenced by AuthSocketMgr::OnSocketAccept().

◆ SelectThreadWithMinConnections()

template<class SocketType >
uint32 SocketMgr< SocketType >::SelectThreadWithMinConnections ( ) const
inline
108 {
109 uint32 min = 0;
110
111 for (int32 i = 1; i < _threadCount; ++i)
112 if (_threads[i].GetConnectionCount() < _threads[min].GetConnectionCount())
113 min = i;
114
115 return min;
116 }
std::int32_t int32
Definition: Define.h:103

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 std::unique_ptr<AsyncAcceptor> acceptor;
43 try
44 {
45 acceptor = std::make_unique<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 return false;
57 }
58
59 _acceptor = std::move(acceptor);
60 _threadCount = threadCount;
61 _threads = std::unique_ptr<NetworkThread<SocketType>[]>(CreateThreads());
62
64
65 for (int32 i = 0; i < _threadCount; ++i)
66 _threads[i].Start();
67
68 _acceptor->SetSocketFactory([this]() { return GetSocketForAccept(); });
69 return true;
70 }
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
std::pair< tcp::socket *, uint32 > GetSocketForAccept()
Definition: SocketMgr.h:118
virtual NetworkThread< SocketType > * CreateThreads() const =0

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

◆ StopNetwork()

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

Reimplemented in WorldSocketMgr.

73 {
74 _acceptor->Close();
75
76 for (int32 i = 0; i < _threadCount; ++i)
77 _threads[i].Stop();
78
79 Wait();
80
81 _acceptor.reset();
82 _threads.reset();
83 _threadCount = 0;
84 }
void Wait()
Definition: SocketMgr.h:86

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

◆ Wait()

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

Member Data Documentation

◆ _acceptor

template<class SocketType >
std::unique_ptr<AsyncAcceptor> SocketMgr< SocketType >::_acceptor
protected

◆ _threadCount

◆ _threads