refactor(Core/Network): Port TrinityCore socket optimizations (#24384)

Co-authored-by: blinkysc <blinkysc@users.noreply.github.com>
Co-authored-by: Shauren <shauren@users.noreply.github.com>
This commit is contained in:
blinkysc
2026-01-15 07:47:58 -06:00
committed by GitHub
parent a8ce95ad71
commit d908b4c2fc
16 changed files with 242 additions and 75 deletions

View File

@@ -29,14 +29,14 @@ void ScriptMgr::OnNetworkStop()
CALL_ENABLED_HOOKS(ServerScript, SERVERHOOK_ON_NETWORK_STOP, script->OnNetworkStop());
}
void ScriptMgr::OnSocketOpen(std::shared_ptr<WorldSocket> socket)
void ScriptMgr::OnSocketOpen(std::shared_ptr<WorldSocket> const& socket)
{
ASSERT(socket);
CALL_ENABLED_HOOKS(ServerScript, SERVERHOOK_ON_SOCKET_OPEN, script->OnSocketOpen(socket));
}
void ScriptMgr::OnSocketClose(std::shared_ptr<WorldSocket> socket)
void ScriptMgr::OnSocketClose(std::shared_ptr<WorldSocket> const& socket)
{
ASSERT(socket);

View File

@@ -46,11 +46,11 @@ public:
virtual void OnNetworkStop() { }
// Called when a remote socket establishes a connection to the server. Do not store the socket object.
virtual void OnSocketOpen(std::shared_ptr<WorldSocket> /*socket*/) { }
virtual void OnSocketOpen(std::shared_ptr<WorldSocket> const& /*socket*/) { }
// Called when a socket is closed. Do not store the socket object, and do not rely on the connection
// being open; it is not.
virtual void OnSocketClose(std::shared_ptr<WorldSocket> /*socket*/) { }
virtual void OnSocketClose(std::shared_ptr<WorldSocket> const& /*socket*/) { }
/**
* @brief This hook called when a packet is sent to a client. The packet object is a copy of the original packet, so reading and modifying it is safe.

View File

@@ -155,8 +155,8 @@ public: /* SpellScriptLoader */
public: /* ServerScript */
void OnNetworkStart();
void OnNetworkStop();
void OnSocketOpen(std::shared_ptr<WorldSocket> socket);
void OnSocketClose(std::shared_ptr<WorldSocket> socket);
void OnSocketOpen(std::shared_ptr<WorldSocket> const& socket);
void OnSocketClose(std::shared_ptr<WorldSocket> const& socket);
bool CanPacketReceive(WorldSession* session, WorldPacket const& packet);
bool CanPacketSend(WorldSession* session, WorldPacket const& packet);

View File

@@ -116,7 +116,7 @@ void EncryptableAndCompressiblePacket::CompressIfNeeded()
SetOpcode(SMSG_COMPRESSED_UPDATE_OBJECT);
}
WorldSocket::WorldSocket(tcp::socket&& socket)
WorldSocket::WorldSocket(IoContextTcpSocket&& socket)
: Socket(std::move(socket)), _OverSpeedPings(0), _worldSession(nullptr), _authed(false), _sendBufferSize(4096), _loggingPackets(false)
{
Acore::Crypto::GetRandomBytes(_authSeed);
@@ -238,10 +238,10 @@ void WorldSocket::OnClose()
}
}
void WorldSocket::ReadHandler()
SocketReadCallbackResult WorldSocket::ReadHandler()
{
if (!IsOpen())
return;
return SocketReadCallbackResult::Stop;
MessageBuffer& packet = GetReadBuffer();
while (packet.GetActiveSize() > 0)
@@ -264,7 +264,7 @@ void WorldSocket::ReadHandler()
if (!ReadHeaderHandler())
{
CloseSocket();
return;
return SocketReadCallbackResult::Stop;
}
}
@@ -295,11 +295,11 @@ void WorldSocket::ReadHandler()
CloseSocket();
}
return;
return SocketReadCallbackResult::Stop;
}
}
AsyncRead();
return SocketReadCallbackResult::KeepReading;
}
bool WorldSocket::ReadHeaderHandler()

View File

@@ -67,19 +67,19 @@ struct ClientPktHeader
struct AuthSession;
class AC_GAME_API WorldSocket : public Socket<WorldSocket>
class AC_GAME_API WorldSocket final : public Socket<WorldSocket>
{
typedef Socket<WorldSocket> BaseSocket;
public:
WorldSocket(tcp::socket&& socket);
WorldSocket(IoContextTcpSocket&& socket);
~WorldSocket();
WorldSocket(WorldSocket const& right) = delete;
WorldSocket& operator=(WorldSocket const& right) = delete;
void Start() override;
bool Update() override;
bool Update() final;
void SendPacket(WorldPacket const& packet);
@@ -90,7 +90,7 @@ public:
protected:
void OnClose() override;
void ReadHandler() override;
SocketReadCallbackResult ReadHandler() final;
bool ReadHeaderHandler();
enum class ReadDataHandlerResult

View File

@@ -25,13 +25,13 @@
class WorldSocketThread : public NetworkThread<WorldSocket>
{
public:
void SocketAdded(std::shared_ptr<WorldSocket> sock) override
void SocketAdded(std::shared_ptr<WorldSocket> const& sock) override
{
sock->SetSendBufferSize(sWorldSocketMgr.GetApplicationSendBufferSize());
sScriptMgr->OnSocketOpen(sock);
}
void SocketRemoved(std::shared_ptr<WorldSocket> sock) override
void SocketRemoved(std::shared_ptr<WorldSocket> const& sock) override
{
sScriptMgr->OnSocketClose(sock);
}
@@ -81,7 +81,7 @@ void WorldSocketMgr::StopNetwork()
sScriptMgr->OnNetworkStop();
}
void WorldSocketMgr::OnSocketOpen(tcp::socket&& sock, uint32 threadIndex)
void WorldSocketMgr::OnSocketOpen(IoContextTcpSocket&& sock, uint32 threadIndex)
{
// set some options here
if (_socketSystemSendBufferSize >= 0)
@@ -109,7 +109,7 @@ void WorldSocketMgr::OnSocketOpen(tcp::socket&& sock, uint32 threadIndex)
}
}
BaseSocketMgr::OnSocketOpen(std::forward<tcp::socket>(sock), threadIndex);
BaseSocketMgr::OnSocketOpen(std::move(sock), threadIndex);
}
NetworkThread<WorldSocket>* WorldSocketMgr::CreateThreads() const

View File

@@ -41,7 +41,7 @@ public:
/// Stops all network threads, It will wait for all running threads .
void StopNetwork() override;
void OnSocketOpen(tcp::socket&& sock, uint32 threadIndex) override;
void OnSocketOpen(IoContextTcpSocket&& sock, uint32 threadIndex) override;
std::size_t GetApplicationSendBufferSize() const { return _socketApplicationSendBufferSize; }
@@ -50,9 +50,9 @@ protected:
NetworkThread<WorldSocket>* CreateThreads() const override;
static void OnSocketAccept(tcp::socket&& sock, uint32 threadIndex)
static void OnSocketAccept(IoContextTcpSocket&& sock, uint32 threadIndex)
{
Instance().OnSocketOpen(std::forward<tcp::socket>(sock), threadIndex);
Instance().OnSocketOpen(std::move(sock), threadIndex);
}
private: