AzeorthCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
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
 
Acore::Asio::IoContext _ioContext
 
tcp::socket _acceptSocket
 
Acore::Asio::DeadlineTimer _updateTimer
 

Detailed Description

template<class SocketType>
class NetworkThread< SocketType >

Member Typedef Documentation

◆ SocketContainer

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

Constructor & Destructor Documentation

◆ NetworkThread()

template<class SocketType >
NetworkThread< SocketType >::NetworkThread ( )
inline
41 : _connections(0), _stopped(false), _thread(nullptr), _ioContext(1),
std::thread * _thread
Definition: NetworkThread.h:166
Acore::Asio::IoContext _ioContext
Definition: NetworkThread.h:173
tcp::socket _acceptSocket
Definition: NetworkThread.h:174
Acore::Asio::DeadlineTimer _updateTimer
Definition: NetworkThread.h:175
std::atomic< bool > _stopped
Definition: NetworkThread.h:164
std::atomic< int32 > _connections
Definition: NetworkThread.h:163

◆ ~NetworkThread()

template<class SocketType >
virtual NetworkThread< SocketType >::~NetworkThread ( )
inlinevirtual
45 {
46 Stop();
47
48 if (_thread)
49 {
50 Wait();
51 delete _thread;
52 }
53 }
void Stop()
Definition: NetworkThread.h:55
void Wait()
Definition: NetworkThread.h:70

References NetworkThread< SocketType >::_thread, NetworkThread< SocketType >::Stop(), and NetworkThread< SocketType >::Wait().

Member Function Documentation

◆ AddNewSockets()

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 }
virtual void SocketRemoved(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:97
SocketContainer _sockets
Definition: NetworkThread.h:168
std::mutex _newSocketsLock
Definition: NetworkThread.h:170
SocketContainer _newSockets
Definition: NetworkThread.h:171

References NetworkThread< SocketType >::_connections, NetworkThread< SocketType >::_newSockets, NetworkThread< SocketType >::_newSocketsLock, NetworkThread< SocketType >::_sockets, and NetworkThread< SocketType >::SocketRemoved().

Referenced by NetworkThread< SocketType >::Update().

◆ AddSocket()

template<class SocketType >
virtual void NetworkThread< SocketType >::AddSocket ( std::shared_ptr< SocketType >  sock)
inlinevirtual
85 {
86 std::lock_guard<std::mutex> lock(_newSocketsLock);
87
89 _newSockets.push_back(sock);
90 SocketAdded(sock);
91 }
virtual void SocketAdded(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:96

References NetworkThread< SocketType >::_connections, NetworkThread< SocketType >::_newSockets, NetworkThread< SocketType >::_newSocketsLock, and NetworkThread< SocketType >::SocketAdded().

◆ GetConnectionCount()

template<class SocketType >
int32 NetworkThread< SocketType >::GetConnectionCount ( ) const
inline
80 {
81 return _connections;
82 }

References NetworkThread< SocketType >::_connections.

◆ GetSocketForAccept()

template<class SocketType >
tcp::socket * NetworkThread< SocketType >::GetSocketForAccept ( )
inline

◆ Run()

template<class SocketType >
void NetworkThread< SocketType >::Run ( )
inlineprotected
121 {
122 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 _ioContext.run();
127
128 LOG_DEBUG("misc", "Network Thread exits");
129 _newSockets.clear();
130 _sockets.clear();
131 }
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:171
std::size_t run()
Definition: IoContext.h:45
Definition: NetworkThread.h:39

References NetworkThread< SocketType >::_ioContext, NetworkThread< SocketType >::_newSockets, NetworkThread< SocketType >::_sockets, NetworkThread< SocketType >::_updateTimer, LOG_DEBUG, and Acore::Asio::IoContext::run().

Referenced by NetworkThread< SocketType >::Start().

◆ SocketAdded()

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

Reimplemented in WorldSocketThread.

96{ }

Referenced by NetworkThread< SocketType >::AddSocket().

◆ SocketRemoved()

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

Reimplemented in WorldSocketThread.

97{ }

Referenced by NetworkThread< SocketType >::AddNewSockets().

◆ Start()

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

References NetworkThread< SocketType >::_thread, and NetworkThread< SocketType >::Run().

◆ Stop()

template<class SocketType >
void NetworkThread< SocketType >::Stop ( )
inline

◆ Update()

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
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 }
void AddNewSockets()
Definition: NetworkThread.h:99

References NetworkThread< SocketType >::_sockets, NetworkThread< SocketType >::_stopped, NetworkThread< SocketType >::_updateTimer, and NetworkThread< SocketType >::AddNewSockets().

◆ Wait()

template<class SocketType >
void NetworkThread< SocketType >::Wait ( )
inline
71 {
73
74 _thread->join();
75 delete _thread;
76 _thread = nullptr;
77 }
#define ASSERT
Definition: Errors.h:68

References NetworkThread< SocketType >::_thread, and ASSERT.

Referenced by NetworkThread< SocketType >::~NetworkThread().

Member Data Documentation

◆ _acceptSocket

template<class SocketType >
tcp::socket NetworkThread< SocketType >::_acceptSocket
private

◆ _connections

template<class SocketType >
std::atomic<int32> NetworkThread< SocketType >::_connections
private

◆ _ioContext

template<class SocketType >
Acore::Asio::IoContext NetworkThread< SocketType >::_ioContext
private

◆ _newSockets

◆ _newSocketsLock

template<class SocketType >
std::mutex NetworkThread< SocketType >::_newSocketsLock
private

◆ _sockets

◆ _stopped

template<class SocketType >
std::atomic<bool> NetworkThread< SocketType >::_stopped
private

◆ _thread

template<class SocketType >
std::thread* NetworkThread< SocketType >::_thread
private

◆ _updateTimer

template<class SocketType >
Acore::Asio::DeadlineTimer NetworkThread< SocketType >::_updateTimer
private