Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m45s
Foundation / checks (network) (push) Successful in 9m37s
Foundation / checks (client) (push) Successful in 12m14s
Foundation / checks (tsan) (push) Successful in 7m39s
Foundation / checks (checks) (push) Successful in 31m15s
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 32m59s
Session protocol v1 (not yet deployed, amended in place) gains realtime intentions on datagrams with receipts and a cumulative simulated watermark, and account-scoped actions with stable ids, receipts, final outcomes and queries. IntentAdmission enforces session binding, rate, authority, epoch, vector, sequence, tick window, duplicates and the unresolved bound; RegionSessions and the client carry it over QUIC. Intentions and actions have no biological effect yet. ADR-0092. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
12 KiB
C++
251 lines
12 KiB
C++
#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{8} * 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;
|
|
constexpr std::size_t intention_bytes = (std::size_t{4} * 8) + 4;
|
|
constexpr std::size_t intention_receipt_bytes = 8 + 1 + 8;
|
|
constexpr std::size_t simulated_bytes = std::size_t{3} * 8;
|
|
constexpr std::size_t action_bytes = (std::size_t{3} * 8) + 2 + 8;
|
|
constexpr std::size_t action_receipt_bytes = 8 + 1 + 8 + 8;
|
|
constexpr std::size_t action_outcome_bytes = 8 + 1 + 8 + 8;
|
|
constexpr std::size_t action_query_bytes = 8;
|
|
constexpr std::uint8_t max_admit_status = static_cast<std::uint8_t>(AdmitStatus::not_ready);
|
|
|
|
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, m.server_tick})
|
|
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;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SessionIntention& m) const {
|
|
auto out = header(SessionMessageType::intention, intention_bytes);
|
|
for (const auto v : {m.session, m.epoch, m.sequence, m.tick})
|
|
put(out, v, 8);
|
|
put(out, static_cast<std::uint16_t>(m.move_x), 2);
|
|
put(out, static_cast<std::uint16_t>(m.move_y), 2);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const IntentionReceipt& m) const {
|
|
auto out = header(SessionMessageType::intention_receipt, intention_receipt_bytes);
|
|
put(out, m.sequence, 8);
|
|
out.push_back(static_cast<std::uint8_t>(m.status));
|
|
put(out, m.server_tick, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SimulatedThrough& m) const {
|
|
auto out = header(SessionMessageType::simulated_through, simulated_bytes);
|
|
for (const auto v : {m.epoch, m.sequence, m.tick})
|
|
put(out, v, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SessionAction& m) const {
|
|
auto out = header(SessionMessageType::action, action_bytes);
|
|
for (const auto v : {m.session, m.epoch, m.action_id})
|
|
put(out, v, 8);
|
|
put(out, m.kind, 2);
|
|
put(out, m.tick, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const ActionReceipt& m) const {
|
|
auto out = header(SessionMessageType::action_receipt, action_receipt_bytes);
|
|
put(out, m.action_id, 8);
|
|
out.push_back(static_cast<std::uint8_t>(m.status));
|
|
put(out, m.server_tick, 8);
|
|
put(out, m.tick, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const ActionOutcome& m) const {
|
|
auto out = header(SessionMessageType::action_outcome, action_outcome_bytes);
|
|
put(out, m.action_id, 8);
|
|
out.push_back(static_cast<std::uint8_t>(m.result));
|
|
put(out, m.tick, 8);
|
|
put(out, m.commit_sequence, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const ActionQuery& m) const {
|
|
auto out = header(SessionMessageType::action_query, action_query_bytes);
|
|
put(out, m.action_id, 8);
|
|
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, &m.server_tick};
|
|
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])};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::intention): {
|
|
if (!expect(intention_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
SessionIntention m;
|
|
m.session = get(payload, 0, 8);
|
|
m.epoch = get(payload, 8, 8);
|
|
m.sequence = get(payload, 16, 8);
|
|
m.tick = get(payload, 24, 8);
|
|
m.move_x = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(payload, 32, 2)));
|
|
m.move_y = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(payload, 34, 2)));
|
|
return m; // Ranges belong to admission, which answers with a status.
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::intention_receipt): {
|
|
if (!expect(intention_receipt_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
if (payload[8] > max_admit_status)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return IntentionReceipt{get(payload, 0, 8), static_cast<AdmitStatus>(payload[8]),
|
|
get(payload, 9, 8)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::simulated_through): {
|
|
if (!expect(simulated_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
return SimulatedThrough{get(payload, 0, 8), get(payload, 8, 8), get(payload, 16, 8)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::action): {
|
|
if (!expect(action_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
SessionAction m;
|
|
m.session = get(payload, 0, 8);
|
|
m.epoch = get(payload, 8, 8);
|
|
m.action_id = get(payload, 16, 8);
|
|
m.kind = static_cast<std::uint16_t>(get(payload, 24, 2)); // Admission checks it.
|
|
m.tick = get(payload, 26, 8);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::action_receipt): {
|
|
if (!expect(action_receipt_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
if (payload[8] > max_admit_status)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return ActionReceipt{get(payload, 0, 8), static_cast<AdmitStatus>(payload[8]),
|
|
get(payload, 9, 8), get(payload, 17, 8)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::action_outcome): {
|
|
if (!expect(action_outcome_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
if (payload[8] < static_cast<std::uint8_t>(ActionResult::committed_no_effect) ||
|
|
payload[8] > static_cast<std::uint8_t>(ActionResult::unknown))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return ActionOutcome{get(payload, 0, 8), static_cast<ActionResult>(payload[8]),
|
|
get(payload, 9, 8), get(payload, 17, 8)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::action_query): {
|
|
if (!expect(action_query_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
return ActionQuery{get(payload, 0, 8)};
|
|
}
|
|
default:
|
|
return std::unexpected(SessionCodecError::unknown_type);
|
|
}
|
|
}
|
|
} // namespace evo::network
|