Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (deploy) (push) Successful in 4m1s
Foundation / checks (tsan) (push) Successful in 9m49s
Foundation / checks (client) (push) Successful in 19m23s
Foundation / checks (network) (push) Successful in 19m31s
Foundation / checks (checks) (push) Successful in 42m41s
Foundation / checks (postgres) (push) Successful in 45m36s
Foundation / postgres-tools (push) Successful in 28s
Foundation / server-image (push) Successful in 3m15s
Foundation / image (push) Successful in 3m18s
An offer the cell cannot afford stayed open, and only one offer is issued at a time, so banked feeding credit never became a new offer. A v10 choice naming operator 0 now declines the exact outstanding offer. It is reserved and published like any choice: schema v40 accepts operator 0 in the reserved input and trace selections and stores result 3, declined. The owner consumes the offer and changes nothing else, and the client reports it as committed. Old traces never carry operator 0, so their replay is unchanged. The Evolution panel gets a "Decline this offer" button. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
982 lines
52 KiB
C++
982 lines
52 KiB
C++
#include "evo/network/session_protocol.hpp"
|
|
#include <algorithm>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
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 cellular_intention_bytes = (std::size_t{4} * 8) + 4 + 1 + 3 + 8;
|
|
constexpr std::size_t cellular_action_bytes = (std::size_t{4} * 8) + 4 + 1 + 3 + 8;
|
|
constexpr std::size_t cellular_mutation_action_bytes = (std::size_t{7} * 8) + 2 + 6;
|
|
constexpr std::size_t cellular_offer_prefix_bytes = (std::size_t{4} * 8) + 2 + 6;
|
|
constexpr std::size_t cellular_life_ended_bytes = (std::size_t{2} * 8) + 1 + 7;
|
|
// A candidate: operator, capability, effect, parameter, maintenance, mass, then its cost to
|
|
// this cell (energy and four matter channels, ADR-0129).
|
|
constexpr std::size_t cellular_offered_candidate_bytes = 2 + 1 + 1 + 4 + 8 + 8 + 8 + (4 * 8);
|
|
constexpr std::size_t cellular_perception_prefix_bytes = 8 + 8 + 2;
|
|
constexpr std::size_t cellular_diagnostics_bytes = 64;
|
|
constexpr std::size_t cellular_perception_v9_prefix_bytes =
|
|
cellular_perception_prefix_bytes + cellular_diagnostics_bytes;
|
|
// v10 appends the bud cost (energy and four matter channels, ADR-0129 amendment).
|
|
constexpr std::size_t cellular_bud_cost_bytes = std::size_t{5} * 8;
|
|
constexpr std::size_t cellular_perception_v10_prefix_bytes =
|
|
cellular_perception_v9_prefix_bytes + cellular_bud_cost_bytes;
|
|
constexpr std::size_t cellular_perceived_patch_bytes = 8 + 8 + (4 * 8);
|
|
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);
|
|
constexpr std::size_t interest_bytes = (3 * std::size_t{8}) + 2;
|
|
constexpr std::size_t snapshot_ack_bytes = 2 * std::size_t{8};
|
|
constexpr std::uint8_t max_snapshot_chunks = 16;
|
|
constexpr std::size_t address_bytes = 8 + (2 * session_address_bytes);
|
|
constexpr std::size_t handoff_offer_bytes = 16 + address_bytes + session_grant_wire_bytes;
|
|
constexpr std::size_t redirect_bytes = address_bytes;
|
|
constexpr std::size_t life_request_bytes = 24;
|
|
constexpr std::size_t life_receipt_bytes = 17;
|
|
constexpr std::size_t life_outcome_bytes = 25;
|
|
constexpr std::size_t life_query_bytes = 8;
|
|
|
|
// Seven capabilities and four effects (biology genome/mutation enums, frozen on the wire).
|
|
bool valid_offer(const CellularMutationOffer& offer) {
|
|
if (offer.tick == 0 || offer.candidates.size() > max_cellular_offered_candidates)
|
|
return false;
|
|
if (offer.candidates.empty())
|
|
return offer.event_index == 0 && offer.offer_tick == 0;
|
|
std::uint16_t previous = 0;
|
|
for (const auto& candidate : offer.candidates) {
|
|
if (candidate.operator_id <= previous || candidate.capability > 6 || candidate.effect > 3)
|
|
return false;
|
|
previous = candidate.operator_id;
|
|
}
|
|
return offer.life_identity != 0 && offer.event_index != 0 && offer.offer_tick != 0 &&
|
|
offer.offer_tick <= offer.tick;
|
|
}
|
|
|
|
bool valid_diagnostics(const CellularLifeDiagnostics& diagnostics) {
|
|
const auto movement = static_cast<std::uint8_t>(diagnostics.movement);
|
|
const auto intake = static_cast<std::uint8_t>(diagnostics.intake);
|
|
if (movement > static_cast<std::uint8_t>(CellularMovementResult::insufficient_energy) ||
|
|
intake > static_cast<std::uint8_t>(CellularIntakeResult::out_of_contact) ||
|
|
diagnostics.engulf > CellularEngulfResult::membrane_too_thick ||
|
|
diagnostics.bud > CellularBudOutcome::no_placement)
|
|
return false;
|
|
if (!diagnostics.has_tick_effects)
|
|
return diagnostics.maintenance_cost_pj == 0 && diagnostics.movement_cost_pj == 0 &&
|
|
diagnostics.movement == CellularMovementResult::not_reported &&
|
|
diagnostics.intake == CellularIntakeResult::not_reported &&
|
|
diagnostics.engulf == CellularEngulfResult::not_reported &&
|
|
diagnostics.bud == CellularBudOutcome::not_reported;
|
|
return diagnostics.movement != CellularMovementResult::not_reported &&
|
|
diagnostics.intake != CellularIntakeResult::not_reported;
|
|
}
|
|
|
|
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::uint16_t version = session_protocol_version) {
|
|
std::vector<std::uint8_t> out;
|
|
out.reserve(session_header_bytes + payload);
|
|
put(out, session_magic, 4);
|
|
put(out, 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::moved);
|
|
}
|
|
// Printable ASCII without spaces, then zero padding to the field size.
|
|
bool valid_text(const std::string& s) {
|
|
return !s.empty() && s.size() <= session_address_bytes &&
|
|
std::ranges::all_of(s, [](char ch) { return ch > 0x20 && ch < 0x7F; });
|
|
}
|
|
void put_text(std::vector<std::uint8_t>& out, const std::string& s) {
|
|
for (std::size_t i = 0; i < session_address_bytes; ++i)
|
|
out.push_back(i < s.size() ? static_cast<std::uint8_t>(s[i]) : std::uint8_t{0});
|
|
}
|
|
std::optional<std::string> text_at(std::span<const std::uint8_t> in, std::size_t at) {
|
|
std::string s;
|
|
std::size_t i = 0;
|
|
for (; i < session_address_bytes && in[at + i] != 0; ++i)
|
|
s.push_back(static_cast<char>(in[at + i]));
|
|
for (; i < session_address_bytes; ++i)
|
|
if (in[at + i] != 0)
|
|
return std::nullopt; // Bytes after the terminator must be padding.
|
|
if (!valid_text(s))
|
|
return std::nullopt;
|
|
return s;
|
|
}
|
|
void put_address(std::vector<std::uint8_t>& out, const RegionAddress& a) {
|
|
put(out, a.region, 8);
|
|
put_text(out, a.endpoint);
|
|
put_text(out, a.server_name);
|
|
}
|
|
std::optional<RegionAddress> address_at(std::span<const std::uint8_t> in, std::size_t at) {
|
|
RegionAddress a;
|
|
a.region = get(in, at, 8);
|
|
auto endpoint = text_at(in, at + 8);
|
|
auto name = text_at(in, at + 8 + session_address_bytes);
|
|
if (a.region == 0 || !endpoint || !name)
|
|
return std::nullopt;
|
|
a.endpoint = std::move(*endpoint);
|
|
a.server_name = std::move(*name);
|
|
return a;
|
|
}
|
|
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) {
|
|
const auto is_life = std::holds_alternative<PrimitiveLifeRequest>(message) ||
|
|
std::holds_alternative<PrimitiveLifeReceipt>(message) ||
|
|
std::holds_alternative<PrimitiveLifeOutcome>(message) ||
|
|
std::holds_alternative<PrimitiveLifeQuery>(message);
|
|
const auto version =
|
|
std::holds_alternative<CellularPerception>(message)
|
|
? (std::get<CellularPerception>(message).diagnostics ? session_protocol_v9
|
|
: session_protocol_v8)
|
|
: std::holds_alternative<CellularAction>(message) ? session_protocol_v7
|
|
: std::holds_alternative<CellularMutationAction>(message) ? session_protocol_v10
|
|
: std::holds_alternative<CellularMutationOffer>(message) ? session_protocol_v10
|
|
: std::holds_alternative<CellularLifeEnded>(message) ? session_protocol_v10
|
|
: std::holds_alternative<CellularIntention>(message) ? session_protocol_v6
|
|
: is_life ? session_protocol_v5
|
|
: session_protocol_version;
|
|
return encode_session(message, version);
|
|
}
|
|
|
|
std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uint16_t version) {
|
|
if (version != session_protocol_version && version != session_protocol_v5 &&
|
|
version != session_protocol_v6 && version != session_protocol_v7 &&
|
|
version != session_protocol_v8 && version != session_protocol_v9 &&
|
|
version != session_protocol_v10)
|
|
return {};
|
|
if (version == session_protocol_version &&
|
|
(std::holds_alternative<PrimitiveLifeRequest>(message) ||
|
|
std::holds_alternative<PrimitiveLifeReceipt>(message) ||
|
|
std::holds_alternative<PrimitiveLifeOutcome>(message) ||
|
|
std::holds_alternative<PrimitiveLifeQuery>(message)))
|
|
return {};
|
|
if (version != session_protocol_v6 && std::holds_alternative<CellularIntention>(message))
|
|
return {};
|
|
if (version != session_protocol_v7 && version != session_protocol_v8 &&
|
|
version != session_protocol_v9 && version != session_protocol_v10 &&
|
|
std::holds_alternative<CellularAction>(message))
|
|
return {};
|
|
if (version != session_protocol_v7 && version != session_protocol_v8 &&
|
|
version != session_protocol_v9 && version != session_protocol_v10 &&
|
|
std::holds_alternative<ActionOutcome>(message) &&
|
|
std::get<ActionOutcome>(message).result == ActionResult::committed)
|
|
return {};
|
|
if (version != session_protocol_v10 && std::holds_alternative<CellularMutationAction>(message))
|
|
return {};
|
|
if (const auto* offer = std::get_if<CellularMutationOffer>(&message);
|
|
offer && (version != session_protocol_v10 || !valid_offer(*offer)))
|
|
return {};
|
|
if (const auto* ended = std::get_if<CellularLifeEnded>(&message);
|
|
ended && (version != session_protocol_v10 || ended->tick == 0 ||
|
|
ended->life_identity == 0 || ended->cause > CellularDeathReason::engulfed))
|
|
return {};
|
|
// v9 has no room for engulf and bud effects or the bud cost; only v10 reports them
|
|
// (ADR-0129).
|
|
if (const auto* perception = std::get_if<CellularPerception>(&message);
|
|
perception && version != session_protocol_v10 && perception->diagnostics &&
|
|
(perception->diagnostics->engulf != CellularEngulfResult::not_reported ||
|
|
perception->diagnostics->bud != CellularBudOutcome::not_reported ||
|
|
perception->diagnostics->bud_cost_energy_pj != 0 ||
|
|
std::ranges::any_of(perception->diagnostics->bud_cost_matter_fg,
|
|
[](std::uint64_t value) { return value != 0; })))
|
|
return {};
|
|
if (const auto* action = std::get_if<CellularAction>(&message);
|
|
action && (action->engulf || action->bud) &&
|
|
(version != session_protocol_v10 || action->has_intake_target || action->intake_x_um ||
|
|
action->intake_y_um || (action->engulf && action->bud)))
|
|
return {};
|
|
if (version != session_protocol_v8 && version != session_protocol_v9 &&
|
|
version != session_protocol_v10 && std::holds_alternative<CellularPerception>(message))
|
|
return {};
|
|
if (const auto* perception = std::get_if<CellularPerception>(&message)) {
|
|
if (!perception->tick || perception->patches.size() > max_cellular_perceived_patches ||
|
|
(!perception->life_identity && !perception->patches.empty()) ||
|
|
(version == session_protocol_v8 && perception->diagnostics) ||
|
|
((version == session_protocol_v9 || version == session_protocol_v10) &&
|
|
perception->life_identity && !perception->diagnostics) ||
|
|
(perception->life_identity == 0 && perception->diagnostics) ||
|
|
(perception->diagnostics && !valid_diagnostics(*perception->diagnostics)) ||
|
|
!std::ranges::is_sorted(perception->patches,
|
|
[](const auto& a, const auto& b) {
|
|
return std::pair{a.x_um, a.y_um} <
|
|
std::pair{b.x_um, b.y_um};
|
|
}) ||
|
|
std::adjacent_find(perception->patches.begin(), perception->patches.end(),
|
|
[](const auto& a, const auto& b) {
|
|
return a.x_um == b.x_um && a.y_um == b.y_um;
|
|
}) != perception->patches.end())
|
|
return {};
|
|
}
|
|
struct Encoder {
|
|
std::uint16_t version;
|
|
std::vector<std::uint8_t> operator()(const SessionHello& m) const {
|
|
auto out = header(SessionMessageType::hello, hello_bytes, version);
|
|
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, version);
|
|
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, version);
|
|
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, version);
|
|
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, version);
|
|
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, version);
|
|
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 CellularIntention& m) const {
|
|
auto out =
|
|
header(SessionMessageType::cellular_intention, cellular_intention_bytes, version);
|
|
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);
|
|
out.push_back(m.has_intake_target ? 1 : 0);
|
|
put(out, 0, 3);
|
|
put(out, m.intake_x_um, 4);
|
|
put(out, m.intake_y_um, 4);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const CellularAction& m) const {
|
|
auto out = header(SessionMessageType::cellular_action, cellular_action_bytes, version);
|
|
for (const auto v : {m.session, m.epoch, m.action_id, 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);
|
|
out.push_back(m.has_intake_target ? 1 : m.engulf ? 2 : m.bud ? 3 : 0); // Target kind.
|
|
put(out, 0, 3);
|
|
put(out, m.intake_x_um, 4);
|
|
put(out, m.intake_y_um, 4);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const CellularMutationAction& m) const {
|
|
auto out = header(SessionMessageType::cellular_mutation_action,
|
|
cellular_mutation_action_bytes, version);
|
|
for (const auto value : {m.session, m.epoch, m.action_id, m.tick, m.life_identity,
|
|
m.event_index, m.offer_tick})
|
|
put(out, value, 8);
|
|
put(out, m.operator_id, 2);
|
|
put(out, 0, 6);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const CellularLifeEnded& m) const {
|
|
auto out =
|
|
header(SessionMessageType::cellular_life_ended, cellular_life_ended_bytes, version);
|
|
put(out, m.tick, 8);
|
|
put(out, m.life_identity, 8);
|
|
out.push_back(static_cast<std::uint8_t>(m.cause));
|
|
put(out, 0, 7);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const CellularMutationOffer& m) const {
|
|
auto out = header(SessionMessageType::cellular_mutation_offer,
|
|
cellular_offer_prefix_bytes +
|
|
m.candidates.size() * cellular_offered_candidate_bytes,
|
|
version);
|
|
for (const auto value : {m.tick, m.life_identity, m.event_index, m.offer_tick})
|
|
put(out, value, 8);
|
|
put(out, m.candidates.size(), 2);
|
|
put(out, 0, 6);
|
|
for (const auto& candidate : m.candidates) {
|
|
put(out, candidate.operator_id, 2);
|
|
out.push_back(candidate.capability);
|
|
out.push_back(candidate.effect);
|
|
put(out, candidate.parameter, 4);
|
|
put(out, candidate.maintenance, 8);
|
|
put(out, candidate.total_mass, 8);
|
|
put(out, candidate.cost_energy_pj, 8);
|
|
for (const auto matter : candidate.cost_matter_fg)
|
|
put(out, matter, 8);
|
|
}
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const CellularPerception& m) const {
|
|
auto out =
|
|
header(SessionMessageType::cellular_perception,
|
|
(version == session_protocol_v10 ? cellular_perception_v10_prefix_bytes
|
|
: version == session_protocol_v9 ? cellular_perception_v9_prefix_bytes
|
|
: cellular_perception_prefix_bytes) +
|
|
m.patches.size() * cellular_perceived_patch_bytes,
|
|
version);
|
|
put(out, m.tick, 8);
|
|
put(out, m.life_identity, 8);
|
|
put(out, m.patches.size(), 2);
|
|
if (version == session_protocol_v9 || version == session_protocol_v10) {
|
|
const auto& d = m.diagnostics;
|
|
put(out, d ? d->reserve_energy_pj : 0, 8);
|
|
for (const auto value : d ? d->reserve_matter_fg : std::array<std::uint64_t, 4>{})
|
|
put(out, value, 8);
|
|
put(out, d ? d->maintenance_cost_pj : 0, 8);
|
|
put(out, d ? d->movement_cost_pj : 0, 8);
|
|
out.push_back(d ? static_cast<std::uint8_t>(d->movement)
|
|
: static_cast<std::uint8_t>(CellularMovementResult::not_reported));
|
|
out.push_back(d ? static_cast<std::uint8_t>(d->intake)
|
|
: static_cast<std::uint8_t>(CellularIntakeResult::not_reported));
|
|
out.push_back(d && d->has_tick_effects ? 1 : 0);
|
|
if (version == session_protocol_v10) {
|
|
out.push_back(d ? static_cast<std::uint8_t>(d->engulf) : 0);
|
|
out.push_back(d ? static_cast<std::uint8_t>(d->bud) : 0);
|
|
put(out, 0, 3);
|
|
put(out, d ? d->bud_cost_energy_pj : 0, 8);
|
|
for (const auto value :
|
|
d ? d->bud_cost_matter_fg : std::array<std::uint64_t, 4>{})
|
|
put(out, value, 8);
|
|
} else {
|
|
put(out, 0, 5);
|
|
}
|
|
}
|
|
for (const auto& patch : m.patches) {
|
|
put(out, patch.x_um, 4);
|
|
put(out, patch.y_um, 4);
|
|
put(out, patch.energy_pj, 8);
|
|
for (const auto matter : patch.matter_fg)
|
|
put(out, matter, 8);
|
|
}
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const IntentionReceipt& m) const {
|
|
auto out =
|
|
header(SessionMessageType::intention_receipt, intention_receipt_bytes, version);
|
|
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, version);
|
|
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, version);
|
|
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, version);
|
|
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, version);
|
|
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, version);
|
|
put(out, m.action_id, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SessionInterest& m) const {
|
|
auto out = header(SessionMessageType::interest, interest_bytes, version);
|
|
for (const auto v : {m.x, m.y, m.z})
|
|
put(out, static_cast<std::uint64_t>(v), 8);
|
|
put(out, m.radius, 2);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SnapshotChunk& m) const {
|
|
auto out = header(SessionMessageType::snapshot_chunk,
|
|
snapshot_chunk_header_bytes + m.records.size(), version);
|
|
for (const auto v :
|
|
{m.epoch, m.tick, m.baseline, m.field_revision, m.organism_revision})
|
|
put(out, v, 8);
|
|
put(out, m.mask, 4);
|
|
put(out, static_cast<std::uint64_t>(m.slice_z), 8);
|
|
out.push_back(m.index);
|
|
out.push_back(m.count);
|
|
out.insert(out.end(), m.records.begin(), m.records.end());
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SnapshotAck& m) const {
|
|
auto out = header(SessionMessageType::snapshot_ack, snapshot_ack_bytes, version);
|
|
put(out, m.epoch, 8);
|
|
put(out, m.tick, 8);
|
|
return out;
|
|
}
|
|
// Invalid text fields are encoded as given; the receiver refuses them.
|
|
std::vector<std::uint8_t> operator()(const HandoffOffer& m) const {
|
|
auto out = header(SessionMessageType::handoff_offer, handoff_offer_bytes, version);
|
|
out.insert(out.end(), m.transfer.begin(), m.transfer.end());
|
|
put_address(out, m.destination);
|
|
out.insert(out.end(), m.ticket.begin(), m.ticket.end());
|
|
out.resize(session_header_bytes + handoff_offer_bytes); // Overlong text truncates.
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const SessionRedirect& m) const {
|
|
auto out = header(SessionMessageType::redirect, redirect_bytes, version);
|
|
put_address(out, m.owner);
|
|
out.resize(session_header_bytes + redirect_bytes);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const PrimitiveLifeRequest& m) const {
|
|
auto out =
|
|
header(SessionMessageType::primitive_life_request, life_request_bytes, version);
|
|
put(out, m.session, 8);
|
|
put(out, m.epoch, 8);
|
|
put(out, m.request_id, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const PrimitiveLifeReceipt& m) const {
|
|
auto out =
|
|
header(SessionMessageType::primitive_life_receipt, life_receipt_bytes, version);
|
|
put(out, m.request_id, 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 PrimitiveLifeOutcome& m) const {
|
|
auto out =
|
|
header(SessionMessageType::primitive_life_outcome, life_outcome_bytes, version);
|
|
put(out, m.request_id, 8);
|
|
out.push_back(static_cast<std::uint8_t>(m.result));
|
|
put(out, m.life_identity, 8);
|
|
put(out, m.commit_sequence, 8);
|
|
return out;
|
|
}
|
|
std::vector<std::uint8_t> operator()(const PrimitiveLifeQuery& m) const {
|
|
auto out = header(SessionMessageType::primitive_life_query, life_query_bytes, version);
|
|
put(out, m.request_id, 8);
|
|
return out;
|
|
}
|
|
};
|
|
return std::visit(Encoder{version}, message);
|
|
}
|
|
|
|
std::expected<SessionMessage, SessionCodecError>
|
|
decode_session(std::span<const std::uint8_t> bytes) {
|
|
return decode_session(bytes, nullptr);
|
|
}
|
|
|
|
std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const std::uint8_t> bytes,
|
|
std::uint16_t* version) {
|
|
if (bytes.size() < session_header_bytes)
|
|
return std::unexpected(SessionCodecError::truncated);
|
|
if (get(bytes, 0, 4) != session_magic)
|
|
return std::unexpected(SessionCodecError::bad_magic);
|
|
const auto wire_version = static_cast<std::uint16_t>(get(bytes, 4, 2));
|
|
if (wire_version != session_protocol_version && wire_version != session_protocol_v5 &&
|
|
wire_version != session_protocol_v6 && wire_version != session_protocol_v7 &&
|
|
wire_version != session_protocol_v8 && wire_version != session_protocol_v9 &&
|
|
wire_version != session_protocol_v10)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
if (version)
|
|
*version = wire_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::cellular_intention): {
|
|
if (wire_version != session_protocol_v6)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
if (!expect(cellular_intention_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
CellularIntention 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)));
|
|
if (payload[36] > 1 || payload[37] != 0 || payload[38] != 0 || payload[39] != 0)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.has_intake_target = payload[36] != 0;
|
|
m.intake_x_um = static_cast<std::uint32_t>(get(payload, 40, 4));
|
|
m.intake_y_um = static_cast<std::uint32_t>(get(payload, 44, 4));
|
|
if (!m.has_intake_target && (m.intake_x_um != 0 || m.intake_y_um != 0))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::cellular_action): {
|
|
if (wire_version != session_protocol_v7 && wire_version != session_protocol_v8 &&
|
|
wire_version != session_protocol_v9 && wire_version != session_protocol_v10)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
if (!expect(cellular_action_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
CellularAction m;
|
|
m.session = get(payload, 0, 8);
|
|
m.epoch = get(payload, 8, 8);
|
|
m.action_id = 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)));
|
|
// Target kind: 0 none, 1 patch intake, 2 engulf and 3 bud (v10 only, ADR-0124/0125).
|
|
if (payload[36] > (wire_version == session_protocol_v10 ? 3 : 1) || payload[37] != 0 ||
|
|
payload[38] != 0 || payload[39] != 0)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.has_intake_target = payload[36] == 1;
|
|
m.engulf = payload[36] == 2;
|
|
m.bud = payload[36] == 3;
|
|
m.intake_x_um = static_cast<std::uint32_t>(get(payload, 40, 4));
|
|
m.intake_y_um = static_cast<std::uint32_t>(get(payload, 44, 4));
|
|
if (!m.session || !m.epoch || !m.action_id || !m.tick ||
|
|
(!m.has_intake_target && (m.intake_x_um || m.intake_y_um)))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::cellular_mutation_action): {
|
|
if (wire_version != session_protocol_v10)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
if (!expect(cellular_mutation_action_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
CellularMutationAction m;
|
|
m.session = get(payload, 0, 8);
|
|
m.epoch = get(payload, 8, 8);
|
|
m.action_id = get(payload, 16, 8);
|
|
m.tick = get(payload, 24, 8);
|
|
m.life_identity = get(payload, 32, 8);
|
|
m.event_index = get(payload, 40, 8);
|
|
m.offer_tick = get(payload, 48, 8);
|
|
m.operator_id = static_cast<std::uint16_t>(get(payload, 56, 2));
|
|
if (!m.session || !m.epoch || !m.action_id || !m.tick || !m.life_identity ||
|
|
!m.event_index || !m.offer_tick || m.offer_tick >= m.tick || // operator 0 declines
|
|
std::ranges::any_of(payload.subspan(58, 6),
|
|
[](std::uint8_t value) { return value != 0; }))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::cellular_life_ended): {
|
|
if (wire_version != session_protocol_v10)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
if (!expect(cellular_life_ended_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
CellularLifeEnded m;
|
|
m.tick = get(payload, 0, 8);
|
|
m.life_identity = get(payload, 8, 8);
|
|
if (m.tick == 0 || m.life_identity == 0 ||
|
|
payload[16] > static_cast<std::uint8_t>(CellularDeathReason::engulfed) ||
|
|
std::ranges::any_of(payload.subspan(17, 7),
|
|
[](std::uint8_t value) { return value != 0; }))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.cause = static_cast<CellularDeathReason>(payload[16]);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::cellular_mutation_offer): {
|
|
if (wire_version != session_protocol_v10)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
if (payload.size() < cellular_offer_prefix_bytes)
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
const auto count = static_cast<std::size_t>(get(payload, 32, 2));
|
|
if (count > max_cellular_offered_candidates ||
|
|
payload.size() !=
|
|
cellular_offer_prefix_bytes + count * cellular_offered_candidate_bytes)
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
CellularMutationOffer m;
|
|
m.tick = get(payload, 0, 8);
|
|
m.life_identity = get(payload, 8, 8);
|
|
m.event_index = get(payload, 16, 8);
|
|
m.offer_tick = get(payload, 24, 8);
|
|
if (std::ranges::any_of(payload.subspan(34, 6),
|
|
[](std::uint8_t value) { return value != 0; }))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
const auto at = cellular_offer_prefix_bytes + i * cellular_offered_candidate_bytes;
|
|
CellularOfferedCandidate candidate{static_cast<std::uint16_t>(get(payload, at, 2)),
|
|
payload[at + 2],
|
|
payload[at + 3],
|
|
static_cast<std::uint32_t>(get(payload, at + 4, 4)),
|
|
get(payload, at + 8, 8),
|
|
get(payload, at + 16, 8),
|
|
get(payload, at + 24, 8),
|
|
{}};
|
|
for (std::size_t channel = 0; channel < candidate.cost_matter_fg.size(); ++channel)
|
|
candidate.cost_matter_fg[channel] = get(payload, at + 32 + channel * 8, 8);
|
|
m.candidates.push_back(candidate);
|
|
}
|
|
if (!valid_offer(m))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::cellular_perception): {
|
|
if (wire_version != session_protocol_v8 && wire_version != session_protocol_v9 &&
|
|
wire_version != session_protocol_v10)
|
|
return std::unexpected(SessionCodecError::unsupported_version);
|
|
const auto prefix =
|
|
wire_version == session_protocol_v10 ? cellular_perception_v10_prefix_bytes
|
|
: wire_version == session_protocol_v9 ? cellular_perception_v9_prefix_bytes
|
|
: cellular_perception_prefix_bytes;
|
|
if (payload.size() < prefix)
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
const auto count = static_cast<std::size_t>(get(payload, 16, 2));
|
|
if (count > max_cellular_perceived_patches ||
|
|
payload.size() != prefix + count * cellular_perceived_patch_bytes)
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
CellularPerception m;
|
|
m.tick = get(payload, 0, 8);
|
|
m.life_identity = get(payload, 8, 8);
|
|
if (!m.tick || (!m.life_identity && count))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
if (wire_version == session_protocol_v9 || wire_version == session_protocol_v10) {
|
|
CellularLifeDiagnostics d;
|
|
d.reserve_energy_pj = get(payload, 18, 8);
|
|
for (std::size_t channel = 0; channel < d.reserve_matter_fg.size(); ++channel)
|
|
d.reserve_matter_fg[channel] = get(payload, 26 + channel * 8, 8);
|
|
d.maintenance_cost_pj = get(payload, 58, 8);
|
|
d.movement_cost_pj = get(payload, 66, 8);
|
|
if (payload[74] >
|
|
static_cast<std::uint8_t>(CellularMovementResult::insufficient_energy) ||
|
|
payload[75] > static_cast<std::uint8_t>(CellularIntakeResult::out_of_contact) ||
|
|
payload[76] > 1 ||
|
|
std::ranges::any_of(payload.subspan(wire_version == session_protocol_v10 ? 79 : 77,
|
|
wire_version == session_protocol_v10 ? 3 : 5),
|
|
[](std::uint8_t value) { return value != 0; }) ||
|
|
payload[77] > static_cast<std::uint8_t>(CellularEngulfResult::membrane_too_thick) ||
|
|
payload[78] > static_cast<std::uint8_t>(CellularBudOutcome::no_placement))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
if (wire_version == session_protocol_v10) {
|
|
d.engulf = static_cast<CellularEngulfResult>(payload[77]);
|
|
d.bud = static_cast<CellularBudOutcome>(payload[78]);
|
|
d.bud_cost_energy_pj = get(payload, 82, 8);
|
|
for (std::size_t channel = 0; channel < d.bud_cost_matter_fg.size(); ++channel)
|
|
d.bud_cost_matter_fg[channel] = get(payload, 90 + channel * 8, 8);
|
|
}
|
|
d.movement = static_cast<CellularMovementResult>(payload[74]);
|
|
d.intake = static_cast<CellularIntakeResult>(payload[75]);
|
|
d.has_tick_effects = payload[76] != 0;
|
|
if (m.life_identity)
|
|
m.diagnostics = d;
|
|
else if (d.reserve_energy_pj ||
|
|
std::ranges::any_of(d.reserve_matter_fg,
|
|
[](std::uint64_t value) { return value != 0; }) ||
|
|
d.maintenance_cost_pj || d.movement_cost_pj || d.has_tick_effects ||
|
|
d.movement != CellularMovementResult::not_reported ||
|
|
d.intake != CellularIntakeResult::not_reported ||
|
|
d.engulf != CellularEngulfResult::not_reported ||
|
|
d.bud != CellularBudOutcome::not_reported || d.bud_cost_energy_pj ||
|
|
std::ranges::any_of(d.bud_cost_matter_fg,
|
|
[](std::uint64_t value) { return value != 0; }))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
}
|
|
if ((wire_version == session_protocol_v9 || wire_version == session_protocol_v10) &&
|
|
m.life_identity && !m.diagnostics)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
if (m.diagnostics && !valid_diagnostics(*m.diagnostics))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.patches.reserve(count);
|
|
std::size_t offset = prefix;
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
CellularPerceivedPatch patch;
|
|
patch.x_um = static_cast<std::uint32_t>(get(payload, offset, 4));
|
|
patch.y_um = static_cast<std::uint32_t>(get(payload, offset + 4, 4));
|
|
patch.energy_pj = get(payload, offset + 8, 8);
|
|
for (std::size_t channel = 0; channel < patch.matter_fg.size(); ++channel)
|
|
patch.matter_fg[channel] = get(payload, offset + 16 + channel * 8, 8);
|
|
if (!m.patches.empty() && !(std::pair{m.patches.back().x_um, m.patches.back().y_um} <
|
|
std::pair{patch.x_um, patch.y_um}))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.patches.push_back(patch);
|
|
offset += cellular_perceived_patch_bytes;
|
|
}
|
|
return m;
|
|
}
|
|
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::committed) ||
|
|
(payload[8] == static_cast<std::uint8_t>(ActionResult::committed) &&
|
|
wire_version != session_protocol_v7 && wire_version != session_protocol_v8 &&
|
|
wire_version != session_protocol_v9 && wire_version != session_protocol_v10))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
const ActionOutcome m{get(payload, 0, 8), static_cast<ActionResult>(payload[8]),
|
|
get(payload, 9, 8), get(payload, 17, 8)};
|
|
if (m.result == ActionResult::committed && (!m.tick || !m.commit_sequence))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
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)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::interest): {
|
|
if (!expect(interest_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
return SessionInterest{static_cast<std::int64_t>(get(payload, 0, 8)),
|
|
static_cast<std::int64_t>(get(payload, 8, 8)),
|
|
static_cast<std::int64_t>(get(payload, 16, 8)),
|
|
static_cast<std::uint16_t>(get(payload, 24, 2))};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::snapshot_chunk): {
|
|
// Variable length: a fixed header and up to one datagram of records.
|
|
if (payload.size() < snapshot_chunk_header_bytes ||
|
|
payload.size() > snapshot_chunk_header_bytes + max_snapshot_records_bytes)
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
SnapshotChunk m;
|
|
m.epoch = get(payload, 0, 8);
|
|
m.tick = get(payload, 8, 8);
|
|
m.baseline = get(payload, 16, 8);
|
|
m.field_revision = get(payload, 24, 8);
|
|
m.organism_revision = get(payload, 32, 8);
|
|
m.mask = static_cast<std::uint32_t>(get(payload, 40, 4));
|
|
m.slice_z = static_cast<std::int64_t>(get(payload, 44, 8));
|
|
m.index = payload[52];
|
|
m.count = payload[53];
|
|
if (m.epoch == 0 || m.tick == 0 || m.baseline >= m.tick || m.count == 0 ||
|
|
m.count > max_snapshot_chunks || m.index >= m.count)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.records.assign(payload.begin() + snapshot_chunk_header_bytes, payload.end());
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::snapshot_ack): {
|
|
if (!expect(snapshot_ack_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
return SnapshotAck{get(payload, 0, 8), get(payload, 8, 8)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::handoff_offer): {
|
|
if (!expect(handoff_offer_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
HandoffOffer m;
|
|
std::copy_n(payload.begin(), m.transfer.size(), m.transfer.begin());
|
|
auto destination = address_at(payload, 16);
|
|
if (!destination || std::ranges::all_of(m.transfer, [](auto b) { return b == 0; }))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
m.destination = std::move(*destination);
|
|
m.ticket = grant_at(payload, 16 + address_bytes);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::redirect): {
|
|
if (!expect(redirect_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
auto owner = address_at(payload, 0);
|
|
if (!owner)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return SessionRedirect{std::move(*owner)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::primitive_life_request): {
|
|
if (wire_version < session_protocol_v5)
|
|
return std::unexpected(SessionCodecError::unknown_type);
|
|
if (!expect(life_request_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
PrimitiveLifeRequest m{get(payload, 0, 8), get(payload, 8, 8), get(payload, 16, 8)};
|
|
if (!m.session || !m.epoch || !m.request_id)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::primitive_life_receipt): {
|
|
if (wire_version < session_protocol_v5)
|
|
return std::unexpected(SessionCodecError::unknown_type);
|
|
if (!expect(life_receipt_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
if (payload[8] > max_admit_status)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return PrimitiveLifeReceipt{get(payload, 0, 8), static_cast<AdmitStatus>(payload[8]),
|
|
get(payload, 9, 8)};
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::primitive_life_outcome): {
|
|
if (wire_version < session_protocol_v5)
|
|
return std::unexpected(SessionCodecError::unknown_type);
|
|
if (!expect(life_outcome_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
if (payload[8] < 1 || payload[8] > static_cast<std::uint8_t>(PrimitiveLifeResult::unknown))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
const PrimitiveLifeOutcome m{get(payload, 0, 8),
|
|
static_cast<PrimitiveLifeResult>(payload[8]),
|
|
get(payload, 9, 8), get(payload, 17, 8)};
|
|
if (!m.request_id ||
|
|
(m.result == PrimitiveLifeResult::born && (!m.life_identity || !m.commit_sequence)) ||
|
|
(m.result != PrimitiveLifeResult::born && (m.life_identity || m.commit_sequence)))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return m;
|
|
}
|
|
case static_cast<std::uint8_t>(SessionMessageType::primitive_life_query): {
|
|
if (wire_version < session_protocol_v5)
|
|
return std::unexpected(SessionCodecError::unknown_type);
|
|
if (!expect(life_query_bytes))
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
const auto id = get(payload, 0, 8);
|
|
if (!id)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return PrimitiveLifeQuery{id};
|
|
}
|
|
default:
|
|
return std::unexpected(SessionCodecError::unknown_type);
|
|
}
|
|
}
|
|
|
|
std::array<std::uint8_t, cellular_action_input_bytes>
|
|
encode_cellular_action_input(const CellularActionInput& input) {
|
|
std::vector<std::uint8_t> bytes;
|
|
bytes.reserve(cellular_action_input_bytes);
|
|
put(bytes, input.tick, 8);
|
|
put(bytes, static_cast<std::uint16_t>(input.move_x), 2);
|
|
put(bytes, static_cast<std::uint16_t>(input.move_y), 2);
|
|
bytes.push_back(input.has_intake_target ? 1 // Target kind.
|
|
: input.engulf ? 2
|
|
: input.bud ? 3
|
|
: 0);
|
|
put(bytes, 0, 3);
|
|
put(bytes, input.intake_x_um, 4);
|
|
put(bytes, input.intake_y_um, 4);
|
|
std::array<std::uint8_t, cellular_action_input_bytes> out{};
|
|
std::ranges::copy(bytes, out.begin());
|
|
return out;
|
|
}
|
|
|
|
std::expected<CellularActionInput, SessionCodecError>
|
|
decode_cellular_action_input(std::span<const std::uint8_t> bytes) {
|
|
if (bytes.size() != cellular_action_input_bytes)
|
|
return std::unexpected(SessionCodecError::wrong_length);
|
|
if (bytes[12] > 3 || bytes[13] != 0 || bytes[14] != 0 || bytes[15] != 0)
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
CellularActionInput input;
|
|
input.tick = get(bytes, 0, 8);
|
|
input.move_x = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(bytes, 8, 2)));
|
|
input.move_y = static_cast<std::int16_t>(static_cast<std::uint16_t>(get(bytes, 10, 2)));
|
|
input.has_intake_target = bytes[12] == 1;
|
|
input.engulf = bytes[12] == 2;
|
|
input.bud = bytes[12] == 3;
|
|
input.intake_x_um = static_cast<std::uint32_t>(get(bytes, 16, 4));
|
|
input.intake_y_um = static_cast<std::uint32_t>(get(bytes, 20, 4));
|
|
if (input.tick == 0 ||
|
|
(!input.has_intake_target && (input.intake_x_um != 0 || input.intake_y_um != 0)))
|
|
return std::unexpected(SessionCodecError::invalid_value);
|
|
return input;
|
|
}
|
|
} // namespace evo::network
|