refactor: master packets as structs

Every MASTER service packet is now an LUBitStream struct (docs/PacketArchitecture.md,
PR 14) and the master server's switch is a dispatch map (PacketDispatcher), as are the
master handlers of the world, chat and dashboard servers.

- dNet/MasterPackets.h: RequestZoneTransfer, RequestZoneTransferResponse, ServerInfo,
  RequestSessionKey, SetSessionKey, SessionKeyResponse, NewSessionAlert, PlayerAdded /
  PlayerRemoved, CreatePrivateZone, RequestPrivateZone (passwords still cut to 50
  characters when read), WorldReady, WorldReadyInfo (WORLD_READY to the dashboard),
  PrepZone, Shutdown, ShutdownResponse, WorldShutDown (SHUTDOWN_RESPONSE to the
  dashboard), ShutdownUniverse, AffirmTransferRequest/Response, RequestServerList,
  ServerListResponse, DashboardShutdown, ConfigReload, InstanceShutdown. The Send*
  functions are gone; MasterPackets::SendToMaster(msg) and SendTo(sysAddr, msg) send a
  struct.
- The dashboard and instance migration structs (PlayerAction, DataChanged, Dashboard
  messages, MessageCapture, InstanceMigration) are LUBitStreams of the MASTER service now
  and moved to dNet/master/, included by MasterPackets.h. Their payloads are unchanged;
  master forwards them by re-serializing the struct instead of copying raw bytes.
- InstanceManager, ZoneInstanceManager, MigrationCoordinator, dServer (server info, zone
  transfer response), auth (SET_SESSION_KEY), the world (session keys, player added and
  removed, world ready, shutdown response, affirmations, prep zone, shutdown universe) and
  the dashboard (server list, instance shutdown, config reload, announcements, player
  actions, message capture) send and read structs.
- The login stamps are a `stamps` field of RequestZoneTransfer and
  RequestZoneTransferResponse (read leniently as before: a message without them reads as
  empty); master adds its stamps in the REQUEST_ZONE_TRANSFER handler and when it answers,
  as it did.
- InstanceManager::GetInstanceBySysAddr takes a const address.

Verified: tests/dGameTests/dNetTests/Legacy/MasterPacketsLegacy.h is a verbatim copy of
the old writers and readers; MasterPacketsTests requires identical bytes for a grid of
inputs, checks the old readers read what the structs write, round trips and truncation,
hand written golden packets, that zone transfers without stamps still read, that the dashboard/migration structs write what
"header + Serialize" wrote, and that the dispatcher drops truncated packets. No wire
bytes changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-26 23:34:47 -05:00
parent 796fd1b941
commit 5a9a3e380b
54 changed files with 2275 additions and 1248 deletions

View File

@@ -1,6 +1,7 @@
SET(DNET_TESTS
"ChatPacketsTests.cpp"
"CommonAuthPacketsTests.cpp"
"MasterPacketsTests.cpp"
"WorldPacketsTests.cpp")
# Get the folder name and prepend it to the files above

View File

