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

#include <NetworkThread.h>

Public Member Functions

 NetworkThread ()
 
virtual ~NetworkThread ()
 
void Stop ()
 
bool Start ()
 
void Wait ()
 
int32 GetConnectionCount () const
 
virtual void AddSocket (std::shared_ptr< SocketType > sock)
 
tcp::socket * GetSocketForAccept ()
 

Protected Member Functions

virtual void SocketAdded (std::shared_ptr< SocketType >)
 
virtual void SocketRemoved (std::shared_ptr< SocketType >)
 
void AddNewSockets ()
 
void Run ()
 
void Update ()
 

Private Types

typedef std::vector
< std::shared_ptr< SocketType > > 
SocketContainer
 

Private Attributes

std::atomic< int32_connections
 
std::atomic< bool_stopped
 
std::thread * _thread
 
SocketContainer _sockets
 
std::mutex _newSocketsLock
 
SocketContainer _newSockets
 
boost::asio::io_service _io_service
 
tcp::socket _acceptSocket
 
boost::asio::deadline_timer _updateTimer
 

Member Typedef Documentation

template<class SocketType>
typedef std::vector<std::shared_ptr<SocketType> > NetworkThread< SocketType >::SocketContainer
private

Constructor & Destructor Documentation

template<class SocketType>
NetworkThread< SocketType >::NetworkThread ( )
inline
40  : _connections(0), _stopped(false), _thread(nullptr),
42  {
43  }
boost::asio::io_service _io_service
Definition: NetworkThread.h:173
std::atomic< bool > _stopped
Definition: NetworkThread.h:164
std::atomic< int32 > _connections
Definition: NetworkThread.h:163
std::thread * _thread
Definition: NetworkThread.h:166
tcp::socket _acceptSocket
Definition: NetworkThread.h:174
boost::asio::deadline_timer _updateTimer
Definition: NetworkThread.h:175
template<class SocketType>
virtual NetworkThread< SocketType >::~NetworkThread ( )
inlinevirtual
46  {
47  Stop();
48  if (_thread)
49  {
50  Wait();
51  delete _thread;
52  }
53  }
void Wait()
Definition: NetworkThread.h:70
void Stop()
Definition: NetworkThread.h:55
std::thread * _thread
Definition: NetworkThread.h:166

Member Function Documentation

