Files
aevolution/src/server/session/region_sessions.cpp
T
dingo a6b1c62168
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 2m1s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 22s
Foundation / checks (network) (push) Canceled after 1m58s
Foundation / checks (client) (push) Canceled after 2m39s
Route cellular logins to existing life owners
2026-10-11 01:32:14 -04:00

969 lines
47 KiB
C++

#include "evo/server/region_sessions.hpp"
#include <algorithm>
#include <set>
#include <utility>
namespace evo::server {
namespace {
using network::ControlError;
using network::SessionCloseReason;
using network::SessionGrantError;
using Clock = std::chrono::steady_clock;
SessionCloseReason reason_for(ControlError e) {
switch (e) {
case ControlError::wrong_environment:
case ControlError::wrong_world:
case ControlError::wrong_region:
return SessionCloseReason::wrong_scope;
case ControlError::expired:
return SessionCloseReason::expired;
case ControlError::stale_grant:
return SessionCloseReason::stale_grant;
case ControlError::rate_limited:
return SessionCloseReason::rate_limited;
case ControlError::sessions_full:
case ControlError::accounts_full:
return SessionCloseReason::capacity;
default:
return SessionCloseReason::unauthenticated;
}
}
SessionCloseReason reason_for(network::ControlLedgerError e) {
switch (e) {
case network::ControlLedgerError::stale_grant:
return SessionCloseReason::stale_grant;
case network::ControlLedgerError::fenced:
return SessionCloseReason::shutdown; // This region cannot grant control now.
default:
return SessionCloseReason::capacity; // Not decided: the client may retry later.
}
}
SessionCloseReason reason_for(network::HandoffTicketError e) {
switch (e) {
case network::HandoffTicketError::wrong_environment:
case network::HandoffTicketError::wrong_scope:
return SessionCloseReason::wrong_scope;
case network::HandoffTicketError::expired:
return SessionCloseReason::expired;
default:
return SessionCloseReason::unauthenticated;
}
}
SessionCloseReason reason_for(network::HandoffLedgerError e) {
switch (e) {
case network::HandoffLedgerError::refused:
return SessionCloseReason::unauthenticated;
case network::HandoffLedgerError::fenced:
return SessionCloseReason::shutdown;
default:
return SessionCloseReason::capacity;
}
}
SessionCloseReason reason_for(SessionGrantError e) {
return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope
: SessionCloseReason::unauthenticated;
}
bool valid_route_text(const std::string& value) {
return !value.empty() && value.size() <= network::session_address_bytes &&
std::ranges::all_of(value, [](char ch) { return ch > 0x20 && ch < 0x7F; });
}
} // namespace
struct RegionSessions::Impl {
// pending: admitted and durably claimed, waiting out another region's control.
enum class State : std::uint8_t { hello, pending, established, closing };
struct Connection {
network::quic::ConnectionId id{};
State state{State::hello};
std::uint64_t session{};
std::uint16_t protocol_version{network::session_protocol_version};
std::string account;
Clock::time_point deadline; // Hello, pending (Welcome due) or close flush.
bool close_requested{};
// Durable control (ledger only).
network::AccountId account_id;
std::uint64_t generation{};
Clock::time_point control_deadline, next_renew;
network::SessionWelcome welcome;
// Durable probe (probe records only): its ownership generation and position.
std::optional<ProbeHome> probe;
};
struct Outgoing {
network::TransferId transfer{};
std::uint64_t deadline{}; // Wall seconds: abort unless committed by then.
};
Impl(RegionSessionsConfig c, std::unique_ptr<network::quic::Endpoint> e,
network::GrantVerifier v, std::optional<network::HandoffVerifier> h,
network::SessionControl s, network::IntentAdmission i, network::ReplicationServer r)
: config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)},
tickets{std::move(h)}, control{std::move(s)}, intents{std::move(i)},
replication{std::move(r)}, markers{config.control.max_sessions} {}
RegionSessionsConfig config;
std::unique_ptr<network::quic::Endpoint> endpoint;
network::GrantVerifier verifier;
std::optional<network::HandoffVerifier> tickets; // Handoff keys configured.
network::SessionControl control;
network::IntentAdmission intents;
network::ReplicationServer replication;
network::motion::ProbeMarkers markers; // Probe markers (ADR-0094, durable: ADR-0096).
std::optional<network::motion::Area> own_area; // This region's part of the marker area.
std::int64_t spawn_x{}, spawn_y{}; // Where new probes start.
std::vector<Outgoing> outgoing; // Handed off, not yet known committed.
Clock::time_point next_ghosts{}, next_overdue{};
std::optional<network::SessionInterest> default_interest;
bool refuse_logins{}; // Load governor (ADR-0097).
std::uint64_t refused_load{}; // Grant logins refused for load.
HostFacts facts;
std::vector<Connection> connections;
std::vector<RegionSessionEvent> events;
Connection* find(network::quic::ConnectionId id) {
const auto it =
std::ranges::find_if(connections, [&](const Connection& c) { return c.id == id; });
return it == connections.end() ? nullptr : &*it;
}
Connection* by_session(std::uint64_t session) {
const auto it = std::ranges::find_if(connections, [&](const Connection& c) {
return c.session == session &&
(c.state == State::established || c.state == State::pending);
});
return it == connections.end() ? nullptr : &*it;
}
void send(const Connection& c, const network::SessionMessage& m) {
// A refused send (budget) leaves the peer to observe the transport close instead.
(void)endpoint->send_message(c.id, network::encode_session(m, c.protocol_version))
.has_value();
}
// Realtime answers: a refused datagram is dropped; cumulative watermarks cover it.
void send_datagram(const Connection& c, const network::SessionMessage& m) {
(void)endpoint->send_datagram(c.id, network::encode_session(m, c.protocol_version))
.has_value();
}
[[nodiscard]] network::AdmissionFacts admission() const {
return {facts.ready, control.epoch(), facts.simulated_tick};
}
// Sends Close, releases control, reports, and closes after the flush deadline.
void close(Connection& c, SessionCloseReason reason, Clock::time_point now) {
if (c.state == State::closing)
return;
send(c, network::SessionClose{reason});
RegionSessionEvent event;
event.kind = c.state == State::established ? RegionSessionEvent::Kind::closed
: RegionSessionEvent::Kind::refused;
event.session = c.session;
event.account = c.account;
event.reason = reason;
events.push_back(std::move(event));
if (c.state == State::established) {
intents.close(c.session);
replication.close(c.session);
save_probe(c);
markers.close(c.session);
}
c.probe.reset();
if (c.session != 0) // May already be released (superseded, expired, epoch).
(void)control.close(c.session).has_value();
// A superseded session's durable control already moved to its successor.
if (config.ledger && c.generation != 0 && reason != SessionCloseReason::superseded)
config.ledger->release(c.account_id, c.generation);
c.generation = 0;
c.session = 0;
c.state = State::closing;
c.deadline = now + config.close_flush;
}
// Best effort, at session end: the durable probe's position (a frozen or moved probe is
// fenced by the store and stays as it was).
void save_probe(const Connection& c) {
if (!config.probes || !c.probe)
return;
if (const auto* m = markers.of(c.session); m && !m->frozen && own_area)
config.probes->save(c.account_id, c.probe->generation, m->state.x, m->state.y,
own_area->z);
}
// After the durable control claim of a grant login: where the account's probe lives.
// False when the session must not open here (the connection is then closed).
bool place_probe(Connection& c, const network::AccountId& account,
const std::optional<network::ControlLease>& lease, Clock::time_point now) {
if (!config.probes || !own_area)
return true;
const auto home = config.probes->open(account, spawn_x, spawn_y, own_area->z);
const auto refuse = [&](SessionCloseReason why) {
if (lease)
config.ledger->release(account, lease->generation);
close(c, why, now);
return false;
};
if (!home)
return refuse(home.error() == ProbeLedgerError::fenced ? SessionCloseReason::shutdown
: SessionCloseReason::capacity);
if (home->owner != config.control.region) {
const auto n = std::ranges::find_if(config.neighbours, [&](const Neighbour& x) {
return x.address.region == home->owner;
});
if (n != config.neighbours.end())
send(c, network::SessionRedirect{n->address});
return refuse(SessionCloseReason::moved);
}
if (home->frozen) // A transfer is open: decided by its commit or abort, retry later.
return refuse(SessionCloseReason::capacity);
c.probe = *home;
// After an aborted handoff the probe was saved where it crossed: back inside, so the
// new session does not cross again at once.
c.probe->x = std::clamp(c.probe->x, own_area->min_x, own_area->max_x);
c.probe->y = std::clamp(c.probe->y, own_area->min_y, own_area->max_y);
return true;
}
void hello(Connection& c, const network::SessionHello& m, std::uint64_t now_seconds,
Clock::time_point now) {
if (m.required_features != 0)
return close(c, SessionCloseReason::protocol, now);
if (!control.attempt(now_seconds)) // Region-wide, before signature work.
return close(c, SessionCloseReason::rate_limited, now);
if (network::looks_like_handoff_ticket(m.grant))
return ticket_hello(c, m, now_seconds, now);
if (refuse_logins) { // Overload (ADR-0097): before signature work.
++refused_load;
return close(c, SessionCloseReason::capacity, now);
}
const auto grant = verifier.verify(m.grant);
if (!grant)
return close(c, reason_for(grant.error()), now);
const auto& claims = grant->claims();
c.account = network::log_label(claims.account);
if (const auto checked = control.check(*grant, now_seconds); !checked)
return close(c, reason_for(checked.error()), now);
if (!config.life_owner_routes.empty()) {
if (!config.ledger)
return close(c, SessionCloseReason::capacity, now);
const auto owner = config.ledger->primitive_life_owner(claims.account);
if (!owner)
return close(c, reason_for(owner.error()), now);
if (*owner && **owner != config.control.region) {
const auto route = std::ranges::find_if(
config.life_owner_routes, [&](const LifeOwnerRoute& candidate) {
return candidate.address.region == **owner;
});
if (route == config.life_owner_routes.end())
return close(c, SessionCloseReason::capacity, now);
if (c.protocol_version >= network::session_protocol_version)
send(c, network::SessionRedirect{route->address});
return close(c, SessionCloseReason::moved, now);
}
}
// Durable control first, so the world never has two controlling regions; then the
// local admission, which check() has just confirmed.
std::optional<network::ControlLease> lease;
const auto promised = control.next_session(); // The session the ledger records.
if (config.ledger) {
auto claimed = config.ledger->claim(claims.account, claims.serial, promised);
if (!claimed)
return close(c, reason_for(claimed.error()), now);
lease = *claimed;
}
if (!place_probe(c, claims.account, lease, now))
return;
admitted(c, control.open(*grant, now_seconds), lease, promised, claims.account, now_seconds,
now);
}
// M10.8b: a Hello carrying a handoff ticket (ADR-0096). Verified, checked locally, then
// made durable (stage, commit, control) before the local admission.
void ticket_hello(Connection& c, const network::SessionHello& m, std::uint64_t now_seconds,
Clock::time_point now) {
if (!tickets)
return close(c, SessionCloseReason::unauthenticated, now);
const auto ticket = tickets->verify(m.grant, now_seconds);
if (!ticket)
return close(c, reason_for(ticket.error()), now);
const auto& claims = ticket->claims();
c.account = network::log_label(claims.account);
if (const auto checked = control.check_handoff(*ticket, now_seconds); !checked)
return close(c, reason_for(checked.error()), now);
std::optional<network::ControlLease> lease;
const auto promised = control.next_session();
if (config.handoff) {
auto moved = config.handoff->admit(claims, promised);
if (!moved)
return close(c, reason_for(moved.error()), now);
lease = moved->lease;
if (config.probes)
c.probe =
ProbeHome{config.control.region, moved->generation, moved->x, moved->y, false};
} else if (config.ledger) {
return close(c, SessionCloseReason::capacity, now); // Misconfigured: fail closed.
}
admitted(c, control.open_handoff(*ticket, now_seconds), lease, promised, claims.account,
now_seconds, now);
}
// Common to grants and tickets: the local admission's result after any durable claim.
void admitted(Connection& c, const std::expected<network::OpenedSession, ControlError>& opened,
const std::optional<network::ControlLease>& lease, std::uint64_t promised,
const network::AccountId& account, std::uint64_t now_seconds,
Clock::time_point now) {
if (!opened || opened->session != promised) {
if (opened)
(void)control.close(opened->session).has_value();
if (lease)
config.ledger->release(account, lease->generation);
return close(c, opened ? SessionCloseReason::capacity : reason_for(opened.error()),
now);
}
if (opened->superseded != 0)
if (auto* old = by_session(opened->superseded))
close(*old, SessionCloseReason::superseded, now);
c.session = opened->session;
c.account_id = account;
c.welcome.session = opened->session;
c.welcome.epoch = opened->epoch;
c.welcome.world = config.control.world.world;
c.welcome.frame = config.control.world.frame;
c.welcome.region = config.control.region;
c.welcome.expires_at = opened->expires_at;
c.welcome.server_seconds = now_seconds;
if (lease) {
c.generation = lease->generation;
c.control_deadline = now + lease->wait + lease->granted - config.safety_margin;
c.next_renew = now + config.renew_interval;
if (lease->wait.count() > 0) {
c.state = State::pending;
c.deadline = now + lease->wait;
return;
}
}
welcome(c, now);
}
void welcome(Connection& c, Clock::time_point now) {
if (!intents.open(c.session, c.account_id))
return close(c, SessionCloseReason::capacity, now);
if (!replication.open(c.session, c.welcome.epoch)) {
intents.close(c.session);
return close(c, SessionCloseReason::capacity, now);
}
if (default_interest && c.protocol_version != network::session_protocol_v6)
(void)replication.set_interest(c.session, *default_interest).has_value();
if (markers.area() && c.protocol_version != network::session_protocol_v6 &&
c.protocol_version != network::session_protocol_v7)
// Full or refused: the session simply has no marker.
(void)(c.probe ? markers.open_at(c.session, c.probe->x, c.probe->y)
: markers.open(c.session))
.has_value();
c.state = State::established;
c.welcome.server_tick = facts.simulated_tick;
send(c, c.welcome);
events.push_back({RegionSessionEvent::Kind::opened, c.session, c.account, {}});
}
// Keeps durable control alive; closes sessions that lost it or cannot keep it.
void renew(Connection& c, Clock::time_point now) {
if (!config.ledger || c.generation == 0 ||
(c.state != State::established && c.state != State::pending))
return;
if (now >= c.next_renew) {
c.next_renew = now + config.renew_interval;
const auto renewed = config.ledger->renew(c.account_id, c.generation);
if (renewed)
c.control_deadline =
std::max(c.control_deadline, now + *renewed - config.safety_margin);
else if (renewed.error() != network::ControlLedgerError::unavailable)
return close(c, SessionCloseReason::superseded, now);
}
if (now >= c.control_deadline)
close(c, SessionCloseReason::shutdown, now);
}
void refresh(Connection& c, const network::SessionRefresh& m, std::uint64_t now_seconds,
Clock::time_point now) {
network::SessionRefreshResult result;
if (!control.attempt(now_seconds)) {
result.status = static_cast<std::uint8_t>(SessionCloseReason::rate_limited);
} else if (const auto grant = verifier.verify(m.grant); !grant) {
result.status = static_cast<std::uint8_t>(reason_for(grant.error()));
} else if (const auto renewed = control.refresh(c.session, *grant, now_seconds); !renewed) {
result.status = static_cast<std::uint8_t>(renewed.error() == ControlError::wrong_account
? SessionCloseReason::unauthenticated
: reason_for(renewed.error()));
} else if (config.ledger) {
// Record the newer serial durably; the session keeps its id.
const auto claimed =
config.ledger->claim(grant->claims().account, grant->claims().serial, c.session);
if (claimed) {
c.generation = claimed->generation;
c.control_deadline =
std::max(c.control_deadline,
now + claimed->wait + claimed->granted - config.safety_margin);
result.expires_at = *renewed;
} else if (claimed.error() == network::ControlLedgerError::unavailable) {
result.status = static_cast<std::uint8_t>(SessionCloseReason::capacity);
} else { // Another region accepted a newer grant: this session is superseded.
return close(c, SessionCloseReason::superseded, now);
}
} else {
result.expires_at = *renewed;
}
send(c, result);
}
// Only established sessions may send datagrams, and only intentions.
void datagram(Connection& c, std::span<const std::uint8_t> bytes, Clock::time_point now) {
if (c.state == State::closing)
return;
std::uint16_t wire_version{};
const auto decoded = network::decode_session(bytes, &wire_version);
if (c.state != State::established || !decoded || wire_version != c.protocol_version ||
(c.protocol_version != network::session_protocol_version &&
!((config.accept_cellular_intentions &&
c.protocol_version == network::session_protocol_v6) ||
(config.accept_cellular_actions &&
(c.protocol_version == network::session_protocol_v7 ||
c.protocol_version == network::session_protocol_v8)))))
return close(c, SessionCloseReason::protocol, now);
if (const auto* ack = std::get_if<network::SnapshotAck>(&*decoded))
return replication.acknowledge(c.session, *ack);
if (c.protocol_version == network::session_protocol_v6) {
const auto* m = std::get_if<network::CellularIntention>(&*decoded);
if (!m)
return close(c, SessionCloseReason::protocol, now);
send_datagram(c, intents.intention(c.session, *m, admission()));
return;
}
if (c.protocol_version == network::session_protocol_v7)
return close(c, SessionCloseReason::protocol, now); // No M10 datagrams in v7.
const auto* m = std::get_if<network::SessionIntention>(&*decoded);
if (!m)
return close(c, SessionCloseReason::protocol, now);
const auto receipt = intents.intention(c.session, *m, admission());
if (receipt.status == network::AdmitStatus::admitted) // Moves the session's marker.
(void)markers.schedule(c.session, m->sequence, m->tick, {m->move_x, m->move_y})
.has_value();
send_datagram(c, receipt);
}
[[nodiscard]] bool handoffs_enabled() const {
return config.probes != nullptr && config.handoff_key != nullptr &&
!config.neighbours.empty() && own_area.has_value();
}
// Markers that left this region's area: into a neighbour's area they are handed over,
// anywhere else they are put back at the edge.
void crossings(std::uint64_t now_seconds, Clock::time_point now) {
if (!own_area)
return;
const auto inside = [](const network::motion::Area& a, std::int64_t x, std::int64_t y) {
return x >= a.min_x && x <= a.max_x && y >= a.min_y && y <= a.max_y;
};
for (auto& c : connections) {
if (c.state != State::established)
continue;
const auto* m = markers.of(c.session);
if (m == nullptr || m->frozen || inside(*own_area, m->state.x, m->state.y))
continue;
const auto n = std::ranges::find_if(config.neighbours, [&](const Neighbour& x) {
return inside(x.area, m->state.x, m->state.y);
});
if (n == config.neighbours.end() || !handoffs_enabled() || !c.probe) {
const auto x = std::clamp(m->state.x, own_area->min_x, own_area->max_x);
const auto y = std::clamp(m->state.y, own_area->min_y, own_area->max_y);
markers.place(c.session, x, y); // Back at the edge (`m` is stale after this).
continue;
}
hand_off(c, *n, now_seconds, now);
}
}
// The source side of a crossing: freeze, prepare durably, release control, offer the
// ticket, close. On any failure the probe stays here, at the edge.
void hand_off(Connection& c, const Neighbour& n, std::uint64_t now_seconds,
Clock::time_point now) {
if (!c.probe || !own_area) // Checked by the caller (handoffs_enabled, c.probe).
return;
const auto generation = c.probe->generation;
const auto z = own_area->z;
markers.freeze(c.session);
const auto* m = markers.of(c.session);
network::TransferId transfer{};
if (m == nullptr || !network::random_bytes(transfer))
return close(c, SessionCloseReason::shutdown, now);
const auto x = m->state.x, y = m->state.y;
if (const auto prepared = config.probes->prepare(c.account_id, generation, transfer,
n.address.region, x, y, z);
!prepared)
return close(c, SessionCloseReason::shutdown, now);
c.probe.reset(); // Frozen under the transfer: never saved here again.
network::TicketClaims claims;
claims.transfer = transfer;
claims.account = c.account_id;
claims.world = config.control.world;
claims.source = config.control.region;
claims.destination = n.address.region;
claims.expires_at = static_cast<std::uint32_t>(now_seconds + config.ticket_lifetime);
claims.session_expires_at = static_cast<std::uint32_t>(c.welcome.expires_at);
const auto ticket = config.handoff_key->sign(claims);
// Control goes first so the destination need not wait it out.
if (config.ledger && c.generation != 0)
config.ledger->release(c.account_id, c.generation);
c.generation = 0;
outgoing.push_back({transfer, now_seconds + config.ticket_lifetime + 5});
if (ticket)
send(c, network::HandoffOffer{transfer, n.address, *ticket});
close(c, SessionCloseReason::handed_off, now);
}
// Transfers whose client never arrived are aborted (the probe resumes here); committed
// ones are forgotten. Undecided calls retry next poll.
void expire_outgoing(std::uint64_t now_seconds) {
std::erase_if(outgoing, [&](const Outgoing& o) {
if (now_seconds < o.deadline)
return false;
return config.probes->abort(o.transfer).has_value();
});
}
// Recovery (M10.8d): the in-memory deadlines die with the process, so outgoing transfers
// still open well after any ticket for them expired are aborted from the store's list.
// Abort is serialized with the destination's commit: a committed one stays committed.
void abort_overdue(Clock::time_point now) {
if (now < next_overdue)
return;
next_overdue = now + config.overdue_poll;
const auto overdue =
config.probes->overdue(std::chrono::seconds{config.ticket_lifetime + 10});
if (!overdue)
return; // Retried next poll.
for (const auto& t : *overdue)
(void)config.probes->abort(t).has_value();
}
void refresh_ghosts(Clock::time_point now) {
if (now < next_ghosts)
return;
next_ghosts = now + config.ghost_poll;
const auto incoming = config.probes->incoming();
if (!incoming)
return; // Keep the current ghosts until the next answer.
std::vector<network::motion::Ghost> ghosts;
for (const auto& p : *incoming)
ghosts.push_back({p.transfer, p.x, p.y});
markers.set_ghosts(ghosts);
}
void answer(Connection& c, const network::ActionReceipt& receipt) {
send(c, receipt);
// A duplicate of a final action: answer with the outcome as well.
if (receipt.status == network::AdmitStatus::duplicate)
if (const auto o = intents.query(c.session, {receipt.action_id}, admission());
o.result == network::ActionResult::committed_no_effect ||
o.result == network::ActionResult::committed ||
o.result == network::ActionResult::not_committed)
send(c, o);
}
void deliver(const std::vector<network::AccountOutcome>& outcomes) {
for (const auto& o : outcomes)
if (const auto session = intents.session_of(o.account))
if (auto* c = by_session(*session); c && c->state == State::established)
send(*c, o.outcome);
}
void message(Connection& c, std::span<const std::uint8_t> bytes, std::uint64_t now_seconds,
Clock::time_point now) {
if (c.state == State::closing)
return;
if (c.state == State::pending) // Nothing is expected before Welcome.
return close(c, SessionCloseReason::protocol, now);
std::uint16_t wire_version{};
const auto decoded = network::decode_session(bytes, &wire_version);
if (!decoded)
return close(c,
decoded.error() == network::SessionCodecError::unsupported_version
? SessionCloseReason::unsupported_version
: SessionCloseReason::protocol,
now);
if (c.state == State::hello) {
c.protocol_version = wire_version;
const bool primitive_v5 = static_cast<bool>(config.create_primitive_life);
const bool cellular_v6 = config.accept_cellular_intentions;
const bool cellular_actions = config.accept_cellular_actions;
if ((primitive_v5 && wire_version < network::session_protocol_v5) ||
(wire_version == network::session_protocol_v5 && !primitive_v5) ||
(wire_version == network::session_protocol_v6 && !cellular_v6) ||
((wire_version == network::session_protocol_v7 ||
wire_version == network::session_protocol_v8) &&
!cellular_actions) ||
wire_version > network::session_protocol_v8)
return close(c, SessionCloseReason::unsupported_version, now);
} else if (wire_version != c.protocol_version) {
return close(c, SessionCloseReason::unsupported_version, now);
}
if (c.state == State::hello) {
if (const auto* m = std::get_if<network::SessionHello>(&*decoded))
return hello(c, *m, now_seconds, now);
return close(c, SessionCloseReason::protocol, now);
}
if (const auto* m = std::get_if<network::SessionRefresh>(&*decoded))
return refresh(c, *m, now_seconds, now);
if (const auto* m = std::get_if<network::SessionAction>(&*decoded)) {
if (c.protocol_version == network::session_protocol_v5 ||
c.protocol_version == network::session_protocol_v6 ||
c.protocol_version == network::session_protocol_v7)
return close(c, SessionCloseReason::protocol, now);
// A durable uniqueness check waits for this poll's single `verify` (M10.9d).
if (const auto receipt = intents.admit(c.session, *m, admission()))
answer(c, *receipt);
return;
}
if (const auto* m = std::get_if<network::CellularAction>(&*decoded)) {
if (c.protocol_version != network::session_protocol_v7 &&
c.protocol_version != network::session_protocol_v8)
return close(c, SessionCloseReason::protocol, now);
if (const auto receipt = intents.admit(c.session, *m, admission()))
answer(c, *receipt);
return;
}
if (const auto* m = std::get_if<network::ActionQuery>(&*decoded)) {
if (c.protocol_version == network::session_protocol_v5 ||
c.protocol_version == network::session_protocol_v6)
return close(c, SessionCloseReason::protocol, now);
return send(c, intents.query(c.session, *m, admission()));
}
if (const auto* m = std::get_if<network::SessionInterest>(&*decoded)) {
if (c.protocol_version == network::session_protocol_v5 ||
c.protocol_version == network::session_protocol_v6)
return close(c, SessionCloseReason::protocol, now);
(void)replication.set_interest(c.session, *m).has_value(); // Clamped by the server.
return;
}
if (const auto* m = std::get_if<network::SessionClose>(&*decoded);
m && m->reason == SessionCloseReason::client_closed) {
close(c, SessionCloseReason::client_closed, now);
c.deadline = now; // Nothing left to flush to a peer that is leaving.
return;
}
if (const auto* m = std::get_if<network::PrimitiveLifeRequest>(&*decoded)) {
const auto status = m->session != c.session ? network::AdmitStatus::unknown_session
: m->epoch != control.epoch() ? network::AdmitStatus::stale_epoch
: !facts.ready || c.generation == 0
? network::AdmitStatus::not_ready
: network::AdmitStatus::admitted;
send(c, network::PrimitiveLifeReceipt{m->request_id, status, facts.simulated_tick});
if (status == network::AdmitStatus::admitted)
send(c, config.create_primitive_life(c.account_id, c.generation, m->request_id));
return;
}
if (const auto* m = std::get_if<network::PrimitiveLifeQuery>(&*decoded)) {
if (c.generation == 0)
return send(c, network::PrimitiveLifeOutcome{
m->request_id, network::PrimitiveLifeResult::unknown, 0, 0});
return send(c, config.query_primitive_life(c.account_id, c.generation, m->request_id));
}
close(c, SessionCloseReason::protocol, now);
}
};
RegionSessions::RegionSessions(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
RegionSessions::~RegionSessions() = default;
std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError>
RegionSessions::create(RegionSessionsConfig config) {
bool has_own_life_route = config.life_owner_routes.empty();
std::set<std::uint64_t> life_route_regions;
for (const auto& route : config.life_owner_routes) {
const auto& address = route.address;
if (address.region == 0 || !valid_route_text(address.endpoint) ||
!valid_route_text(address.server_name) ||
!life_route_regions.insert(address.region).second)
return std::unexpected(RegionSessionsError::configuration);
has_own_life_route |= address.region == config.control.region;
}
// Connections beyond sessions are headroom for logins and closes still flushing.
if (config.issuer_keys.empty() ||
(!config.life_owner_routes.empty() &&
(config.ledger == nullptr || config.life_owner_routes.size() > 4096 ||
!has_own_life_route)) ||
(config.handoff != nullptr && config.ledger == nullptr) ||
(config.handoff_key != nullptr && config.probes == nullptr) ||
config.ticket_lifetime == 0 || config.ticket_lifetime > network::max_ticket_lifetime ||
((config.create_primitive_life || config.query_primitive_life) &&
(!config.create_primitive_life || !config.query_primitive_life || !config.ledger)) ||
(config.accept_cellular_intentions && config.create_primitive_life) ||
(config.accept_cellular_actions &&
(!config.actions || !config.ledger || config.accept_cellular_intentions)) ||
config.transport.limits.max_connections < config.control.max_sessions ||
config.hello_deadline.count() <= 0 || config.close_flush.count() <= 0 ||
config.renew_interval.count() <= 0 || config.safety_margin.count() <= 0 ||
config.overdue_poll.count() <= 0)
return std::unexpected(RegionSessionsError::configuration);
auto control = network::SessionControl::create(config.control);
if (!control)
return std::unexpected(RegionSessionsError::configuration);
auto verifier = network::GrantVerifier::create(config.control.environment);
if (!verifier)
return std::unexpected(RegionSessionsError::keys);
for (const auto& k : config.issuer_keys)
if (!verifier->add_key(k.key_id, k.public_key))
return std::unexpected(RegionSessionsError::keys);
std::optional<network::HandoffVerifier> tickets;
if (!config.handoff_keys.empty()) {
auto made = network::HandoffVerifier::create(config.control.environment,
config.control.world, config.control.region);
if (!made)
return std::unexpected(RegionSessionsError::keys);
for (const auto& k : config.handoff_keys)
if (!made->add_key(k.source_region, k.key_id, k.public_key))
return std::unexpected(RegionSessionsError::keys);
tickets.emplace(std::move(*made));
}
config.intents.max_sessions = config.control.max_sessions;
auto intents = network::IntentAdmission::create(config.intents);
if (!intents)
return std::unexpected(RegionSessionsError::configuration);
config.replication.max_sessions = config.control.max_sessions;
auto replication = network::ReplicationServer::create(config.replication);
if (!replication)
return std::unexpected(RegionSessionsError::configuration);
intents->attach(config.actions);
auto endpoint = network::quic::Endpoint::serve(config.transport);
if (!endpoint)
return std::unexpected(RegionSessionsError::transport);
return std::unique_ptr<RegionSessions>{new RegionSessions{std::make_unique<Impl>(
std::move(config), std::move(*endpoint), std::move(*verifier), std::move(tickets),
std::move(*control), std::move(*intents), std::move(*replication))}};
}
void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds,
Clock::time_point now) {
auto& m = *impl_;
m.facts = facts;
if (facts.epoch > m.control.epoch()) {
// Every session ends with the epoch (ADR-0089); control has released them. Actions
// never carried are final; carried ones await their durable record.
const auto closed = m.control.advance_epoch(facts.epoch);
if (closed)
for (const auto session : *closed)
if (auto* c = m.by_session(session))
m.close(*c, SessionCloseReason::stale_epoch, now);
(void)m.intents.abandon(); // Kept for queries; no session is left to tell.
m.markers.clear(); // The new epoch may restart at an earlier tick.
}
// Markers reach the simulated tick before this poll's intentions are admitted.
if (facts.ready) {
m.markers.advance(facts.simulated_tick);
m.crossings(now_seconds, now);
if (m.config.probes) {
m.expire_outgoing(now_seconds);
m.abort_overdue(now);
m.refresh_ghosts(now);
}
}
for (auto& e : m.endpoint->poll()) {
using network::quic::EventKind;
if (e.kind == EventKind::connected) {
Impl::Connection c;
c.id = e.connection;
c.deadline = now + m.config.hello_deadline;
m.connections.push_back(std::move(c));
} else if (e.kind == EventKind::message) {
if (auto* c = m.find(e.connection))
m.message(*c, e.bytes, now_seconds, now);
} else if (e.kind == EventKind::closed) {
const auto it = std::ranges::find_if(
m.connections, [&](const Impl::Connection& c) { return c.id == e.connection; });
if (it == m.connections.end())
continue;
if (it->state == Impl::State::established) {
m.intents.close(it->session);
m.replication.close(it->session);
m.save_probe(*it);
m.markers.close(it->session);
}
if (it->state == Impl::State::pending) {
(void)m.control.close(it->session).has_value();
if (m.config.ledger && it->generation != 0)
m.config.ledger->release(it->account_id, it->generation);
} else if (it->state == Impl::State::established) {
(void)m.control.close(it->session).has_value();
if (m.config.ledger && it->generation != 0)
m.config.ledger->release(it->account_id, it->generation);
// The client closes its connection right after sending Close, which may be
// discarded with it: a peer close is the client leaving, anything else is loss.
m.events.push_back({RegionSessionEvent::Kind::closed, it->session, it->account,
e.cause == network::quic::CloseCause::peer
? SessionCloseReason::client_closed
: SessionCloseReason::connection_lost});
}
m.connections.erase(it);
} else if (e.kind == EventKind::datagram) {
if (auto* c = m.find(e.connection))
m.datagram(*c, e.bytes, now);
}
}
// One ledger request for every action admitted by this poll's messages.
for (const auto& [session, receipt] : m.intents.verify(m.admission()))
if (auto* c = m.by_session(session); c && c->state == Impl::State::established)
m.answer(*c, receipt);
for (const auto& [session, watermark] : m.intents.simulated(m.admission()))
if (auto* c = m.by_session(session);
c && c->state == Impl::State::established &&
(c->protocol_version == network::session_protocol_version ||
c->protocol_version == network::session_protocol_v6))
m.send_datagram(*c, watermark);
for (const auto session : m.control.expire(now_seconds))
if (auto* c = m.by_session(session))
m.close(*c, SessionCloseReason::expired, now);
for (auto& c : m.connections) {
m.renew(c, now);
if (c.state == Impl::State::pending && now >= c.deadline)
m.welcome(c, now);
if (c.state == Impl::State::hello && now >= c.deadline)
m.close(c, SessionCloseReason::timeout, now);
if (c.state == Impl::State::closing && now >= c.deadline && !c.close_requested) {
c.close_requested = true;
m.endpoint->close(c.id); // Its closed event removes it.
}
}
}
std::vector<network::CarriedAction> RegionSessions::carry(std::uint64_t tick) {
return impl_->intents.carry(tick);
}
std::vector<network::ScheduledCellularIntention>
RegionSessions::cellular_intentions(std::uint64_t tick) const {
return impl_->intents.scheduled_cellular_intentions(tick);
}
void RegionSessions::committed(std::span<const network::CarriedAction> actions,
std::uint64_t commit_sequence) {
impl_->deliver(impl_->intents.committed(actions, commit_sequence));
}
void RegionSessions::dropped(std::span<const network::CarriedAction> actions, bool final) {
impl_->deliver(impl_->intents.dropped(actions, final));
}
void RegionSessions::set_default_interest(network::SessionInterest interest) {
auto& m = *impl_;
m.default_interest = interest;
for (const auto& c : m.connections)
if (c.state == Impl::State::established &&
c.protocol_version != network::session_protocol_v6 &&
!m.replication.interest(c.session))
(void)m.replication.set_interest(c.session, interest).has_value();
}
void RegionSessions::replicate(
std::uint64_t tick,
const std::function<network::ReplicationSource(const network::SessionInterest&)>& source,
const network::Perceives& perceives) {
auto& m = *impl_;
const auto payload = [&](std::uint64_t session) -> std::size_t {
const auto* c = m.by_session(session);
return c && c->state == Impl::State::established ? m.endpoint->datagram_limit(c->id) : 0;
};
// The host's state plus the probe markers (all of them; filtered per session).
const auto with_markers = [&](const network::SessionInterest& in) {
auto s = source(in);
s.markers = m.markers.markers();
s.marker_area = m.markers.area();
return s;
};
for (const auto& [session, chunks] : m.replication.tick(tick, with_markers, perceives, payload))
if (const auto* c = m.by_session(session);
c && c->state == Impl::State::established &&
c->protocol_version != network::session_protocol_v6)
for (const auto& chunk : chunks)
m.send_datagram(*c, chunk); // A refused chunk leaves the snapshot unacknowledged.
}
bool RegionSessions::send_cellular_perception(const network::AccountId& account,
const network::CellularPerception& perception) {
auto& m = *impl_;
for (const auto& c : m.connections) {
if (c.state != Impl::State::established ||
c.protocol_version != network::session_protocol_v8 || c.account_id != account)
continue;
return m.endpoint
->send_message(c.id, network::encode_session(perception, c.protocol_version))
.has_value();
}
return false;
}
void RegionSessions::clear_cellular_perceptions(std::uint64_t tick) {
if (tick == 0)
return;
auto& m = *impl_;
const network::CellularPerception cleared{tick, 0, {}};
for (const auto& c : m.connections)
if (c.state == Impl::State::established &&
c.protocol_version == network::session_protocol_v8)
m.send(c, cleared);
}
network::ReplicationStats RegionSessions::replication_stats() const {
return impl_->replication.stats();
}
bool RegionSessions::set_motion_area(const network::motion::Area& area, std::int64_t spawn_x,
std::int64_t spawn_y) {
auto& m = *impl_;
auto movement = area;
for (const auto& n : m.config.neighbours) {
movement.min_x = std::min(movement.min_x, n.area.min_x);
movement.min_y = std::min(movement.min_y, n.area.min_y);
movement.max_x = std::max(movement.max_x, n.area.max_x);
movement.max_y = std::max(movement.max_y, n.area.max_y);
}
if (!network::motion::valid(area) || !m.markers.set_area(movement, spawn_x, spawn_y))
return false;
m.own_area = area;
m.spawn_x = std::clamp(spawn_x, area.min_x, area.max_x);
m.spawn_y = std::clamp(spawn_y, area.min_y, area.max_y);
for (const auto& c : m.connections)
if (c.state == Impl::State::established &&
c.protocol_version != network::session_protocol_v6 &&
c.protocol_version != network::session_protocol_v7 && !m.markers.of(c.session))
(void)m.markers.open(c.session).has_value();
return true;
}
const network::motion::ProbeMarkers& RegionSessions::markers() const noexcept {
return impl_->markers;
}
void RegionSessions::shutdown(Clock::time_point now) {
for (auto& c : impl_->connections)
impl_->close(c, SessionCloseReason::shutdown, now);
}
std::vector<RegionSessionEvent> RegionSessions::take_events() {
return std::exchange(impl_->events, {});
}
std::optional<std::uint64_t> RegionSessions::holder(const network::AccountId& account) const {
return impl_->control.holder(account);
}
void RegionSessions::set_load(bool refuse_logins, std::uint8_t shed) {
impl_->refuse_logins = refuse_logins;
impl_->replication.set_shed(shed);
}
std::uint64_t RegionSessions::refused_for_load() const noexcept {
return impl_->refused_load;
}
std::size_t RegionSessions::sessions() const noexcept {
return impl_->control.sessions();
}
std::size_t RegionSessions::connections() const noexcept {
return impl_->connections.size();
}
std::uint16_t RegionSessions::port() const noexcept {
return impl_->endpoint->port();
}
} // namespace evo::server