#pragma once #include #include #include #include #include #include #include // 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; 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; [[nodiscard]] std::vector encode_session(const SessionMessage& message); // Exactly one message; any other length for its type rejects. [[nodiscard]] std::expected decode_session(std::span bytes); } // namespace evo::network