mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 19:03:43 +00:00
First step of moving hand written bitstream code to struct based messages (see docs/PacketArchitecture.md). No wire changes. - GameMsg is now only a server-internal event (delivered to handlers registered with RegisterMsg); NetGameMsg is a wire message with Send(sysAddr) (UNASSIGNED broadcasts), SendToClient(sysAddr) (one client, never broadcasts), WritePacket, Serialize, Deserialize and Handle. Mixing them up is now a compile error. NetGameMsgEvent<T> / DeliverLocally carry a wire message to local handlers; loot drops, pickup, the object debugger, GM invisibility and model RequestUse use them. The GM invisibility message keeps its target, so its bytes are unchanged. - Behaviour change: GameMessageHandler logs and drops messages whose Deserialize fails instead of handling them with default fields (the 4 messages already registered: RequestUse, RequestServerObjectInfo, ShootingGalleryFire, PickupItem). - LUBitStream (kept as on main) gets a virtual destructor (Mail deleted derived packets through the base) and WritePacket, also used by ChatPackets::SendRoutedMsg with identical bytes. - BitStreamUtils::WriteOptional/ReadOptional for default-flag fields and WriteLengthPrefixed/ReadLengthPrefixed for length prefixed strings. - Every message ID enumerator (MessageType::*, ServiceType, Mail's wire enums) is pinned with static_asserts, so renumbering or removing one fails to compile. - Tests: dServerMock copies each sent packet (it kept a pointer to the caller's destroyed BitStream); PacketTestUtils.h compares packets bit for bit; helper tests use hand computed golden bytes and equality with the hand written patterns they replace; compile-time checks keep the wire/internal split in place. - docs/PacketArchitecture.md: survey, target architecture, conventions, verification method and migration plan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include "BitStreamUtils.h"
|
|
#include "GameDependencies.h"
|
|
#include "PacketTestUtils.h"
|
|
|
|
#include <vector>
|
|
|
|
namespace {
|
|
std::vector<uint8_t> Bytes(RakNet::BitStream& bitStream) {
|
|
return { bitStream.GetData(), bitStream.GetData() + bitStream.GetNumberOfBytesUsed() };
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
struct TestPacket : public LUBitStream {
|
|
uint32_t value = 0;
|
|
TestPacket() : LUBitStream(ServiceType::CHAT, 0x1234u) {}
|
|
void Serialize(RakNet::BitStream& bitStream) const override { bitStream.Write(value); }
|
|
bool Deserialize(RakNet::BitStream& bitStream) override { return bitStream.Read(value); }
|
|
};
|
|
}
|
|
|
|
TEST(LUBitStreamStructTests, WritePacketGoldenAndRoundTrip) {
|
|
TestPacket packet;
|
|
packet.value = 0xAABBCCDD;
|
|
RakNet::BitStream bitStream;
|
|
packet.WritePacket(bitStream);
|
|
// 0x53 | service u16 | packet id u32 | pad u8 | payload
|
|
const std::vector<uint8_t> golden = { 0x53, 0x02, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0xDD, 0xCC, 0xBB, 0xAA };
|
|
ASSERT_EQ(Bytes(bitStream), golden);
|
|
|
|
TestPacket read;
|
|
ASSERT_TRUE(read.ReadHeader(bitStream));
|
|
EXPECT_EQ(read.connectionType, ServiceType::CHAT);
|
|
EXPECT_EQ(read.internalPacketID, 0x1234u);
|
|
ASSERT_TRUE(read.Deserialize(bitStream));
|
|
EXPECT_EQ(read.value, 0xAABBCCDD);
|
|
}
|
|
|
|
class LUBitStreamTests : public GameDependenciesTest {
|
|
protected:
|
|
void SetUp() override { SetUpDependencies(); }
|
|
void TearDown() override { TearDownDependencies(); }
|
|
};
|
|
|
|
TEST_F(LUBitStreamTests, SendWritesWhatWritePacketWrites) {
|
|
TestPacket packet;
|
|
packet.value = 7;
|
|
RakNet::BitStream expected;
|
|
packet.WritePacket(expected);
|
|
const auto sent = PacketTestUtils::Capture([&] { packet.Send(UNASSIGNED_SYSTEM_ADDRESS); });
|
|
ASSERT_EQ(sent.size(), 1);
|
|
EXPECT_TRUE(sent[0].broadcast);
|
|
EXPECT_PACKET_EQ(PacketTestUtils::FromBitStream(expected), PacketTestUtils::FromCapture(sent[0]));
|
|
}
|