17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 19:09:25 +02:00
Files
maszyna/network/manager.h
Hirek193 0112d81b77 multiplayer: names instead of peer numbers, a chat, and the exe as a dedicated server
Three things that turn out to be one thing: a session with people in it needs
to be able to say who they are, and a machine that only referees one should not
have to open a window to do it.

Names. A peer id is what the protocol addresses; "player 4" is no use to anybody
reading a lobby. CLIENT_HELLO carries a requested nickname now, and the server
decides what it actually is: trimmed of control characters and runs of spaces,
capped at 24, and made unique - two people called Marcin are two people and the
lobby has to say which is which. The result goes out as PEER_ROSTER whenever
somebody joins or leaves, and to a joining peer just before its snapshot, so the
crew section resolves to names rather than numbers. The name comes from
multiplayer.nickname in the ini or --nick on the command line, falling back to
the account name and, failing that, to the old numbering. The host is a peer
like any other here too, and registers its own name at startup.

Chat, on the key left of 1. Going up a line carries only its text: who said it
is decided by the connection it arrived on, never by the field in the packet,
which is what stops anybody putting words in anybody's mouth. The server tidies
and caps it, says it, and passes it on - a client waits to hear itself back, so
that everybody sees the conversation in the same order. While the box has the
keyboard imgui swallows the input before the driver mode sees it, so typing "d"
types "d" instead of opening the doors of whatever you are driving; that also
means the key that opened the panel cannot close it, hence escape.

And --nogui. No glfw, no window, no imgui, the null renderer, sound off, log to
the console it was started from, and nobody in a cab - the scenario is left to
its own drivers and this process is a referee rather than a participant. Every
window-dependent path is guarded on an empty window list rather than on a flag
scattered through the code, and the loop is paced at fifty steps a second
because nothing is waiting on a swapchain to slow it down. Chat lines go to the
log, which is what a machine with no window has instead of a chat panel.

    eu07.exe -s scenario.scn --host 0.0.0.0:7420 --nogui --nick Dispatcher
2026-09-07 13:54:34 +00:00

86 lines
3.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();
// sends the peers what the world actually looks like. this is what keeps a client
// in line - the command stream alone cannot, because a client runs its own physics
void publish_state();
// 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);
// hands a line of chat to every peer
void broadcast_chat(PeerId author, const std::string &text);
// tells the peers who is taking part
void publish_roster();
private:
// how many frames may pass between two unconditional broadcasts of the roster
static const int PUBLISH_INTERVAL_FRAMES = 120;
// how often the authoritative state goes out, and how often it goes out whole
// rather than as what changed since the last one
// a horn can be pressed and let go inside a couple of frames, so the state does not
// wait long before it goes out
static const int STATE_INTERVAL_FRAMES = 6;
static const int STATE_FULL_EVERY = 60;
std::vector<vehicle_entry> last_published_list;
int publish_countdown = 0;
int crew_publish_countdown = 0;
int state_countdown = 0;
int state_updates = 0;
int heartbeat_countdown = 0;
// how many frames with nothing in them may pass before one is sent anyway
static const int HEARTBEAT_INTERVAL_FRAMES = 10;
};
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);
// something the local participant said. the host says it to everybody at once, a
// client hands it to the host to say on its behalf
void say(const std::string &text);
};
}