template<class SocketType>
void NetworkThread< SocketType >::AddNewSockets ( )
inlineprotected
100  {
101  std::lock_guard<std::mutex> lock(_newSocketsLock);
102 
103  if (_newSockets.empty())
104  return;
105 
106  for (std::shared_ptr<SocketType> sock : _newSockets)
107  {
108  if (!sock->IsOpen())
109  {
110  SocketRemoved(sock);
111  --_connections;
112  }
113  else
114  _sockets.push_back(sock);
115  }
116 
117  _newSockets.clear();
118  }
SocketContainer _newSockets
Definition: NetworkThread.h:171
SocketContainer _sockets
Definition: NetworkThread.h:168
std::atomic< int32 > _connections
Definition: NetworkThread.h:163
std::mutex _newSocketsLock
Definition: NetworkThread.h:170
virtual void SocketRemoved(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:97

+ Here is the caller graph for this function:

template<class SocketType>
virtual void NetworkThread< SocketType >::AddSocket ( std::shared_ptr< SocketType >  sock)
inlinevirtual
85  {
86  std::lock_guard<std::mutex> lock(_newSocketsLock);
87 
88  ++_connections;
89  _newSockets.push_back(sock);
90  SocketAdded(sock);
91  }
SocketContainer _newSockets
Definition: NetworkThread.h:171
std::atomic< int32 > _connections
Definition: NetworkThread.h:163
std::mutex _newSocketsLock
Definition: NetworkThread.h:170
virtual void SocketAdded(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:96
template<class SocketType>
int32 NetworkThread< SocketType >::GetConnectionCount ( ) const
inline
80  {
81  return _connections;
82  }
std::atomic< int32 > _connections
Definition: NetworkThread.h:163
template<class SocketType>
tcp::socket* NetworkThread< SocketType >::GetSocketForAccept ( )
inline
93 { return &_acceptSocket; }
tcp::socket _acceptSocket
Definition: NetworkThread.h:174
template<class SocketType>
void NetworkThread< SocketType >::Run ( )
inlineprotected
121  {
122  TC_LOG_DEBUG("misc", "Network Thread Starting");
123 
124  _updateTimer.expires_from_now(boost::posix_time::milliseconds(10));
125  _updateTimer.async_wait(std::bind(&NetworkThread<SocketType>::Update, this));
126  _io_service.run();
127 
128  TC_LOG_DEBUG("misc", "Network Thread exits");
129  _newSockets.clear();
130  _sockets.clear();
131  }
boost::asio::io_service _io_service
Definition: NetworkThread.h:173
SocketContainer _newSockets
Definition: NetworkThread.h:171
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:198
SocketContainer _sockets
Definition: NetworkThread.h:168
Definition: NetworkThread.h:37
boost::asio::deadline_timer _updateTimer
Definition: NetworkThread.h:175
float milliseconds()
Definition: units.h:92

+ Here is the caller graph for this function:

template<class SocketType>
virtual void NetworkThread< SocketType >::SocketAdded ( std::shared_ptr< SocketType >  )
inlineprotectedvirtual

Reimplemented in WorldSocketThread.

96 { }

+ Here is the caller graph for this function:

template<class SocketType>
virtual void NetworkThread< SocketType >::SocketRemoved ( std::shared_ptr< SocketType >  )
inlineprotectedvirtual

Reimplemented in WorldSocketThread.

97 { }

+ Here is the caller graph for this function:

template<class SocketType>
bool NetworkThread< SocketType >::Start ( )
inline
62  {
63  if (_thread)
64  return false;
65 
66  _thread = new std::thread(&NetworkThread::Run, this);
67  return true;
68  }
void Run()
Definition: NetworkThread.h:120
std::thread * _thread
Definition: NetworkThread.h:166
template<class SocketType>
void NetworkThread< SocketType >::Stop ( )
inline
56  {
57  _stopped = true;
58  _io_service.stop();
59  }
boost::asio::io_service _io_service
Definition: NetworkThread.h:173
std::atomic< bool > _stopped
Definition: NetworkThread.h:164

+ Here is the caller graph for this function:

template<class SocketType>
void NetworkThread< SocketType >::Update ( )
inlineprotected
134  {
135  if (_stopped)
136  return;
137 
138  _updateTimer.expires_from_now(boost::posix_time::milliseconds(10));
139  _updateTimer.async_wait(std::bind(&NetworkThread<SocketType>::Update, this));
140 
141  AddNewSockets();
142 
143  _sockets.erase(std::remove_if(_sockets.begin(), _sockets.end(), [this](std::shared_ptr<SocketType> sock)
144  {
145  if (!sock->Update())
146  {
147  if (sock->IsOpen())
148  sock->CloseSocket();
149 
150  this->SocketRemoved(sock);
151 
152  --this->_connections;
153  return true;
154  }
155 
156  return false;
157  }), _sockets.end());
158  }
std::atomic< bool > _stopped
Definition: NetworkThread.h:164
SocketContainer _sockets
Definition: NetworkThread.h:168
std::atomic< int32 > _connections
Definition: NetworkThread.h:163
Definition: NetworkThread.h:37
virtual void SocketRemoved(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:97
boost::asio::deadline_timer _updateTimer
Definition: NetworkThread.h:175
float milliseconds()
Definition: units.h:92
void AddNewSockets()
Definition: NetworkThread.h:99
template<class SocketType>
void NetworkThread< SocketType >::Wait ( )
inline
71  {
72  ASSERT(_thread);
73 
74  _thread->join();
75  delete _thread;
76  _thread = nullptr;
77  }
std::thread * _thread
Definition: NetworkThread.h:166
#define ASSERT
Definition: Errors.h:55

+ Here is the caller graph for this function:

Member Data Documentation

template<class SocketType>
tcp::socket NetworkThread< SocketType >::_acceptSocket
private
template<class SocketType>
std::atomic<int32> NetworkThread< SocketType >::_connections
private
template<class SocketType>
boost::asio::io_service NetworkThread< SocketType >::_io_service
private
template<class SocketType>
SocketContainer NetworkThread< SocketType >::_newSockets
private
template<class SocketType>
std::mutex NetworkThread< SocketType >::_newSocketsLock
private
template<class SocketType>
SocketContainer NetworkThread< SocketType >::_sockets
private
template<class SocketType>
std::atomic<bool> NetworkThread< SocketType >::_stopped
private
template<class SocketType>
std::thread* NetworkThread< SocketType >::_thread
private
template<class SocketType>
boost::asio::deadline_timer NetworkThread< SocketType >::_updateTimer
private

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