mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-25 22:22:48 +00:00
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:
111
dNet/master/DashboardMessages.h
Normal file
111
dNet/master/DashboardMessages.h
Normal file
@@ -0,0 +1,111 @@
|
||||
#ifndef __DASHBOARDMESSAGES__H__
|
||||
#define __DASHBOARDMESSAGES__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "BitStreamUtils.h"
|
||||
#include "MessageType/Master.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
namespace DashboardMessages {
|
||||
// Length-prefixed string, capped so a bad packet can't ask for huge allocations
|
||||
inline void WriteText(RakNet::BitStream& stream, const std::string& text, uint16_t max) {
|
||||
const auto length = static_cast<uint16_t>(std::min<size_t>(text.size(), max));
|
||||
stream.Write(length);
|
||||
stream.Write(text.data(), length);
|
||||
}
|
||||
|
||||
inline bool ReadText(RakNet::BitStream& stream, std::string& text, uint16_t max) {
|
||||
uint16_t length{};
|
||||
if (!stream.Read(length) || length > max) return false;
|
||||
text.resize(length);
|
||||
return length == 0 || stream.Read(text.data(), length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PLAYER_POSITIONS (world -> master -> dashboard): where each player in a world instance is, sent every
|
||||
* second while anyone is online and once more when the world empties.
|
||||
*/
|
||||
struct PlayerPositions : public LUBitStream {
|
||||
PlayerPositions() : LUBitStream(ServiceType::MASTER, MessageType::Master::PLAYER_POSITIONS) {}
|
||||
|
||||
struct Player {
|
||||
LWOOBJID characterId{};
|
||||
float x{}, y{}, z{};
|
||||
};
|
||||
|
||||
static constexpr uint16_t MAX_PLAYERS = 1000;
|
||||
|
||||
uint32_t zoneId{};
|
||||
uint32_t instanceId{};
|
||||
uint32_t cloneId{};
|
||||
std::vector<Player> players;
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(zoneId);
|
||||
stream.Write(instanceId);
|
||||
stream.Write(cloneId);
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(players.size(), MAX_PLAYERS));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) {
|
||||
stream.Write(players[i].characterId);
|
||||
stream.Write(players[i].x);
|
||||
stream.Write(players[i].y);
|
||||
stream.Write(players[i].z);
|
||||
}
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(zoneId) || !stream.Read(instanceId) || !stream.Read(cloneId) || !stream.Read(count) || count > MAX_PLAYERS) return false;
|
||||
players.resize(count);
|
||||
for (auto& player : players) {
|
||||
if (!stream.Read(player.characterId) || !stream.Read(player.x) || !stream.Read(player.y) || !stream.Read(player.z)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ANNOUNCE (dashboard -> master -> worlds): a message shown to every player online, or only to those in the listed
|
||||
* zones (master only forwards it to worlds of those zones).
|
||||
*/
|
||||
struct Announcement : public LUBitStream {
|
||||
Announcement() : LUBitStream(ServiceType::MASTER, MessageType::Master::ANNOUNCE) {}
|
||||
|
||||
static constexpr uint16_t MAX_TITLE = 100;
|
||||
static constexpr uint16_t MAX_MESSAGE = 1000;
|
||||
static constexpr uint16_t MAX_ZONES = 200;
|
||||
|
||||
std::string title;
|
||||
std::string message;
|
||||
std::vector<uint32_t> zones; // empty: every world
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
DashboardMessages::WriteText(stream, title, MAX_TITLE);
|
||||
DashboardMessages::WriteText(stream, message, MAX_MESSAGE);
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(zones.size(), MAX_ZONES));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) stream.Write(zones[i]);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
if (!DashboardMessages::ReadText(stream, title, MAX_TITLE) || !DashboardMessages::ReadText(stream, message, MAX_MESSAGE)) return false;
|
||||
uint16_t count{};
|
||||
if (!stream.Read(count) || count > MAX_ZONES) return false;
|
||||
zones.resize(count);
|
||||
for (auto& zone : zones) if (!stream.Read(zone)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShownIn(uint32_t zoneId) const {
|
||||
return zones.empty() || std::find(zones.begin(), zones.end(), zoneId) != zones.end();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__DASHBOARDMESSAGES__H__
|
||||
61
dNet/master/DataChanged.h
Normal file
61
dNet/master/DataChanged.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef __DATACHANGED__H__
|
||||
#define __DATACHANGED__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "BitStreamUtils.h"
|
||||
#include "MessageType/Master.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* DATA_CHANGED payload: the tables and rows a game server just wrote. The dashboard turns each entry into a
|
||||
* table_changed event so open pages refresh without waiting for its periodic database check.
|
||||
*/
|
||||
struct DataChanged : public LUBitStream {
|
||||
DataChanged() : LUBitStream(ServiceType::MASTER, MessageType::Master::DATA_CHANGED) {}
|
||||
|
||||
struct Entry {
|
||||
std::string table; // dashboard table name, e.g. "characters", "mail", "economy"
|
||||
LWOOBJID id{}; // row the change is about, 0 for the table as a whole
|
||||
};
|
||||
|
||||
static constexpr size_t MAX_ENTRIES = 1024;
|
||||
static constexpr size_t MAX_TABLE_NAME = 32;
|
||||
|
||||
std::vector<Entry> entries;
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
const auto count = static_cast<uint16_t>(std::min(entries.size(), MAX_ENTRIES));
|
||||
stream.Write(count);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const auto& entry = entries[i];
|
||||
const auto length = static_cast<uint8_t>(std::min(entry.table.size(), MAX_TABLE_NAME));
|
||||
stream.Write(length);
|
||||
stream.Write(entry.table.data(), length);
|
||||
stream.Write(entry.id);
|
||||
}
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(count) || count > MAX_ENTRIES) return false;
|
||||
entries.clear();
|
||||
entries.reserve(count);
|
||||
for (uint16_t i = 0; i < count; i++) {
|
||||
uint8_t length{};
|
||||
if (!stream.Read(length) || length > MAX_TABLE_NAME) return false;
|
||||
Entry entry;
|
||||
entry.table.resize(length);
|
||||
if (length > 0 && !stream.Read(entry.table.data(), length)) return false;
|
||||
if (!stream.Read(entry.id)) return false;
|
||||
entries.push_back(std::move(entry));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__DATACHANGED__H__
|
||||
405
dNet/master/InstanceMigration.h
Normal file
405
dNet/master/InstanceMigration.h
Normal file
@@ -0,0 +1,405 @@
|
||||
#ifndef __INSTANCEMIGRATION__H__
|
||||
#define __INSTANCEMIGRATION__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "BitStreamUtils.h"
|
||||
#include "MessageType/Master.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* Moving everyone in one world instance to another instance of the same zone: to replace an instance (a live update:
|
||||
* a fresh world server, possibly a newer binary, takes over) or to merge two quiet instances into one.
|
||||
*
|
||||
* Normally the client shows a loading screen (see docs/SeamlessTransfer.md): the source world saves each player,
|
||||
* locks them and sends TRANSFER_TO_WORLD with the "Mythran shift" flag, which shows the client's own "Mythran
|
||||
* Dimensional Shift Succeeded!" notice, and the target sends LOAD_STATIC_ZONE. Same zone means the player lands where
|
||||
* they stood. An experimental "seamless" mode skips LOAD_STATIC_ZONE so the client keeps its scene.
|
||||
*
|
||||
* Messages (all appended to MessageType::Master):
|
||||
* INSTANCE_MIGRATE world (a GM's /replaceinstance or /mergeinstance) or a future web dashboard -> master
|
||||
* InstanceMigrationRequest
|
||||
* MIGRATE_PLAYERS master -> source world MigratePlayersOrder
|
||||
* MIGRATE_STATUS source world -> master -> every world (for the GM who asked) MigrationStatus
|
||||
* MIGRATE_PLAYER_STATE source world -> master -> target world CarriedPlayerState
|
||||
*/
|
||||
namespace InstanceMigration {
|
||||
// Length-prefixed string, capped so a bad packet can't ask for huge allocations
|
||||
inline void WriteText(RakNet::BitStream& stream, const std::string& text, uint16_t max) {
|
||||
const auto length = static_cast<uint16_t>(std::min<size_t>(text.size(), max));
|
||||
stream.Write(length);
|
||||
stream.Write(text.data(), length);
|
||||
}
|
||||
|
||||
inline bool ReadText(RakNet::BitStream& stream, std::string& text, uint16_t max) {
|
||||
uint16_t length{};
|
||||
if (!stream.Read(length) || length > max) return false;
|
||||
text.resize(length);
|
||||
return length == 0 || stream.Read(text.data(), length);
|
||||
}
|
||||
|
||||
enum class eKind : uint8_t {
|
||||
REPLACE = 0, // start a fresh instance of the zone, move everyone there, shut the old one down
|
||||
MERGE = 1, // move everyone into another running instance of the zone, shut this one down
|
||||
};
|
||||
|
||||
enum class eState : uint8_t {
|
||||
STARTING_TARGET = 0, // waiting for the new instance to be ready
|
||||
WARNING, // players were told they'll be moved
|
||||
MOVING,
|
||||
DONE,
|
||||
FAILED,
|
||||
};
|
||||
|
||||
inline const char* KindName(eKind kind) { return kind == eKind::MERGE ? "merge" : "replace"; }
|
||||
|
||||
inline const char* StateName(eState state) {
|
||||
switch (state) {
|
||||
case eState::STARTING_TARGET: return "starting_target";
|
||||
case eState::WARNING: return "warning";
|
||||
case eState::MOVING: return "moving";
|
||||
case eState::DONE: return "done";
|
||||
case eState::FAILED: return "failed";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
// What master knows about one running instance, enough to plan moves
|
||||
struct InstanceView {
|
||||
uint32_t zoneId{};
|
||||
uint32_t instanceId{};
|
||||
uint32_t cloneId{};
|
||||
int32_t players{};
|
||||
int32_t softCap{};
|
||||
int32_t hardCap{};
|
||||
int32_t reserved{}; // seats held for players being moved in
|
||||
bool ready{};
|
||||
bool isPrivate{};
|
||||
bool shuttingDown{};
|
||||
bool draining{}; // its players are being moved away; nobody new is sent there
|
||||
|
||||
int32_t Load() const { return players + reserved; }
|
||||
};
|
||||
|
||||
enum class eRefusal : uint8_t {
|
||||
NONE = 0,
|
||||
NOT_RUNNING,
|
||||
CHARACTER_SELECT,
|
||||
PRIVATE_INSTANCE,
|
||||
PROPERTY_OR_CLONE,
|
||||
ACTIVITY_ZONE,
|
||||
SHUTTING_DOWN,
|
||||
ALREADY_MIGRATING,
|
||||
NOT_READY,
|
||||
TARGET_IS_SOURCE,
|
||||
TARGET_OTHER_ZONE,
|
||||
TARGET_FULL,
|
||||
NO_TARGET,
|
||||
MASTER_SHUTTING_DOWN,
|
||||
};
|
||||
|
||||
inline const char* Describe(eRefusal refusal) {
|
||||
switch (refusal) {
|
||||
case eRefusal::NONE: return "";
|
||||
case eRefusal::NOT_RUNNING: return "That instance isn't running";
|
||||
case eRefusal::CHARACTER_SELECT: return "Character selection can't be moved";
|
||||
case eRefusal::PRIVATE_INSTANCE: return "Private instances can't be moved";
|
||||
case eRefusal::PROPERTY_OR_CLONE: return "Properties and other cloned instances can't be moved";
|
||||
case eRefusal::ACTIVITY_ZONE: return "Activity instances (races, minigames) can't be moved";
|
||||
case eRefusal::SHUTTING_DOWN: return "That instance is shutting down";
|
||||
case eRefusal::ALREADY_MIGRATING: return "That instance is already being moved or taking players in";
|
||||
case eRefusal::NOT_READY: return "That instance is still starting";
|
||||
case eRefusal::TARGET_IS_SOURCE: return "Pick a different instance to merge into";
|
||||
case eRefusal::TARGET_OTHER_ZONE: return "Instances can only be merged within the same zone";
|
||||
case eRefusal::TARGET_FULL: return "The other instance doesn't have room for everyone";
|
||||
case eRefusal::NO_TARGET: return "No other instance of that zone has room for everyone";
|
||||
case eRefusal::MASTER_SHUTTING_DOWN: return "The server is shutting down";
|
||||
}
|
||||
return "Unknown reason";
|
||||
}
|
||||
|
||||
// Whether the players of this instance may be moved at all. Clones (properties) and activity zones (races,
|
||||
// minigames) keep state that lives only in that world, and private instances belong to someone.
|
||||
inline eRefusal CheckSource(const InstanceView& source, bool activityZone) {
|
||||
if (source.zoneId == 0) return eRefusal::CHARACTER_SELECT;
|
||||
if (source.isPrivate) return eRefusal::PRIVATE_INSTANCE;
|
||||
if (source.cloneId != 0) return eRefusal::PROPERTY_OR_CLONE;
|
||||
if (activityZone) return eRefusal::ACTIVITY_ZONE;
|
||||
if (source.shuttingDown) return eRefusal::SHUTTING_DOWN;
|
||||
if (source.draining) return eRefusal::ALREADY_MIGRATING;
|
||||
if (!source.ready) return eRefusal::NOT_READY;
|
||||
return eRefusal::NONE;
|
||||
}
|
||||
|
||||
// Whether everyone in source fits into target. Merges may fill up to the hard cap: the soft cap only keeps
|
||||
// room for friends joining, and a merge is those players staying together anyway.
|
||||
inline eRefusal CheckMergeTarget(const InstanceView& target, const InstanceView& source) {
|
||||
if (target.zoneId == source.zoneId && target.instanceId == source.instanceId) return eRefusal::TARGET_IS_SOURCE;
|
||||
if (target.zoneId != source.zoneId || target.cloneId != source.cloneId) return eRefusal::TARGET_OTHER_ZONE;
|
||||
if (target.isPrivate) return eRefusal::PRIVATE_INSTANCE;
|
||||
if (target.shuttingDown) return eRefusal::SHUTTING_DOWN;
|
||||
if (target.draining) return eRefusal::ALREADY_MIGRATING;
|
||||
if (!target.ready) return eRefusal::NOT_READY;
|
||||
if (target.Load() + source.players > target.hardCap) return eRefusal::TARGET_FULL;
|
||||
return eRefusal::NONE;
|
||||
}
|
||||
|
||||
// The best instance to merge source into: one where everyone fits under the soft cap if there is one, and of
|
||||
// those the fullest (so players end up together). Ties go to the older instance.
|
||||
inline std::optional<uint32_t> PickMergeTarget(const std::vector<InstanceView>& instances, const InstanceView& source) {
|
||||
const InstanceView* best = nullptr;
|
||||
bool bestUnderSoft = false;
|
||||
for (const auto& candidate : instances) {
|
||||
if (CheckMergeTarget(candidate, source) != eRefusal::NONE) continue;
|
||||
const bool underSoft = candidate.Load() + source.players <= candidate.softCap;
|
||||
if (!best || (underSoft && !bestUnderSoft) ||
|
||||
(underSoft == bestUnderSoft && (candidate.Load() > best->Load() ||
|
||||
(candidate.Load() == best->Load() && candidate.instanceId < best->instanceId)))) {
|
||||
best = &candidate;
|
||||
bestUnderSoft = underSoft;
|
||||
}
|
||||
}
|
||||
if (!best) return std::nullopt;
|
||||
return best->instanceId;
|
||||
}
|
||||
|
||||
struct MergeSuggestion {
|
||||
uint32_t zoneId{};
|
||||
uint32_t sourceInstance{};
|
||||
uint32_t targetInstance{};
|
||||
int32_t players{}; // moved
|
||||
int32_t resulting{}; // in the target afterwards
|
||||
};
|
||||
|
||||
// Quiet instances worth merging: per zone, the emptiest instance goes into the fullest one that stays under the
|
||||
// soft cap, until nothing more fits. Empty instances aren't listed (they shut themselves down).
|
||||
inline std::vector<MergeSuggestion> SuggestMerges(const std::vector<InstanceView>& instances) {
|
||||
std::map<uint32_t, std::vector<InstanceView>> byZone;
|
||||
for (const auto& instance : instances) {
|
||||
if (CheckSource(instance, false) != eRefusal::NONE) continue;
|
||||
byZone[instance.zoneId].push_back(instance);
|
||||
}
|
||||
|
||||
std::vector<MergeSuggestion> suggestions;
|
||||
for (auto& [zone, list] : byZone) {
|
||||
if (list.size() < 2) continue;
|
||||
std::sort(list.begin(), list.end(), [](const InstanceView& a, const InstanceView& b) {
|
||||
return a.Load() != b.Load() ? a.Load() < b.Load() : a.instanceId > b.instanceId;
|
||||
});
|
||||
std::vector<bool> mergedAway(list.size(), false);
|
||||
std::vector<bool> takingIn(list.size(), false);
|
||||
for (size_t i = 0; i < list.size(); i++) {
|
||||
if (takingIn[i] || list[i].players <= 0) continue;
|
||||
// Into the fullest one it still fits in; only ones at least as full (later in the list)
|
||||
for (size_t j = list.size(); j-- > i + 1;) {
|
||||
if (mergedAway[j] || list[j].Load() + list[i].players > list[j].softCap) continue;
|
||||
suggestions.push_back({ zone, list[i].instanceId, list[j].instanceId, list[i].players, list[j].Load() + list[i].players });
|
||||
list[j].reserved += list[i].players;
|
||||
mergedAway[i] = true;
|
||||
takingIn[j] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
enum class ePlayerDecision : uint8_t {
|
||||
MOVE, // move now
|
||||
CANCEL_TRADE_MOVE, // cancel the open trade (nothing changes hands), then move
|
||||
WAIT, // try again next tick
|
||||
};
|
||||
|
||||
// Whether a player can be moved right now. Players who are dead or building wait a little (their state there
|
||||
// isn't saved), then go anyway so a migration always finishes.
|
||||
inline ePlayerDecision DecidePlayer(bool inTrade, bool buildMode, bool dead, float waitedSeconds, float maxWaitSeconds) {
|
||||
if ((buildMode || dead) && waitedSeconds < maxWaitSeconds) return ePlayerDecision::WAIT;
|
||||
if (inTrade) return ePlayerDecision::CANCEL_TRADE_MOVE;
|
||||
return ePlayerDecision::MOVE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* INSTANCE_MIGRATE (a world for a GM command, or anything else connected to master): move everyone in one instance. targetInstance 0 picks a target for
|
||||
* merges (the best fit); replaces always start a fresh instance.
|
||||
*/
|
||||
struct InstanceMigrationRequest : public LUBitStream {
|
||||
InstanceMigrationRequest() : LUBitStream(ServiceType::MASTER, MessageType::Master::INSTANCE_MIGRATE) {}
|
||||
|
||||
static constexpr uint16_t MAX_BY = 64;
|
||||
static constexpr uint16_t MAX_WARN_SECONDS = 300;
|
||||
|
||||
uint32_t requestId{};
|
||||
InstanceMigration::eKind kind{ InstanceMigration::eKind::REPLACE };
|
||||
uint32_t zoneId{};
|
||||
uint32_t sourceInstance{};
|
||||
uint32_t targetInstance{};
|
||||
uint16_t warnSeconds{};
|
||||
bool shutdownSource{ true };
|
||||
bool seamless{}; // experimental: no loading screen (see MigratePlayersOrder::seamless)
|
||||
LWOOBJID requesterId{}; // the character who asked, told how it goes; 0 for none
|
||||
std::string requestedBy;
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(requestId);
|
||||
stream.Write(static_cast<uint8_t>(kind));
|
||||
stream.Write(zoneId);
|
||||
stream.Write(sourceInstance);
|
||||
stream.Write(targetInstance);
|
||||
stream.Write(std::min(warnSeconds, MAX_WARN_SECONDS));
|
||||
stream.Write<uint8_t>(shutdownSource ? 1 : 0);
|
||||
stream.Write<uint8_t>(seamless ? 1 : 0);
|
||||
stream.Write(requesterId);
|
||||
InstanceMigration::WriteText(stream, requestedBy, MAX_BY);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint8_t kindValue{}, shutdown{}, seamlessValue{};
|
||||
if (!stream.Read(requestId) || !stream.Read(kindValue) || !stream.Read(zoneId) || !stream.Read(sourceInstance) ||
|
||||
!stream.Read(targetInstance) || !stream.Read(warnSeconds) || !stream.Read(shutdown) || !stream.Read(seamlessValue) ||
|
||||
!stream.Read(requesterId)) return false;
|
||||
if (kindValue > static_cast<uint8_t>(InstanceMigration::eKind::MERGE) || warnSeconds > MAX_WARN_SECONDS) return false;
|
||||
kind = static_cast<InstanceMigration::eKind>(kindValue);
|
||||
shutdownSource = shutdown != 0;
|
||||
seamless = seamlessValue != 0;
|
||||
return InstanceMigration::ReadText(stream, requestedBy, MAX_BY);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* MIGRATE_PLAYERS (master -> source world): send everyone to this instance. The target is already running and has
|
||||
* seats held for them.
|
||||
*/
|
||||
struct MigratePlayersOrder : public LUBitStream {
|
||||
MigratePlayersOrder() : LUBitStream(ServiceType::MASTER, MessageType::Master::MIGRATE_PLAYERS) {}
|
||||
|
||||
static constexpr uint16_t MAX_IP = 255;
|
||||
|
||||
uint32_t migrationId{};
|
||||
uint32_t targetZone{};
|
||||
uint32_t targetInstance{};
|
||||
uint32_t targetClone{};
|
||||
std::string targetIp;
|
||||
uint16_t targetPort{};
|
||||
uint16_t warnSeconds{};
|
||||
uint16_t playersPerSecond{ 10 };
|
||||
bool mythranShift{ true }; // the client's own "moved to a new dimension" notice after the transfer
|
||||
/**
|
||||
* Experimental: keep the client's scene. The source destroys every object it sent the client (the client keeps
|
||||
* its own player object when its ghost goes), then transfers; the target skips LOAD_STATIC_ZONE and constructs
|
||||
* everything at once, and the client adopts its existing player object for the new ghost
|
||||
* (LwoClientGhostManager::OnReceiveConstruction). Not tested with the real client yet.
|
||||
*/
|
||||
bool seamless{};
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(migrationId);
|
||||
stream.Write(targetZone);
|
||||
stream.Write(targetInstance);
|
||||
stream.Write(targetClone);
|
||||
InstanceMigration::WriteText(stream, targetIp, MAX_IP);
|
||||
stream.Write(targetPort);
|
||||
stream.Write(warnSeconds);
|
||||
stream.Write(playersPerSecond);
|
||||
stream.Write<uint8_t>(mythranShift ? 1 : 0);
|
||||
stream.Write<uint8_t>(seamless ? 1 : 0);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint8_t shift{}, seamlessValue{};
|
||||
if (!stream.Read(migrationId) || !stream.Read(targetZone) || !stream.Read(targetInstance) || !stream.Read(targetClone) ||
|
||||
!InstanceMigration::ReadText(stream, targetIp, MAX_IP) || !stream.Read(targetPort) || !stream.Read(warnSeconds) ||
|
||||
!stream.Read(playersPerSecond) || !stream.Read(shift) || !stream.Read(seamlessValue)) return false;
|
||||
mythranShift = shift != 0;
|
||||
seamless = seamlessValue != 0;
|
||||
if (playersPerSecond == 0) playersPerSecond = 1;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* MIGRATE_STATUS (source world -> master, master -> every world): progress of one migration. Master passes it on
|
||||
* with who asked for it, and sends its own (STARTING_TARGET, FAILED) the same way.
|
||||
*/
|
||||
struct MigrationStatus : public LUBitStream {
|
||||
MigrationStatus() : LUBitStream(ServiceType::MASTER, MessageType::Master::MIGRATE_STATUS) {}
|
||||
|
||||
static constexpr uint16_t MAX_MESSAGE = 300;
|
||||
|
||||
uint32_t migrationId{};
|
||||
InstanceMigration::eState state{ InstanceMigration::eState::STARTING_TARGET };
|
||||
InstanceMigration::eKind kind{ InstanceMigration::eKind::REPLACE };
|
||||
uint32_t zoneId{};
|
||||
uint32_t sourceInstance{};
|
||||
uint32_t targetInstance{};
|
||||
uint16_t moved{};
|
||||
uint16_t remaining{};
|
||||
uint16_t failed{};
|
||||
LWOOBJID requesterId{}; // filled in by master
|
||||
std::string message;
|
||||
|
||||
bool Finished() const { return state == InstanceMigration::eState::DONE || state == InstanceMigration::eState::FAILED; }
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(migrationId);
|
||||
stream.Write(static_cast<uint8_t>(state));
|
||||
stream.Write(static_cast<uint8_t>(kind));
|
||||
stream.Write(zoneId);
|
||||
stream.Write(sourceInstance);
|
||||
stream.Write(targetInstance);
|
||||
stream.Write(moved);
|
||||
stream.Write(remaining);
|
||||
stream.Write(failed);
|
||||
stream.Write(requesterId);
|
||||
InstanceMigration::WriteText(stream, message, MAX_MESSAGE);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint8_t stateValue{}, kindValue{};
|
||||
if (!stream.Read(migrationId) || !stream.Read(stateValue) || !stream.Read(kindValue) || !stream.Read(zoneId) ||
|
||||
!stream.Read(sourceInstance) || !stream.Read(targetInstance) || !stream.Read(moved) || !stream.Read(remaining) ||
|
||||
!stream.Read(failed) || !stream.Read(requesterId)) return false;
|
||||
if (stateValue > static_cast<uint8_t>(InstanceMigration::eState::FAILED) || kindValue > static_cast<uint8_t>(InstanceMigration::eKind::MERGE)) return false;
|
||||
state = static_cast<InstanceMigration::eState>(stateValue);
|
||||
kind = static_cast<InstanceMigration::eKind>(kindValue);
|
||||
return InstanceMigration::ReadText(stream, message, MAX_MESSAGE);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* MIGRATE_PLAYER_STATE (source world -> master -> target world): what a moved player had that their saved
|
||||
* character doesn't keep, put back when they finish loading in the target. Everything else (position, health,
|
||||
* imagination, armor, buffs, inventory, missions) is in the character XML, saved just before the transfer.
|
||||
*/
|
||||
struct CarriedPlayerState : public LUBitStream {
|
||||
CarriedPlayerState() : LUBitStream(ServiceType::MASTER, MessageType::Master::MIGRATE_PLAYER_STATE) {}
|
||||
|
||||
uint32_t targetZone{};
|
||||
uint32_t targetInstance{};
|
||||
LWOOBJID characterId{};
|
||||
LWOOBJID petItemId{}; // the pet that was out (its item), summoned again; 0 for none
|
||||
bool seamless{}; // the client kept its scene: skip LOAD_STATIC_ZONE and load the player at once
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(targetZone);
|
||||
stream.Write(targetInstance);
|
||||
stream.Write(characterId);
|
||||
stream.Write(petItemId);
|
||||
stream.Write<uint8_t>(seamless ? 1 : 0);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint8_t seamlessValue{};
|
||||
if (!stream.Read(targetZone) || !stream.Read(targetInstance) || !stream.Read(characterId) || !stream.Read(petItemId) || !stream.Read(seamlessValue)) return false;
|
||||
seamless = seamlessValue != 0;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__INSTANCEMIGRATION__H__
|
||||
269
dNet/master/MessageCapture.h
Normal file
269
dNet/master/MessageCapture.h
Normal file
@@ -0,0 +1,269 @@
|
||||
#ifndef __MESSAGECAPTURE__H__
|
||||
#define __MESSAGECAPTURE__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "BitStreamUtils.h"
|
||||
#include "MessageType/Master.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* The dashboard's game message inspector: staff capture the game messages one online player sends and receives.
|
||||
*
|
||||
* MESSAGE_CAPTURE_CONTROL (dashboard -> master -> every world) starts or stops a capture; only the world holding the
|
||||
* character acts on it. MESSAGE_CAPTURE_DATA (world -> master -> dashboard) reports that the capture started, carries
|
||||
* batches of captured messages, and says when and why it ended there. Worlds stop a capture on their own at its time
|
||||
* limit, so one can never be left running.
|
||||
*/
|
||||
enum class eMessageDirection : uint8_t {
|
||||
TO_SERVER, // sent by the player's client
|
||||
TO_CLIENT, // sent to the player's client (including broadcasts they receive)
|
||||
};
|
||||
|
||||
enum class eMessageCaptureControl : uint8_t {
|
||||
START,
|
||||
STOP,
|
||||
};
|
||||
|
||||
enum class eMessageCaptureStatus : uint8_t {
|
||||
STARTED, // the world holding the character started capturing
|
||||
ENTRIES, // captured messages (also sent empty every few seconds while capturing)
|
||||
ENDED, // the world stopped capturing; `reason` says why
|
||||
};
|
||||
|
||||
enum class eMessageCaptureEnd : uint8_t {
|
||||
STOPPED, // staff stopped it
|
||||
TIME_LIMIT, // its time ran out
|
||||
PLAYER_LEFT, // the player left this world (logged out or changed zones)
|
||||
};
|
||||
|
||||
namespace MessageCapture {
|
||||
// A capture runs at most this long
|
||||
constexpr uint32_t MAX_SECONDS = 15 * 60;
|
||||
// Captured bytes kept per message; larger messages are cut (the entry keeps their full size)
|
||||
constexpr uint16_t MAX_PAYLOAD = 2048;
|
||||
// Decoded fields (JSON text) kept per message
|
||||
constexpr uint16_t MAX_DECODED = 8192;
|
||||
// Message IDs in a filter list
|
||||
constexpr uint16_t MAX_FILTER = 256;
|
||||
|
||||
// Lowercase hex, two digits per byte
|
||||
inline std::string ToHex(std::string_view bytes) {
|
||||
static constexpr char DIGITS[] = "0123456789abcdef";
|
||||
std::string hex;
|
||||
hex.reserve(bytes.size() * 2);
|
||||
for (const unsigned char byte : bytes) {
|
||||
hex += DIGITS[byte >> 4];
|
||||
hex += DIGITS[byte & 0x0f];
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
inline void WriteString(RakNet::BitStream& stream, const std::string& text, uint16_t max) {
|
||||
const auto length = static_cast<uint16_t>(std::min<size_t>(text.size(), max));
|
||||
stream.Write(length);
|
||||
if (length) stream.Write(text.data(), length);
|
||||
}
|
||||
|
||||
inline bool ReadString(RakNet::BitStream& stream, std::string& text, uint16_t max) {
|
||||
uint16_t length{};
|
||||
if (!stream.Read(length) || length > max) return false;
|
||||
text.resize(length);
|
||||
return length == 0 || stream.Read(text.data(), length);
|
||||
}
|
||||
|
||||
inline void WriteIds(RakNet::BitStream& stream, const std::vector<uint16_t>& ids) {
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(ids.size(), MAX_FILTER));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) stream.Write(ids[i]);
|
||||
}
|
||||
|
||||
inline bool ReadIds(RakNet::BitStream& stream, std::vector<uint16_t>& ids) {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(count) || count > MAX_FILTER) return false;
|
||||
ids.resize(count);
|
||||
for (auto& id : ids) if (!stream.Read(id)) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// MESSAGE_CAPTURE_CONTROL payload
|
||||
struct MessageCaptureControl : public LUBitStream {
|
||||
MessageCaptureControl() : LUBitStream(ServiceType::MASTER, MessageType::Master::MESSAGE_CAPTURE_CONTROL) {}
|
||||
|
||||
uint32_t captureId{};
|
||||
eMessageCaptureControl action{};
|
||||
LWOOBJID characterId{};
|
||||
uint32_t seconds{}; // START: how long to capture (capped at MessageCapture::MAX_SECONDS)
|
||||
bool toServer{ true }; // START: capture what the client sends
|
||||
bool toClient{ true }; // START: capture what the client receives
|
||||
std::vector<uint16_t> only; // START: capture only these message IDs (empty: all)
|
||||
std::vector<uint16_t> skip; // START: never capture these message IDs
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(captureId);
|
||||
stream.Write(action);
|
||||
stream.Write(characterId);
|
||||
stream.Write(std::min(seconds, MessageCapture::MAX_SECONDS));
|
||||
stream.Write<uint8_t>(toServer);
|
||||
stream.Write<uint8_t>(toClient);
|
||||
MessageCapture::WriteIds(stream, only);
|
||||
MessageCapture::WriteIds(stream, skip);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint8_t server{}, client{};
|
||||
if (!stream.Read(captureId) || !stream.Read(action) || !stream.Read(characterId) || !stream.Read(seconds) ||
|
||||
!stream.Read(server) || !stream.Read(client)) return false;
|
||||
seconds = std::min(seconds, MessageCapture::MAX_SECONDS);
|
||||
toServer = server != 0;
|
||||
toClient = client != 0;
|
||||
return MessageCapture::ReadIds(stream, only) && MessageCapture::ReadIds(stream, skip);
|
||||
}
|
||||
|
||||
// Whether a message passes this capture's filters
|
||||
bool Wants(eMessageDirection direction, uint16_t messageId) const {
|
||||
if (!(direction == eMessageDirection::TO_SERVER ? toServer : toClient)) return false;
|
||||
if (std::ranges::find(skip, messageId) != skip.end()) return false;
|
||||
return only.empty() || std::ranges::find(only, messageId) != only.end();
|
||||
}
|
||||
};
|
||||
|
||||
// One captured game message
|
||||
struct MessageCaptureEntry {
|
||||
uint32_t sequence{}; // per capture, from 1; gaps mean messages were dropped
|
||||
int64_t timeMs{}; // Unix time in milliseconds
|
||||
eMessageDirection direction{};
|
||||
uint16_t messageId{}; // MessageType::Game
|
||||
LWOOBJID objectId{}; // the object the message is addressed to
|
||||
uint32_t bits{}; // full size of the message's fields, in bits
|
||||
std::string payload; // the fields' bytes (at most MAX_PAYLOAD)
|
||||
std::string decoded; // the fields as JSON when the server has a typed struct for the message, else empty
|
||||
|
||||
// Roughly how many bytes this takes in a packet, for batching
|
||||
size_t WireSize() const { return 32 + payload.size() + decoded.size(); }
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
stream.Write(sequence);
|
||||
stream.Write(timeMs);
|
||||
stream.Write(direction);
|
||||
stream.Write(messageId);
|
||||
stream.Write(objectId);
|
||||
stream.Write(bits);
|
||||
MessageCapture::WriteString(stream, payload, MessageCapture::MAX_PAYLOAD);
|
||||
MessageCapture::WriteString(stream, decoded, MessageCapture::MAX_DECODED);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
return stream.Read(sequence) && stream.Read(timeMs) && stream.Read(direction) && stream.Read(messageId) &&
|
||||
stream.Read(objectId) && stream.Read(bits) &&
|
||||
MessageCapture::ReadString(stream, payload, MessageCapture::MAX_PAYLOAD) &&
|
||||
MessageCapture::ReadString(stream, decoded, MessageCapture::MAX_DECODED);
|
||||
}
|
||||
};
|
||||
|
||||
// MESSAGE_CAPTURE_DATA payload
|
||||
struct MessageCaptureData : public LUBitStream {
|
||||
MessageCaptureData() : LUBitStream(ServiceType::MASTER, MessageType::Master::MESSAGE_CAPTURE_DATA) {}
|
||||
|
||||
static constexpr uint16_t MAX_ENTRIES = 500;
|
||||
|
||||
uint32_t captureId{};
|
||||
eMessageCaptureStatus status{};
|
||||
LWOOBJID characterId{};
|
||||
uint32_t zoneId{};
|
||||
uint32_t instanceId{};
|
||||
eMessageCaptureEnd reason{}; // ENDED
|
||||
uint32_t dropped{}; // messages left out since the last batch (over the rate or buffer limit)
|
||||
std::vector<MessageCaptureEntry> entries;
|
||||
uint32_t cloneId{}; // the world's clone (a property's owner), 0 elsewhere
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const override {
|
||||
stream.Write(captureId);
|
||||
stream.Write(status);
|
||||
stream.Write(characterId);
|
||||
stream.Write(zoneId);
|
||||
stream.Write(instanceId);
|
||||
stream.Write(reason);
|
||||
stream.Write(dropped);
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(entries.size(), MAX_ENTRIES));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) entries[i].Serialize(stream);
|
||||
stream.Write(cloneId);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) override {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(captureId) || !stream.Read(status) || !stream.Read(characterId) || !stream.Read(zoneId) ||
|
||||
!stream.Read(instanceId) || !stream.Read(reason) || !stream.Read(dropped) || !stream.Read(count) || count > MAX_ENTRIES) return false;
|
||||
entries.resize(count);
|
||||
for (auto& entry : entries) if (!entry.Deserialize(stream)) return false;
|
||||
return stream.Read(cloneId);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Captured messages waiting to be sent, bounded by count and by rate so a busy player can't flood master and the
|
||||
* dashboard. What goes over either limit is counted as dropped instead.
|
||||
*/
|
||||
class MessageCaptureQueue {
|
||||
public:
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
MessageCaptureQueue(size_t maxPending, uint32_t maxPerSecond) : m_MaxPending(maxPending), m_MaxPerSecond(maxPerSecond) {}
|
||||
|
||||
// False if the entry was dropped
|
||||
bool Push(MessageCaptureEntry entry, Clock::time_point now) {
|
||||
if (now - m_WindowStart >= std::chrono::seconds(1)) {
|
||||
m_WindowStart = now;
|
||||
m_InWindow = 0;
|
||||
}
|
||||
if (m_InWindow >= m_MaxPerSecond || m_Pending.size() >= m_MaxPending) {
|
||||
m_Dropped++;
|
||||
return false;
|
||||
}
|
||||
m_InWindow++;
|
||||
m_Pending.push_back(std::move(entry));
|
||||
return true;
|
||||
}
|
||||
|
||||
// The oldest entries, up to maxEntries and about maxBytes (always at least one when any are waiting)
|
||||
std::vector<MessageCaptureEntry> Take(size_t maxEntries, size_t maxBytes) {
|
||||
std::vector<MessageCaptureEntry> batch;
|
||||
size_t bytes = 0;
|
||||
while (!m_Pending.empty() && batch.size() < maxEntries) {
|
||||
const auto size = m_Pending.front().WireSize();
|
||||
if (!batch.empty() && bytes + size > maxBytes) break;
|
||||
bytes += size;
|
||||
batch.push_back(std::move(m_Pending.front()));
|
||||
m_Pending.pop_front();
|
||||
}
|
||||
return batch;
|
||||
}
|
||||
|
||||
// Entries dropped since the last call
|
||||
uint32_t TakeDropped() {
|
||||
const auto dropped = m_Dropped;
|
||||
m_Dropped = 0;
|
||||
return dropped;
|
||||
}
|
||||
|
||||
size_t Pending() const { return m_Pending.size(); }
|
||||
|
||||
private:
|
||||
size_t m_MaxPending;
|
||||
uint32_t m_MaxPerSecond;
|
||||
std::deque<MessageCaptureEntry> m_Pending;
|
||||
Clock::time_point m_WindowStart{};
|
||||
uint32_t m_InWindow{};
|
||||
uint32_t m_Dropped{};
|
||||
};
|
||||
|
||||
#endif //!__MESSAGECAPTURE__H__
|
||||
128
dNet/master/PlayerAction.h
Normal file
128
dNet/master/PlayerAction.h
Normal file
@@ -0,0 +1,128 @@
|
||||
#ifndef __PLAYERACTION__H__
|
||||
#define __PLAYERACTION__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "BitStreamUtils.h"
|
||||
#include "MessageType/Master.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* Actions the dashboard asks world servers to apply to players who are currently online.
|
||||
* The dashboard sends MessageType::Master::PLAYER_ACTION to master, which forwards it to every world.
|
||||
* Each world answers PLAYER_ACTION_RESULT, and master sends the dashboard a single aggregated result
|
||||
* once every world has answered or the request timed out.
|
||||
*/
|
||||
enum class ePlayerAction : uint8_t {
|
||||
KICK_ACCOUNT, // Disconnect every session of accountId
|
||||
REFRESH_ACCOUNT, // Reload account data (GM level) for online sessions of accountId
|
||||
REFRESH_CHARACTER, // Reload character data (restrictions) for characterId if loaded
|
||||
RESCUE_CHARACTER, // Transfer characterId to zoneId if it is loaded in a world, landing on spawn point `text` (empty: the zone's default)
|
||||
// Tell a player a moderator decided on something they asked for (approved = yes/no, text = name or reason)
|
||||
NAME_MODERATED, // characterId's requested name; text is the name
|
||||
PET_NAME_MODERATED, // pet targetId's name; text is the name
|
||||
PROPERTY_MODERATED, // property targetId owned by characterId; text is the rejection reason
|
||||
TELEPORT_TO_PLAYER, // move characterId to where character targetId stands (only when both are in the same world)
|
||||
WARN_ACCOUNT, // show accountId's online character a moderator's warning (text)
|
||||
RELOAD_VANITY, // respawn the vanity NPCs from the vanity/*.xml files (every world; answers 1 per world reloaded)
|
||||
CHAT_MESSAGE, // show `text` in chat from `name` (e.g. "[Discord] Bob") in every world, or only zoneId; answers players reached
|
||||
RELOAD_CHAT_FILTER, // load the chat filter's words added on the dashboard again (every world, answering 1 each; master also passes it to the chat server)
|
||||
// Change mission targetId of characterId if it is loaded in a world, as the GM commands do
|
||||
MISSION_COMPLETE, // as /completemission; approved = give its rewards
|
||||
MISSION_RESET, // as /resetmission
|
||||
MISSION_ACCEPT, // as /addmission (prerequisites are skipped)
|
||||
RELOAD_LIVE_OPS, // load the running live events and open challenges again (every world, answering 1 each; see LiveEvents.h)
|
||||
};
|
||||
|
||||
struct PlayerActionRequest : public LUBitStream {
|
||||
PlayerActionRequest() : LUBitStream(ServiceType::MASTER, MessageType::Master::PLAYER_ACTION) {}
|
||||
|
||||
uint32_t requestId{};
|
||||
ePlayerAction action{};
|
||||
uint32_t accountId{};
|
||||
LWOOBJID characterId{};
|
||||
LWOMAPID zoneId{};
|
||||
// eServerDisconnectIdentifiers shown to the client for KICK_ACCOUNT
|
||||
uint32_t disconnectReason{};
|
||||
// For the *_MODERATED actions
|
||||
LWOOBJID targetId{};
|
||||
bool approved{};
|
||||
std::string text;
|
||||
std::string name; // CHAT_MESSAGE: who it's from
|
||||
int32_t instanceId{ -1 }; // CHAT_MESSAGE: only this instance of zoneId (-1: every instance)
|
||||
|
||||
static constexpr uint16_t MAX_TEXT = 300;
|
||||
|
||||
// How many bytes of UTF-8 text fit in maxBytes without cutting a character in half
|
||||
static size_t Utf8Length(const std::string& value, size_t maxBytes) {
|
||||
if (value.size() <= maxBytes) return value.size();
|
||||
size_t length = maxBytes;
|
||||
// Back up over continuation bytes (10xxxxxx) to the start of the character that doesn't fit
|
||||
while (length > 0 && (static_cast<unsigned char>(value[length]) & 0xC0) == 0x80) length--;
|
||||
return length;
|
||||
}
|
||||
|
||||
void Serialize(RakNet::BitStream& bitStream) const override {
|
||||
bitStream.Write(requestId);
|
||||
bitStream.Write(action);
|
||||
bitStream.Write(accountId);
|
||||
bitStream.Write(characterId);
|
||||
bitStream.Write(zoneId);
|
||||
bitStream.Write(disconnectReason);
|
||||
bitStream.Write(targetId);
|
||||
bitStream.Write<uint8_t>(approved);
|
||||
const auto length = static_cast<uint16_t>(Utf8Length(text, MAX_TEXT));
|
||||
bitStream.Write(length);
|
||||
bitStream.Write(text.data(), length);
|
||||
const auto nameLength = static_cast<uint16_t>(Utf8Length(name, MAX_TEXT));
|
||||
bitStream.Write(nameLength);
|
||||
bitStream.Write(name.data(), nameLength);
|
||||
bitStream.Write(instanceId);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& bitStream) override {
|
||||
uint8_t approvedByte{};
|
||||
uint16_t length{};
|
||||
if (!(bitStream.Read(requestId) && bitStream.Read(action) && bitStream.Read(accountId) && bitStream.Read(characterId) &&
|
||||
bitStream.Read(zoneId) && bitStream.Read(disconnectReason) && bitStream.Read(targetId) && bitStream.Read(approvedByte) &&
|
||||
bitStream.Read(length)) || length > MAX_TEXT) return false;
|
||||
approved = approvedByte != 0;
|
||||
text.resize(length);
|
||||
if (length != 0 && !bitStream.Read(text.data(), length)) return false;
|
||||
uint16_t nameLength{};
|
||||
if (!bitStream.Read(nameLength) || nameLength > MAX_TEXT) return false;
|
||||
name.resize(nameLength);
|
||||
if (nameLength != 0 && !bitStream.Read(name.data(), nameLength)) return false;
|
||||
return bitStream.Read(instanceId);
|
||||
}
|
||||
};
|
||||
|
||||
struct PlayerActionResult : public LUBitStream {
|
||||
PlayerActionResult() : LUBitStream(ServiceType::MASTER, MessageType::Master::PLAYER_ACTION_RESULT) {}
|
||||
|
||||
uint32_t requestId{};
|
||||
ePlayerAction action{};
|
||||
// Number of sessions/characters the action applied to. Zero means the player was not online.
|
||||
uint32_t affected{};
|
||||
// Set by master when not every world answered before the timeout
|
||||
bool timedOut{};
|
||||
|
||||
void Serialize(RakNet::BitStream& bitStream) const override {
|
||||
bitStream.Write(requestId);
|
||||
bitStream.Write(action);
|
||||
bitStream.Write(affected);
|
||||
bitStream.Write<uint8_t>(timedOut);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& bitStream) override {
|
||||
uint8_t timedOutByte{};
|
||||
const bool ok = bitStream.Read(requestId) && bitStream.Read(action) && bitStream.Read(affected) && bitStream.Read(timedOutByte);
|
||||
timedOut = timedOutByte != 0;
|
||||
return ok;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__PLAYERACTION__H__
|
||||
Reference in New Issue
Block a user