#pragma once #include "evo/network/admission.hpp" #include "evo/network/dev_grant.hpp" #include "evo/network/grant_ledger.hpp" #include "evo/network/tcp.hpp" #include "evo/network/tls_channel.hpp" #include #include #include #include #include #include #include #include #include #include #include // M9.7 dev session server (ADR-0083). Host-agnostic and non-blocking: the region // host calls poll() from its own loop, publishes views of its state, lets admitted // intentions ride host inputs via carry(), and reports durable/lost outcomes. It // never touches simulation state and never decides an outcome. namespace evo::network { using RequestId = std::array; struct SessionServerConfig { std::string listen; // Numeric address:port. ServerCredentials credentials; AdmissionConfig admission; // `epoch` is ignored: it follows RegionFacts. std::size_t max_connections{4}; std::chrono::milliseconds hello_deadline{5000}; // Accept to Welcome. std::size_t max_output_bytes{std::size_t{1} << 20}; std::size_t max_input_bytes_per_second{65536}; std::size_t max_pending_intents{64}; // Admitted, not yet carried. std::size_t max_carried_intents{1024}; // Carried, awaiting durable/lost. // Liveness both ways: a Heartbeat is sent when nothing else was sent for an // interval; a peer silent for the timeout is closed with `timeout`. std::chrono::milliseconds heartbeat_interval{1000}, liveness_timeout{5000}; // Durable consumed-grant ledger (required); survives process and pod restarts. std::filesystem::path grant_ledger; }; // The host's current authority facts, read once per loop iteration. struct RegionFacts { bool ready{}; std::uint64_t epoch{}, simulated_tick{}, committed_tick{}, commit_sequence{}; }; enum class SessionServerError : std::uint8_t { configuration, listen, credentials, ledger }; struct SessionEvent { enum class Kind : std::uint8_t { opened, closed, refused } kind{}; std::uint64_t session{}, subject{}; CloseReason reason{}; }; class SessionServer { public: [[nodiscard]] static std::expected, SessionServerError> create(SessionServerConfig config, DevGrantKey key, std::uint64_t now_seconds); SessionServer(const SessionServer&) = delete; SessionServer& operator=(const SessionServer&) = delete; ~SessionServer(); [[nodiscard]] std::uint16_t port() const noexcept; // Accepts, handshakes, admits and writes; never blocks. `now_seconds` is wall // time for grant validity; `now` is monotonic time for connection deadlines. void poll(const RegionFacts& facts, std::uint64_t now_seconds, std::chrono::steady_clock::time_point now); // The latest views; each session receives only the newest (older unsent views are // replaced, never queued). New sessions receive the latest as their baseline. void publish(const client::EnvironmentPresentation& environment, const client::OrganismPresentation& organisms, const RegionFacts& facts); // The oldest admitted intention whose intended tick is <= `tick`, to ride the host // input for `tick`, under a fresh server-issued carrier id ('I','N', 48-bit epoch, // 64-bit ordinal). The host input's content stays host-chosen. [[nodiscard]] std::optional carry(std::uint64_t tick); // Host final acknowledgement / drop for a request id; ids not carried here are ignored. void committed(const RequestId& request, std::uint64_t tick, std::uint64_t commit_sequence); void not_committed(const RequestId& request); [[nodiscard]] std::vector take_events(); [[nodiscard]] std::size_t connections() const noexcept; [[nodiscard]] std::size_t sessions() const noexcept; private: struct Impl; explicit SessionServer(std::unique_ptr impl); std::unique_ptr impl_; }; } // namespace evo::network