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
222 lines
10 KiB
C++
222 lines
10 KiB
C++
#pragma once
|
|
|
|
#include "evo/client/organism_view.hpp"
|
|
#include "evo/network/session_grant.hpp"
|
|
#include "evo/server/cellular_region_host.hpp"
|
|
#include "evo/server/pg_slot.hpp"
|
|
#include "evo/server/postgres_action_ledger.hpp"
|
|
#include "evo/server/postgres_control_ledger.hpp"
|
|
#include "evo/server/region_sessions.hpp"
|
|
#include "evo/server/session_window.hpp"
|
|
#include "m10_bridge.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <charconv>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace evo::server_app {
|
|
// Profile-4 network boundary. Biology and persistence remain owned by the cellular host;
|
|
// this bridge handles admission, v7/v8 action receipts, public cell replication and
|
|
// owner-filtered v8 perception.
|
|
class CellularBridge {
|
|
public:
|
|
static std::optional<CellularBridge> open(const M10Options& options,
|
|
server::CellularRegionHost& host,
|
|
persistence::RegionKey key, std::uint64_t frame,
|
|
const persistence::PgOptions& pg) {
|
|
if (!options.complete())
|
|
return fail("error cellular-session-config");
|
|
CellularBridge bridge;
|
|
const auto window = server::SessionWindow::open(options.session_window, wall_seconds());
|
|
if (!window)
|
|
return fail("error cellular-session-window");
|
|
const auto colon = options.listen.rfind(':');
|
|
std::uint16_t port{};
|
|
if (colon == std::string::npos ||
|
|
std::from_chars(options.listen.data() + colon + 1,
|
|
options.listen.data() + options.listen.size(), port)
|
|
.ec != std::errc{})
|
|
return fail("error cellular-session-listen");
|
|
const auto issuer = read(options.issuer);
|
|
if (issuer.size() != network::grant_public_key_bytes)
|
|
return fail("error cellular-session-issuer");
|
|
|
|
server::RegionSessionsConfig config;
|
|
config.transport.listen_address = options.listen.substr(0, colon);
|
|
config.transport.port = port;
|
|
config.transport.certificate_pem_file = options.certificate;
|
|
config.transport.key_pem_file = options.key;
|
|
config.control.environment =
|
|
options.environment == "dev" ? network::DeploymentEnvironment::dev
|
|
: options.environment == "stage" ? network::DeploymentEnvironment::stage
|
|
: network::DeploymentEnvironment::world0;
|
|
config.control.world = {key.world, frame};
|
|
config.control.region = key.region;
|
|
config.control.epoch = 1;
|
|
config.control.max_sessions = 512;
|
|
config.transport.limits.max_connections = 576;
|
|
const auto life_routes = parse_life_owner_routes(options);
|
|
if (!life_routes || life_routes->empty())
|
|
return fail("error cellular-life-routes");
|
|
config.life_owner_routes = *life_routes;
|
|
network::GrantPublicKey public_key{};
|
|
std::copy(issuer.begin(), issuer.end(), public_key.begin());
|
|
config.issuer_keys.push_back({1, public_key});
|
|
|
|
bridge.window_ = *window;
|
|
bridge.slot_ = std::make_unique<server::PgSlot>(pg, std::chrono::seconds{2});
|
|
const auto lease = [&host] { return host.authority(); };
|
|
bridge.control_ =
|
|
std::make_unique<server::PostgresControlLedger>(*bridge.slot_, key, lease);
|
|
bridge.actions_ = std::make_unique<server::PostgresActionLedger>(*bridge.slot_, key, lease);
|
|
config.ledger = bridge.control_.get();
|
|
config.actions = bridge.actions_.get();
|
|
config.accept_cellular_actions = true;
|
|
config.create_primitive_life = [&host](const network::AccountId& account,
|
|
std::uint64_t generation, std::uint64_t request) {
|
|
return host.create_primitive_life(account.bytes, generation, request);
|
|
};
|
|
config.query_primitive_life = [&host](const network::AccountId& account,
|
|
std::uint64_t generation, std::uint64_t request) {
|
|
return host.query_primitive_life(account.bytes, generation, request);
|
|
};
|
|
auto sessions = server::RegionSessions::create(std::move(config));
|
|
if (!sessions)
|
|
return fail(sessions.error() == server::RegionSessionsError::transport
|
|
? "error cellular-session-listen"
|
|
: "error cellular-session-config");
|
|
bridge.sessions_ = std::move(*sessions);
|
|
bridge.sessions_->set_default_interest({0, 0, 0, 7});
|
|
std::printf("cellular listening %u\n", static_cast<unsigned>(bridge.sessions_->port()));
|
|
return std::optional<CellularBridge>{std::move(bridge)};
|
|
}
|
|
|
|
[[nodiscard]] bool accepting(std::uint64_t now_seconds) {
|
|
const bool open = window_ && window_->accepting(now_seconds);
|
|
if (!open && !shutdown_) {
|
|
sessions_->shutdown(std::chrono::steady_clock::now());
|
|
shutdown_ = true;
|
|
}
|
|
return open;
|
|
}
|
|
[[nodiscard]] server::RegionSessions& sessions() noexcept {
|
|
return *sessions_;
|
|
}
|
|
[[nodiscard]] std::vector<network::CarriedAction> carry(std::uint64_t tick) {
|
|
return sessions_->carry(tick);
|
|
}
|
|
void committed(std::span<const network::CarriedAction> actions, std::uint64_t sequence) {
|
|
sessions_->committed(actions, sequence);
|
|
}
|
|
void dropped(std::span<const network::CarriedAction> actions, bool final) {
|
|
sessions_->dropped(actions, final);
|
|
}
|
|
void poll(const server::HostStatus& status, std::uint64_t now_seconds) {
|
|
sessions_->poll({status.ready, status.epoch, status.simulated_tick}, now_seconds,
|
|
std::chrono::steady_clock::now());
|
|
for (const auto& event : sessions_->take_events())
|
|
std::printf("cellular %s session=%llu account=%s reason=%u\n",
|
|
event.kind == server::RegionSessionEvent::Kind::opened ? "opened"
|
|
: event.kind == server::RegionSessionEvent::Kind::closed ? "closed"
|
|
: "refused",
|
|
static_cast<unsigned long long>(event.session), event.account.c_str(),
|
|
static_cast<unsigned>(event.reason));
|
|
}
|
|
void replicate(const server::CellularLives& state) {
|
|
const auto tick = state.tick();
|
|
if (replicated_tick_ == tick)
|
|
return;
|
|
const auto source = [&](const network::SessionInterest& interest) {
|
|
network::ReplicationSource result;
|
|
result.identity = {state.world(), state.frame()};
|
|
result.organism_revision = tick;
|
|
for (const auto& life : state.lives()) {
|
|
const auto voxel_x = life.position.x_um / 1'000'000;
|
|
const auto voxel_y = life.position.y_um / 1'000'000;
|
|
if (voxel_x >
|
|
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()) ||
|
|
voxel_y > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
|
|
continue;
|
|
const world::VoxelAddress voxel{static_cast<std::int64_t>(voxel_x),
|
|
static_cast<std::int64_t>(voxel_y), 0};
|
|
if (voxel.x < interest.x - interest.radius ||
|
|
voxel.x > interest.x + interest.radius ||
|
|
voxel.y < interest.y - interest.radius ||
|
|
voxel.y > interest.y + interest.radius)
|
|
continue;
|
|
const auto genome = state.archive().find(life.identity.genome);
|
|
if (!genome)
|
|
continue;
|
|
const biology::EnvironmentState environment{state.ambient_temperature_millikelvin(),
|
|
life.reserve};
|
|
auto cell = client::present_organism(
|
|
life.identity.organism, voxel, {life.position.x_um % 1'000'000},
|
|
{life.position.y_um % 1'000'000}, *genome, life.growth, environment,
|
|
client::OrganismState::alive);
|
|
if (cell)
|
|
result.organisms.push_back(*cell);
|
|
}
|
|
std::ranges::sort(result.organisms, {}, &client::OrganismCell::id);
|
|
return result;
|
|
};
|
|
sessions_->replicate(tick, source);
|
|
sessions_->clear_cellular_perceptions(tick);
|
|
for (const auto& life : state.lives()) {
|
|
const auto perceived = state.perceive(life.identity.life);
|
|
if (!perceived)
|
|
continue;
|
|
network::CellularPerception view;
|
|
view.tick = tick;
|
|
view.life_identity = life.identity.life;
|
|
view.patches.reserve(perceived->size());
|
|
for (const auto& patch : *perceived) {
|
|
network::CellularPerceivedPatch visible;
|
|
visible.x_um = patch.address.x_um;
|
|
visible.y_um = patch.address.y_um;
|
|
visible.energy_pj = patch.available.energy.value;
|
|
for (std::size_t i = 0; i < visible.matter_fg.size(); ++i)
|
|
visible.matter_fg[i] = patch.available.matter[i].value;
|
|
view.patches.push_back(visible);
|
|
}
|
|
(void)sessions_->send_cellular_perception(network::AccountId{life.account}, view);
|
|
}
|
|
replicated_tick_ = tick;
|
|
}
|
|
void shutdown() {
|
|
sessions_->shutdown(std::chrono::steady_clock::now());
|
|
}
|
|
|
|
private:
|
|
static std::uint64_t wall_seconds() {
|
|
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
.count());
|
|
}
|
|
static std::string read(const std::string& path) {
|
|
std::ifstream file{path, std::ios::binary};
|
|
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
|
}
|
|
static std::optional<CellularBridge> fail(const char* message) {
|
|
std::puts(message);
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::unique_ptr<server::PgSlot> slot_;
|
|
std::unique_ptr<server::PostgresControlLedger> control_;
|
|
std::unique_ptr<server::PostgresActionLedger> actions_;
|
|
std::unique_ptr<server::RegionSessions> sessions_;
|
|
std::optional<server::SessionWindow> window_;
|
|
std::optional<std::uint64_t> replicated_tick_;
|
|
bool shutdown_{};
|
|
};
|
|
} // namespace evo::server_app
|