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

◆ ~NetworkThread()

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

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

Member Function Documentation

◆ AddNewSockets()

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

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

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
83 {
84 return _connections;
85 }

References NetworkThread< SocketType >::_connections.

◆ GetSocketForAccept()

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

◆ HandleNewSocketsProxyReadingOnConnect()

template<class SocketType >
void NetworkThread< SocketType >::HandleNewSocketsProxyReadingOnConnect ( )
inlineprotected
136 {
137 std::size_t index = 0;
138 std::vector<int> newSocketsToRemoveIndexes;
139 for (auto sock_iter = _newSockets.begin(); sock_iter != _newSockets.end(); ++sock_iter, ++index)
140 {
141 std::shared_ptr<SocketType> sock = *sock_iter;
142
143 if (!sock->IsOpen())
144 {
145 newSocketsToRemoveIndexes.emplace_back(index);
146 SocketRemoved(sock);
147 --_connections;
148 continue;
149 }
150
151 const auto proxyHeaderReadingState = sock->GetProxyHeaderReadingState();
152 if (proxyHeaderReadingState == PROXY_HEADER_READING_STATE_STARTED)
153 continue;
154
155 switch (proxyHeaderReadingState) {
157 sock->AsyncReadProxyHeader();
158 break;
159
161 newSocketsToRemoveIndexes.emplace_back(index);
162 _sockets.emplace_back(sock);
163
164 sock->Start();
165
166 break;
167
168 default:
169 newSocketsToRemoveIndexes.emplace_back(index);
170 SocketRemoved(sock);
171 --_connections;
172 break;
173 }
174 }
175
176 for (auto it = newSocketsToRemoveIndexes.rbegin(); it != newSocketsToRemoveIndexes.rend(); ++it)
177 _newSockets.erase(_newSockets.begin() + *it);
178 }
@ 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
181 {
182 LOG_DEBUG("misc", "Network Thread Starting");
183
184 _updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
185 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
186 _ioContext.run();
187
188 LOG_DEBUG("misc", "Network Thread exits");
189 _newSockets.clear();
190 _sockets.clear();
191 }
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:168
std::size_t run()
Definition: IoContext.h:45
void Update()
Definition: NetworkThread.h:193

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.

101{ }

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
62 {
63 if (_thread)
64 return false;
65
66 _thread = std::make_unique<std::thread>([this]() { NetworkThread::Run(); });
67 return true;
68 }
void Run()
Definition: NetworkThread.h:180

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

◆ Stop()

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

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

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