185 lines
7.8 KiB
C++
185 lines
7.8 KiB
C++
#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 <chrono>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#ifdef EVO_CLIENT_NETWORK
|
|
#include "evo/network/session_client.hpp"
|
|
#include <charconv>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <memory>
|
|
#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<RemoteSession> 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<std::uint16_t>(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<std::ptrdiff_t>(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<render::Rectangle>& 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<std::uint8_t> read(const std::string& path) {
|
|
std::ifstream file{path, std::ios::binary};
|
|
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
|
}
|
|
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<unsigned long long>(e.welcome.session),
|
|
static_cast<unsigned long long>(e.welcome.epoch),
|
|
static_cast<unsigned long long>(e.welcome.server_tick),
|
|
static_cast<unsigned long long>(e.welcome.commit_watermark));
|
|
break;
|
|
case Kind::closed:
|
|
std::printf("session closed reason=%u\n", static_cast<unsigned>(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<unsigned long long>(e.sequence),
|
|
static_cast<unsigned>(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<unsigned long long>(e.sequence),
|
|
static_cast<unsigned>(e.outcome.finality),
|
|
static_cast<unsigned long long>(e.outcome.tick),
|
|
static_cast<unsigned long long>(e.outcome.commit_sequence));
|
|
break;
|
|
case Kind::unknown:
|
|
intent_ = unknown;
|
|
std::printf("intent %llu outcome unknown (connection lost)\n",
|
|
static_cast<unsigned long long>(e.sequence));
|
|
break;
|
|
case Kind::backpressure:
|
|
std::puts("session event backlog: disconnected; draining before reconnect");
|
|
break;
|
|
case Kind::exhausted:
|
|
std::puts("session grants exhausted; issue a new bundle to reconnect");
|
|
break;
|
|
}
|
|
std::fflush(stdout);
|
|
}
|
|
std::unique_ptr<network::SessionClient> client_;
|
|
std::optional<std::uint64_t> 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<RemoteSession> 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<render::Rectangle>&) const {}
|
|
|
|
private:
|
|
client::EnvironmentLayer environment_;
|
|
client::OrganismLayer organisms_;
|
|
};
|
|
#endif
|
|
} // namespace evo::client_app
|