Add M10.3b session login over QUIC
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m13s
Foundation / checks (client) (push) Successful in 12m12s
Foundation / checks (network) (push) Successful in 8m56s
Foundation / checks (tsan) (push) Successful in 7m42s
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 19m14s
Foundation / checks (checks) (push) Canceled after 31m28s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m13s
Foundation / checks (client) (push) Successful in 12m12s
Foundation / checks (network) (push) Successful in 8m56s
Foundation / checks (tsan) (push) Successful in 7m42s
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 19m14s
Foundation / checks (checks) (push) Canceled after 31m28s
Session protocol v1 session messages (Hello with grant, Welcome, Refresh, RefreshResult, Close with reasons) on the QUIC reliable stream. RegionSessions verifies grants with the issuer public keys and admits them through SessionControl: a newer grant closes the account's old connection as superseded, expiry and epoch advances close sessions, every close is announced and flushed. RegionSessionClient logs in, refreshes in place and reports why a session ended. ADR-0091. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
#include "evo/client/region_session.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace evo::client {
|
||||
namespace {
|
||||
using network::SessionCloseReason;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
} // namespace
|
||||
|
||||
struct RegionSessionClient::Impl {
|
||||
std::unique_ptr<network::quic::Endpoint> endpoint;
|
||||
network::WireGrant grant{};
|
||||
Clock::time_point deadline;
|
||||
RegionSessionState state{RegionSessionState::connecting};
|
||||
network::quic::ConnectionId connection{};
|
||||
std::optional<network::SessionWelcome> welcome;
|
||||
std::optional<SessionCloseReason> reason;
|
||||
std::optional<network::SessionRefreshResult> refresh_result;
|
||||
|
||||
bool send(const network::SessionMessage& m) {
|
||||
return connection != 0 &&
|
||||
endpoint->send_message(connection, network::encode_session(m)).has_value();
|
||||
}
|
||||
// Ends the session locally; `tell` sends a Close first (best effort).
|
||||
void end(SessionCloseReason why, bool tell) {
|
||||
if (state == RegionSessionState::closed)
|
||||
return;
|
||||
if (tell)
|
||||
(void)send(network::SessionClose{why});
|
||||
state = RegionSessionState::closed;
|
||||
reason = why;
|
||||
grant.fill(0);
|
||||
if (connection != 0)
|
||||
endpoint->close(connection);
|
||||
}
|
||||
void message(std::span<const std::uint8_t> bytes) {
|
||||
const auto decoded = network::decode_session(bytes);
|
||||
if (!decoded)
|
||||
return end(SessionCloseReason::protocol, true);
|
||||
if (const auto* m = std::get_if<network::SessionClose>(&*decoded))
|
||||
return end(m->reason, false);
|
||||
if (state == RegionSessionState::awaiting_welcome) {
|
||||
if (const auto* m = std::get_if<network::SessionWelcome>(&*decoded)) {
|
||||
welcome = *m;
|
||||
state = RegionSessionState::established;
|
||||
return;
|
||||
}
|
||||
} else if (state == RegionSessionState::established) {
|
||||
if (const auto* m = std::get_if<network::SessionRefreshResult>(&*decoded)) {
|
||||
if (m->status == 0)
|
||||
welcome->expires_at = m->expires_at;
|
||||
refresh_result = *m;
|
||||
return;
|
||||
}
|
||||
}
|
||||
end(SessionCloseReason::protocol, true);
|
||||
}
|
||||
};
|
||||
|
||||
RegionSessionClient::RegionSessionClient(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
||||
RegionSessionClient::~RegionSessionClient() = default;
|
||||
|
||||
std::expected<std::unique_ptr<RegionSessionClient>, network::quic::EndpointError>
|
||||
RegionSessionClient::connect(RegionSessionClientConfig config, Clock::time_point now) {
|
||||
auto endpoint = network::quic::Endpoint::connect(config.transport);
|
||||
if (!endpoint)
|
||||
return std::unexpected(endpoint.error());
|
||||
auto impl = std::make_unique<Impl>();
|
||||
impl->endpoint = std::move(*endpoint);
|
||||
impl->grant = config.grant;
|
||||
config.grant.fill(0);
|
||||
impl->deadline = now + config.welcome_deadline;
|
||||
return std::unique_ptr<RegionSessionClient>{new RegionSessionClient{std::move(impl)}};
|
||||
}
|
||||
|
||||
void RegionSessionClient::poll(Clock::time_point now) {
|
||||
auto& m = *impl_;
|
||||
for (auto& e : m.endpoint->poll()) {
|
||||
using network::quic::EventKind;
|
||||
if (m.state == RegionSessionState::closed)
|
||||
continue;
|
||||
if (e.kind == EventKind::connected && m.state == RegionSessionState::connecting) {
|
||||
m.connection = e.connection;
|
||||
m.state = RegionSessionState::awaiting_welcome;
|
||||
network::SessionHello hello;
|
||||
hello.grant = m.grant;
|
||||
m.grant.fill(0); // The grant is a credential: keep it no longer than needed.
|
||||
if (!m.send(hello))
|
||||
m.end(SessionCloseReason::connection_lost, false);
|
||||
} else if (e.kind == EventKind::message) {
|
||||
m.message(e.bytes);
|
||||
} else if (e.kind == EventKind::closed) {
|
||||
m.connection = 0; // Already gone; nothing to close or send.
|
||||
m.end(SessionCloseReason::connection_lost, false);
|
||||
}
|
||||
}
|
||||
if ((m.state == RegionSessionState::connecting ||
|
||||
m.state == RegionSessionState::awaiting_welcome) &&
|
||||
now >= m.deadline)
|
||||
m.end(SessionCloseReason::timeout, false);
|
||||
}
|
||||
|
||||
bool RegionSessionClient::refresh(const network::WireGrant& grant) {
|
||||
return impl_->state == RegionSessionState::established &&
|
||||
impl_->send(network::SessionRefresh{grant});
|
||||
}
|
||||
|
||||
std::optional<network::SessionRefreshResult> RegionSessionClient::take_refresh_result() {
|
||||
return std::exchange(impl_->refresh_result, std::nullopt);
|
||||
}
|
||||
|
||||
void RegionSessionClient::close() {
|
||||
impl_->end(SessionCloseReason::client_closed, true);
|
||||
}
|
||||
|
||||
RegionSessionState RegionSessionClient::state() const noexcept {
|
||||
return impl_->state;
|
||||
}
|
||||
|
||||
const std::optional<network::SessionWelcome>& RegionSessionClient::welcome() const noexcept {
|
||||
return impl_->welcome;
|
||||
}
|
||||
|
||||
std::optional<SessionCloseReason> RegionSessionClient::close_reason() const noexcept {
|
||||
return impl_->reason;
|
||||
}
|
||||
} // namespace evo::client
|
||||
@@ -0,0 +1,134 @@
|
||||
#include "evo/network/session_protocol.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
constexpr std::size_t hello_bytes = 4 + session_grant_wire_bytes;
|
||||
constexpr std::size_t welcome_bytes = std::size_t{7} * 8;
|
||||
constexpr std::size_t refresh_bytes = session_grant_wire_bytes;
|
||||
constexpr std::size_t refresh_result_bytes = 1 + 8;
|
||||
constexpr std::size_t close_bytes = 1;
|
||||
|
||||
void put(std::vector<std::uint8_t>& out, std::uint64_t v, std::size_t count) {
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
out.push_back(static_cast<std::uint8_t>(v >> (8 * i)));
|
||||
}
|
||||
std::uint64_t get(std::span<const std::uint8_t> in, std::size_t at, std::size_t count) {
|
||||
std::uint64_t v = 0;
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
v |= static_cast<std::uint64_t>(in[at + i]) << (8 * i);
|
||||
return v;
|
||||
}
|
||||
std::vector<std::uint8_t> header(SessionMessageType type, std::size_t payload) {
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(session_header_bytes + payload);
|
||||
put(out, session_magic, 4);
|
||||
put(out, session_protocol_version, 2);
|
||||
out.push_back(static_cast<std::uint8_t>(type));
|
||||
out.push_back(0);
|
||||
return out;
|
||||
}
|
||||
bool wire_reason(std::uint64_t v) {
|
||||
return v >= static_cast<std::uint8_t>(SessionCloseReason::client_closed) &&
|
||||
v <= static_cast<std::uint8_t>(SessionCloseReason::shutdown);
|
||||
}
|
||||
WireGrant grant_at(std::span<const std::uint8_t> in, std::size_t at) {
|
||||
WireGrant g{};
|
||||
std::copy_n(in.begin() + static_cast<std::ptrdiff_t>(at), g.size(), g.begin());
|
||||
return g;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<std::uint8_t> encode_session(const SessionMessage& message) {
|
||||
struct Encoder {
|
||||
std::vector<std::uint8_t> operator()(const SessionHello& m) const {
|
||||
auto out = header(SessionMessageType::hello, hello_bytes);
|
||||
put(out, m.required_features, 4);
|
||||
out.insert(out.end(), m.grant.begin(), m.grant.end());
|
||||
return out;
|
||||
}
|
||||
std::vector<std::uint8_t> operator()(const SessionWelcome& m) const {
|
||||
auto out = header(SessionMessageType::welcome, welcome_bytes);
|
||||
for (const auto v :
|
||||
{m.session, m.epoch, m.world, m.frame, m.region, m.expires_at, m.server_seconds})
|
||||
put(out, v, 8);
|
||||
return out;
|
||||
}
|
||||
std::vector<std::uint8_t> operator()(const SessionRefresh& m) const {
|
||||
auto out = header(SessionMessageType::refresh, refresh_bytes);
|
||||
out.insert(out.end(), m.grant.begin(), m.grant.end());
|
||||
return out;
|
||||
}
|
||||
std::vector<std::uint8_t> operator()(const SessionRefreshResult& m) const {
|
||||
auto out = header(SessionMessageType::refresh_result, refresh_result_bytes);
|
||||
out.push_back(m.status);
|
||||
put(out, m.expires_at, 8);
|
||||
return out;
|
||||
}
|
||||
std::vector<std::uint8_t> operator()(const SessionClose& m) const {
|
||||
auto out = header(SessionMessageType::close, close_bytes);
|
||||
out.push_back(static_cast<std::uint8_t>(m.reason));
|
||||
return out;
|
||||
}
|
||||
};
|
||||
return std::visit(Encoder{}, message);
|
||||
}
|
||||
|
||||
std::expected<SessionMessage, SessionCodecError>
|
||||
decode_session(std::span<const std::uint8_t> bytes) {
|
||||
if (bytes.size() < session_header_bytes)
|
||||
return std::unexpected(SessionCodecError::truncated);
|
||||
if (get(bytes, 0, 4) != session_magic)
|
||||
return std::unexpected(SessionCodecError::bad_magic);
|
||||
if (get(bytes, 4, 2) != session_protocol_version)
|
||||
return std::unexpected(SessionCodecError::unsupported_version);
|
||||
if (bytes[7] != 0)
|
||||
return std::unexpected(SessionCodecError::reserved_flags);
|
||||
const auto payload = bytes.subspan(session_header_bytes);
|
||||
const auto expect = [&](std::size_t n) { return payload.size() == n; };
|
||||
switch (bytes[6]) {
|
||||
case static_cast<std::uint8_t>(SessionMessageType::hello): {
|
||||
if (!expect(hello_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
SessionHello m;
|
||||
m.required_features = static_cast<std::uint32_t>(get(payload, 0, 4));
|
||||
m.grant = grant_at(payload, 4);
|
||||
return m;
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::welcome): {
|
||||
if (!expect(welcome_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
SessionWelcome m;
|
||||
std::uint64_t* fields[] = {&m.session, &m.epoch, &m.world, &m.frame,
|
||||
&m.region, &m.expires_at, &m.server_seconds};
|
||||
for (std::size_t i = 0; i < std::size(fields); ++i)
|
||||
*fields[i] = get(payload, 8 * i, 8);
|
||||
if (m.session == 0 || m.epoch == 0)
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
return m;
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::refresh): {
|
||||
if (!expect(refresh_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
return SessionRefresh{grant_at(payload, 0)};
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::refresh_result): {
|
||||
if (!expect(refresh_result_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
SessionRefreshResult m{payload[0], get(payload, 1, 8)};
|
||||
if (m.status != 0 && !wire_reason(m.status))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
return m;
|
||||
}
|
||||
case static_cast<std::uint8_t>(SessionMessageType::close): {
|
||||
if (!expect(close_bytes))
|
||||
return std::unexpected(SessionCodecError::wrong_length);
|
||||
if (!wire_reason(payload[0]))
|
||||
return std::unexpected(SessionCodecError::invalid_value);
|
||||
return SessionClose{static_cast<SessionCloseReason>(payload[0])};
|
||||
}
|
||||
default:
|
||||
return std::unexpected(SessionCodecError::unknown_type);
|
||||
}
|
||||
}
|
||||
} // namespace evo::network
|
||||
@@ -0,0 +1,266 @@
|
||||
#include "evo/server/region_sessions.hpp"
|
||||
#include <algorithm>
|
||||
#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(SessionGrantError e) {
|
||||
return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope
|
||||
: SessionCloseReason::unauthenticated;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct RegionSessions::Impl {
|
||||
enum class State : std::uint8_t { hello, established, closing };
|
||||
struct Connection {
|
||||
network::quic::ConnectionId id{};
|
||||
State state{State::hello};
|
||||
std::uint64_t session{};
|
||||
std::string account;
|
||||
Clock::time_point deadline;
|
||||
bool close_requested{};
|
||||
};
|
||||
|
||||
Impl(RegionSessionsConfig c, std::unique_ptr<network::quic::Endpoint> e,
|
||||
network::GrantVerifier v, network::SessionControl s)
|
||||
: config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)},
|
||||
control{std::move(s)} {}
|
||||
|
||||
RegionSessionsConfig config;
|
||||
std::unique_ptr<network::quic::Endpoint> endpoint;
|
||||
network::GrantVerifier verifier;
|
||||
network::SessionControl control;
|
||||
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;
|
||||
});
|
||||
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)).has_value();
|
||||
}
|
||||
// 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.session != 0) // May already be released (superseded, expired, epoch).
|
||||
(void)control.close(c.session).has_value();
|
||||
c.session = 0;
|
||||
c.state = State::closing;
|
||||
c.deadline = now + config.close_flush;
|
||||
}
|
||||
|
||||
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);
|
||||
const auto grant = verifier.verify(m.grant);
|
||||
if (!grant)
|
||||
return close(c, reason_for(grant.error()), now);
|
||||
c.account = network::log_label(grant->claims().account);
|
||||
const auto opened = control.open(*grant, now_seconds);
|
||||
if (!opened)
|
||||
return close(c, reason_for(opened.error()), now);
|
||||
if (opened->superseded != 0)
|
||||
if (auto* old = by_session(opened->superseded))
|
||||
close(*old, SessionCloseReason::superseded, now);
|
||||
c.state = State::established;
|
||||
c.session = opened->session;
|
||||
network::SessionWelcome welcome;
|
||||
welcome.session = opened->session;
|
||||
welcome.epoch = opened->epoch;
|
||||
welcome.world = config.control.world.world;
|
||||
welcome.frame = config.control.world.frame;
|
||||
welcome.region = config.control.region;
|
||||
welcome.expires_at = opened->expires_at;
|
||||
welcome.server_seconds = now_seconds;
|
||||
send(c, welcome);
|
||||
events.push_back({RegionSessionEvent::Kind::opened, c.session, c.account, {}});
|
||||
}
|
||||
|
||||
void refresh(Connection& c, const network::SessionRefresh& m, std::uint64_t now_seconds) {
|
||||
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 {
|
||||
result.expires_at = *renewed;
|
||||
}
|
||||
send(c, result);
|
||||
}
|
||||
|
||||
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;
|
||||
const auto decoded = network::decode_session(bytes);
|
||||
if (!decoded)
|
||||
return close(c,
|
||||
decoded.error() == network::SessionCodecError::unsupported_version
|
||||
? SessionCloseReason::unsupported_version
|
||||
: SessionCloseReason::protocol,
|
||||
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);
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
// Connections beyond sessions are headroom for logins and closes still flushing.
|
||||
if (config.issuer_keys.empty() ||
|
||||
config.transport.limits.max_connections < config.control.max_sessions ||
|
||||
config.hello_deadline.count() <= 0 || config.close_flush.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);
|
||||
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(*control))}};
|
||||
}
|
||||
|
||||
void RegionSessions::poll(std::uint64_t epoch, std::uint64_t now_seconds, Clock::time_point now) {
|
||||
auto& m = *impl_;
|
||||
if (epoch > m.control.epoch()) {
|
||||
// Every session ends with the epoch (ADR-0089); control has released them.
|
||||
const auto closed = m.control.advance_epoch(epoch);
|
||||
if (closed)
|
||||
for (const auto session : *closed)
|
||||
if (auto* c = m.by_session(session))
|
||||
m.close(*c, SessionCloseReason::stale_epoch, now);
|
||||
}
|
||||
for (auto& e : m.endpoint->poll()) {
|
||||
using network::quic::EventKind;
|
||||
if (e.kind == EventKind::connected) {
|
||||
m.connections.push_back(
|
||||
{e.connection, Impl::State::hello, 0, {}, now + m.config.hello_deadline, false});
|
||||
} 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) {
|
||||
(void)m.control.close(it->session).has_value();
|
||||
// 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);
|
||||
} // Datagrams carry nothing before 10.4.
|
||||
}
|
||||
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) {
|
||||
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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user