#include "evo/network/session_server.hpp" #include #include namespace evo::network { namespace { using Clock = std::chrono::steady_clock; constexpr std::size_t max_accepts_per_poll = 4; constexpr std::size_t max_read_per_poll = 65536; CloseReason close_reason(AdmissionError e) { switch (e) { case AdmissionError::duplicate_subject: return CloseReason::duplicate_session; case AdmissionError::expired: return CloseReason::expired; case AdmissionError::sessions_full: case AdmissionError::grants_full: return CloseReason::capacity; default: return CloseReason::unauthenticated; } } CloseReason close_reason(ProtocolError e) { return e == ProtocolError::unsupported_version ? CloseReason::unsupported_version : CloseReason::malformed; } void append(std::vector& out, const std::vector& bytes) { out.insert(out.end(), bytes.begin(), bytes.end()); } } // namespace struct SessionServer::Impl { enum class State : std::uint8_t { handshake, hello, established, closing }; struct Connection { std::uint64_t id{}; std::unique_ptr stream; // Declared first: outlives the channel. std::unique_ptr tls; State state{State::handshake}; Clock::time_point deadline; FrameReader reader; std::vector out, view; std::uint64_t session{}, subject{}, epoch{}, expires_at{}; std::size_t window_bytes{}; Clock::time_point window_start; bool dead{}; }; struct Pending { RequestId request{}; std::uint64_t intended_tick{}, connection{}, sequence{}; }; struct Carried { RequestId request{}; std::uint64_t connection{}, sequence{}; }; Impl(SessionServerConfig c, DevGrantKey k, std::unique_ptr l) : config{std::move(c)}, key{std::move(k)}, listener{std::move(l)} {} SessionServerConfig config; DevGrantKey key; std::unique_ptr listener; std::optional admission; std::vector connections; std::deque pending; std::vector carried; std::vector events; std::optional environment; std::optional organisms; RegionFacts published{}; std::uint64_t baseline{}, next_connection{1}; Clock::time_point polled_at{}; // Time of the current poll, for Close deadlines. Connection* find(std::uint64_t id) { const auto it = std::ranges::find_if(connections, [&](const Connection& c) { return c.id == id && !c.dead && c.state == State::established; }); return it == connections.end() ? nullptr : &*it; } void close(Connection& c, CloseReason reason) { if (c.state == State::closing || c.dead) return; if (c.state == State::hello || c.state == State::established) append(c.out, encode(reason)); else c.dead = true; // No channel yet to say anything over. events.push_back({c.state == State::established ? SessionEvent::Kind::closed : SessionEvent::Kind::refused, c.session, c.subject, reason}); c.state = State::closing; c.deadline = polled_at + std::chrono::seconds{1}; // Bounded flush of the Close frame. c.view.clear(); } std::vector views_for(const Connection& c) const { std::vector bytes; if (!environment || !organisms) return bytes; const ViewHeader header{c.session, c.epoch, published.simulated_tick, published.committed_tick, baseline}; append(bytes, encode(header, *environment)); append(bytes, encode(header, *organisms)); return bytes; } void hello(Connection& c, const OwnedFrame& f, const RegionFacts& facts, std::uint64_t now_seconds) { if (f.type != MessageType::hello) { close(c, CloseReason::malformed); return; } const auto message = decode_hello({f.type, f.payload}); if (!message) { close(c, close_reason(message.error())); return; } if (!admission || !facts.ready || facts.epoch != admission->epoch()) { close(c, CloseReason::server_shutdown); // No current authority: retry later. return; } const auto grant = key.verify(message->grant); if (!grant) { close(c, CloseReason::unauthenticated); return; } const auto welcome = admission->open(*grant, now_seconds, facts.simulated_tick, facts.committed_tick); if (!welcome) { c.subject = grant->claims().subject; close(c, close_reason(welcome.error())); return; } c.session = welcome->session; c.subject = grant->claims().subject; c.epoch = welcome->epoch; c.expires_at = welcome->grant_expires_at; c.state = State::established; append(c.out, encode(*welcome)); c.view = views_for(c); // Fresh baseline for every new session. events.push_back({SessionEvent::Kind::opened, c.session, c.subject, {}}); } void intent(Connection& c, const OwnedFrame& f, const RegionFacts& facts, std::uint64_t now_seconds) { const auto m = decode_intent({f.type, f.payload}); if (!m) { close(c, close_reason(m.error())); return; } IntentResult result{m->sequence, IntentStatus::unknown_session, facts.simulated_tick}; // A connection may only speak for its own session. if (m->session == c.session && admission) { if (pending.size() >= config.max_pending_intents) result.status = IntentStatus::rate_limited; // Host backlog, not admission. else result = admission->admit(*m, facts.simulated_tick, now_seconds); } if (result.status == IntentStatus::admitted) pending.push_back({request_id(c, m->sequence), m->tick, c.id, m->sequence}); append(c.out, encode(result)); } RequestId request_id(const Connection& c, std::uint64_t sequence) const { RequestId id{'I', 'N', 'T', '1'}; const std::array parts{c.epoch, c.session, sequence}; for (std::size_t p = 0; p < parts.size(); ++p) for (unsigned i = 0; i < 4; ++i) id.at(4 + 4 * p + i) = static_cast(parts.at(p) >> (8U * (3U - i))); return id; } void receive(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds, Clock::time_point now) { std::array buffer{}; std::size_t total = 0; while (!c.dead && c.state != State::closing && total < max_read_per_poll) { const auto n = c.tls->receive(buffer); if (!n) { c.dead = true; return; } if (*n == 0) return; total += *n; if (now - c.window_start >= std::chrono::seconds{1}) { c.window_start = now; c.window_bytes = 0; } c.window_bytes += *n; if (c.window_bytes > config.max_input_bytes_per_second) { close(c, CloseReason::rate_limited); return; } std::span rest{buffer.data(), *n}; while (!rest.empty() && c.state != State::closing) { const auto taken = c.reader.feed(rest); if (!taken) { close(c, close_reason(taken.error())); return; } rest = rest.subspan(*taken); while (auto f = c.reader.take()) { if (c.state == State::hello) hello(c, *f, facts, now_seconds); else if (f->type == MessageType::intent) intent(c, *f, facts, now_seconds); else if (f->type == MessageType::close) { c.state = State::closing; // Peer is leaving; nothing to say. events.push_back({SessionEvent::Kind::closed, c.session, c.subject, CloseReason::normal}); } else close(c, CloseReason::malformed); if (c.state == State::closing) break; } } } } void transmit(Connection& c) { if (c.out.empty() && !c.view.empty() && c.state == State::established) c.out.swap(c.view); // Only the newest view waits; it is never queued twice. std::size_t sent = 0; while (sent < c.out.size()) { const auto n = c.tls->send(std::span{c.out}.subspan(sent)); if (!n) { c.dead = true; return; } if (*n == 0) break; sent += *n; } c.out.erase(c.out.begin(), c.out.begin() + static_cast(sent)); if (c.out.size() + c.view.size() > config.max_output_bytes) c.dead = true; // Slow consumer: drop rather than buffer without bound. if (c.state == State::closing && c.out.empty()) { // Best-effort close_notify: the connection is dropped either way. static_cast(c.tls->close().has_value()); c.dead = true; } } void step(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds, Clock::time_point now) { if (c.state == State::handshake) { const auto p = c.tls->handshake(); if (!p) { c.dead = true; return; } if (*p == TlsProgress::done) c.state = State::hello; } if (c.state == State::closing && now > c.deadline) { c.dead = true; // Peer would not take the Close frame. return; } if ((c.state == State::handshake || c.state == State::hello) && now > c.deadline) close(c, CloseReason::unauthenticated); if (c.state == State::established && now_seconds >= c.expires_at) close(c, CloseReason::expired); if (c.state == State::established && admission && c.epoch != admission->epoch()) close(c, CloseReason::stale_epoch); if (c.state == State::hello || c.state == State::established) receive(c, facts, now_seconds, now); if (!c.dead && c.state != State::handshake) transmit(c); } void bury() { for (auto& c : connections) { if (!c.dead) continue; if (c.session != 0 && admission) // The session is ours; only an already-closed session could fail. static_cast(admission->close(c.session).has_value()); std::erase_if(pending, [&](const Pending& p) { return p.connection == c.id; }); } std::erase_if(connections, [](const Connection& c) { return c.dead; }); } }; SessionServer::SessionServer(std::unique_ptr impl) : impl_{std::move(impl)} {} SessionServer::~SessionServer() = default; std::expected, SessionServerError> SessionServer::create(SessionServerConfig config, DevGrantKey key) { if (config.max_connections == 0 || config.max_connections > 64 || config.max_pending_intents == 0 || config.max_carried_intents == 0 || config.max_output_bytes < 2 * (frame_header_bytes + max_frame_payload) || config.max_input_bytes_per_second < frame_header_bytes + max_grant_bytes + 6) return std::unexpected(SessionServerError::configuration); // Validate the admission limits now, with a placeholder epoch. auto probe = config.admission; probe.epoch = 1; if (!SessionAdmission::create(probe)) return std::unexpected(SessionServerError::configuration); // Credentials must parse before the port opens. struct Null final : ByteTransport { std::expected write(std::span) override { return 0; } std::expected read(std::span) override { return 0; } } null; if (!TlsChannel::server(config.credentials, null)) return std::unexpected(SessionServerError::credentials); auto listener = TcpListener::listen(config.listen); if (!listener) return std::unexpected(SessionServerError::listen); return std::unique_ptr(new SessionServer( std::make_unique(std::move(config), std::move(key), std::move(*listener)))); } std::uint16_t SessionServer::port() const noexcept { return impl_->listener->port(); } void SessionServer::poll(const RegionFacts& facts, std::uint64_t now_seconds, std::chrono::steady_clock::time_point now) { auto& m = *impl_; m.polled_at = now; if (facts.ready && facts.epoch != 0) { if (!m.admission) { auto config = m.config.admission; config.epoch = facts.epoch; if (auto created = SessionAdmission::create(config)) m.admission.emplace(std::move(*created)); } else if (facts.epoch > m.admission->epoch()) { // Strictly newer (checked above), so the advance cannot be refused. static_cast(m.admission->advance_epoch(facts.epoch).has_value()); // Intentions of the old epoch never ride new-epoch inputs. m.pending.clear(); } } for (std::size_t i = 0; i < max_accepts_per_poll; ++i) { auto accepted = m.listener->accept(); if (!accepted || !*accepted) break; if (m.connections.size() >= m.config.max_connections) continue; // Over capacity: the stream closes as it goes out of scope. Impl::Connection c; c.id = m.next_connection++; c.stream = std::move(*accepted); auto tls = TlsChannel::server(m.config.credentials, *c.stream); if (!tls) continue; c.tls = std::move(*tls); c.deadline = now + m.config.hello_deadline; c.window_start = now; m.connections.push_back(std::move(c)); } for (auto& c : m.connections) if (!c.dead) m.step(c, facts, now_seconds, now); m.bury(); } void SessionServer::publish(const client::EnvironmentPresentation& environment, const client::OrganismPresentation& organisms, const RegionFacts& facts) { auto& m = *impl_; m.environment = environment; m.organisms = organisms; m.published = facts; ++m.baseline; for (auto& c : m.connections) if (!c.dead && c.state == Impl::State::established) c.view = m.views_for(c); } std::optional SessionServer::carry(std::uint64_t tick) { auto& m = *impl_; if (m.carried.size() >= m.config.max_carried_intents) return std::nullopt; const auto it = std::ranges::find_if( m.pending, [&](const Impl::Pending& p) { return p.intended_tick <= tick; }); if (it == m.pending.end()) return std::nullopt; const Impl::Carried carried{it->request, it->connection, it->sequence}; m.pending.erase(it); m.carried.push_back(carried); return carried.request; } void SessionServer::committed(const RequestId& request, std::uint64_t tick, std::uint64_t commit_sequence) { auto& m = *impl_; const auto it = std::ranges::find_if( m.carried, [&](const Impl::Carried& c) { return c.request == request; }); if (it == m.carried.end()) return; if (auto* c = m.find(it->connection)) append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::committed_no_effect, tick, commit_sequence})); m.carried.erase(it); } void SessionServer::not_committed(const RequestId& request) { auto& m = *impl_; const auto it = std::ranges::find_if( m.carried, [&](const Impl::Carried& c) { return c.request == request; }); if (it == m.carried.end()) return; if (auto* c = m.find(it->connection)) append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::not_committed, 0, 0})); m.carried.erase(it); } std::vector SessionServer::take_events() { return std::exchange(impl_->events, {}); } std::size_t SessionServer::connections() const noexcept { return impl_->connections.size(); } std::size_t SessionServer::sessions() const noexcept { const auto& admission = impl_->admission; return admission ? admission->sessions() : 0; } } // namespace evo::network