Add M10.3b session login over QUIC
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m13s
Foundation / checks (client) (push) Successful in 12m12s
Foundation / checks (network) (push) Successful in 8m56s
Foundation / checks (tsan) (push) Successful in 7m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 19m14s
Foundation / checks (checks) (push) Canceled after 31m28s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m13s
Foundation / checks (client) (push) Successful in 12m12s
Foundation / checks (network) (push) Successful in 8m56s
Foundation / checks (tsan) (push) Successful in 7m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 19m14s
Foundation / checks (checks) (push) Canceled after 31m28s
Session protocol v1 session messages (Hello with grant, Welcome, Refresh, RefreshResult, Close with reasons) on the QUIC reliable stream. RegionSessions verifies grants with the issuer public keys and admits them through SessionControl: a newer grant closes the account's old connection as superseded, expiry and epoch advances close sessions, every close is announced and flushed. RegionSessionClient logs in, refreshes in place and reports why a session ended. ADR-0091. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include "evo/network/quic_transport.hpp"
|
||||
#include "evo/network/session_protocol.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
// M10.3b client-side session login over QUIC (ADR-0091, schemas/network/session-v1.md).
|
||||
// Owned by one thread, which calls poll(). Sends Hello with the grant once the pinned
|
||||
// connection is up (then forgets the grant), waits for Welcome or Close, and can renew
|
||||
// the session in place with a newer grant. A Close from the server, a malformed message,
|
||||
// the Welcome deadline or a lost transport ends the session; reconnecting needs a new
|
||||
// client and a fresh grant (ADR-0089). Intentions and views are later segments.
|
||||
namespace evo::client {
|
||||
struct RegionSessionClientConfig {
|
||||
network::quic::ClientConfig transport;
|
||||
network::WireGrant grant{};
|
||||
std::chrono::milliseconds welcome_deadline{5000}; // Connect to Welcome.
|
||||
};
|
||||
enum class RegionSessionState : std::uint8_t { connecting, awaiting_welcome, established, closed };
|
||||
|
||||
class RegionSessionClient {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<RegionSessionClient>,
|
||||
network::quic::EndpointError>
|
||||
connect(RegionSessionClientConfig config, std::chrono::steady_clock::time_point now);
|
||||
RegionSessionClient(const RegionSessionClient&) = delete;
|
||||
RegionSessionClient& operator=(const RegionSessionClient&) = delete;
|
||||
~RegionSessionClient();
|
||||
void poll(std::chrono::steady_clock::time_point now);
|
||||
// Sends a newer grant for the open session; false unless established and sent.
|
||||
[[nodiscard]] bool refresh(const network::WireGrant& grant);
|
||||
// The latest refresh answer, once (an accepted one also updates welcome().expires_at).
|
||||
[[nodiscard]] std::optional<network::SessionRefreshResult> take_refresh_result();
|
||||
// Sends Close{client_closed} and closes the connection.
|
||||
void close();
|
||||
[[nodiscard]] RegionSessionState state() const noexcept;
|
||||
[[nodiscard]] const std::optional<network::SessionWelcome>& welcome() const noexcept;
|
||||
[[nodiscard]] std::optional<network::SessionCloseReason> close_reason() const noexcept;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
explicit RegionSessionClient(std::unique_ptr<Impl> impl);
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
} // namespace evo::client
|
||||
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <span>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
// M10 session protocol v1, session part (ADR-0091, schemas/network/session-v1.md).
|
||||
// Messages travel on the QUIC reliable stream (one message per framed QUIC message).
|
||||
// Each starts with an 8-byte header: magic "EVSN", version u16, type u8, flags u8 (0);
|
||||
// every payload has a fixed size and explicit little-endian fields. Grants are opaque
|
||||
// 160-byte credentials here. Later M10 segments add message types under a new version.
|
||||
namespace evo::network {
|
||||
inline constexpr std::uint32_t session_magic = 0x4E535645; // Bytes "EVSN".
|
||||
inline constexpr std::uint16_t session_protocol_version = 1;
|
||||
inline constexpr std::size_t session_header_bytes = 8;
|
||||
inline constexpr std::size_t session_grant_wire_bytes = 160;
|
||||
|
||||
enum class SessionMessageType : std::uint8_t {
|
||||
hello = 1, // Client -> server: grant.
|
||||
welcome, // Server -> client.
|
||||
refresh, // Client -> server: newer grant for the open session.
|
||||
refresh_result, // Server -> client.
|
||||
close // Either way, then the sender closes the connection.
|
||||
};
|
||||
// Wire values 1..13. `connection_lost` is local only (the transport ended without a Close).
|
||||
enum class SessionCloseReason : std::uint8_t {
|
||||
client_closed = 1,
|
||||
superseded, // A newer grant for the same account opened another session.
|
||||
expired,
|
||||
stale_epoch,
|
||||
unauthenticated,
|
||||
wrong_scope, // Environment, world or region.
|
||||
stale_grant, // Replayed or older than a grant already presented.
|
||||
rate_limited,
|
||||
capacity,
|
||||
protocol,
|
||||
timeout, // No Hello before the deadline.
|
||||
unsupported_version,
|
||||
shutdown,
|
||||
connection_lost = 255
|
||||
};
|
||||
enum class SessionCodecError : std::uint8_t {
|
||||
truncated,
|
||||
bad_magic,
|
||||
unsupported_version,
|
||||
unknown_type,
|
||||
reserved_flags,
|
||||
wrong_length,
|
||||
invalid_value
|
||||
};
|
||||
|
||||
using WireGrant = std::array<std::uint8_t, session_grant_wire_bytes>;
|
||||
struct SessionHello {
|
||||
std::uint32_t required_features{}; // Must be zero in v1.
|
||||
WireGrant grant{};
|
||||
};
|
||||
struct SessionWelcome {
|
||||
std::uint64_t session{}, epoch{}, world{}, frame{}, region{}, expires_at{};
|
||||
std::uint64_t server_seconds{}; // Server wall clock at Welcome, for refresh timing.
|
||||
friend bool operator==(const SessionWelcome&, const SessionWelcome&) = default;
|
||||
};
|
||||
struct SessionRefresh {
|
||||
WireGrant grant{};
|
||||
};
|
||||
// accepted: 0 and the new expiry. Otherwise a refusal reason (stale_grant,
|
||||
// unauthenticated, wrong_scope, expired, rate_limited); the session keeps its expiry.
|
||||
struct SessionRefreshResult {
|
||||
std::uint8_t status{};
|
||||
std::uint64_t expires_at{};
|
||||
friend bool operator==(const SessionRefreshResult&, const SessionRefreshResult&) = default;
|
||||
};
|
||||
struct SessionClose {
|
||||
SessionCloseReason reason{};
|
||||
};
|
||||
using SessionMessage =
|
||||
std::variant<SessionHello, SessionWelcome, SessionRefresh, SessionRefreshResult, SessionClose>;
|
||||
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode_session(const SessionMessage& message);
|
||||
// Exactly one message; any other length for its type rejects.
|
||||
[[nodiscard]] std::expected<SessionMessage, SessionCodecError>
|
||||
decode_session(std::span<const std::uint8_t> bytes);
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
#include "evo/network/quic_transport.hpp"
|
||||
#include "evo/network/session_control.hpp"
|
||||
#include "evo/network/session_grant.hpp"
|
||||
#include "evo/network/session_protocol.hpp"
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// M10.3b region-side session login over QUIC (ADR-0091, schemas/network/session-v1.md).
|
||||
// Owned by the region host's thread, which calls poll(). A connection must send Hello
|
||||
// with a grant before the deadline; the grant is verified with the region's issuer
|
||||
// public keys and admitted by SessionControl. A newer grant for an account closes the
|
||||
// account's previous connection with `superseded`. Refusals and closes send Close with a
|
||||
// reason, then the connection is closed after a short flush. Intentions and views are
|
||||
// later segments (10.4, 10.5). Events carry account log labels only, never grants.
|
||||
namespace evo::server {
|
||||
struct IssuerKey {
|
||||
std::uint32_t key_id{};
|
||||
network::GrantPublicKey public_key{};
|
||||
};
|
||||
struct RegionSessionsConfig {
|
||||
network::quic::ServerConfig transport;
|
||||
network::ControlConfig control; // `epoch` is the initial ownership epoch.
|
||||
std::vector<IssuerKey> issuer_keys;
|
||||
std::chrono::milliseconds hello_deadline{5000}; // Connected to Hello (ADR-0089).
|
||||
std::chrono::milliseconds close_flush{1000}; // Close sent to connection closed.
|
||||
};
|
||||
enum class RegionSessionsError : std::uint8_t { configuration, keys, transport };
|
||||
struct RegionSessionEvent {
|
||||
enum class Kind : std::uint8_t { opened, closed, refused } kind{};
|
||||
std::uint64_t session{}; // 0 for refusals.
|
||||
std::string account; // log_label, empty when unauthenticated.
|
||||
network::SessionCloseReason reason{};
|
||||
};
|
||||
|
||||
class RegionSessions {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError>
|
||||
create(RegionSessionsConfig config);
|
||||
RegionSessions(const RegionSessions&) = delete;
|
||||
RegionSessions& operator=(const RegionSessions&) = delete;
|
||||
~RegionSessions();
|
||||
// `epoch` is the region's current ownership epoch: a newer one closes every session
|
||||
// (stale_epoch). `now_seconds` is wall time for grants, `now` monotonic for deadlines.
|
||||
void poll(std::uint64_t epoch, std::uint64_t now_seconds,
|
||||
std::chrono::steady_clock::time_point now);
|
||||
// Closes every connection with `shutdown` (flushed by later polls).
|
||||
void shutdown(std::chrono::steady_clock::time_point now);
|
||||
[[nodiscard]] std::vector<RegionSessionEvent> take_events();
|
||||
[[nodiscard]] std::optional<std::uint64_t> holder(const network::AccountId& account) const;
|
||||
[[nodiscard]] std::size_t sessions() const noexcept;
|
||||
[[nodiscard]] std::size_t connections() const noexcept;
|
||||
[[nodiscard]] std::uint16_t port() const noexcept;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
explicit RegionSessions(std::unique_ptr<Impl> impl);
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
} // namespace evo::server
|
||||
Reference in New Issue
Block a user