#pragma once #include "evo/network/quic_transport.hpp" #include "evo/network/replication.hpp" #include "evo/network/session_protocol.hpp" #include #include #include #include #include #include // 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). M10.4 (ADR-0092): intentions as datagrams with // receipts and a simulated watermark; actions with stable ids, receipts and final outcomes. // The client never treats a receipt or the watermark as a final outcome. // M10.5 (ADR-0093): snapshot chunks are assembled and acknowledged here; the newest complete // snapshot is available as a view and as validated presentations. 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, 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 take_refresh_result(); // Sends Close{client_closed} and closes the connection. void close(); // Realtime intention for `tick` (server_tick + 1 ..= + 8); returns its sequence, or // nothing unless established and sent (also while the QUIC datagram extension is not yet // negotiated, just after Welcome: retry). Datagrams may be lost: no receipt is not a // refusal. [[nodiscard]] std::optional send_intention(std::uint64_t tick, std::int16_t move_x, std::int16_t move_y); // A discrete action with a caller-chosen id, unique for the account (stable across // retries and reconnects). False unless established and sent. [[nodiscard]] bool send_action(std::uint64_t action_id, network::ActionKind kind, std::uint64_t tick); [[nodiscard]] bool query_action(std::uint64_t action_id); [[nodiscard]] std::vector take_intention_receipts(); [[nodiscard]] std::vector take_action_receipts(); [[nodiscard]] std::vector take_action_outcomes(); // Newest simulated watermark of this session's epoch. [[nodiscard]] std::optional simulated() const noexcept; // Latest server tick seen (Welcome, receipts, watermarks); 0 before Welcome. [[nodiscard]] std::uint64_t server_tick() const noexcept; // Asks for another spatial focus (the server clamps it and restarts from a fresh // baseline). False unless established and sent. [[nodiscard]] bool set_interest(const network::SessionInterest& interest); // The newest complete snapshot, or null before the first. [[nodiscard]] const network::ReplicatedView* view() const noexcept; [[nodiscard]] std::expected environment() const; [[nodiscard]] std::expected organisms() const; // Snapshot datagrams and bytes received (whole messages), for reporting. [[nodiscard]] std::uint64_t snapshot_chunks() const noexcept; [[nodiscard]] std::uint64_t snapshot_bytes() const noexcept; [[nodiscard]] RegionSessionState state() const noexcept; [[nodiscard]] const std::optional& welcome() const noexcept; [[nodiscard]] std::optional close_reason() const noexcept; // v4 (ADR-0096): the offer to continue at another region (kept until taken; the session // then closes with handed_off), and where the account's probe is when a login was closed // with moved. [[nodiscard]] std::optional take_handoff(); [[nodiscard]] const std::optional& redirect() const noexcept; // Why the QUIC connection itself ended (e.g. `untrusted`: the server answered but its // certificate failed the pinned check), when it ended without a session Close. [[nodiscard]] std::optional transport_cause() const noexcept; private: struct Impl; explicit RegionSessionClient(std::unique_ptr impl); std::unique_ptr impl_; }; } // namespace evo::client