@@ -433,9 +433,21 @@ TEST_F(CommonAuthPacketsTests, StampsRoundTrip) {
}
TEST_F(CommonAuthPacketsTests, ZoneTransferResponseCarriesStamps) {
const auto sendResponse = [](const Stamps& stamps) {
MasterPackets::RequestZoneTransferResponse response;
response.requestID = 99;
response.mythranShift = true;
response.zoneID = 1000;
response.zoneInstance = 7;
response.zoneClone = 0;
response.serverIP = LUString("127.0.0.1", 255);
response.serverPort = 2001;
response.stamps = stamps;
MasterPackets::SendTo(TestAddress(), response);
};
Stamps stamps;
stamps.list = { Stamp(eStamps::PASSPORT_AUTH_START, 0, 10), Stamp(eStamps::PASSPORT_AUTH_WORLD_SESSION_CONFIRM_TO_AUTH, 7, 11) };
const auto sent = Capture([&] { MasterPackets::SendZoneTransferResponse(Game::server, TestAddress(), 99, true, 1000, 7, 0, "127.0.0.1", 2001, stamps); });
const auto sent = Capture([&] { sendResponse(stamps); });
ASSERT_EQ(sent.size(), 1);
RakNet::BitStream bitStream(const_cast<uint8_t*>(sent[0].bytes.data()), sent[0].bytes.size(), true);
@@ -469,7 +481,7 @@ TEST_F(CommonAuthPacketsTests, ZoneTransferResponseCarriesStamps) {
EXPECT_EQ(copy.list[1].timestamp, 11);
// Without a login the list is empty: just its size field
const auto plain = Capture([&] { MasterPackets::SendZoneTransferResponse(Game::server, TestAddress(), 99, true, 1000, 7, 0, "127.0.0.1", 2001); });
const auto plain = Capture([&] { sendResponse({}); });
ASSERT_EQ(plain.size(), 1);
EXPECT_EQ(plain[0].bytes.size() + 32, sent[0].bytes.size());
const std::vector<uint8_t> tail(plain[0].bytes.end() - 4, plain[0].bytes.end());

View File

@@ -0,0 +1,392 @@
#ifndef MASTERPACKETSLEGACY_H
#define MASTERPACKETSLEGACY_H
// FROZEN ORACLE - DO NOT EDIT.
// Verbatim copies of the hand written master packet code that dNet/MasterPackets.h replaced (dNet/MasterPackets.cpp,
// dNet/ZoneInstanceManager.cpp, dMasterServer/{MasterServer,InstanceManager}.cpp, dWorldServer/WorldServer.cpp,
// dDashboardServer/DashboardServer.cpp and the other senders at 9a392167, with the login stamps). Only the namespace changed, except:
// - senders write into `bitStream` instead of sending it (SendToMaster and the master's peers aren't mocked);
// - values the old code took from a server or an instance are passed in;
// - old readers return what they read in a struct instead of acting on it.
// These messages never reach a client, but master, worlds, auth, chat and the dashboard can run different builds
// during an update, so their bytes are pinned too.
#include "BitStreamUtils.h"
#include "dCommonVars.h"
#include "MessageType/Master.h"
#include "ServiceType.h"
#include "Stamps.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
namespace LegacyMaster {
// dNet/MasterPackets.cpp
inline void SendZoneTransferRequest(RakNet::BitStream& bitStream, uint64_t requestID, bool mythranShift, uint32_t zoneID, uint32_t cloneID, const Stamps& stamps = {}) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::REQUEST_ZONE_TRANSFER);
bitStream.Write(requestID);
bitStream.Write<uint8_t>(mythranShift);
bitStream.Write(zoneID);
bitStream.Write(cloneID);
stamps.Serialize(bitStream);
}
inline void SendZoneCreatePrivate(RakNet::BitStream& bitStream, uint32_t zoneID, uint32_t cloneID, const std::string& password) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::CREATE_PRIVATE_ZONE);
bitStream.Write(zoneID);
bitStream.Write(cloneID);
bitStream.Write<uint32_t>(password.size());
for (auto character : password) {
bitStream.Write<char>(character);
}
}
inline void SendZoneRequestPrivate(RakNet::BitStream& bitStream, uint64_t requestID, bool mythranShift, const std::string& password) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::REQUEST_PRIVATE_ZONE);
bitStream.Write(requestID);
bitStream.Write<uint8_t>(mythranShift);
bitStream.Write<uint32_t>(password.size());
for (auto character : password) {
bitStream.Write<char>(character);
}
}
inline void SendWorldReady(RakNet::BitStream& bitStream, LWOMAPID zoneId, LWOINSTANCEID instanceId) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::WORLD_READY);
bitStream.Write(zoneId);
bitStream.Write(instanceId);
}
inline void SendZoneTransferResponse(RakNet::BitStream& bitStream, uint64_t requestID, bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, const std::string& serverIP, uint32_t serverPort, const Stamps& stamps = {}) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::REQUEST_ZONE_TRANSFER_RESPONSE);
bitStream.Write(requestID);
bitStream.Write<uint8_t>(mythranShift);
bitStream.Write(zoneID);
bitStream.Write(zoneInstance);
bitStream.Write(zoneClone);
bitStream.Write<uint16_t>(serverPort);
bitStream.Write(LUString(serverIP, 255));
stamps.Serialize(bitStream);
}
// SendServerInfo, with dServer's getters (int port, unsigned int zone, int instance) passed in
inline void SendServerInfo(RakNet::BitStream& bitStream, int port, unsigned int zoneID, int instanceID, ServiceType serverType, const std::string& ip) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SERVER_INFO);
bitStream.Write(port);
bitStream.Write(zoneID);
bitStream.Write(instanceID);
bitStream.Write(serverType);
bitStream.Write(LUString(ip));
}
// dMasterServer/MasterServer.cpp: a server went offline (to the dashboard)
inline void WriteServerOffline(RakNet::BitStream& bitStream, ServiceType type) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SERVER_INFO);
bitStream.Write<uint32_t>(0);
bitStream.Write<uint32_t>(0);
bitStream.Write<uint32_t>(0);
bitStream.Write(type);
bitStream.Write(LUString("offline"));
}
// dMasterServer/MasterServer.cpp: a world disconnected (to the dashboard)
inline void WriteWorldShutDown(RakNet::BitStream& bitStream, LWOMAPID mapID, LWOINSTANCEID instanceID) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SHUTDOWN_RESPONSE);
bitStream.Write(mapID);
bitStream.Write(instanceID);
}
// dMasterServer/MasterServer.cpp: WORLD_READY (to the dashboard)
inline void WriteWorldReadyInfo(RakNet::BitStream& bitStream, LWOMAPID zoneID, LWOINSTANCEID instanceID, LWOCLONEID cloneID, const std::string& ip, uint32_t port, bool isPrivate) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::WORLD_READY);
bitStream.Write(zoneID);
bitStream.Write(instanceID);
bitStream.Write(cloneID);
bitStream.Write(LUString(ip));
bitStream.Write(port);
bitStream.Write<uint8_t>(isPrivate ? 1 : 0);
}
// dMasterServer/MasterServer.cpp SET_SESSION_KEY, and dNet/AuthPackets.cpp
inline void WriteSessionKey(RakNet::BitStream& bitStream, MessageType::Master type, uint32_t sessionKey, const LUString& username) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, type);
bitStream.Write(sessionKey);
bitStream.Write(username);
}
// dMasterServer/MasterServer.cpp REQUEST_SESSION_KEY
inline void WriteSessionKeyResponse(RakNet::BitStream& bitStream, uint32_t sessionKey, const LUWString& username) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SESSION_KEY_RESPONSE);
bitStream.Write(sessionKey);
bitStream.Write(username);
}
// dWorldServer/WorldServer.cpp
inline void WriteRequestSessionKey(RakNet::BitStream& bitStream, const LUWString& username) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::REQUEST_SESSION_KEY);
bitStream.Write(username);
}
// dWorldServer/WorldServer.cpp and dMasterServer/MasterServer.cpp (PLAYER_ADDED and PLAYER_REMOVED)
inline void WritePlayerCount(RakNet::BitStream& bitStream, MessageType::Master type, LWOMAPID zoneID, LWOINSTANCEID instanceID) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, type);
bitStream.Write<LWOMAPID>(zoneID);
bitStream.Write<LWOINSTANCEID>(instanceID);
}
// dMasterServer/InstanceManager.cpp and dWorldServer/WorldServer.cpp (AFFIRM_TRANSFER_REQUEST and _RESPONSE)
inline void WriteAffirmTransfer(RakNet::BitStream& bitStream, MessageType::Master type, uint64_t requestID) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, type);
bitStream.Write(requestID);
}
// Messages with nothing after the header: SHUTDOWN, SHUTDOWN_RESPONSE (world), SHUTDOWN_UNIVERSE,
// REQUEST_SERVER_LIST, CONFIG_RELOAD, DASHBOARD_SHUTDOWN
inline void WriteEmpty(RakNet::BitStream& bitStream, MessageType::Master type) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, type);
}
// dGame/dComponents/RocketLaunchpadControlComponent.cpp
inline void TellMasterToPrepZone(RakNet::BitStream& bitStream, int zoneID) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::PREP_ZONE);
bitStream.Write(zoneID);
}
// dDashboardServer/routes/LiveWorld.cpp
inline void WriteInstanceShutdown(RakNet::BitStream& bitStream, uint32_t zone, uint32_t instance) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::INSTANCE_SHUTDOWN);
bitStream.Write(zone);
bitStream.Write(instance);
}
struct ServerListInstance {
LWOMAPID mapID{};
LWOINSTANCEID instanceID{};
LWOCLONEID cloneID{};
uint32_t players{};
std::string ip;
uint32_t port{};
bool isPrivate{};
};
// dMasterServer/MasterServer.cpp REQUEST_SERVER_LIST, with the ready instances passed in
inline void WriteServerList(RakNet::BitStream& bitStream, bool authOnline, bool chatOnline, const std::vector<ServerListInstance>& instances) {
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SERVER_LIST_RESPONSE);
bitStream.Write<uint8_t>(authOnline ? 1 : 0);
bitStream.Write<uint8_t>(chatOnline ? 1 : 0);
uint32_t instanceCount = instances.size();
bitStream.Write(instanceCount);
for (const auto& inst : instances) {
bitStream.Write(inst.mapID);
bitStream.Write(inst.instanceID);
bitStream.Write(inst.cloneID);
bitStream.Write<uint32_t>(inst.players);
bitStream.Write(LUString(inst.ip));
bitStream.Write(inst.port);
bitStream.Write<uint8_t>(inst.isPrivate ? 1 : 0);
}
}
// Readers. The stream is past the packet header.
struct TransferRequestRead {
uint64_t requestID = 0;
uint8_t mythranShift = false;
uint32_t zoneID = 0;
uint32_t zoneClone = 0;
Stamps stamps;
};
// dMasterServer/MasterServer.cpp REQUEST_ZONE_TRANSFER
inline TransferRequestRead ReadZoneTransferRequest(RakNet::BitStream& inStream) {
uint64_t requestID = 0;
uint8_t mythranShift = false;
uint32_t zoneID = 0;
uint32_t zoneClone = 0;
inStream.Read(requestID);
inStream.Read(mythranShift);
inStream.Read(zoneID);
inStream.Read(zoneClone);
// The login stamps travelling with the request (see Stamps.h)
Stamps stamps;
if (!stamps.Deserialize(inStream)) stamps = {};
return { requestID, mythranShift, zoneID, zoneClone, stamps };
}
struct TransferResponseRead {
uint64_t requestID{};
bool mythranShift{};
uint32_t zoneID{};
uint32_t zoneInstance{};
uint32_t zoneClone{};
uint16_t serverPort{};
std::string serverIP;
Stamps stamps;
};
// dNet/ZoneInstanceManager.cpp HandleRequestZoneTransferResponse
inline TransferResponseRead ReadZoneTransferResponse(RakNet::BitStream& inStream) {
uint64_t requestID;
inStream.Read(requestID);
bool mythranShift;
uint8_t tmp;
inStream.Read(tmp);
mythranShift = tmp > 0;
uint32_t zoneID;
inStream.Read(zoneID);
uint32_t zoneInstance;
inStream.Read(zoneInstance);
uint32_t zoneClone;
inStream.Read(zoneClone);
uint16_t serverPort;
inStream.Read(serverPort);
LUString serverIP(255);
inStream.Read(serverIP);
Stamps stamps;
if (!stamps.Deserialize(inStream)) stamps = {};
return { requestID, mythranShift, zoneID, zoneInstance, zoneClone, serverPort, serverIP.string, stamps };
}
struct ServerInfoRead {
uint32_t theirPort = 0;
uint32_t theirZoneID = 0;
uint32_t theirInstanceID = 0;
ServiceType theirServerType;
std::string theirIP;
};
// dMasterServer/MasterServer.cpp and dDashboardServer/DashboardServer.cpp SERVER_INFO
inline ServerInfoRead ReadServerInfo(RakNet::BitStream& inStream) {
uint32_t theirPort = 0;
uint32_t theirZoneID = 0;
uint32_t theirInstanceID = 0;
ServiceType theirServerType;
LUString theirIP;
inStream.Read(theirPort);
inStream.Read(theirZoneID);
inStream.Read(theirInstanceID);
inStream.Read(theirServerType);
inStream.Read(theirIP);
return { theirPort, theirZoneID, theirInstanceID, theirServerType, theirIP.string };
}
// dMasterServer/MasterServer.cpp CREATE_PRIVATE_ZONE
inline std::pair<std::pair<uint32_t, LWOCLONEID>, std::string> ReadCreatePrivateZone(RakNet::BitStream& inStream) {
uint32_t mapId;
LWOCLONEID cloneId;
std::string password;
inStream.Read(mapId);
inStream.Read(cloneId);
uint32_t len;
inStream.Read<uint32_t>(len);
len = std::min<uint32_t>(len, 50); // cap the master password at 50 characters
for (uint32_t i = 0; len > i; i++) {
char character;
inStream.Read<char>(character);
password += character;
}
return { { mapId, cloneId }, password };
}
// dMasterServer/MasterServer.cpp REQUEST_PRIVATE_ZONE
inline std::pair<std::pair<uint64_t, uint8_t>, std::string> ReadRequestPrivateZone(RakNet::BitStream& inStream) {
uint64_t requestID = 0;
uint8_t mythranShift = false;
std::string password;
inStream.Read(requestID);
inStream.Read(mythranShift);
uint32_t len;
inStream.Read<uint32_t>(len);
len = std::min<uint32_t>(len, 50);
for (uint32_t i = 0; i < len; i++) {
char character; inStream.Read<char>(character);
password += character;
}
return { { requestID, mythranShift }, password };
}
struct ServerListRead {
uint8_t authOnline = 0;
uint8_t chatOnline = 0;
std::vector<ServerListInstance> instances;
};
// dDashboardServer/DashboardServer.cpp SERVER_LIST_RESPONSE
inline ServerListRead ReadServerList(RakNet::BitStream& inStream) {
ServerListRead out;
uint8_t authOnline = 0;
uint8_t chatOnline = 0;
uint32_t instanceCount = 0;
inStream.Read(authOnline);
inStream.Read(chatOnline);
inStream.Read(instanceCount);
out.authOnline = authOnline;
out.chatOnline = chatOnline;
for (uint32_t i = 0; i < instanceCount; i++) {
ServerListInstance info;
LUString ip;
// Same types as MasterServer writes them (map and instance IDs are 16 bits)
LWOMAPID mapID = 0;
LWOINSTANCEID instanceID = 0;
inStream.Read(mapID);
inStream.Read(instanceID);
info.mapID = mapID;
info.instanceID = instanceID;
inStream.Read(info.cloneID);
inStream.Read(info.players);
inStream.Read(ip);
info.ip = ip.string;
inStream.Read(info.port);
uint8_t isPrivate = 0;
inStream.Read(isPrivate);
info.isPrivate = isPrivate != 0;
out.instances.push_back(info);
}
return out;
}
// dDashboardServer/DashboardServer.cpp WORLD_READY
inline ServerListInstance ReadWorldReadyInfo(RakNet::BitStream& inStream) {
LWOMAPID zoneID;
LWOINSTANCEID instanceID;
LWOCLONEID cloneID;
LUString ip;
uint32_t port;
uint8_t isPrivate;
inStream.Read(zoneID);
inStream.Read(instanceID);
inStream.Read(cloneID);
inStream.Read(ip);
inStream.Read(port);
inStream.Read(isPrivate);
return { zoneID, instanceID, cloneID, 0, ip.string, port, isPrivate != 0 };
}
}
#endif // MASTERPACKETSLEGACY_H

