mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 21:29:19 +02:00
Two things were still missing once a session ran for a while: a peer that had drifted apart from the authority had no way back short of restarting, and a connection that blinked cost the player their seat and handed their vehicle to the AI while the other driver was still sitting in it. - a client that disagrees with the authoritative digest for 120 steps in a row asks to be put back in line, and the server answers with a fresh snapshot. There is a cooldown between requests, so a correction that needs a moment to take hold does not turn into a stream of them - the server hands out a session token with its hello and remembers it. Present it again on the next connection and you come back as the same peer, with the same id - a lost connection no longer removes the peer from its crews: the seat is held for a minute. Come back in time and nothing changed; stay away and the seat is given up properly, handing the vehicle back to the AI only when that leaves the crew empty and the AI was driving it in the first place - after applying a snapshot a peer that finds itself on a crew walks into that cab, which is what makes a reconnect land the player back where they were - the vehicle roster is sent to a joining peer right before its snapshot, so the crew section resolves to names instead of waiting for the next broadcast
62 lines
2.0 KiB
C++
62 lines
2.0 KiB
C++
#pragma once
|
|
#include <memory>
|
|
#include "network/network.h"
|
|
#include "input/command.h"
|
|
|
|
namespace network
|
|
{
|
|
class server_manager
|
|
{
|
|
private:
|
|
std::vector<std::shared_ptr<server>> servers;
|
|
std::shared_ptr<std::fstream> backbuffer;
|
|
|
|
public:
|
|
server_manager();
|
|
|
|
void push_delta(double render_dt, double dt, uint64_t tick, uint64_t state_hash, const command_queue::commands_map &commands);
|
|
command_queue::commands_map pop_commands();
|
|
void create_server(const std::string &backend, const std::string &conf);
|
|
// refreshes the authoritative vehicle roster and publishes it when it changed
|
|
void publish_vehicle_list();
|
|
// brings the world in line with the crew roster and announces every change
|
|
void update_crews();
|
|
// crew request coming from the local participant, which needs no transport
|
|
void apply_local_claim(NetworkEntityId entity_id);
|
|
void apply_local_leave(NetworkEntityId entity_id);
|
|
|
|
private:
|
|
// how many frames may pass between two unconditional broadcasts of the roster
|
|
static const int PUBLISH_INTERVAL_FRAMES = 120;
|
|
|
|
std::vector<vehicle_entry> last_published_list;
|
|
int publish_countdown = 0;
|
|
int crew_publish_countdown = 0;
|
|
};
|
|
|
|
class manager
|
|
{
|
|
public:
|
|
manager();
|
|
|
|
std::optional<server_manager> servers;
|
|
std::shared_ptr<network::client> client;
|
|
|
|
void create_server(const std::string &backend, const std::string &conf);
|
|
void connect(const std::string &backend, const std::string &conf);
|
|
void update();
|
|
|
|
// routes a lobby request to whoever holds the authority: straight into the crew
|
|
// registry when we are the server, over the wire when we are a client
|
|
void request_claim(NetworkEntityId entity_id);
|
|
void request_leave(NetworkEntityId entity_id);
|
|
|
|
// the local scenario is loaded; a client asks the server for a snapshot of the
|
|
// world as it stands right now
|
|
void notify_scenario_loaded();
|
|
|
|
// our world drifted away from the authoritative one; ask to be corrected
|
|
void request_resync(uint64_t tick, uint64_t state_hash);
|
|
};
|
|
}
|