mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 17:59:19 +02:00
TTrain::id() numbers cabs with a local counter, in the order each peer happens to create them, so it cannot address a vehicle across the session. Neither was there any way for a player to see what is available to drive. - network/entities: PeerId (the host is peer 1, remote peers are numbered from 2, so it is a participant rather than a special case) and NetworkEntityId, plus a registry that numbers the drivable vehicles of the loaded scenario in name order and resolves ids back to TDynamicObject - the server owns the numbering and publishes it as VEHICLE_LIST - name, crew occupancy, crew capacity, AI state, availability - on every change and at least every 120 frames, out of band so it stays out of the replay backbuffer; a client binds the received ids to its own copy of the scenario by name - SERVER_HELLO hands the joining peer its PeerId - a multiplayer lobby listing that roster, with vehicles taken by other players shown as such rather than hidden; entering one goes through the existing entervehicle command, the vehicle is never moved The lobby lives inside the driver mode instead of a mode of its own: the world has to keep running while somebody is choosing (a client stops consuming the server's frames the moment its mode is not a command processor), and the same screen is what a player needs to change vehicles later on. Crew occupancy is still derived from the presence of a cab instance; the real crew model, claim handshake and AI takeover follow in stage 3.
335 lines
8.5 KiB
C++
335 lines
8.5 KiB
C++
#include "stdafx.h"
|
|
#include "network/network.h"
|
|
#include "network/message.h"
|
|
#include "utilities/Logs.h"
|
|
#include "scene/sn_utils.h"
|
|
#include "utilities/Timer.h"
|
|
#include "application/application.h"
|
|
#include "utilities/Globals.h"
|
|
|
|
// 2 - legacy lockstep protocol
|
|
// 3 - handshake carries build identification and an explicit rejection message
|
|
std::uint32_t const EU07_NETWORK_VERSION = 3;
|
|
|
|
namespace network {
|
|
|
|
backend_list_t& backend_list() {
|
|
// HACK: static initialization order fiasco fix
|
|
// NOTE: potential static deinitialization order fiasco
|
|
static backend_list_t backend_list;
|
|
return backend_list;
|
|
}
|
|
|
|
}
|
|
// connection
|
|
|
|
void network::connection::disconnect() {
|
|
WriteLog("net: peer dropped", logtype::net);
|
|
state = DEAD;
|
|
}
|
|
|
|
void network::connection::set_handler(std::function<void (const message &)> handler) {
|
|
message_handler = handler;
|
|
}
|
|
|
|
network::connection::connection(bool client, size_t counter) {
|
|
packet_counter = counter;
|
|
is_client = client;
|
|
state = AWAITING_HELLO;
|
|
}
|
|
|
|
void network::connection::connected()
|
|
{
|
|
WriteLog("net: socket connected", logtype::net);
|
|
|
|
if (is_client) {
|
|
client_hello msg;
|
|
msg.version = EU07_NETWORK_VERSION;
|
|
msg.start_packet = packet_counter;
|
|
msg.app_version = Global.asVersion;
|
|
send_message(msg);
|
|
}
|
|
}
|
|
|
|
void network::connection::catch_up()
|
|
{
|
|
backbuffer->seekg(backbuffer_pos);
|
|
|
|
std::vector<std::shared_ptr<message>> messages;
|
|
|
|
for (int i = 0; i < CATCHUP_PACKETS; i++) {
|
|
if (backbuffer->peek() == EOF) {
|
|
send_messages(messages);
|
|
state = ACTIVE;
|
|
backbuffer->seekg(0, std::ios_base::end);
|
|
return;
|
|
}
|
|
|
|
auto msg = deserialize_message(*backbuffer.get());
|
|
|
|
if (packet_counter) {
|
|
packet_counter--;
|
|
i--; // TODO: it would be better to skip frames in chunks
|
|
continue;
|
|
}
|
|
|
|
messages.push_back(msg);
|
|
}
|
|
|
|
backbuffer_pos = backbuffer->tellg();
|
|
backbuffer->seekg(0, std::ios_base::end);
|
|
|
|
send_messages(messages);
|
|
}
|
|
|
|
void network::connection::send_complete(std::shared_ptr<std::string> buf)
|
|
{
|
|
if (!is_client && state == CATCHING_UP) {
|
|
catch_up();
|
|
}
|
|
}
|
|
|
|
// --------------
|
|
|
|
// server
|
|
network::server::server(std::shared_ptr<std::istream> buf) : backbuffer(buf)
|
|
{
|
|
|
|
}
|
|
|
|
void network::server::push_delta(const frame_info &msg)
|
|
{
|
|
for (auto it = clients.begin(); it != clients.end(); ) {
|
|
if ((*it)->state == connection::DEAD) {
|
|
it = clients.erase(it);
|
|
continue;
|
|
}
|
|
|
|
if ((*it)->state == connection::ACTIVE)
|
|
(*it)->send_message(msg);
|
|
|
|
it++;
|
|
}
|
|
}
|
|
|
|
void network::server::push_message(const message &msg)
|
|
{
|
|
for (auto const &client : clients) {
|
|
if (client->state == connection::ACTIVE)
|
|
client->send_message(msg);
|
|
}
|
|
}
|
|
|
|
command_queue::commands_map network::server::pop_commands()
|
|
{
|
|
command_queue::commands_map map(client_commands_queue);
|
|
client_commands_queue.clear();
|
|
return map;
|
|
}
|
|
|
|
void network::server::handle_message(std::shared_ptr<connection> conn, const message &msg)
|
|
{
|
|
if (msg.type == message::TYPE_MAX)
|
|
{
|
|
conn->disconnect();
|
|
return;
|
|
}
|
|
|
|
if (msg.type == message::CLIENT_HELLO) {
|
|
const auto& cmd = dynamic_cast<const client_hello&>(msg);
|
|
|
|
std::string rejection;
|
|
if (cmd.version != (int32_t)EU07_NETWORK_VERSION) {
|
|
rejection = "incompatible protocol version: server speaks "
|
|
+ std::to_string(EU07_NETWORK_VERSION)
|
|
+ ", client speaks " + std::to_string(cmd.version);
|
|
}
|
|
else if (!Global.ready_to_load || Global.SceneryFile.empty()) {
|
|
// the host has not picked a scenario yet, there is nothing to join
|
|
rejection = "the server has no scenario running yet";
|
|
}
|
|
|
|
if (!rejection.empty()) {
|
|
WriteLog("net: rejecting peer: " + rejection, logtype::net);
|
|
|
|
server_reject reply;
|
|
reply.reason = rejection;
|
|
conn->send_message(reply);
|
|
conn->disconnect();
|
|
return;
|
|
}
|
|
|
|
server_hello reply;
|
|
reply.seed = Global.random_seed;
|
|
reply.timestamp = Global.starting_timestamp;
|
|
reply.config = 0; // TODO: pass bitfield with state of relevant setting switches
|
|
reply.scenario = Global.SceneryFile;
|
|
reply.app_version = Global.asVersion;
|
|
reply.peer_id = allocate_peer_id();
|
|
conn->peer_id = reply.peer_id;
|
|
conn->state = connection::CATCHING_UP;
|
|
conn->backbuffer = backbuffer;
|
|
conn->backbuffer_pos = 0;
|
|
conn->packet_counter = cmd.start_packet;
|
|
|
|
conn->send_message(reply);
|
|
|
|
WriteLog("net: peer " + std::to_string(conn->peer_id) + " connected, build \"" + cmd.app_version
|
|
+ "\", scenario \"" + Global.SceneryFile + "\"", logtype::net);
|
|
}
|
|
else if (msg.type == message::REQUEST_COMMAND) {
|
|
const auto& cmd = dynamic_cast<const request_command&>(msg);
|
|
|
|
for (auto const &kv : cmd.commands)
|
|
client_commands_queue.emplace(kv);
|
|
}
|
|
}
|
|
|
|
// ------------
|
|
|
|
void network::client::update()
|
|
{
|
|
if (conn && conn->state == connection::DEAD) {
|
|
conn.reset();
|
|
}
|
|
|
|
if (!Global.network_reject_reason.empty()) {
|
|
// the server explicitly refused us; retrying would only spam it
|
|
return;
|
|
}
|
|
|
|
if (!conn) {
|
|
if (!reconnect_delay) {
|
|
connect();
|
|
reconnect_delay = RECONNECT_DELAY_FRAMES;
|
|
}
|
|
reconnect_delay--;
|
|
}
|
|
}
|
|
|
|
// client
|
|
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta(int counter)
|
|
{
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
if (counter == 1) {
|
|
frame_time = now - last_frame;
|
|
last_frame = now;
|
|
}
|
|
|
|
if (delta_queue.empty()) {
|
|
// buffer underflow
|
|
return std::tuple<double, double,
|
|
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
|
}
|
|
|
|
|
|
float size = delta_queue.size() - consume_counter;
|
|
const auto& entry = delta_queue.front();
|
|
float mult = entry.render_dt / std::chrono::duration_cast<std::chrono::duration<float>>(frame_time).count();
|
|
|
|
if (counter == 1 && size < MAX_BUFFER_SIZE * 2.0f) {
|
|
last_target = last_target * TARGET_MIX +
|
|
(std::min(TARGET_MIN + jitteriness * JITTERINESS_MULTIPIER, MAX_BUFFER_SIZE)) * (1.0f - TARGET_MIX);
|
|
float diff = size - last_target;
|
|
jitteriness = std::max(jitteriness * JITTERINESS_MIX, std::abs(diff));
|
|
|
|
float speed = 1.0f + diff * CONSUME_MULTIPIER;
|
|
|
|
consume_counter += speed;
|
|
}
|
|
|
|
float last_rcv_diff = std::chrono::duration_cast<std::chrono::duration<float>>(now - last_rcv).count();
|
|
|
|
if (size > MAX_BUFFER_SIZE || consume_counter > mult || last_rcv_diff > 1.0f) {
|
|
if (consume_counter > mult) {
|
|
consume_counter = std::clamp(consume_counter - mult, -MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
|
|
}
|
|
|
|
delta_queue.pop();
|
|
|
|
return std::make_tuple(entry.dt, entry.sync, entry.commands);
|
|
} else {
|
|
// nothing to push
|
|
return std::tuple<double, double,
|
|
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
|
}
|
|
}
|
|
|
|
void network::client::send_commands(command_queue::commands_map commands)
|
|
{
|
|
if (!conn || conn->state == connection::DEAD || commands.empty())
|
|
return;
|
|
// eh, maybe queue lost messages
|
|
|
|
request_command msg;
|
|
msg.commands = commands;
|
|
|
|
conn->send_message(msg);
|
|
}
|
|
|
|
void network::client::handle_message(std::shared_ptr<connection> conn, const message &msg)
|
|
{
|
|
if (msg.type >= message::TYPE_MAX)
|
|
{
|
|
conn->disconnect();
|
|
return;
|
|
}
|
|
|
|
if (msg.type == message::SERVER_REJECT) {
|
|
const auto& cmd = dynamic_cast<const server_reject&>(msg);
|
|
|
|
ErrorLog("net: connection refused by the server: " + cmd.reason, logtype::net);
|
|
Global.network_reject_reason = cmd.reason;
|
|
Global.network_status = "Connection refused: " + cmd.reason;
|
|
conn->disconnect();
|
|
return;
|
|
}
|
|
|
|
if (msg.type == message::SERVER_HELLO) {
|
|
const auto& cmd = dynamic_cast<const server_hello&>(msg);
|
|
conn->state = connection::ACTIVE;
|
|
|
|
if (!Global.ready_to_load) {
|
|
Global.random_seed = cmd.seed;
|
|
Global.random_engine.seed(Global.random_seed);
|
|
Global.starting_timestamp = cmd.timestamp;
|
|
// TODO: configure simulation settings according to received cmd.config
|
|
Global.SceneryFile = cmd.scenario;
|
|
Global.ready_to_load = true;
|
|
|
|
WriteLog("net: scenario handshake: \"" + cmd.scenario + "\", server build \"" + cmd.app_version + "\"", logtype::net);
|
|
} else if (Global.random_seed != cmd.seed) {
|
|
ErrorLog("net: seed mismatch", logtype::net);
|
|
conn->disconnect();
|
|
return;
|
|
}
|
|
|
|
if (cmd.peer_id != PEER_NONE) {
|
|
Global.network_peer_id = cmd.peer_id;
|
|
WriteLog("net: assigned peer id " + std::to_string(cmd.peer_id), logtype::net);
|
|
}
|
|
|
|
Global.network_status.clear();
|
|
|
|
WriteLog("net: accept received", logtype::net);
|
|
}
|
|
|
|
if (conn->state != connection::ACTIVE)
|
|
return;
|
|
|
|
if (msg.type == message::VEHICLE_LIST) {
|
|
const auto& cmd = dynamic_cast<const vehicle_list&>(msg);
|
|
Entities.adopt(cmd.vehicles);
|
|
}
|
|
|
|
if (msg.type == message::FRAME_INFO) {
|
|
resume_frame_counter++;
|
|
|
|
auto delta = dynamic_cast<const frame_info&>(msg);
|
|
delta_queue.push(delta);
|
|
last_rcv = std::chrono::high_resolution_clock::now();
|
|
}
|
|
}
|
|
|
|
// --------------
|