TrinityCore
Loading...
Searching...
No Matches
SocketMgr.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 TRINITYCORE_SOCKET_MGR_H
19#define TRINITYCORE_SOCKET_MGR_H
20
21#include "AsyncAcceptor.h"
22#include "Errors.h"
23#include "NetworkThread.h"
24#include "Socket.h"
25#include <boost/asio/ip/tcp.hpp>
26#include <memory>
27
28namespace Trinity::Net
29{
30template<class SocketType>
32{
33public:
34 SocketMgr(SocketMgr const&) = delete;
35 SocketMgr(SocketMgr&&) = delete;
36 SocketMgr& operator=(SocketMgr const&) = delete;
38
39 virtual ~SocketMgr()
40 {
41 ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction");
42 }
43
44 virtual bool StartNetwork(Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount)
45 {
46 ASSERT(threadCount > 0);
47
48 std::unique_ptr<AsyncAcceptor> acceptor = nullptr;
49 try
50 {
51 acceptor = std::make_unique<AsyncAcceptor>(ioContext, bindIp, port);
52 }
53 catch (boost::system::system_error const& err)
54 {
55 TC_LOG_ERROR("network", "Exception caught in SocketMgr.StartNetwork ({}:{}): {}", bindIp, port, err.what());
56 return false;
57 }
58
59 if (!acceptor->Bind())
60 {
61 TC_LOG_ERROR("network", "StartNetwork failed to bind socket acceptor");
62 return false;
63 }
64
65 _acceptor = std::move(acceptor);
66 _threadCount = threadCount;
67 _threads.reset(CreateThreads());
68
70
71 for (int32 i = 0; i < _threadCount; ++i)
72 _threads[i].Start();
73
74 _acceptor->SetSocketFactory([this]() { return GetSocketForAccept(); });
75
76 return true;
77 }
78
79 virtual void StopNetwork()
80 {
81 _acceptor->Close();
82
83 for (int32 i = 0; i < _threadCount; ++i)
84 _threads[i].Stop();
85
86 Wait();
87
88 _acceptor = nullptr;
89 _threads = nullptr;
90 _threadCount = 0;
91 }
92
93 void Wait()
94 {
95 for (int32 i = 0; i < _threadCount; ++i)
96 _threads[i].Wait();
97 }
98
99 virtual void OnSocketOpen(IoContextTcpSocket&& sock, uint32 threadIndex)
100 {
101 try
102 {
103 std::shared_ptr<SocketType> newSocket = std::make_shared<SocketType>(std::move(sock));
104 newSocket->Start();
105
106 _threads[threadIndex].AddSocket(newSocket);
107 }
108 catch (boost::system::system_error const& err)
109 {
110 TC_LOG_WARN("network", "Failed to retrieve client's remote address {}", err.what());
111 }
112 }
113
115
117 {
118 uint32 min = 0;
119
120 for (int32 i = 1; i < _threadCount; ++i)
121 if (_threads[i].GetConnectionCount() < _threads[min].GetConnectionCount())
122 min = i;
123
124 return min;
125 }
126
127 std::pair<IoContextTcpSocket*, uint32> GetSocketForAccept()
128 {
130 return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex);
131 }
132
133protected:
135 {
136 }
137
139
140 std::unique_ptr<AsyncAcceptor> _acceptor;
141 std::unique_ptr<NetworkThread<SocketType>[]> _threads;
143};
144}
145
146#endif // TRINITYCORE_SOCKET_MGR_H
int32_t int32
Definition Define.h:129
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
#define ASSERT
Definition Errors.h:68
#define TC_LOG_WARN(filterType__,...)
Definition Log.h:162
#define TC_LOG_ERROR(filterType__,...)
Definition Log.h:165
SocketMgr(SocketMgr const &)=delete
std::unique_ptr< NetworkThread< SocketType >[]> _threads
Definition SocketMgr.h:141
uint32 SelectThreadWithMinConnections() const
Definition SocketMgr.h:116
virtual void OnSocketOpen(IoContextTcpSocket &&sock, uint32 threadIndex)
Definition SocketMgr.h:99
int32 GetNetworkThreadCount() const
Definition SocketMgr.h:114
SocketMgr(SocketMgr &&)=delete
std::pair< IoContextTcpSocket *, uint32 > GetSocketForAccept()
Definition SocketMgr.h:127
std::unique_ptr< AsyncAcceptor > _acceptor
Definition SocketMgr.h:140
SocketMgr & operator=(SocketMgr &&)=delete
virtual bool StartNetwork(Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int threadCount)
Definition SocketMgr.h:44
virtual void StopNetwork()
Definition SocketMgr.h:79
virtual NetworkThread< SocketType > * CreateThreads() const =0
SocketMgr & operator=(SocketMgr const &)=delete
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
Definition Socket.h:41