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 "Config.h"
19#include "NetworkThread.h"
20#include "ScriptMgr.h"
21#include "WorldSocket.h"
22#include "WorldSocketMgr.h"
23
24#include <boost/system/error_code.hpp>
25
27{
28public:
29 void SocketAdded(std::shared_ptr<WorldSocket> const& sock) override
30 {
31 sock->SetSendBufferSize(sWorldSocketMgr.GetApplicationSendBufferSize());
32 sScriptMgr->OnSocketOpen(sock);
33 }
34
35 void SocketRemoved(std::shared_ptr<WorldSocket>const& sock) override
36 {
37 sScriptMgr->OnSocketClose(sock);
38 }
39};
40
41WorldSocketMgr::WorldSocketMgr() : BaseSocketMgr(), _socketSystemSendBufferSize(-1), _socketApplicationSendBufferSize(65536), _tcpNoDelay(true)
42{
43}
44
46{
47 static WorldSocketMgr instance;
48 return instance;
49}
50
51bool WorldSocketMgr::StartWorldNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount)
52{
53 _tcpNoDelay = sConfigMgr->GetBoolDefault("Network.TcpNodelay", true);
54
55 int const max_connections = TRINITY_MAX_LISTEN_CONNECTIONS;
56 TC_LOG_DEBUG("misc", "Max allowed socket connections {}", max_connections);
57
58 // -1 means use default
59 _socketSystemSendBufferSize = sConfigMgr->GetIntDefault("Network.OutKBuff", -1);
60
61 _socketApplicationSendBufferSize = sConfigMgr->GetIntDefault("Network.OutUBuff", 65536);
62
64 {
65 TC_LOG_ERROR("misc", "Network.OutUBuff is wrong in your config file");
66 return false;
67 }
68
69 if (!BaseSocketMgr::StartNetwork(ioContext, bindIp, port, threadCount))
70 return false;
71
72 _acceptor->AsyncAccept([this](Trinity::Net::IoContextTcpSocket&& sock, uint32 threadIndex)
73 {
74 OnSocketOpen(std::move(sock), threadIndex);
75 });
76
77 sScriptMgr->OnNetworkStart();
78 return true;
79}
80
82{
83 BaseSocketMgr::StopNetwork();
84
85 sScriptMgr->OnNetworkStop();
86}
87
89{
90 // set some options here
92 {
93 boost::system::error_code err;
94 sock.set_option(boost::asio::socket_base::send_buffer_size(_socketSystemSendBufferSize), err);
95 if (err && err != boost::system::errc::not_supported)
96 {
97 TC_LOG_ERROR("misc", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::socket_base::send_buffer_size) err = {}", err.message());
98 return;
99 }
100 }
101
102 // Set TCP_NODELAY.
103 if (_tcpNoDelay)
104 {
105 boost::system::error_code err;
106 sock.set_option(boost::asio::ip::tcp::no_delay(true), err);
107 if (err)
108 {
109 TC_LOG_ERROR("misc", "WorldSocketMgr::OnSocketOpen sock.set_option(boost::asio::ip::tcp::no_delay) err = {}", err.message());
110 return;
111 }
112 }
113
114 //sock->m_OutBufferSize = static_cast<size_t> (m_SockOutUBuff);
115
116 BaseSocketMgr::OnSocketOpen(std::move(sock), threadIndex);
117}
118
#define TRINITY_MAX_LISTEN_CONNECTIONS
#define sConfigMgr
Definition Config.h:60
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
#define TC_LOG_DEBUG(filterType__,...)
Definition Log.h:156
#define TC_LOG_ERROR(filterType__,...)
Definition Log.h:165
#define sScriptMgr
Definition ScriptMgr.h:1168
#define sWorldSocketMgr
std::unique_ptr< AsyncAcceptor > _acceptor
Definition SocketMgr.h:140
Manages all sockets connected to peers and network threads.
int32 _socketSystemSendBufferSize
void OnSocketOpen(Trinity::Net::IoContextTcpSocket &&sock, uint32 threadIndex) override
SocketMgr< WorldSocket > BaseSocketMgr
int32 _socketApplicationSendBufferSize
Trinity::Net::NetworkThread< WorldSocket > * CreateThreads() const override
static WorldSocketMgr & Instance()
bool StartWorldNetwork(Trinity::Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int networkThreads)
Start network, listen at address:port .
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:41