#pragma once // M9.7 remote dev session for the engineering client (ADR-0083). Without // EVO_CLIENT_NETWORK (a build without EVO_BUILD_NETWORK) remote options are refused. // The client only presents copies and sends bounded intentions; it never predicts. #include "evo/client/environment_view.hpp" #include "evo/client/input.hpp" #include "evo/client/organism_view.hpp" #include "evo/render/frame.hpp" #include #include #include #include #include #ifdef EVO_CLIENT_NETWORK #include "evo/network/session_client.hpp" #include #include #include #include #include #endif namespace evo::client_app { struct RemoteOptions { std::string connect, server_name, authority, grants; [[nodiscard]] bool any() const noexcept { return !connect.empty() || !server_name.empty() || !authority.empty() || !grants.empty(); } [[nodiscard]] bool complete() const noexcept { return !connect.empty() && !server_name.empty() && !authority.empty() && !grants.empty(); } }; #ifdef EVO_CLIENT_NETWORK inline constexpr bool remote_available = true; class RemoteSession { public: static std::optional open(const RemoteOptions& o) { const auto colon = o.connect.rfind(':'); unsigned port = 0; if (colon == std::string::npos || colon == 0) return std::nullopt; const auto* digits = o.connect.data() + colon + 1; const auto [end, error] = std::from_chars(digits, o.connect.data() + o.connect.size(), port); if (error != std::errc{} || end != o.connect.data() + o.connect.size() || port == 0 || port > 65535) return std::nullopt; const auto bundle = read(o.grants); if (bundle.empty() || bundle.size() % network::dev_grant_bytes != 0 || bundle.size() / network::dev_grant_bytes > 64) return std::nullopt; network::SessionClientConfig config; config.host = o.connect.substr(0, colon); config.port = static_cast(port); const auto pem = read(o.authority); config.trust = {{pem.begin(), pem.end()}, o.server_name}; for (std::size_t at = 0; at < bundle.size(); at += network::dev_grant_bytes) { network::DevGrant grant{}; std::copy_n(bundle.begin() + static_cast(at), grant.size(), grant.begin()); config.grants.push_back(grant); } auto made = network::SessionClient::create(std::move(config)); if (!made) return std::nullopt; RemoteSession session; session.client_ = std::move(*made); return session; } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return client_->environment(); } [[nodiscard]] const client::OrganismLayer& organisms() const noexcept { return client_->organisms(); } // Services the connection and sends the intention when its vector changed, at // most once per received server tick (the server also rate-limits). void step(std::chrono::steady_clock::time_point now, const client::Intention& intention) { client_->poll(now); const auto view = client_->latest_view(); const bool changed = intention.move_x != sent_x_ || intention.move_y != sent_y_; if (view && changed && (!sent_tick_ || view->server_tick > *sent_tick_) && client_->send(intention)) { sent_x_ = intention.move_x; sent_y_ = intention.move_y; sent_tick_ = view->server_tick; } for (const auto& e : client_->take_events()) report(e); } // Connection chip and last-intention chip, below the capture bar. void status(std::vector& out) const { using network::SessionPhase; const auto phase = client_->phase(); const render::Color connection = phase == SessionPhase::established ? render::Color{64, 180, 140, 255} : phase == SessionPhase::exhausted ? render::Color{200, 40, 40, 255} : render::Color{240, 170, 40, 255}; out.push_back({8, 72, 28, 6, connection}); out.push_back({40, 72, 6, 6, intent_}); } private: static std::vector read(const std::string& path) { std::ifstream file{path, std::ios::binary}; return {std::istreambuf_iterator{file}, std::istreambuf_iterator{}}; } void report(const network::ClientEvent& e) { using Kind = network::ClientEvent::Kind; constexpr render::Color pending{240, 240, 240, 255}, durable{64, 180, 140, 255}, refused{200, 40, 40, 255}, unknown{130, 130, 130, 255}; switch (e.kind) { case Kind::welcome: sent_tick_.reset(); // New session: resend the current intention. sent_x_ = sent_y_ = 0; std::printf("session welcome session=%llu epoch=%llu tick=%llu commit=%llu\n", static_cast(e.welcome.session), static_cast(e.welcome.epoch), static_cast(e.welcome.server_tick), static_cast(e.welcome.commit_watermark)); break; case Kind::closed: std::printf("session closed reason=%u\n", static_cast(e.reason)); break; case Kind::lost: std::puts("session lost (reconnecting with a fresh grant)"); break; case Kind::result: intent_ = e.result.status == network::IntentStatus::admitted ? pending : refused; std::printf("intent %llu status=%u\n", static_cast(e.sequence), static_cast(e.result.status)); break; case Kind::outcome: intent_ = e.outcome.finality == network::IntentFinality::committed_no_effect ? durable : refused; std::printf("intent %llu final=%u tick=%llu commit=%llu\n", static_cast(e.sequence), static_cast(e.outcome.finality), static_cast(e.outcome.tick), static_cast(e.outcome.commit_sequence)); break; case Kind::unknown: intent_ = unknown; std::printf("intent %llu outcome unknown (connection lost)\n", static_cast(e.sequence)); break; case Kind::exhausted: std::puts("session grants exhausted; issue a new bundle to reconnect"); break; } std::fflush(stdout); } std::unique_ptr client_; std::optional sent_tick_; std::int16_t sent_x_{}, sent_y_{}; render::Color intent_{60, 60, 66, 255}; }; #else inline constexpr bool remote_available = false; class RemoteSession { public: static std::optional open(const RemoteOptions&) { return std::nullopt; } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return environment_; } [[nodiscard]] const client::OrganismLayer& organisms() const noexcept { return organisms_; } void step(std::chrono::steady_clock::time_point, const client::Intention&) {} void status(std::vector&) const {} private: client::EnvironmentLayer environment_; client::OrganismLayer organisms_; }; #endif } // namespace evo::client_app