#pragma once #include "evo/client/environment_view.hpp" #include "evo/client/input.hpp" #include "evo/client/organism_view.hpp" #include "evo/network/dev_grant.hpp" #include "evo/network/protocol.hpp" #include "evo/network/tls_channel.hpp" #include #include #include #include #include #include #include #include // M9.7 dev session client (ADR-0083). Non-blocking; the desktop host calls poll() // once per frame. Holds presentation copies only: it never predicts, simulates or // treats a local value as authority. Each Hello consumes one single-use grant (except // after a `server_shutdown`/`capacity` refusal, when the server never recorded it); // every new session starts from a fresh server baseline. namespace evo::network { struct SessionClientConfig { std::string host; std::uint16_t port{}; ClientTrust trust; std::vector grants; // Consumed in order, one per Hello. std::uint32_t lead_ticks{4}; // Intended tick = newest view tick + lead. std::chrono::milliseconds attempt_deadline{5000}; // Connect to Welcome. std::chrono::milliseconds first_retry{500}, max_retry{8000}; // Liveness after Welcome: heartbeat when idle; a silent server past the timeout is // treated as lost (stale presentation, unknown outcomes, reconnect). std::chrono::milliseconds heartbeat_interval{1000}, liveness_timeout{5000}; }; enum class SessionPhase : std::uint8_t { backoff, connecting, established, exhausted }; struct ClientEvent { enum class Kind : std::uint8_t { welcome, closed, lost, // Transport/TLS failure or deadline: no Close received. result, // IntentResult. outcome, // IntentOutcome (durable or not committed). unknown, // Sent intention whose outcome died with its connection. backpressure, // Local event backlog: reconnect waits for take_events(). exhausted // No grants left; reconnection stops. } kind{}; Welcome welcome{}; CloseReason reason{}; IntentResult result{}; IntentOutcome outcome{}; std::uint64_t sequence{}; }; // Received-frame counters for timing and bandwidth experiments (M10.1); bytes include // the frame header. Monotonic for the client's lifetime, across reconnects. struct ClientTraffic { std::uint64_t frames{}, bytes{}; std::uint64_t environment_views{}, environment_view_bytes{}; std::uint64_t organism_views{}, organism_view_bytes{}; }; enum class SessionClientError : std::uint8_t { configuration }; class SessionClient { public: static constexpr std::size_t max_outstanding = 256, max_events = 1024; [[nodiscard]] static std::expected, SessionClientError> create(SessionClientConfig config); SessionClient(const SessionClient&) = delete; SessionClient& operator=(const SessionClient&) = delete; ~SessionClient(); void poll(std::chrono::steady_clock::time_point now); // Queues one intention when established, a view has arrived, fewer than // max_outstanding are unresolved and the channel queue has room; otherwise false. // Never carried across connections. [[nodiscard]] bool send(const client::Intention& intention); [[nodiscard]] SessionPhase phase() const noexcept; [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept; [[nodiscard]] const client::OrganismLayer& organisms() const noexcept; [[nodiscard]] std::optional welcome() const noexcept; [[nodiscard]] std::optional latest_view() const noexcept; [[nodiscard]] std::size_t grants_left() const noexcept; // Attempts that failed before any session event (TCP connect or TLS handshake), so a // caller can bound "unreachable" without an event per retry. Never reset. [[nodiscard]] std::uint64_t unreachable_attempts() const noexcept; [[nodiscard]] ClientTraffic traffic() const noexcept; // Drains at most max_events and resumes reconnect after local backpressure. [[nodiscard]] std::vector take_events(); private: struct Impl; explicit SessionClient(std::unique_ptr impl); std::unique_ptr impl_; }; } // namespace evo::network