View File

@@ -0,0 +1,472 @@
#include "MasterPackets.h"
#include "master/DashboardMessages.h"
#include "master/DataChanged.h"
#include "master/InstanceMigration.h"
#include "master/MessageCapture.h"
#include "master/PlayerAction.h"
#include "PacketDispatcher.h"
#include "PacketTestUtils.h"
#include "Game.h"
#include "Logger.h"
#include "dGameMessagesTests/GameMessageTestUtils.h"
#include "Legacy/MasterPacketsLegacy.h"
#include <array>
#include <functional>
#include <limits>
#include <gtest/gtest.h>
using namespace PacketTestUtils;
using GameMessageTestUtils::RoundTrip;
using GameMessageTestUtils::ExpectTruncatedFails;
namespace {
const std::array<std::string, 4> g_Strings = { "", "localhost", "a password that is longer than fifty characters, which master cuts", "10.0.0.255" };
const std::array<uint64_t, 3> g_RequestIds = { 0, 1, 0x0102030405060708ULL };
const std::array<uint32_t, 3> g_Numbers = { 0, 1100, 0xFFFFFFFF };
PacketBytes StructPacket(const LUBitStream& msg) {
RakNet::BitStream bitStream;
msg.WritePacket(bitStream);
return FromBitStream(bitStream);
}
PacketBytes Written(const std::function<void(RakNet::BitStream&)>& write) {
RakNet::BitStream bitStream;
write(bitStream);
return FromBitStream(bitStream);
}
// A stream positioned after the header of msg's packet
void LoadPayload(RakNet::BitStream& bitStream, const LUBitStream& msg) {
msg.WritePacket(bitStream);
bitStream.IgnoreBytes(8);
}
// The old way the dashboard and migration structs were sent: the header, then Serialize
template<typename T>
void ExpectHeaderThenSerialize(const T& msg, MessageType::Master id) {
RakNet::BitStream old;
BitStreamUtils::WriteHeader(old, ServiceType::MASTER, id);
msg.Serialize(old);
EXPECT_PACKET_EQ(FromBitStream(old), StructPacket(msg));
RoundTrip(msg);
}
}
TEST(MasterPacketsTests, ZoneTransferMatchesLegacy) {
Stamps someStamps;
someStamps.list.emplace_back(eStamps::PASSPORT_AUTH_START, 0, 1700000000);
someStamps.list.emplace_back(eStamps::PASSPORT_AUTH_WORLD_PACKET_RECEIVED, 1100, 1700000001);
for (const auto& stamps : { Stamps{}, someStamps })
for (const auto requestID : g_RequestIds) {
for (const bool mythranShift : { false, true }) {
for (const auto zone : g_Numbers) {
MasterPackets::RequestZoneTransfer request;
request.requestID = requestID;
request.mythranShift = mythranShift;
request.zoneID = zone;
request.cloneID = zone ^ 0x5555;
request.stamps = stamps;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::SendZoneTransferRequest(b, requestID, mythranShift, zone, zone ^ 0x5555, stamps); }), StructPacket(request));
RakNet::BitStream stream; LoadPayload(stream, request);
const auto legacy = LegacyMaster::ReadZoneTransferRequest(stream);
const auto copy = RoundTrip(request);
EXPECT_EQ(legacy.requestID, copy.requestID);
EXPECT_EQ(legacy.mythranShift, copy.mythranShift);
EXPECT_EQ(legacy.zoneID, copy.zoneID);
EXPECT_EQ(legacy.zoneClone, copy.cloneID);
EXPECT_EQ(legacy.stamps.size(), copy.stamps.size());
for (const auto& ip : g_Strings) {
for (const uint32_t port : { 0u, 2001u, 70000u }) {
MasterPackets::RequestZoneTransferResponse response;
response.requestID = requestID;
response.mythranShift = mythranShift;
response.zoneID = zone;
response.zoneInstance = zone / 2;
response.zoneClone = zone / 3;
response.serverPort = static_cast<uint16_t>(port);
response.serverIP = LUString(ip, 255);
response.stamps = stamps;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::SendZoneTransferResponse(b, requestID, mythranShift, zone, zone / 2, zone / 3, ip, port, stamps); }), StructPacket(response));
RakNet::BitStream responseStream; LoadPayload(responseStream, response);
const auto legacyResponse = LegacyMaster::ReadZoneTransferResponse(responseStream);
const auto responseCopy = RoundTrip(response);
EXPECT_EQ(legacyResponse.requestID, responseCopy.requestID);
EXPECT_EQ(legacyResponse.mythranShift, responseCopy.mythranShift > 0);
EXPECT_EQ(legacyResponse.zoneInstance, responseCopy.zoneInstance);
EXPECT_EQ(legacyResponse.serverPort, responseCopy.serverPort);
EXPECT_EQ(legacyResponse.serverIP, responseCopy.serverIP.string);
EXPECT_EQ(legacyResponse.stamps.size(), responseCopy.stamps.size());
}
}
}
}
}
}
// Senders without the login stamps (older servers) still read, with no stamps; a cut off fixed part does not
TEST(MasterPacketsTests, ZoneTransferStampsAreOptional) {
RakNet::BitStream noStamps;
noStamps.Write<uint64_t>(5);
noStamps.Write<uint8_t>(1);
noStamps.Write<uint32_t>(1100);
noStamps.Write<uint32_t>(0);
MasterPackets::RequestZoneTransfer request;
ASSERT_TRUE(request.Deserialize(noStamps));
EXPECT_EQ(request.requestID, 5u);
EXPECT_TRUE(request.stamps.empty());
MasterPackets::RequestZoneTransfer full;
full.requestID = 5;
RakNet::BitStream all;
full.Serialize(all);
for (uint32_t bits = 0; bits < 8 * (8 + 1 + 4 + 4); bits++) {
RakNet::BitStream prefix;
prefix.WriteBits(all.GetData(), bits, false);
MasterPackets::RequestZoneTransfer copy;
EXPECT_FALSE(copy.Deserialize(prefix)) << bits;
}
}
TEST(MasterPacketsTests, ServerInfoMatchesLegacy) {
for (const auto type : { ServiceType::AUTH, ServiceType::CHAT, ServiceType::WORLD, ServiceType::DASHBOARD }) {
for (const auto& ip : g_Strings) {
if (ip.size() > 33) continue; // LUString(33)
for (const int instance : { 0, 7, -1 }) {
MasterPackets::ServerInfo info;
info.port = 2001;
info.zoneID = 1100;
info.instanceID = static_cast<uint32_t>(instance);
info.serverType = type;
info.ip = LUString(ip);
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::SendServerInfo(b, 2001, 1100u, instance, type, ip); }), StructPacket(info));
RakNet::BitStream stream; LoadPayload(stream, info);
const auto legacy = LegacyMaster::ReadServerInfo(stream);
const auto copy = RoundTrip(info);
EXPECT_EQ(legacy.theirPort, copy.port);
EXPECT_EQ(legacy.theirInstanceID, copy.instanceID);
EXPECT_EQ(legacy.theirServerType, copy.serverType);
EXPECT_EQ(legacy.theirIP, copy.ip.string);
ExpectTruncatedFails(info);
}
}
MasterPackets::ServerInfo offline;
offline.serverType = type;
offline.ip = LUString("offline");
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteServerOffline(b, type); }), StructPacket(offline));
}
}
TEST(MasterPacketsTests, SessionKeysMatchLegacy) {
for (const uint32_t key : { 0u, 12345u, 0xFFFFFFFFu }) {
for (const auto& name : g_Strings) {
if (name.size() > 33) continue;
MasterPackets::SetSessionKey set;
set.sessionKey = key;
set.username = LUString(name);
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteSessionKey(b, MessageType::Master::SET_SESSION_KEY, key, LUString(name)); }), StructPacket(set));
RoundTrip(set);
ExpectTruncatedFails(set);
MasterPackets::NewSessionAlert alert;
alert.sessionKey = key;
alert.username = LUString(name);
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteSessionKey(b, MessageType::Master::NEW_SESSION_ALERT, key, LUString(name)); }), StructPacket(alert));
RoundTrip(alert);
MasterPackets::SessionKeyResponse response;
response.sessionKey = key;
response.username = LUWString(name);
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteSessionKeyResponse(b, key, LUWString(name)); }), StructPacket(response));
RoundTrip(response);
ExpectTruncatedFails(response);
MasterPackets::RequestSessionKey request;
request.username = LUWString(name);
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteRequestSessionKey(b, LUWString(name)); }), StructPacket(request));
RoundTrip(request);
ExpectTruncatedFails(request);
}
}
}
TEST(MasterPacketsTests, PrivateZonesMatchLegacy) {
for (const auto& password : g_Strings) {
MasterPackets::CreatePrivateZone create;
create.zoneID = 1150;
create.cloneID = 0xAABBCCDD;
create.password = password;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::SendZoneCreatePrivate(b, 1150, 0xAABBCCDD, password); }), StructPacket(create));
RakNet::BitStream stream; LoadPayload(stream, create);
const auto legacy = LegacyMaster::ReadCreatePrivateZone(stream);
RakNet::BitStream stream2; LoadPayload(stream2, create);
MasterPackets::CreatePrivateZone read;
ASSERT_TRUE(read.Deserialize(stream2));
EXPECT_EQ(legacy.first.first, read.zoneID);
EXPECT_EQ(legacy.first.second, read.cloneID);
EXPECT_EQ(legacy.second, read.password); // both cut at 50 characters
if (password.size() <= 50) RoundTrip(create);
for (const bool mythranShift : { false, true }) {
MasterPackets::RequestPrivateZone request;
request.requestID = 0x0102030405060708ULL;
request.mythranShift = mythranShift;
request.password = password;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::SendZoneRequestPrivate(b, 0x0102030405060708ULL, mythranShift, password); }), StructPacket(request));
RakNet::BitStream requestStream; LoadPayload(requestStream, request);
const auto legacyRequest = LegacyMaster::ReadRequestPrivateZone(requestStream);
RakNet::BitStream requestStream2; LoadPayload(requestStream2, request);
MasterPackets::RequestPrivateZone readRequest;
ASSERT_TRUE(readRequest.Deserialize(requestStream2));
EXPECT_EQ(legacyRequest.first.first, readRequest.requestID);
EXPECT_EQ(legacyRequest.first.second, readRequest.mythranShift);
EXPECT_EQ(legacyRequest.second, readRequest.password);
if (password.size() <= 50) RoundTrip(request);
}
}
}
TEST(MasterPacketsTests, WorldStateMatchesLegacy) {
for (const LWOMAPID zone : { static_cast<LWOMAPID>(0), static_cast<LWOMAPID>(1100), static_cast<LWOMAPID>(0xFFFF) }) {
for (const LWOINSTANCEID instance : { static_cast<LWOINSTANCEID>(0), static_cast<LWOINSTANCEID>(3), static_cast<LWOINSTANCEID>(0xFFFF) }) {
MasterPackets::WorldReady ready;
ready.zoneID = zone;
ready.instanceID = instance;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::SendWorldReady(b, zone, instance); }), StructPacket(ready));
RoundTrip(ready);
ExpectTruncatedFails(ready);
MasterPackets::PlayerAdded added;
added.zoneID = zone;
added.instanceID = instance;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WritePlayerCount(b, MessageType::Master::PLAYER_ADDED, zone, instance); }), StructPacket(added));
RoundTrip(added);
ExpectTruncatedFails(added);
MasterPackets::PlayerRemoved removed;
removed.zoneID = zone;
removed.instanceID = instance;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WritePlayerCount(b, MessageType::Master::PLAYER_REMOVED, zone, instance); }), StructPacket(removed));
RoundTrip(removed);
MasterPackets::WorldShutDown shutDown;
shutDown.zoneID = zone;
shutDown.instanceID = instance;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteWorldShutDown(b, zone, instance); }), StructPacket(shutDown));
RoundTrip(shutDown);
for (const bool isPrivate : { false, true }) {
MasterPackets::WorldReadyInfo info;
info.zoneID = zone;
info.instanceID = instance;
info.cloneID = 0xDEADBEEF;
info.ip = LUString("127.0.0.1");
info.port = 3000;
info.isPrivate = isPrivate ? 1 : 0;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteWorldReadyInfo(b, zone, instance, 0xDEADBEEF, "127.0.0.1", 3000, isPrivate); }), StructPacket(info));
RakNet::BitStream stream; LoadPayload(stream, info);
const auto legacy = LegacyMaster::ReadWorldReadyInfo(stream);
const auto copy = RoundTrip(info);
EXPECT_EQ(legacy.cloneID, copy.cloneID);
EXPECT_EQ(legacy.ip, copy.ip.string);
EXPECT_EQ(legacy.isPrivate, copy.isPrivate != 0);
ExpectTruncatedFails(info);
}
}
}
for (const int zone : { 0, 1100, -1 }) {
MasterPackets::PrepZone prep;
prep.zoneID = zone;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::TellMasterToPrepZone(b, zone); }), StructPacket(prep));
RoundTrip(prep);
ExpectTruncatedFails(prep);
}
for (const auto requestID : g_RequestIds) {
MasterPackets::AffirmTransferRequest request;
request.requestID = requestID;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteAffirmTransfer(b, MessageType::Master::AFFIRM_TRANSFER_REQUEST, requestID); }), StructPacket(request));
RoundTrip(request);
ExpectTruncatedFails(request);
MasterPackets::AffirmTransferResponse response;
response.requestID = requestID;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteAffirmTransfer(b, MessageType::Master::AFFIRM_TRANSFER_RESPONSE, requestID); }), StructPacket(response));
RoundTrip(response);
}
for (const auto zone : g_Numbers) {
MasterPackets::InstanceShutdown shutdown;
shutdown.zoneID = zone;
shutdown.instanceID = zone / 7;
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteInstanceShutdown(b, zone, zone / 7); }), StructPacket(shutdown));
RoundTrip(shutdown);
ExpectTruncatedFails(shutdown);
}
}
TEST(MasterPacketsTests, EmptyMessagesMatchLegacy) {
EXPECT_PACKET_EQ(Written([](RakNet::BitStream& b) { LegacyMaster::WriteEmpty(b, MessageType::Master::SHUTDOWN); }), StructPacket(MasterPackets::Shutdown()));
EXPECT_PACKET_EQ(Written([](RakNet::BitStream& b) { LegacyMaster::WriteEmpty(b, MessageType::Master::SHUTDOWN_RESPONSE); }), StructPacket(MasterPackets::ShutdownResponse()));
EXPECT_PACKET_EQ(Written([](RakNet::BitStream& b) { LegacyMaster::WriteEmpty(b, MessageType::Master::SHUTDOWN_UNIVERSE); }), StructPacket(MasterPackets::ShutdownUniverse()));
EXPECT_PACKET_EQ(Written([](RakNet::BitStream& b) { LegacyMaster::WriteEmpty(b, MessageType::Master::REQUEST_SERVER_LIST); }), StructPacket(MasterPackets::RequestServerList()));
EXPECT_PACKET_EQ(Written([](RakNet::BitStream& b) { LegacyMaster::WriteEmpty(b, MessageType::Master::CONFIG_RELOAD); }), StructPacket(MasterPackets::ConfigReload()));
EXPECT_PACKET_EQ(Written([](RakNet::BitStream& b) { LegacyMaster::WriteEmpty(b, MessageType::Master::DASHBOARD_SHUTDOWN); }), StructPacket(MasterPackets::DashboardShutdown()));
}
TEST(MasterPacketsTests, Golden) {
MasterPackets::PlayerAdded added;
added.zoneID = 1100;
added.instanceID = 2;
// 53 | MASTER (6) | PLAYER_ADDED (7) | pad | u16 1100 | u16 2
EXPECT_PACKET_EQ(FromHex("53 06 00 07 00 00 00 00 4c 04 02 00"), StructPacket(added));
MasterPackets::AffirmTransferRequest affirm;
affirm.requestID = 0x0102030405060708ULL;
// AFFIRM_TRANSFER_REQUEST (17) | u64
EXPECT_PACKET_EQ(FromHex("53 06 00 11 00 00 00 00 08 07 06 05 04 03 02 01"), StructPacket(affirm));
}
TEST(MasterPacketsTests, ServerListMatchesLegacy) {
for (size_t count = 0; count <= 3; count++) {
std::vector<LegacyMaster::ServerListInstance> instances;
MasterPackets::ServerListResponse response;
response.authOnline = 1;
response.chatOnline = count % 2;
for (size_t i = 0; i < count; i++) {
LegacyMaster::ServerListInstance instance{ static_cast<LWOMAPID>(1000 + i), static_cast<LWOINSTANCEID>(i), static_cast<LWOCLONEID>(i * 7), static_cast<uint32_t>(i * 3), "10.0.0." + std::to_string(i), static_cast<uint32_t>(3000 + i), i == 2 };
instances.push_back(instance);
auto& entry = response.instances.emplace_back();
entry.mapID = instance.mapID;
entry.instanceID = instance.instanceID;
entry.cloneID = instance.cloneID;
entry.players = instance.players;
entry.ip = LUString(instance.ip);
entry.port = instance.port;
entry.isPrivate = instance.isPrivate ? 1 : 0;
}
EXPECT_PACKET_EQ(Written([&](RakNet::BitStream& b) { LegacyMaster::WriteServerList(b, true, count % 2, instances); }), StructPacket(response));
RakNet::BitStream stream; LoadPayload(stream, response);
const auto legacy = LegacyMaster::ReadServerList(stream);
const auto copy = RoundTrip(response);
ASSERT_EQ(legacy.instances.size(), copy.instances.size());
for (size_t i = 0; i < count; i++) {
EXPECT_EQ(legacy.instances[i].mapID, copy.instances[i].mapID);
EXPECT_EQ(legacy.instances[i].players, copy.instances[i].players);
EXPECT_EQ(legacy.instances[i].ip, copy.instances[i].ip.string);
EXPECT_EQ(legacy.instances[i].isPrivate, copy.instances[i].isPrivate != 0);
}
ExpectTruncatedFails(response);
}
}
// The dashboard and migration structs kept their payloads; they now carry their own header
TEST(MasterPacketsTests, DashboardAndMigrationStructsKeepTheirBytes) {
PlayerActionRequest action;
action.requestId = 7;
action.action = ePlayerAction::CHAT_MESSAGE;
action.text = "hello";
action.name = "[Discord] Bob";
ExpectHeaderThenSerialize(action, MessageType::Master::PLAYER_ACTION);
PlayerActionResult result;
result.requestId = 7;
result.affected = 3;
result.timedOut = true;
ExpectHeaderThenSerialize(result, MessageType::Master::PLAYER_ACTION_RESULT);
DataChanged changed;
changed.entries.push_back({ "characters", 42 });
ExpectHeaderThenSerialize(changed, MessageType::Master::DATA_CHANGED);
PlayerPositions positions;
positions.zoneId = 1100;
positions.players.push_back({ 42, 1.0f, 2.0f, 3.0f });
ExpectHeaderThenSerialize(positions, MessageType::Master::PLAYER_POSITIONS);
Announcement announcement;
announcement.title = "Title";
announcement.message = "Message";
announcement.zones = { 1100, 1200 };
ExpectHeaderThenSerialize(announcement, MessageType::Master::ANNOUNCE);
MessageCaptureControl control;
control.captureId = 3;
control.seconds = 60;
control.only = { 1, 2 };
ExpectHeaderThenSerialize(control, MessageType::Master::MESSAGE_CAPTURE_CONTROL);
MessageCaptureData data;
data.captureId = 3;
data.entries.resize(1);
data.entries[0].payload = "abc";
ExpectHeaderThenSerialize(data, MessageType::Master::MESSAGE_CAPTURE_DATA);
InstanceMigrationRequest migrate;
migrate.requestId = 9;
migrate.requestedBy = "GM";
ExpectHeaderThenSerialize(migrate, MessageType::Master::INSTANCE_MIGRATE);
MigratePlayersOrder order;
order.targetIp = "127.0.0.1";
ExpectHeaderThenSerialize(order, MessageType::Master::MIGRATE_PLAYERS);
MigrationStatus status;
status.message = "Moving players";
ExpectHeaderThenSerialize(status, MessageType::Master::MIGRATE_STATUS);
CarriedPlayerState state;
state.characterId = 42;
ExpectHeaderThenSerialize(state, MessageType::Master::MIGRATE_PLAYER_STATE);
}
TEST(MasterPacketsTests, DispatcherRoutesAndDropsBadPackets) {
// Dropped packets are logged
auto* const previousLogger = Game::logger;
Game::logger = new Logger("./testing.log", true, true);
PacketDispatcher<MessageType::Master> handlers;
int calls = 0;
MasterPackets::PlayerAdded seen;
handlers.On<MasterPackets::PlayerAdded>(MessageType::Master::PLAYER_ADDED, [&](const MasterPackets::PlayerAdded& msg, const SystemAddress&) { calls++; seen = msg; });
MasterPackets::PlayerAdded added;
added.zoneID = 1100;
added.instanceID = 4;
RakNet::BitStream good;
added.WritePacket(good);
Packet packet{};
packet.data = good.GetData();
packet.length = good.GetNumberOfBytesUsed();
EXPECT_TRUE(handlers.Dispatch(&packet, ServiceType::MASTER));
EXPECT_EQ(calls, 1);
EXPECT_EQ(seen.zoneID, 1100);
EXPECT_EQ(seen.instanceID, 4);
// Wrong service: not ours
EXPECT_FALSE(handlers.Dispatch(&packet, ServiceType::CHAT));
// Truncated: handled (dropped), the handler isn't called
packet.length -= 1;
EXPECT_TRUE(handlers.Dispatch(&packet, ServiceType::MASTER));
EXPECT_EQ(calls, 1);
// No handler for this ID
MasterPackets::PlayerRemoved removed;
RakNet::BitStream other;
removed.WritePacket(other);
packet.data = other.GetData();
packet.length = other.GetNumberOfBytesUsed();
EXPECT_FALSE(handlers.Dispatch(&packet, ServiceType::MASTER));
delete Game::logger;
Game::logger = previousLogger;
}