AzerothCore 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

using SocketContainer = std::vector< std::shared_ptr< SocketType > >
 

Private Attributes

std::atomic< int32_connections {}
 
std::atomic< bool > _stopped {}
 
std::unique_ptr< 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 >
using NetworkThread< SocketType >::SocketContainer = std::vector<std::shared_ptr<SocketType> >
private

Constructor & Destructor Documentation

◆ NetworkThread()

template<class SocketType >
NetworkThread< SocketType >::NetworkThread ( )
inline
41 :
Acore::Asio::IoContext _ioContext
Definition: NetworkThread.h:177
tcp::socket _acceptSocket
Definition: NetworkThread.h:178
Acore::Asio::DeadlineTimer _updateTimer
Definition: NetworkThread.h:179

◆ ~NetworkThread()

template<class SocketType >
virtual NetworkThread< SocketType >::~NetworkThread ( )
inlinevirtual
45 {
46 Stop();
47
48 if (_thread)
49 {
50 Wait();
51 }
52 }
void Stop()
Definition: NetworkThread.h:54
void Wait()
Definition: NetworkThread.h:69
std::unique_ptr< std::thread > _thread
Definition: NetworkThread.h:170

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

Member Function Documentation

◆ AddNewSockets()

template<class SocketType >
void NetworkThread< SocketType >::AddNewSockets ( )
inlineprotected
102 {
103 std::lock_guard<std::mutex> lock(_newSocketsLock);
104
105 if (_newSockets.empty())
106 return;
107
108 for (std::shared_ptr<SocketType> sock : _newSockets)
109 {
110 if (!sock->IsOpen())
111 {
112 SocketRemoved(sock);
113 --_connections;
114 }
115 else
116 {
117 _sockets.emplace_back(sock);
118 }
119 }
120
121 _newSockets.clear();
122 }
virtual void SocketRemoved(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:99
SocketContainer _sockets
Definition: NetworkThread.h:172
std::mutex _newSocketsLock
Definition: NetworkThread.h:174
SocketContainer _newSockets
Definition: NetworkThread.h:175
std::atomic< int32 > _connections
Definition: NetworkThread.h:167

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
87 {
88 std::lock_guard<std::mutex> lock(_newSocketsLock);
89
91 _newSockets.emplace_back(sock);
92 SocketAdded(sock);
93 }
virtual void SocketAdded(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:98

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

◆ GetConnectionCount()

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

References NetworkThread< SocketType >::_connections.

◆ GetSocketForAccept()

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

◆ Run()

template<class SocketType >
void NetworkThread< SocketType >::Run ( )
inlineprotected
125 {
126 LOG_DEBUG("misc", "Network Thread Starting");
127
128 _updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
129 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
130 _ioContext.run();
131
132 LOG_DEBUG("misc", "Network Thread exits");
133 _newSockets.clear();
134 _sockets.clear();
135 }
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:169
std::size_t run()
Definition: IoContext.h:45
void Update()
Definition: NetworkThread.h:137

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

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

◆ SocketAdded()

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

Reimplemented in WorldSocketThread.

98{ }

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

◆ SocketRemoved()

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

Reimplemented in WorldSocketThread.

99{ }

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

◆ Start()

template<class SocketType >
bool NetworkThread< SocketType >::Start ( )
inline
61 {
62 if (_thread)
63 return false;
64
65 _thread = std::make_unique<std::thread>([this]() { NetworkThread::Run(); });
66 return true;
67 }
void Run()
Definition: NetworkThread.h:124

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

◆ Stop()

template<class SocketType >
void NetworkThread< SocketType >::Stop ( )
inline
55 {
56 _stopped = true;
58 }
void stop()
Definition: IoContext.h:46
std::atomic< bool > _stopped
Definition: NetworkThread.h:168

References NetworkThread< SocketType >::_ioContext, NetworkThread< SocketType >::_stopped, and Acore::Asio::IoContext::stop().

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

◆ Update()

template<class SocketType >
void NetworkThread< SocketType >::Update ( )
inlineprotected
138 {
139 if (_stopped)
140 return;
141
142 _updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
143 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
144
146
147 _sockets.erase(std::remove_if(_sockets.begin(), _sockets.end(), [this](std::shared_ptr<SocketType> sock)
148 {
149 if (!sock->Update())
150 {
151 if (sock->IsOpen())
152 sock->CloseSocket();
153
154 this->SocketRemoved(sock);
155
156 --this->_connections;
157 return true;
158 }
159
160 return false;
161 }), _sockets.end());
162 }
void AddNewSockets()
Definition: NetworkThread.h:101

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

Referenced by NetworkThread< SocketType >::Run(), and NetworkThread< SocketType >::Update().

◆ Wait()

template<class SocketType >
void NetworkThread< SocketType >::Wait ( )
inline
70 {
72
73 if (_thread->joinable())
74 {
75 _thread->join();
76 }
77
78 _thread.reset();
79 }
#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::unique_ptr<std::thread> NetworkThread< SocketType >::_thread
private

◆ _updateTimer

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