TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SocketMgr< SocketType > Class Template Referenceabstract

#include <SocketMgr.h>

Public Member Functions

virtual ~SocketMgr ()
 
virtual bool StartNetwork (boost::asio::io_service &service, 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
 

Constructor & Destructor Documentation

template<class SocketType>
virtual SocketMgr< SocketType >::~SocketMgr ( )
inlinevirtual
34  {
35  ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction");
36  }
int32 _threadCount
Definition: SocketMgr.h:130
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:128
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
#define ASSERT
Definition: Errors.h:55
template<class SocketType>
SocketMgr< SocketType >::SocketMgr ( )
inlineprotected
122  : _acceptor(nullptr), _threads(nullptr), _threadCount(1)
123  {
124  }
int32 _threadCount
Definition: SocketMgr.h:130
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:128
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129

Member Function Documentation

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

Implemented in WorldSocketMgr, and Battlenet::SessionManager.

+ Here is the caller graph for this function:

template<class SocketType>
int32 SocketMgr< SocketType >::GetNetworkThreadCount ( ) const
inline
102 { return _threadCount; }
int32 _threadCount
Definition: SocketMgr.h:130
template<class SocketType>
std::pair<tcp::socket*, uint32> SocketMgr< SocketType >::GetSocketForAccept ( )
inline
116  {
117  uint32 threadIndex = SelectThreadWithMinConnections();
118  return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex);
119  }
uint32 SelectThreadWithMinConnections() const
Definition: SocketMgr.h:104
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
uint32_t uint32
Definition: Define.h:150
std::pair< tcp::socket *, uint32 > GetSocketForAccept()
Definition: SocketMgr.h:115

+ Here is the caller graph for this function:

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

Reimplemented in WorldSocketMgr.

88  {
89  try
90  {
91  std::shared_ptr<SocketType> newSocket = std::make_shared<SocketType>(std::move(sock));
92  newSocket->Start();
93 
94  _threads[threadIndex].AddSocket(newSocket);
95  }
96  catch (boost::system::system_error const& err)
97  {
98  TC_LOG_WARN("network", "Failed to retrieve client's remote address %s", err.what());
99  }
100  }
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
template<class SocketType>
uint32 SocketMgr< SocketType >::SelectThreadWithMinConnections ( ) const
inline
105  {
106  uint32 min = 0;
107 
108  for (int32 i = 1; i < _threadCount; ++i)
109  if (_threads[i].GetConnectionCount() < _threads[min].GetConnectionCount())
110  min = i;
111 
112  return min;
113  }
int32 _threadCount
Definition: SocketMgr.h:130
T min(const T &x, const T &y)
Definition: g3dmath.h:305
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150

+ Here is the caller graph for this function:

template<class SocketType>
virtual bool SocketMgr< SocketType >::StartNetwork ( boost::asio::io_service &  service,
std::string const bindIp,
uint16  port,
int  threadCount 
)
inlinevirtual

Reimplemented in WorldSocketMgr, and Battlenet::SessionManager.

39  {
40  ASSERT(threadCount > 0);
41 
42  try
43  {
44  _acceptor = new AsyncAcceptor(service, bindIp, port);
45  }
46  catch (boost::system::system_error const& err)
47  {
48  TC_LOG_ERROR("network", "Exception caught in SocketMgr.StartNetwork (%s:%u): %s", bindIp.c_str(), port, err.what());
49  return false;
50  }
51 
52  _threadCount = threadCount;
54 
56 
57  for (int32 i = 0; i < _threadCount; ++i)
58  _threads[i].Start();
59 
60  return true;
61  }
int32 _threadCount
Definition: SocketMgr.h:130
Definition: AsyncAcceptor.h:28
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:128
virtual NetworkThread< SocketType > * CreateThreads() const =0
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
int32_t int32
Definition: Define.h:146
#define ASSERT
Definition: Errors.h:55
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
template<class SocketType>
virtual void SocketMgr< SocketType >::StopNetwork ( )
inlinevirtual

Reimplemented in WorldSocketMgr.

64  {
65  _acceptor->Close();
66 
67  if (_threadCount != 0)
68  for (int32 i = 0; i < _threadCount; ++i)
69  _threads[i].Stop();
70 
71  Wait();
72 
73  delete _acceptor;
74  _acceptor = nullptr;
75  delete[] _threads;
76  _threads = nullptr;
77  _threadCount = 0;
78  }
void Wait()
Definition: SocketMgr.h:80
int32 _threadCount
Definition: SocketMgr.h:130
void Close()
Definition: AsyncAcceptor.h:69
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:128
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
int32_t int32
Definition: Define.h:146
template<class SocketType>
void SocketMgr< SocketType >::Wait ( )
inline
81  {
82  if (_threadCount != 0)
83  for (int32 i = 0; i < _threadCount; ++i)
84  _threads[i].Wait();
85  }
void Wait()
Definition: SocketMgr.h:80
int32 _threadCount
Definition: SocketMgr.h:130
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
int32_t int32
Definition: Define.h:146

+ Here is the caller graph for this function:

Member Data Documentation

template<class SocketType>
AsyncAcceptor* SocketMgr< SocketType >::_acceptor
protected
template<class SocketType>
int32 SocketMgr< SocketType >::_threadCount
protected
template<class SocketType>
NetworkThread<SocketType>* SocketMgr< SocketType >::_threads
protected

The documentation for this class was generated from the following file: