Files
DarkflameServer/dDashboardServer/routes/PlayerActions.cpp
Aaron Kimbrell 5a9a3e380b 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>
2026-09-28 22:30:50 -05:00

148 lines
5.1 KiB
C++

#include "PlayerActions.h"
#include "MasterPackets.h"
#include <chrono>
#include <deque>
#include <map>
#include "BitStreamUtils.h"
#include "Game.h"
#include "Logger.h"
#include "MessageType/Master.h"
#include "ServiceType.h"
#include "Web.h"
#include "dServer.h"
namespace {
// Master answers within its own 3s timeout; this only covers a lost connection
constexpr auto DASHBOARD_TIMEOUT = std::chrono::seconds(10);
constexpr size_t MAX_REMEMBERED_RESULTS = 200;
struct Pending {
PlayerActions::Completion onComplete;
ePlayerAction action{};
std::chrono::steady_clock::time_point deadline;
uint32_t owner{};
};
struct ResultData {
uint32_t owner{};
nlohmann::json data;
};
std::map<uint32_t, ResultData> g_ResultData;
uint32_t g_NextRequestId = 1;
std::map<uint32_t, Pending> g_Pending;
struct StoredResult {
uint32_t owner{};
nlohmann::json status;
};
std::map<uint32_t, StoredResult> g_Results;
std::deque<uint32_t> g_ResultOrder;
void Complete(uint32_t requestId, const PlayerActionResult& result, bool job = false) {
const auto it = g_Pending.find(requestId);
if (it == g_Pending.end()) return;
auto completion = std::move(it->second.onComplete);
const auto owner = it->second.owner;
g_Pending.erase(it);
PlayerActions::Outcome outcome;
try {
outcome = completion(result);
} catch (const std::exception& ex) {
LOG("Player action %u completion failed: %s", requestId, ex.what());
outcome = { false, "The action failed to complete" };
}
nlohmann::json status{
{"requestId", requestId},
{"status", "done"},
{"success", outcome.success},
{"message", outcome.message},
{"timedOut", result.timedOut}
};
if (outcome.affected) status["affected"] = *outcome.affected;
else if (!job) status["affected"] = result.affected;
status["hasData"] = !outcome.data.is_null();
g_Results[requestId] = { owner, status };
if (!outcome.data.is_null()) g_ResultData[requestId] = { owner, outcome.data };
g_ResultOrder.push_back(requestId);
if (g_ResultOrder.size() > MAX_REMEMBERED_RESULTS) {
g_Results.erase(g_ResultOrder.front());
g_ResultData.erase(g_ResultOrder.front());
g_ResultOrder.pop_front();
}
// Only the account that started it hears the outcome (messages can name players or email addresses)
Game::web.SendWSMessageToAccount("action_result", status, owner);
}
}
uint32_t PlayerActions::Request(PlayerActionRequest request, uint32_t ownerAccountId, Completion onComplete) {
request.requestId = g_NextRequestId++;
g_Pending[request.requestId] = { std::move(onComplete), request.action, std::chrono::steady_clock::now() + DASHBOARD_TIMEOUT, ownerAccountId };
// Without master no world server is running, so nobody can be online
if (!Game::server || !Game::server->GetIsConnectedToMaster()) {
PlayerActionResult result;
result.requestId = request.requestId;
result.action = request.action;
Complete(request.requestId, result);
return request.requestId;
}
MasterPackets::SendToMaster(request);
return request.requestId;
}
void PlayerActions::HandleResult(const PlayerActionResult& result) {
Complete(result.requestId, result);
}
void PlayerActions::Update() {
const auto now = std::chrono::steady_clock::now();
std::vector<std::pair<uint32_t, ePlayerAction>> expired;
for (const auto& [id, pending] : g_Pending) {
if (now >= pending.deadline) expired.emplace_back(id, pending.action);
}
for (const auto& [id, action] : expired) {
PlayerActionResult result;
result.requestId = id;
result.action = action;
result.timedOut = true;
Complete(id, result);
}
}
uint32_t PlayerActions::Begin(uint32_t ownerAccountId, std::chrono::seconds timeout) {
const auto requestId = g_NextRequestId++;
// Completed by Finish; the deadline only guards against work that never reports back
g_Pending[requestId] = { [](const PlayerActionResult&) { return Outcome{ false, "Timed out" }; }, ePlayerAction::KICK_ACCOUNT,
std::chrono::steady_clock::now() + timeout, ownerAccountId };
return requestId;
}
void PlayerActions::Finish(uint32_t requestId, const Outcome& outcome) {
const auto it = g_Pending.find(requestId);
if (it == g_Pending.end()) return;
it->second.onComplete = [outcome](const PlayerActionResult&) { return outcome; };
PlayerActionResult result;
result.requestId = requestId;
Complete(requestId, result, true);
}
nlohmann::json PlayerActions::GetStatus(uint32_t requestId, uint32_t requesterAccountId) {
// Request ids are sequential, so only the account that started a request may read it
const auto it = g_Results.find(requestId);
if (it != g_Results.end()) {
if (it->second.owner == 0 || it->second.owner != requesterAccountId) return { {"requestId", requestId}, {"status", "unknown"} };
auto status = it->second.status;
const auto data = g_ResultData.find(requestId);
if (data != g_ResultData.end()) status["data"] = data->second.data;
return status;
}
const auto pending = g_Pending.find(requestId);
if (pending != g_Pending.end() && pending->second.owner != 0 && pending->second.owner == requesterAccountId) return { {"requestId", requestId}, {"status", "pending"} };
return { {"requestId", requestId}, {"status", "unknown"} };
}