TrinityCore
WorldSocketMgr.cpp
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#include "WorldSocketMgr.h"
19#include "Config.h"
20#include "NetworkThread.h"
21#include "ScriptMgr.h"
22#include "WorldSocket.h"
23#include <boost/system/error_code.hpp>
24
25static void OnSocketAccept(boost::asio::ip::tcp::socket&& sock, uint32 threadIndex)
26{
27 sWorldSocketMgr.OnSocketOpen(std::forward<boost::asio::ip::tcp::socket>(sock), threadIndex);
28}
29
30class WorldSocketThread : public NetworkThread<WorldSocket>
31{
32public:
33 void SocketAdded(std::shared_ptr<WorldSocket> sock) override
34 {
35 sock->SetSendBufferSize(sWorldSocketMgr.GetApplicationSendBufferSize());
36 sScriptMgr->OnSocketOpen(sock);
37 }
38
39 void SocketRemoved(std::shared_ptr<WorldSocket> sock) override
40 {
41 sScriptMgr->OnSocketClose(sock);
42 }
43};
44
45WorldSocketMgr::WorldSocketMgr() : BaseSocketMgr(), _instanceAcceptor(nullptr), _socketSystemSendBufferSize(-1), _socketApplicationSendBufferSize(65536), _tcpNoDelay(true)
46{
47}
48
50{
51 ASSERT(!_instanceAcceptor, "StopNetwork must be called prior to WorldSocketMgr destruction");
52}
53
55{
56 static WorldSocketMgr instance;
57 return instance;
58}
59
60bool WorldSocketMgr::StartWorldNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, uint16 instancePort, int threadCount)
61{
62 _tcpNoDelay = sConfigMgr->GetBoolDefault("Network.TcpNodelay", true);
63
64 int const max_connections = TRINITY_MAX_LISTEN_CONNECTIONS;
65 TC_LOG_DEBUG("misc", "Max allowed socket connections {}", max_connections);
66
67 // -1 means use default
68 _socketSystemSendBufferSize = sConfigMgr->GetIntDefault("Network.OutKBuff", -1);
69
70 _socketApplicationSendBufferSize = sConfigMgr->GetIntDefault("Network.OutUBuff", 65536);
71
73 {
74 TC_LOG_ERROR("misc", "Network.OutUBuff is wrong in your config file");
75 return false;
76 }
77
78 if (!BaseSocketMgr::StartNetwork(ioContext, bindIp, port, threadCount))
79 return false;
80
81 AsyncAcceptor* instanceAcceptor = nullptr;
82 try
83 {
84 instanceAcceptor = new AsyncAcceptor(ioContext, bindIp, instancePort);
85 }
86 catch (boost::system::system_error const& err)
87 {
88 TC_LOG_ERROR("network", "Exception caught in WorldSocketMgr::StartNetwork ({}:{}): {}", bindIp, port, err.what());
89 return false;
90 }
91
92 if (!instanceAcceptor->Bind())
93 {
94 TC_LOG_ERROR("network", "StartNetwork failed to bind instance socket acceptor");
95 delete instanceAcceptor;
96 return false;
97 }
98
99 _instanceAcceptor = instanceAcceptor;
100
102
105
106 sScriptMgr->OnNetworkStart();
107 return true;
108}
109
111{
114
116
117 delete _instanceAcceptor;
118 _instanceAcceptor = nullptr;
119
120 sScriptMgr->OnNetworkStop();
121}
122
123void WorldSocketMgr::OnSocketOpen(boost::asio::ip::tcp::socket&& sock, uint32 threadIndex)
124{
125 // set some options here
127 {
128 boost::system::error_code err;
129 sock.set_option(boost::asio::socket_base::send_buffer_size(_socketSystemSendBufferSize), err);
130 if (err && err != boost::system::errc::not_supported)
131 {
132 TC_LOG_ERROR("misc", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::socket_base::send_buffer_size) err = {}", err.message());
133 return;
134 }
135 }
136
137 // Set TCP_NODELAY.
138 if (_tcpNoDelay)
139 {
140 boost::system::error_code err;
141 sock.set_option(boost::asio::ip::tcp::no_delay(true), err);
142 if (err)
143 {
144 TC_LOG_ERROR("misc", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::ip::tcp::no_delay) err = {}", err.message());
145 return;
146 }
147 }
148
149 //sock->m_OutBufferSize = static_cast<size_t> (m_SockOutUBuff);
150
151 BaseSocketMgr::OnSocketOpen(std::forward<boost::asio::ip::tcp::socket>(sock), threadIndex);
152}
153
155{
157}
#define TRINITY_MAX_LISTEN_CONNECTIONS
Definition: AsyncAcceptor.h:28
#define sConfigMgr
Definition: Config.h:61
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define sScriptMgr
Definition: ScriptMgr.h:1418
static void OnSocketAccept(boost::asio::ip::tcp::socket &&sock, uint32 threadIndex)
void SetSocketFactory(std::function< std::pair< boost::asio::ip::tcp::socket *, uint32 >()> func)
void AsyncAcceptWithCallback()
Definition: AsyncAcceptor.h:45
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:137
virtual bool StartNetwork(Trinity::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int threadCount)
Definition: SocketMgr.h:36
virtual void OnSocketOpen(boost::asio::ip::tcp::socket &&sock, uint32 threadIndex)
Definition: SocketMgr.h:96
virtual void StopNetwork()
Definition: SocketMgr.h:72
std::pair< boost::asio::ip::tcp::socket *, uint32 > GetSocketForAccept()
Definition: SocketMgr.h:124
int32 GetNetworkThreadCount() const
Definition: SocketMgr.h:111
Manages all sockets connected to peers and network threads.
int32 _socketSystemSendBufferSize
NetworkThread< WorldSocket > * CreateThreads() const override
AsyncAcceptor * _instanceAcceptor
int32 _socketApplicationSendBufferSize
bool StartWorldNetwork(Trinity::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, uint16 instancePort, int networkThreads)
Start network, listen at address:port .
static WorldSocketMgr & Instance()
void OnSocketOpen(boost::asio::ip::tcp::socket &&sock, uint32 threadIndex) override
void StopNetwork() override
Stops all network threads, It will wait for all running threads .
void SocketRemoved(std::shared_ptr< WorldSocket > sock) override
void SocketAdded(std::shared_ptr< WorldSocket > sock) override
#define sWorldSocketMgr