Integrate session-scoped command dispatch into M01
This commit is contained in:
commit
645abefac5
6 files changed
+709
-2
No files matched your search
@@ -0,0 +1,133 @@
|
||||
#pragma once
|
||||
|
||||
#include "settlement/core/world.hpp"
|
||||
|
||||
#include <Void/Core/Uuid.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
namespace settlement {
|
||||
|
||||
inline constexpr std::uint32_t command_envelope_version = 1;
|
||||
|
||||
struct SessionIdentity {
|
||||
Void::Uuid::Uuid world;
|
||||
Void::Uuid::Uuid session;
|
||||
std::uint64_t city = 1;
|
||||
bool operator==(const SessionIdentity&) const = default;
|
||||
};
|
||||
|
||||
// Created by the trusted transport/local adapter, never decoded from payload data.
|
||||
// Reconnected clients keep their assigned slot within the same session.
|
||||
struct TrustedClient {
|
||||
std::uint8_t player;
|
||||
};
|
||||
|
||||
struct CommandPayload {
|
||||
CommandKind kind = CommandKind::PlaceBed;
|
||||
GridPosition cell{};
|
||||
std::uint8_t value = 0;
|
||||
GridPosition destination{};
|
||||
AgentId resident = 0;
|
||||
bool operator==(const CommandPayload&) const = default;
|
||||
};
|
||||
|
||||
struct LegacyCabinPayload {
|
||||
GridPosition cell;
|
||||
GridPosition sawmill;
|
||||
GridPosition stonecutter;
|
||||
bool operator==(const LegacyCabinPayload&) const = default;
|
||||
};
|
||||
|
||||
using SessionPayload = std::variant<CommandPayload, LegacyCabinPayload>;
|
||||
|
||||
struct CommandEnvelope {
|
||||
std::uint32_t version = command_envelope_version;
|
||||
Void::Uuid::Uuid world;
|
||||
Void::Uuid::Uuid session;
|
||||
std::uint64_t city = 1;
|
||||
std::uint64_t request = 0;
|
||||
SessionPayload payload;
|
||||
};
|
||||
|
||||
enum class DispatchStatus : std::uint8_t {
|
||||
Completed,
|
||||
UnsupportedVersion,
|
||||
WrongWorld,
|
||||
StaleSession,
|
||||
UnauthorizedClient,
|
||||
InvalidRequest,
|
||||
OutOfOrder,
|
||||
Conflict,
|
||||
Busy,
|
||||
Faulted
|
||||
};
|
||||
|
||||
struct DispatchReceipt {
|
||||
DispatchStatus status;
|
||||
// Meaningful for Completed. Revision counts successful dispatcher mutations
|
||||
// in this session, including legacy cabin placement, independently of ticks.
|
||||
CommandStatus command_status = CommandStatus::Unavailable;
|
||||
std::uint64_t tick = 0;
|
||||
std::uint64_t revision = 0;
|
||||
bool operator==(const DispatchReceipt&) const = default;
|
||||
};
|
||||
|
||||
enum class CommandMutationPhase : std::uint8_t { BeforeMutation, AfterMutation };
|
||||
|
||||
struct CommandDispatcherOptions {
|
||||
std::size_t max_receipts_per_client = 65'536;
|
||||
// Optional failure observer around authoritative application. An exception
|
||||
// fences the staged dispatcher, so the owner must discard its candidate.
|
||||
void (*mutation_observer)(CommandMutationPhase) = nullptr;
|
||||
};
|
||||
|
||||
// Single-owner component. The caller must pass the session's authoritative World.
|
||||
// Exact repeats retain their original result for the entire session. At capacity,
|
||||
// new requests receive Busy and stored receipts remain available. No eviction.
|
||||
// Domain rejections consume a request number; transient dispatch errors do not.
|
||||
// Copies may be staged with a candidate World and committed only after publication.
|
||||
// Any mutation exception poisons this instance. Old completed receipts remain
|
||||
// readable, while further effects require a newly constructed session/dispatcher.
|
||||
// Upstream replacement contract and tests: Void/Library#6
|
||||
// https://git.faumaray.com/Void/Library/issues/6
|
||||
class CommandDispatcher {
|
||||
public:
|
||||
explicit CommandDispatcher(SessionIdentity identity, CommandDispatcherOptions options = {});
|
||||
|
||||
[[nodiscard]] const SessionIdentity& identity() const noexcept { return identity_; }
|
||||
[[nodiscard]] std::uint64_t revision() const noexcept { return revision_; }
|
||||
[[nodiscard]] bool faulted() const noexcept { return faulted_; }
|
||||
[[nodiscard]] std::size_t receipt_count(TrustedClient client) const noexcept;
|
||||
[[nodiscard]] std::uint64_t next_request(TrustedClient client) const noexcept;
|
||||
|
||||
[[nodiscard]] DispatchReceipt dispatch(World& world, TrustedClient client, const CommandEnvelope& command);
|
||||
// Invalid context/conflicting reuse returns its dispatch error. An otherwise
|
||||
// valid request without a retained receipt returns nullopt without side effects.
|
||||
[[nodiscard]] std::optional<DispatchReceipt> lookup(TrustedClient client, const CommandEnvelope& command) const;
|
||||
|
||||
private:
|
||||
struct Record {
|
||||
std::uint64_t city;
|
||||
SessionPayload payload;
|
||||
DispatchReceipt receipt{DispatchStatus::Faulted};
|
||||
bool completed = false;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::optional<DispatchStatus> validate_context(
|
||||
TrustedClient client, const CommandEnvelope& command) const noexcept;
|
||||
[[nodiscard]] CommandStatus apply(World& world, TrustedClient client, const CommandEnvelope& command);
|
||||
|
||||
SessionIdentity identity_;
|
||||
CommandDispatcherOptions options_;
|
||||
std::array<std::vector<Record>, colony_player_slots> records_;
|
||||
std::uint64_t revision_ = 0;
|
||||
bool faulted_ = false;
|
||||
};
|
||||
|
||||
} // namespace settlement
|
||||
Reference in new issue
Block a user