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 ()
 
void EnableProxyProtocol ()
 

Protected Member Functions

virtual void SocketAdded (std::shared_ptr< SocketType >)
 
virtual void SocketRemoved (std::shared_ptr< SocketType >)
 
void AddNewSockets ()
 
void HandleNewSocketsProxyReadingOnConnect ()
 
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
 
bool _proxyHeaderReadingEnabled
 

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
40 :
Acore::Asio::IoContext _ioContext
Definition: NetworkThread.h:231
tcp::socket _acceptSocket
Definition: NetworkThread.h:232
bool _proxyHeaderReadingEnabled
Definition: NetworkThread.h:235
Acore::Asio::DeadlineTimer _updateTimer
Definition: NetworkThread.h:233

◆ ~NetworkThread()

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

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

Member Function Documentation

◆ AddNewSockets()

template<class SocketType >
void NetworkThread< SocketType >::AddNewSockets ( )
inlineprotected
103 {
104 std::lock_guard<std::mutex> lock(_newSocketsLock);
105
106 if (_newSockets.empty())
107 return;
108
110 {
111 for (std::shared_ptr<SocketType> sock : _newSockets)
112 {
113 if (!sock->IsOpen())
114 {
115 SocketRemoved(sock);
116 --_connections;
117 continue;
118 }
119
120 _sockets.emplace_back(sock);
121
122 sock->Start();
123 }
124
125 _newSockets.clear();
126 }
127 else
128 {
130 }
131 }
virtual void SocketRemoved(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:100
SocketContainer _sockets
Definition: NetworkThread.h:226
void HandleNewSocketsProxyReadingOnConnect()
Definition: NetworkThread.h:133
std::mutex _newSocketsLock
Definition: NetworkThread.h:228
SocketContainer _newSockets
Definition: NetworkThread.h:229
std::atomic< int32 > _connections
Definition: NetworkThread.h:221

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

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

◆ AddSocket()

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

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

◆ EnableProxyProtocol()

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

◆ GetConnectionCount()

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

References NetworkThread< SocketType >::_connections.

◆ GetSocketForAccept()

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

◆ HandleNewSocketsProxyReadingOnConnect()

template<class SocketType >
void NetworkThread< SocketType >::HandleNewSocketsProxyReadingOnConnect ( )
inlineprotected
134 {
135 std::size_t index = 0;
136 std::vector<int> newSocketsToRemoveIndexes;
137 for (auto sock_iter = _newSockets.begin(); sock_iter != _newSockets.end(); ++sock_iter, ++index)
138 {
139 std::shared_ptr<SocketType> sock = *sock_iter;
140
141 if (!sock->IsOpen())
142 {
143 newSocketsToRemoveIndexes.emplace_back(index);
144 SocketRemoved(sock);
145 --_connections;
146 continue;
147 }
148
149 const auto proxyHeaderReadingState = sock->GetProxyHeaderReadingState();
150 if (proxyHeaderReadingState == PROXY_HEADER_READING_STATE_STARTED)
151 continue;
152
153 switch (proxyHeaderReadingState) {
155 sock->AsyncReadProxyHeader();
156 break;
157
159 newSocketsToRemoveIndexes.emplace_back(index);
160 _sockets.emplace_back(sock);
161
162 sock->Start();
163
164 break;
165
166 default:
167 newSocketsToRemoveIndexes.emplace_back(index);
168 SocketRemoved(sock);
169 --_connections;
170 break;
171 }
172 }
173
174 for (auto it = newSocketsToRemoveIndexes.rbegin(); it != newSocketsToRemoveIndexes.rend(); ++it)
175 _newSockets.erase(_newSockets.begin() + *it);
176 }
@ PROXY_HEADER_READING_STATE_NOT_STARTED
Definition: Socket.h:39
@ PROXY_HEADER_READING_STATE_FINISHED
Definition: Socket.h:41
@ PROXY_HEADER_READING_STATE_STARTED
Definition: Socket.h:40

References NetworkThread< SocketType >::_connections, NetworkThread< SocketType >::_newSockets, NetworkThread< SocketType >::_sockets, PROXY_HEADER_READING_STATE_FINISHED, PROXY_HEADER_READING_STATE_NOT_STARTED, PROXY_HEADER_READING_STATE_STARTED, and NetworkThread< SocketType >::SocketRemoved().

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

◆ Run()

template<class SocketType >
void NetworkThread< SocketType >::Run ( )
inlineprotected
179 {
180 LOG_DEBUG("misc", "Network Thread Starting");
181
182 _updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
183 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
184 _ioContext.run();
185
186 LOG_DEBUG("misc", "Network Thread exits");
187 _newSockets.clear();
188 _sockets.clear();
189 }
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:169
std::size_t run()
Definition: IoContext.h:39
void Update()
Definition: NetworkThread.h:191

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.

99{ }

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

◆ SocketRemoved()

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

◆ Start()

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

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

◆ Stop()

template<class SocketType >
void NetworkThread< SocketType >::Stop ( )
inline
54 {
55 _stopped = true;
57 }
void stop()
Definition: IoContext.h:40
std::atomic< bool > _stopped
Definition: NetworkThread.h:222

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
192 {
193 if (_stopped)
194 return;
195
196 _updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
197 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
198
200
201 _sockets.erase(std::remove_if(_sockets.begin(), _sockets.end(), [this](std::shared_ptr<SocketType> sock)
202 {
203 if (!sock->Update())
204 {
205 if (sock->IsOpen())
206 sock->CloseSocket();
207
208 this->SocketRemoved(sock);
209
210 --this->_connections;
211 return true;
212 }
213
214 return false;
215 }), _sockets.end());
216 }
void AddNewSockets()
Definition: NetworkThread.h:102

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
69 {
71
72 if (_thread->joinable())
73 {
74 _thread->join();
75 }
76
77 _thread.reset();
78 }
#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

◆ _ioContext

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

◆ _newSockets

◆ _newSocketsLock

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

◆ _proxyHeaderReadingEnabled

template<class SocketType >
bool NetworkThread< SocketType >::_proxyHeaderReadingEnabled
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