#pragma once // M10.5c remote dev session for the engineering client (ADR-0093, over ADR-0091/0092 // sessions). Without EVO_CLIENT_NETWORK (a build without EVO_BUILD_QUIC) remote mode is // unavailable. The client only presents copies of replicated snapshots and sends bounded // intentions; it never predicts. Grant bytes stay inside the session: each is handed to // one connection and wiped here; never printed, shown or persisted. // // One connection per grant (ADR-0089): a lost or closed session reconnects with the next // grant of the bundle after a growing pause (2 s .. 30 s, reset by a Welcome), which keeps // a reconnect loop inside the region's per-account attempt limit. Views come from the // newest applied snapshot. The camera follows the server's default view until the user // moves it; from then on it drives the interest (radius 7), at most every 250 ms. #include "evo/client/camera.hpp" #include "evo/client/environment_view.hpp" #include "evo/client/input.hpp" #include "evo/client/launcher.hpp" #include "evo/client/organism_view.hpp" #include "evo/client/outcome_log.hpp" #include "evo/client/ui_state.hpp" #include #include #include #include #include #include #ifdef EVO_CLIENT_NETWORK #include "evo/client/region_session.hpp" #include "evo/network/quic_transport.hpp" #include "evo/world/coordinates.hpp" #include #include #include #include #include #include #endif namespace evo::client_app { struct RemoteStatus { std::string phase; // connecting / connected / reconnecting / out of grants std::uint64_t session{}, epoch{}, server_tick{}, simulated_tick{}; std::size_t grants_left{}; bool welcomed{}; // Newest applied snapshot and the interest last asked for (ADR-0093). std::uint64_t snapshot_tick{}, snapshot_bytes{}; std::size_t cells{}, organisms{}; std::int64_t focus_x{}, focus_y{}, focus_z{}; bool focused{}; }; #ifdef EVO_CLIENT_NETWORK inline constexpr bool remote_available = true; static_assert(std::is_same_v, "the launcher's grant record is the session's grant"); class RemoteSession { public: static constexpr std::uint16_t interest_radius = 7; static constexpr std::uint64_t intention_lead = 2; // Ticks ahead of the server tick. // Looks the endpoint up once (blocking); nothing connects until the first step(). static std::optional open(const client::ConnectMaterial& m) { const auto address = network::quic::resolve_ipv4(m.endpoint.host); if (!address || m.grants.empty()) return std::nullopt; RemoteSession session; session.transport_.server_address = *address; session.transport_.port = m.endpoint.port; session.transport_.server_name = m.server_name; session.transport_.authority_pem = m.authority_pem; session.transport_.authority_pem_file = m.authority_path; session.grants_ = m.grants; return session; } RemoteSession(RemoteSession&&) noexcept = default; RemoteSession& operator=(RemoteSession&&) noexcept = default; RemoteSession(const RemoteSession&) = delete; RemoteSession& operator=(const RemoteSession&) = delete; ~RemoteSession() { if (client_) client_->close(); // Close{client_closed}; unresolved intentions stay unknown. for (auto& g : grants_) g.fill(0); } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return environment_; } [[nodiscard]] const client::OrganismLayer& organisms() const noexcept { return organisms_; } [[nodiscard]] const client::OutcomeLog& outcomes() const noexcept { return log_; } [[nodiscard]] RemoteStatus status() const { RemoteStatus s; const bool live = client_ && client_->state() == client::RegionSessionState::established; s.phase = live ? "connected" : exhausted_ ? "out of grants" : welcomed_ ? "reconnecting" : "connecting"; s.session = session_; s.epoch = epoch_; s.server_tick = server_tick_; s.simulated_tick = simulated_tick_; s.grants_left = grants_.size() - next_; s.welcomed = welcomed_; s.snapshot_tick = seen_snapshot_; s.snapshot_bytes = snapshot_bytes_ + (client_ ? client_->snapshot_bytes() : 0); if (const auto* e = environment_.current()) s.cells = e->cells().size(); if (const auto* o = organisms_.current()) s.organisms = o->cells().size(); if (sent_interest_) { s.focused = true; s.focus_x = sent_interest_->x; s.focus_y = sent_interest_->y; s.focus_z = sent_interest_->z; } return s; } // Where the camera should be while it follows the server's view: the middle of the // newest snapshot's square, offered whenever it changes (the first snapshots may still // be filling in, nearest cells first). [[nodiscard]] std::optional take_recentre() { return std::exchange(recentre_, std::nullopt); } // Called every frame with the camera centre. While following, a centre other than the // one offered means the user moved the camera: from then on the camera's voxel is the // interest of the next sends. void focus(client::WorldPoint centre) { if (!std::isfinite(centre.x_mm) || !std::isfinite(centre.y_mm)) return; if (following_) { if (!offered_ || (centre.x_mm == offered_->x_mm && centre.y_mm == offered_->y_mm)) return; // Nothing shown yet, or still where the view put it. following_ = false; recentre_.reset(); } constexpr auto pitch = static_cast(world::cell_pitch_mm); constexpr double limit = 9.0e15; // Well inside int64 and the camera's own range. const double x = std::floor(centre.x_mm / pitch), y = std::floor(centre.y_mm / pitch); if (std::abs(x) > limit || std::abs(y) > limit) return; wanted_ = network::SessionInterest{static_cast(x), static_cast(y), slice_z_, interest_radius}; } // Services the connection; sends the intention when its vector changed, at most once // per server tick. Session events update the outcome log (the record), short // notifications and the launcher. void step(std::chrono::steady_clock::time_point now, const client::Intention& intention, client::UiState& ui, client::Launcher& launcher) { if (!client_ && !start(now, launcher)) return; auto& c = *client_; c.poll(now); if (c.state() == client::RegionSessionState::closed) { end(now, ui, launcher); return; } if (c.state() != client::RegionSessionState::established) return; if (!established_) welcome(ui, launcher); server_tick_ = c.server_tick(); send(intention); receipts(); snapshot(); if (wanted_ && (!sent_interest_ || *wanted_ != *sent_interest_) && now - interest_at_ >= std::chrono::milliseconds{250} && c.set_interest(*wanted_)) { sent_interest_ = wanted_; interest_at_ = now; } std::fflush(stdout); } private: using Clock = std::chrono::steady_clock; // Session sequences restart with each session; the log numbers intentions itself. struct Pending { std::uint64_t sequence{}, entry{}, tick{}; bool admitted{}; }; static constexpr std::size_t max_pending = client::OutcomeLog::capacity; RemoteSession() = default; static void notify(client::UiState& ui, const char* text, client::NoticeLevel level) { static_cast(ui.notify(text, level).has_value()); // Bounded; overflow evicts. } // Opens the next connection with the next grant; false while pausing or out of grants. bool start(Clock::time_point now, client::Launcher& launcher) { if (now < retry_at_) return false; if (next_ >= grants_.size()) { if (!exhausted_) { exhausted_ = true; launcher.observe(client::ConnectSignal::exhausted); std::puts("session grants exhausted; issue a new bundle to reconnect"); } return false; } client::RegionSessionClientConfig config; config.transport = transport_; config.grant = grants_[next_]; grants_[next_].fill(0); ++next_; auto made = client::RegionSessionClient::connect(std::move(config), now); if (!made) { launcher.observe(client::ConnectSignal::lost); pause(now); return false; } client_ = std::move(*made); established_ = false; return true; } void pause(Clock::time_point now) { retry_at_ = now + backoff_; backoff_ = std::min(backoff_ * 2, std::chrono::seconds{30}); } void welcome(client::UiState& ui, client::Launcher& launcher) { const auto& w = *client_->welcome(); established_ = true; welcomed_ = true; backoff_ = std::chrono::seconds{2}; if (w.epoch != epoch_) { // Another authority lineage: start the views afresh. environment_.reset(); organisms_.reset(); seen_snapshot_ = 0; } session_ = w.session; epoch_ = w.epoch; sent_tick_.reset(); // New session: resend the current intention. sent_x_ = sent_y_ = 0; sent_interest_.reset(); // The server starts from its default interest. launcher.observe(client::ConnectSignal::welcome); notify(ui, "Connected to the dev region (fresh baseline).", client::NoticeLevel::info); std::printf("session welcome session=%llu epoch=%llu tick=%llu\n", static_cast(w.session), static_cast(w.epoch), static_cast(w.server_tick)); } void send(const client::Intention& intention) { const bool changed = intention.move_x != sent_x_ || intention.move_y != sent_y_; if (!changed || server_tick_ == 0 || (sent_tick_ && server_tick_ <= *sent_tick_)) return; const auto tick = server_tick_ + intention_lead; // Refused until the datagram extension is negotiated: retried next frame. const auto sequence = client_->send_intention(tick, intention.move_x, intention.move_y); if (!sequence) return; sent_x_ = intention.move_x; sent_y_ = intention.move_y; sent_tick_ = server_tick_; const auto entry = next_entry_++; log_.record({entry, client::OutcomeState::sent, 0, tick}); if (pending_.size() == max_pending) pending_.pop_front(); // Its log entry was evicted with it. pending_.push_back({*sequence, entry, tick, false}); } void receipts() { for (const auto& r : client_->take_intention_receipts()) { const auto it = std::find_if(pending_.begin(), pending_.end(), [&](const Pending& p) { return p.sequence == r.sequence; }); if (it == pending_.end()) continue; // Evicted from the bounded record already. std::printf("intent %llu status=%u\n", static_cast(it->entry), static_cast(r.status)); if (r.status == network::AdmitStatus::admitted) { log_.record({it->entry, client::OutcomeState::admitted, 0, it->tick}); it->admitted = true; } else { log_.record({it->entry, client::OutcomeState::rejected, static_cast(r.status), it->tick}); pending_.erase(it); } } const auto s = client_->simulated(); if (!s) return; simulated_tick_ = s->tick; // Cumulative: every admitted intention up to the sequence, at or before the tick. std::erase_if(pending_, [&](const Pending& p) { if (!p.admitted || p.sequence > s->sequence || p.tick > s->tick) return false; log_.record({p.entry, client::OutcomeState::simulated, 0, p.tick}); return true; }); } void snapshot() { const auto* v = client_->view(); if (v == nullptr || v->tick == seen_snapshot_) return; seen_snapshot_ = v->tick; slice_z_ = v->slice_z; if (auto e = client_->environment()) { if (!environment_.accept(*e)) { // E.g. regressed after a restore: start afresh. environment_.reset(); static_cast(environment_.accept(std::move(*e)).has_value()); } } if (auto o = client_->organisms()) { if (!organisms_.accept(*o)) { organisms_.reset(); static_cast(organisms_.accept(std::move(*o)).has_value()); } } if (!following_ || v->environment.empty()) return; const auto [lx, hx] = std::minmax_element(v->environment.begin(), v->environment.end(), [](const auto& a, const auto& b) { return a.voxel.x < b.voxel.x; }); const auto [ly, hy] = std::minmax_element(v->environment.begin(), v->environment.end(), [](const auto& a, const auto& b) { return a.voxel.y < b.voxel.y; }); constexpr auto pitch = static_cast(world::cell_pitch_mm); const client::WorldPoint middle{ (static_cast(lx->voxel.x) + static_cast(hx->voxel.x) + 1) * pitch / 2, (static_cast(ly->voxel.y) + static_cast(hy->voxel.y) + 1) * pitch / 2}; if (!offered_ || middle.x_mm != offered_->x_mm || middle.y_mm != offered_->y_mm) recentre_ = offered_ = middle; } // The connection ended: decide what the launcher hears and pause before the next grant. void end(Clock::time_point now, client::UiState& ui, client::Launcher& launcher) { using network::SessionCloseReason; const auto reason = client_->close_reason().value_or(SessionCloseReason::connection_lost); const bool was_established = established_; snapshot_bytes_ += client_->snapshot_bytes(); for (const auto& p : pending_) log_.record({p.entry, client::OutcomeState::unknown, 0, p.tick}); pending_.clear(); environment_.mark_stale(); organisms_.mark_stale(); client_.reset(); established_ = false; const bool not_ready = reason == SessionCloseReason::shutdown || reason == SessionCloseReason::capacity; const bool lost = reason == SessionCloseReason::connection_lost || reason == SessionCloseReason::timeout || reason == SessionCloseReason::rate_limited || reason == SessionCloseReason::stale_epoch || (was_established && reason == SessionCloseReason::expired); if (reason == SessionCloseReason::superseded) { // Another client holds this account's newer grant: never fight it. launcher.fail(client::LaunchError::grant_refused); notify(ui, "Another client opened a session with this account.", client::NoticeLevel::warning); } else { launcher.observe(not_ready ? client::ConnectSignal::not_ready : lost ? client::ConnectSignal::lost : client::ConnectSignal::refused); notify(ui, not_ready ? "Server not ready; retrying." : was_established ? "Session ended; reconnecting with the next grant." : "Connection attempt failed.", not_ready ? client::NoticeLevel::info : client::NoticeLevel::warning); } std::printf("session closed reason=%u\n", static_cast(reason)); pause(now); } network::quic::ClientConfig transport_; std::vector grants_; std::size_t next_{}; std::unique_ptr client_; client::EnvironmentLayer environment_; client::OrganismLayer organisms_; client::OutcomeLog log_; std::deque pending_; std::uint64_t next_entry_{1}; Clock::time_point retry_at_{}, interest_at_{}; std::chrono::seconds backoff_{2}; std::uint64_t session_{}, epoch_{}, server_tick_{}, simulated_tick_{}, seen_snapshot_{}, snapshot_bytes_{}; std::optional sent_tick_; std::optional wanted_, sent_interest_; std::optional recentre_, offered_; std::int64_t slice_z_{}; std::int16_t sent_x_{}, sent_y_{}; bool established_{}, welcomed_{}, exhausted_{}; bool following_{true}; // The camera follows the server's (default) view. }; #else inline constexpr bool remote_available = false; class RemoteSession { public: static std::optional open(const client::ConnectMaterial&) { return std::nullopt; } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return environment_; } [[nodiscard]] const client::OrganismLayer& organisms() const noexcept { return organisms_; } [[nodiscard]] const client::OutcomeLog& outcomes() const noexcept { return log_; } [[nodiscard]] RemoteStatus status() const { return {"unavailable in this build"}; } [[nodiscard]] std::optional take_recentre() { return std::nullopt; } void focus(client::WorldPoint) {} void step(std::chrono::steady_clock::time_point, const client::Intention&, client::UiState&, client::Launcher&) {} private: client::EnvironmentLayer environment_; client::OrganismLayer organisms_; client::OutcomeLog log_; }; #endif } // namespace evo::client_app