TrinityCore
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NetworkThread.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef NetworkThread_h__
19#define NetworkThread_h__
20
21#include "Define.h"
22#include "DeadlineTimer.h"
23#include "Errors.h"
24#include "IoContext.h"
25#include "Log.h"
26#include "Timer.h"
27#include <boost/asio/ip/tcp.hpp>
28#include <atomic>
29#include <chrono>
30#include <memory>
31#include <mutex>
32#include <set>
33#include <thread>
34
35using boost::asio::ip::tcp;
36
37template<class SocketType>
39{
40public:
43 {
44 }
45
47 {
48 Stop();
49 if (_thread)
50 {
51 Wait();
52 delete _thread;
53 }
54 }
55
56 void Stop()
57 {
58 _stopped = true;
60 }
61
62 bool Start()
63 {
64 if (_thread)
65 return false;
66
67 _thread = new std::thread(&NetworkThread::Run, this);
68 return true;
69 }
70
71 void Wait()
72 {
74
75 _thread->join();
76 delete _thread;
77 _thread = nullptr;
78 }
79
81 {
82 return _connections;
83 }
84
85 virtual void AddSocket(std::shared_ptr<SocketType> sock)
86 {
87 std::lock_guard<std::mutex> lock(_newSocketsLock);
88
90 _newSockets.push_back(sock);
91 SocketAdded(sock);
92 }
93
94 tcp::socket* GetSocketForAccept() { return &_acceptSocket; }
95
96protected:
97 virtual void SocketAdded(std::shared_ptr<SocketType> /*sock*/) { }
98 virtual void SocketRemoved(std::shared_ptr<SocketType> /*sock*/) { }
99
101 {
102 std::lock_guard<std::mutex> lock(_newSocketsLock);
103
104 if (_newSockets.empty())
105 return;
106
107 for (std::shared_ptr<SocketType> sock : _newSockets)
108 {
109 if (!sock->IsOpen())
110 {
111 SocketRemoved(sock);
112 --_connections;
113 }
114 else
115 _sockets.push_back(sock);
116 }
117
118 _newSockets.clear();
119 }
120
121 void Run()
122 {
123 TC_LOG_DEBUG("misc", "Network Thread Starting");
124
125 _updateTimer.expires_after(1ms);
126 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
127 _ioContext.run();
128
129 TC_LOG_DEBUG("misc", "Network Thread exits");
130 _newSockets.clear();
131 _sockets.clear();
132 }
133
134 void Update()
135 {
136 if (_stopped)
137 return;
138
139 _updateTimer.expires_after(1ms);
140 _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
141
143
144 _sockets.erase(std::remove_if(_sockets.begin(), _sockets.end(), [this](std::shared_ptr<SocketType> sock)
145 {
146 if (!sock->Update())
147 {
148 if (sock->IsOpen())
149 sock->CloseSocket();
150
151 this->SocketRemoved(sock);
152
153 --this->_connections;
154 return true;
155 }
156
157 return false;
158 }), _sockets.end());
159 }
160
161private:
162 typedef std::vector<std::shared_ptr<SocketType>> SocketContainer;
163
164 std::atomic<int32> _connections;
165 std::atomic<bool> _stopped;
166
167 std::thread* _thread;
168
170
171 std::mutex _newSocketsLock;
173
175 tcp::socket _acceptSocket;
177};
178
179#endif // NetworkThread_h__
int32_t int32
Definition: Define.h:129
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
virtual void SocketRemoved(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:98
int32 GetConnectionCount() const
Definition: NetworkThread.h:80
std::thread * _thread
SocketContainer _sockets
virtual void SocketAdded(std::shared_ptr< SocketType >)
Definition: NetworkThread.h:97
tcp::socket _acceptSocket
tcp::socket * GetSocketForAccept()
Definition: NetworkThread.h:94
virtual void AddSocket(std::shared_ptr< SocketType > sock)
Definition: NetworkThread.h:85
std::mutex _newSocketsLock
Trinity::Asio::DeadlineTimer _updateTimer
Trinity::Asio::IoContext _ioContext
void AddNewSockets()
std::vector< std::shared_ptr< SocketType > > SocketContainer
std::atomic< bool > _stopped
SocketContainer _newSockets
virtual ~NetworkThread()
Definition: NetworkThread.h:46
std::atomic< int32 > _connections
std::size_t run()
Definition: IoContext.h:37