Files
aevolution/include/evo/network/session_client.hpp
T
dingoandClaude Opus 5.5 897055883a Add M9.8c launcher, legend, labelled inspector and click selection
Toolkit-independent launcher, selection, label and outcome-log models with
bounded inputs and fixed error text; Dear ImGui overlay in the native adapter
with toolkit-first event routing and capture; engineering panels for fixture vs
dev region, legend, inspector, session outcomes, notices and dialog. Additive
SessionClient::unreachable_attempts() bounds unreachable connects. Docs, CI
targets and validation evidence updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 10:09:11 -04:00

85 lines
3.8 KiB
C++

#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 <chrono>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <string>
#include <vector>
// 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<DevGrant> 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{};
};
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<std::unique_ptr<SessionClient>, 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> welcome() const noexcept;
[[nodiscard]] std::optional<ViewHeader> 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;
// Drains at most max_events and resumes reconnect after local backpressure.
[[nodiscard]] std::vector<ClientEvent> take_events();
private:
struct Impl;
explicit SessionClient(std::unique_ptr<Impl> impl);
std::unique_ptr<Impl> impl_;
};
} // namespace evo::network