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 313f821203
commit d8e2a3ae42
54 changed files with 2275 additions and 1248 deletions

View File

@@ -21,6 +21,22 @@ using std::make_unique;
namespace {
const InstancePtr g_Empty{ nullptr };
// Where the player of request goes: instance
void SendZoneTransferResponse(const PendingInstanceRequest& request, const Instance& instance) {
const auto& zoneId = instance.GetZoneID();
MasterPackets::RequestZoneTransferResponse response;
response.requestID = request.id;
response.mythranShift = request.mythranShift;
response.zoneID = zoneId.GetMapID();
response.zoneInstance = zoneId.GetInstanceID();
response.zoneClone = zoneId.GetCloneID();
response.serverPort = static_cast<uint16_t>(instance.GetPort());
response.serverIP = LUString(instance.GetIP(), 255);
response.stamps = request.stamps;
response.stamps.Add(eStamps::PASSPORT_AUTH_WORLD_SESSION_CONFIRM_TO_AUTH, zoneId.GetInstanceID());
MasterPackets::SendTo(request.sysAddr, response);
}
}
InstanceManager::InstanceManager(const std::string& externalIP) : mExternalIP{ externalIP } {
@@ -156,20 +172,7 @@ void InstanceManager::ReadyInstance(const InstancePtr& instance) {
LOG("Responding to pending request %llu -> %i (%i)", request, zoneId.GetMapID(), zoneId.GetCloneID());
auto stamps = request.stamps;
stamps.Add(eStamps::PASSPORT_AUTH_WORLD_SESSION_CONFIRM_TO_AUTH, zoneId.GetInstanceID());
MasterPackets::SendZoneTransferResponse(
Game::server,
request.sysAddr,
request.id,
request.mythranShift,
zoneId.GetMapID(),
zoneId.GetInstanceID(),
zoneId.GetCloneID(),
instance->GetIP(),
instance->GetPort(),
stamps
);
SendZoneTransferResponse(request, *instance);
}
pending.clear();
@@ -178,13 +181,9 @@ void InstanceManager::ReadyInstance(const InstancePtr& instance) {
void InstanceManager::RequestAffirmation(const InstancePtr& instance, const PendingInstanceRequest& request) {
instance->GetPendingAffirmations().push_back(request);
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::AFFIRM_TRANSFER_REQUEST);
bitStream.Write(request.id);
Game::server->Send(bitStream, instance->GetSysAddr(), false);
MasterPackets::AffirmTransferRequest affirm;
affirm.requestID = request.id;
MasterPackets::SendTo(instance->GetSysAddr(), affirm);
LOG("Sent affirmation request %llu to %i/%i", request.id,
static_cast<int>(instance->GetZoneID().GetMapID()),
@@ -200,22 +199,7 @@ void InstanceManager::AffirmTransfer(const InstancePtr& instance, const uint64_t
if (request.id != transferID) continue;
const auto& zoneId = instance->GetZoneID();
auto stamps = request.stamps;
stamps.Add(eStamps::PASSPORT_AUTH_WORLD_SESSION_CONFIRM_TO_AUTH, zoneId.GetInstanceID());
MasterPackets::SendZoneTransferResponse(
Game::server,
request.sysAddr,
request.id,
request.mythranShift,
zoneId.GetMapID(),
zoneId.GetInstanceID(),
zoneId.GetCloneID(),
instance->GetIP(),
instance->GetPort(),
stamps
);
SendZoneTransferResponse(request, *instance);
pending.erase(pending.begin() + i);
@@ -240,7 +224,7 @@ void InstanceManager::RedirectPendingRequests(const InstancePtr& instance) {
}
}
const InstancePtr& InstanceManager::GetInstanceBySysAddr(SystemAddress& sysAddr) {
const InstancePtr& InstanceManager::GetInstanceBySysAddr(const SystemAddress& sysAddr) {
for (const auto& instance : m_Instances) {
if (instance && instance->GetSysAddr() == sysAddr) {
return instance;
@@ -418,11 +402,7 @@ bool Instance::GetShutdownComplete() const {
}
void Instance::Shutdown() {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SHUTDOWN);
Game::server->Send(bitStream, this->m_SysAddr, false);
MasterPackets::SendTo(this->m_SysAddr, MasterPackets::Shutdown());
LOG("Triggered world shutdown for zone/clone/instance %i/%i/%i", GetMapID(), GetCloneID(), GetInstanceID());
}