TrinityCore
Loading...
Searching...
No Matches
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 "ScriptMgr.h"
21#include <boost/system/error_code.hpp>
22
23void WorldSocketThread::SocketAdded(std::shared_ptr<WorldSocket> const& sock)
24{
25 sock->SetSendBufferSize(sWorldSocketMgr.GetApplicationSendBufferSize());
26 sScriptMgr->OnSocketOpen(sock);
27}
28
29void WorldSocketThread::SocketRemoved(std::shared_ptr<WorldSocket>const& sock)
30{
31 sScriptMgr->OnSocketClose(sock);
32}
33
34WorldSocketMgr::WorldSocketMgr() : _socketSystemSendBufferSize(-1), _socketApplicationSendBufferSize(65536), _tcpNoDelay(true)
35{
36}
37
39
41{
42 static WorldSocketMgr instance;
43 return instance;
44}
45
46bool WorldSocketMgr::StartNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount)
47{
48 _tcpNoDelay = sConfigMgr->GetBoolDefault("Network.TcpNodelay", true);
49
50 int const max_connections = TRINITY_MAX_LISTEN_CONNECTIONS;
51 TC_LOG_DEBUG("misc", "Max allowed socket connections {}", max_connections);
52
53 // -1 means use default
54 _socketSystemSendBufferSize = sConfigMgr->GetIntDefault("Network.OutKBuff", -1);
55
56 _socketApplicationSendBufferSize = sConfigMgr->GetIntDefault("Network.OutUBuff", 65536);
57
59 {
60 TC_LOG_ERROR("misc", "Network.OutUBuff is wrong in your config file");
61 return false;
62 }
63
64 if (!SocketMgr::StartNetwork(ioContext, bindIp, port, threadCount))
65 return false;
66
67 sScriptMgr->OnNetworkStart();
68 return true;
69}
70
72{
73 SocketMgr::StopNetwork();
74
75 sScriptMgr->OnNetworkStop();
76}
77
79{
80 // set some options here
82 {
83 boost::system::error_code err;
84 sock.set_option(boost::asio::socket_base::send_buffer_size(_socketSystemSendBufferSize), err);
85 if (err && err != boost::system::errc::not_supported)
86 {
87 TC_LOG_ERROR("misc", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::socket_base::send_buffer_size) err = {}", err.message());
88 return;
89 }
90 }
91
92 // Set TCP_NODELAY.
93 if (_tcpNoDelay)
94 {
95 boost::system::error_code err;
96 sock.set_option(boost::asio::ip::tcp::no_delay(true), err);
97 if (err)
98 {
99 TC_LOG_ERROR("misc", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::ip::tcp::no_delay) err = {}", err.message());
100 return;
101 }
102 }
103
104 SocketMgr::OnSocketOpen(std::move(sock));
105}
#define TRINITY_MAX_LISTEN_CONNECTIONS
#define sConfigMgr
Definition Config.h:64
uint16_t uint16
Definition Define.h:155
#define TC_LOG_DEBUG(filterType__, message__,...)
Definition Log.h:181
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:190
#define sScriptMgr
Definition ScriptMgr.h:1449
#define sWorldSocketMgr
Manages all sockets connected to peers and network threads.
int32 _socketSystemSendBufferSize
bool StartNetwork(Trinity::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int threadCount) override
Start network, listen at address:port .
int32 _socketApplicationSendBufferSize
void OnSocketOpen(Trinity::Net::IoContextTcpSocket &&sock) override
static WorldSocketMgr & Instance()
void StopNetwork() override
Stops all network threads, It will wait for all running threads .
void SocketAdded(std::shared_ptr< WorldSocket > const &sock) override
void SocketRemoved(std::shared_ptr< WorldSocket >const &sock) override
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
Definition Socket.h:40