mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-23 13:46:24 +00:00
feat(Core): replace ACE network with Boost.Asio (#6574)
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
|
||||
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
|
||||
* Copyright (C) 2008-2021 TrinityCore <http://www.trinitycore.org/>
|
||||
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
||||
*/
|
||||
|
||||
#include "PacketLog.h"
|
||||
#include "Config.h"
|
||||
#include "IpAddress.h"
|
||||
#include "Timer.h"
|
||||
#include "WorldPacket.h"
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
// Packet logging structures in PKT 3.1 format
|
||||
// Packet logging structures in PKT 3.1 format
|
||||
struct LogHeader
|
||||
{
|
||||
char Signature[3];
|
||||
@@ -27,11 +28,19 @@ struct LogHeader
|
||||
|
||||
struct PacketHeader
|
||||
{
|
||||
char Direction[4];
|
||||
// used to uniquely identify a connection
|
||||
struct OptionalData
|
||||
{
|
||||
uint8 SocketIPBytes[16];
|
||||
uint32 SocketPort;
|
||||
};
|
||||
|
||||
uint32 Direction;
|
||||
uint32 ConnectionId;
|
||||
uint32 ArrivalTicks;
|
||||
uint32 OptionalDataSize;
|
||||
uint32 Length;
|
||||
OptionalData OptionalData;
|
||||
uint32 Opcode;
|
||||
};
|
||||
|
||||
@@ -45,7 +54,9 @@ PacketLog::PacketLog() : _file(nullptr)
|
||||
PacketLog::~PacketLog()
|
||||
{
|
||||
if (_file)
|
||||
{
|
||||
fclose(_file);
|
||||
}
|
||||
|
||||
_file = nullptr;
|
||||
}
|
||||
@@ -60,9 +71,10 @@ void PacketLog::Initialize()
|
||||
{
|
||||
std::string logsDir = sConfigMgr->GetOption<std::string>("LogsDir", "");
|
||||
|
||||
if (!logsDir.empty())
|
||||
if ((logsDir.at(logsDir.length() - 1) != '/') && (logsDir.at(logsDir.length() - 1) != '\\'))
|
||||
logsDir.push_back('/');
|
||||
if (!logsDir.empty() && (logsDir.at(logsDir.length() - 1) != '/') && (logsDir.at(logsDir.length() - 1) != '\\'))
|
||||
{
|
||||
logsDir.push_back('/');
|
||||
}
|
||||
|
||||
std::string logname = sConfigMgr->GetOption<std::string>("PacketLogFile", "");
|
||||
if (!logname.empty())
|
||||
@@ -80,23 +92,42 @@ void PacketLog::Initialize()
|
||||
header.SniffStartTicks = getMSTime();
|
||||
header.OptionalDataSize = 0;
|
||||
|
||||
fwrite(&header, sizeof(header), 1, _file);
|
||||
if (CanLogPacket())
|
||||
{
|
||||
fwrite(&header, sizeof(header), 1, _file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PacketLog::LogPacket(WorldPacket const& packet, Direction direction)
|
||||
void PacketLog::LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_logPacketLock);
|
||||
|
||||
PacketHeader header;
|
||||
*reinterpret_cast<uint32*>(header.Direction) = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
|
||||
header.Direction = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
|
||||
header.ConnectionId = 0;
|
||||
header.ArrivalTicks = getMSTime();
|
||||
header.OptionalDataSize = 0;
|
||||
header.Length = packet.size() + 4;
|
||||
|
||||
header.OptionalDataSize = sizeof(header.OptionalData);
|
||||
memset(header.OptionalData.SocketIPBytes, 0, sizeof(header.OptionalData.SocketIPBytes));
|
||||
|
||||
if (addr.is_v4())
|
||||
{
|
||||
auto bytes = addr.to_v4().to_bytes();
|
||||
memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
|
||||
}
|
||||
else if (addr.is_v6())
|
||||
{
|
||||
auto bytes = addr.to_v6().to_bytes();
|
||||
memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
header.OptionalData.SocketPort = port;
|
||||
header.Length = packet.size() + sizeof(header.Opcode);
|
||||
header.Opcode = packet.GetOpcode();
|
||||
|
||||
fwrite(&header, sizeof(header), 1, _file);
|
||||
|
||||
if (!packet.empty())
|
||||
{
|
||||
fwrite(packet.contents(), 1, packet.size(), _file);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#define ACORE_PACKETLOG_H
|
||||
|
||||
#include "Common.h"
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
#include <mutex>
|
||||
|
||||
enum Direction
|
||||
@@ -18,7 +19,7 @@ enum Direction
|
||||
|
||||
class WorldPacket;
|
||||
|
||||
class PacketLog
|
||||
class AC_GAME_API PacketLog
|
||||
{
|
||||
private:
|
||||
PacketLog();
|
||||
@@ -31,7 +32,7 @@ public:
|
||||
|
||||
void Initialize();
|
||||
bool CanLogPacket() const { return (_file != nullptr); }
|
||||
void LogPacket(WorldPacket const& packet, Direction direction);
|
||||
void LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port);
|
||||
|
||||
private:
|
||||
FILE* _file;
|
||||
|
||||
50
src/server/game/Server/Protocol/ServerPktHeader.h
Normal file
50
src/server/game/Server/Protocol/ServerPktHeader.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#ifndef __SERVERPKTHDR_H__
|
||||
#define __SERVERPKTHDR_H__
|
||||
|
||||
#include "Log.h"
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
struct ServerPktHeader
|
||||
{
|
||||
/**
|
||||
* size is the length of the payload _plus_ the length of the opcode
|
||||
*/
|
||||
ServerPktHeader(uint32 size, uint16 cmd) : size(size)
|
||||
{
|
||||
uint8 headerIndex=0;
|
||||
if (isLargePacket())
|
||||
{
|
||||
LOG_DEBUG("network", "initializing large server to client packet. Size: %u, cmd: %u", size, cmd);
|
||||
header[headerIndex++] = 0x80 | (0xFF & (size >> 16));
|
||||
}
|
||||
header[headerIndex++] = 0xFF &(size >> 8);
|
||||
header[headerIndex++] = 0xFF & size;
|
||||
|
||||
header[headerIndex++] = 0xFF & cmd;
|
||||
header[headerIndex++] = 0xFF & (cmd >> 8);
|
||||
}
|
||||
|
||||
uint8 getHeaderLength()
|
||||
{
|
||||
// cmd = 2 bytes, size= 2||3bytes
|
||||
return 2 + (isLargePacket() ? 3 : 2);
|
||||
}
|
||||
|
||||
bool isLargePacket() const
|
||||
{
|
||||
return size > 0x7FFF;
|
||||
}
|
||||
|
||||
const uint32 size;
|
||||
uint8 header[5];
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user