Files
DarkflameServer/dGame/dGameMessages/GameMessages.cpp
Aaron Kimbrell 542f0a89f0 refactor: remove the packet macros, WriteHeader and PacketUtils
Nothing in the server writes or reads a packet by hand any more, so the
helpers for doing so go:
- CBITSTREAM, CMSGHEADER, CINSTREAM, CINSTREAM_SKIP_HEADER, SEND_PACKET,
  SEND_PACKET_BROADCAST and HEADER_SIZE leave dCommonVars.h;
- the free BitStreamUtils::WriteHeader (LUBitStream::WriteHeader writes the
  same bytes) and the unused PacketUtils::SavePacket are deleted.
The last raw reads are replaced: WorldServer builds its input stream
directly, the master packet logs read the header with
LUBitStream::ReadHeader instead of peeking at packet->data[1] and [3], and
MessageInspector reads a sent game message's header with the new
NetGameMsg::ReadPacketHeader (the counterpart of WritePacket) instead of
memcmp/memcpy. packet->data[0] is still compared with RakNet's own
connection IDs.

The frozen oracles keep using the macros verbatim through the test-only
tests/dGameTests/LegacyPacketMacros.h; the HeaderSkip tests, which only
tested CINSTREAM_SKIP_HEADER, are removed.

docs/PacketArchitecture.md: "where we are" now describes the final state
and what still touches raw bytes (RakNet IDs, replica headers, behavior bit
streams), and a new section collects the known wire discrepancies found
during the conversion, with client addresses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:30:56 -05:00

59 lines
1.8 KiB
C++

#include "GameMessages.h"
#include "BitStream.h"
#include "BitStreamUtils.h"
#include "EntityManager.h"
#include "Game.h"
#include "MessageType/Client.h"
#include "ServiceType.h"
#include "dServer.h"
namespace GameMessages {
bool GameMsg::Send() {
return Game::entityManager->SendMessage(*this);
}
bool GameMsg::Send(const LWOOBJID _target) {
target = _target;
return Send();
}
void NetGameMsg::WritePacket(RakNet::BitStream& bitStream) const {
LUBitStream(ServiceType::CLIENT, MessageType::Client::GAME_MSG).WriteHeader(bitStream);
bitStream.Write(target); // Who this message will be sent to on the (a) client
bitStream.Write(msgId); // the ID of this message
Serialize(bitStream); // write the message data
}
bool NetGameMsg::ReadPacketHeader(RakNet::BitStream& bitStream, LWOOBJID& target, MessageType::Game& msgId) {
LUBitStream header;
if (!header.ReadHeader(bitStream)) return false;
if (header.connectionType != ServiceType::CLIENT || header.internalPacketID != static_cast<uint32_t>(MessageType::Client::GAME_MSG)) return false;
VALIDATE_READ(bitStream.Read(target));
VALIDATE_READ(bitStream.Read(msgId));
return true;
}
void NetGameMsg::SendToClient(const SystemAddress& sysAddr) const {
RakNet::BitStream bitStream;
WritePacket(bitStream);
Game::server->Send(bitStream, sysAddr, false);
}
void NetGameMsg::BroadcastExcept(const SystemAddress& excluded) const {
RakNet::BitStream bitStream;
WritePacket(bitStream);
Game::server->Send(bitStream, excluded, true);
}
void NetGameMsg::Send(const SystemAddress& sysAddr) const {
RakNet::BitStream bitStream;
WritePacket(bitStream);
// Send to everyone if someone sent unassigned system address, or to one specific client.
Game::server->Send(bitStream, sysAddr, sysAddr == UNASSIGNED_SYSTEM_ADDRESS);
}
}