17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-30 13:10:29 +01:00

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
This commit is contained in:
2026-09-07 13:54:34 +00:00
parent a687e8f91f
commit 0112d81b77
22 changed files with 838 additions and 22 deletions

94
network/chat.cpp Normal file
View File

@@ -0,0 +1,94 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "network/chat.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
namespace
{
std::deque<network::chat_line> g_log;
uint64_t g_serial{0};
} // namespace
std::string network::tidy_chat(std::string const &Raw)
{
std::string text;
text.reserve(Raw.size());
bool space{false};
for (char const character : Raw)
{
unsigned char const value = (unsigned char)character;
// a newline would let one line pretend to be several, including one that looks
// like it came from somebody else
if (value < 0x20 || value == 0x7f)
continue;
if (character == ' ')
{
space = !text.empty();
continue;
}
if (space)
{
text += ' ';
space = false;
}
text += character;
if (text.size() >= CHAT_LENGTH_LIMIT)
break;
}
return text;
}
void network::note_chat(PeerId const Author, std::string const &Text)
{
auto const text = tidy_chat(Text);
if (text.empty())
return;
chat_line line;
line.author = Author;
line.name = Peers.name_of(Author);
line.text = text;
line.time = Global.fTimeAngleDeg;
g_log.emplace_back(line);
while (g_log.size() > CHAT_HISTORY)
g_log.pop_front();
++g_serial;
// on a machine running without a window this is the only place the conversation shows
WriteLog("chat: <" + line.name + "> " + line.text, logtype::net);
}
std::deque<network::chat_line> const &network::chat_log()
{
return g_log;
}
uint64_t network::chat_serial()
{
return g_serial;
}
void network::clear_chat()
{
g_log.clear();
g_serial = 0;
}

51
network/chat.h Normal file
View File

@@ -0,0 +1,51 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <deque>
#include <string>
#include "network/entities.h"
namespace network
{
// the longest thing anybody may say at once. a line, not an essay - and a cap the server
// applies to what it receives rather than trusting the sender to have applied it
constexpr size_t CHAT_LENGTH_LIMIT = 200;
// how much of the conversation is kept
constexpr size_t CHAT_HISTORY = 100;
struct chat_line
{
PeerId author{PEER_NONE};
// resolved when the line arrives: a peer may be gone by the time it is read back
std::string name;
std::string text;
// simulation clock, for the timestamp in front of the line
double time{0.0};
};
// puts a line in the log, whoever it came from. also writes it to the ordinary log, which
// is what a headless server has instead of a chat window
void note_chat(PeerId Author, std::string const &Text);
std::deque<chat_line> const &chat_log();
// how many lines have arrived since the session started. the panel watches this to know
// when to scroll, and the rest of the ui to know there is something new
uint64_t chat_serial();
void clear_chat();
// trims and caps a line, and says whether anything worth sending is left
std::string tidy_chat(std::string const &Raw);
} // namespace network

View File

@@ -22,6 +22,146 @@ namespace network
{
entity_registry Entities;
peer_registry Peers;
namespace
{
// long enough for a name worth having, short enough not to wreck a lobby column
constexpr size_t NICKNAME_LIMIT = 24;
std::string tidy_name(std::string const &Raw)
{
std::string name;
name.reserve(Raw.size());
bool space{false};
for (char const character : Raw)
{
// control characters and tabs have no business in a name that goes on somebody
// else's screen, and a run of spaces is one space
unsigned char const value = (unsigned char)character;
if (value < 0x20 || value == 0x7f)
continue;
if (character == ' ')
{
space = !name.empty();
continue;
}
if (space)
{
name += ' ';
space = false;
}
name += character;
if (name.size() >= NICKNAME_LIMIT)
break;
}
return name;
}
} // namespace
std::string local_nickname()
{
auto name = tidy_name(Global.multiplayer_nickname);
if (!name.empty())
return name;
// nothing configured: whoever is logged in will do, and failing that nothing at all -
// the registry has a stand-in for that case
for (char const *variable : {"EU07_NICKNAME", "USERNAME", "USER", "LOGNAME"})
{
char const *value = std::getenv(variable);
if (value == nullptr)
continue;
name = tidy_name(value);
if (!name.empty())
return name;
}
return std::string();
}
bool peer_registry::taken(std::string const &Name, PeerId const Except) const
{
for (auto const &entry : m_names)
{
if (entry.first == Except)
continue;
if (entry.second == Name)
return true;
}
return false;
}
std::string peer_registry::assign(PeerId const Peer, std::string const &Requested)
{
std::string name = tidy_name(Requested);
if (name.empty())
name = (Peer == PEER_HOST ? std::string("host") : "player " + std::to_string(Peer));
// two people called Marcin are two people, and the lobby has to say which is which
if (taken(name, Peer))
{
std::string const base = name;
for (int suffix = 2; suffix < 100; ++suffix)
{
name = base + " (" + std::to_string(suffix) + ")";
if (!taken(name, Peer))
break;
}
}
m_names[Peer] = name;
return name;
}
void peer_registry::forget(PeerId const Peer)
{
m_names.erase(Peer);
}
void peer_registry::adopt(std::vector<peer_entry> const &Roster)
{
m_names.clear();
for (auto const &entry : Roster)
m_names[entry.id] = entry.name;
}
std::string peer_registry::name_of(PeerId const Peer) const
{
auto const lookup = m_names.find(Peer);
if (lookup != m_names.end())
return lookup->second;
if (Peer == PEER_HOST)
return "host";
if (Peer == PEER_NONE)
return "nobody";
return "player " + std::to_string(Peer);
}
std::vector<peer_entry> peer_registry::roster() const
{
std::vector<peer_entry> entries;
entries.reserve(m_names.size());
for (auto const &entry : m_names)
entries.emplace_back(peer_entry{entry.first, entry.second});
return entries;
}
void peer_registry::clear()
{
m_names.clear();
}
PeerId allocate_peer_id()
{

View File

@@ -107,6 +107,40 @@ private:
// session wide vehicle registry. stays empty outside of multiplayer
extern entity_registry Entities;
// what a participant is called. a peer id is what the protocol addresses, but "player 4"
// is no use to anybody reading a lobby or a chat line
struct peer_entry
{
PeerId id{PEER_NONE};
std::string name;
};
class peer_registry
{
public:
// takes a name a peer asked for and makes it fit to show: trimmed, control characters
// out, capped in length, and made unique within the session
std::string assign(PeerId Peer, std::string const &Requested);
void forget(PeerId Peer);
// a client replaces its whole idea of who is in the session with what the server sent
void adopt(std::vector<peer_entry> const &Roster);
// the name, or a readable stand-in for a peer nobody has told us about yet
std::string name_of(PeerId Peer) const;
std::vector<peer_entry> roster() const;
void clear();
private:
bool taken(std::string const &Name, PeerId Except) const;
std::unordered_map<PeerId, std::string> m_names;
};
// who is in the session. the host keeps the authoritative copy and hands it to everybody
extern peer_registry Peers;
// the name this peer would like to be known by, cleaned up but not yet made unique
std::string local_nickname();
// hands out ids for connecting peers; PEER_HOST is reserved for the local participant
PeerId allocate_peer_id();

View File

@@ -14,6 +14,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Globals.h"
#include "network/statehash.h"
#include "network/snapshot.h"
#include "network/chat.h"
network::server_manager::server_manager()
{
@@ -191,6 +192,44 @@ void network::server_manager::publish_state()
srv->push_message(msg);
}
void network::server_manager::broadcast_chat(PeerId const author, const std::string &text)
{
chat_message msg;
msg.peer = author;
msg.text = text;
for (auto srv : servers)
srv->push_message(msg);
}
void network::server_manager::publish_roster()
{
for (auto srv : servers)
srv->publish_roster();
}
void network::manager::say(const std::string &text)
{
auto const line = tidy_chat(text);
if (line.empty())
return;
if (servers)
{
// the host is the one who decides what was said, so it says it and passes it on
note_chat(Global.network_peer_id, line);
servers->broadcast_chat(Global.network_peer_id, line);
return;
}
if (client)
{
// a client waits to hear itself back from the server, so that everybody sees the
// conversation in the same order
client->send_chat(line);
}
}
void network::manager::request_resync(uint64_t tick, uint64_t state_hash)
{
if (client)

View File

@@ -27,6 +27,10 @@ namespace network
// 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
@@ -73,5 +77,9 @@ namespace network
// 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);
};
}

View File

@@ -8,6 +8,7 @@ void network::client_hello::serialize(std::ostream &stream) const
sn_utils::ls_uint32(stream, start_packet);
sn_utils::s_str(stream, app_version);
sn_utils::ls_uint64(stream, session_token);
sn_utils::s_str(stream, nickname);
}
void network::client_hello::deserialize(std::istream &stream)
@@ -16,6 +17,43 @@ void network::client_hello::deserialize(std::istream &stream)
start_packet = sn_utils::ld_uint32(stream);
app_version = sn_utils::d_str(stream);
session_token = sn_utils::ld_uint64(stream);
nickname = sn_utils::d_str(stream);
}
void network::peer_roster::serialize(std::ostream &stream) const
{
sn_utils::ls_uint32(stream, (uint32_t)peers.size());
for (auto const &entry : peers)
{
sn_utils::ls_uint32(stream, entry.id);
sn_utils::s_str(stream, entry.name);
}
}
void network::peer_roster::deserialize(std::istream &stream)
{
auto const count = sn_utils::ld_uint32(stream);
peers.clear();
peers.reserve(count);
for (uint32_t i = 0; i < count; ++i)
{
peer_entry entry;
entry.id = sn_utils::ld_uint32(stream);
entry.name = sn_utils::d_str(stream);
peers.emplace_back(std::move(entry));
}
}
void network::chat_message::serialize(std::ostream &stream) const
{
sn_utils::ls_uint32(stream, peer);
sn_utils::s_str(stream, text);
}
void network::chat_message::deserialize(std::istream &stream)
{
peer = sn_utils::ld_uint32(stream);
text = sn_utils::d_str(stream);
}
void network::server_reject::serialize(std::ostream &stream) const
@@ -307,6 +345,10 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
msg = std::make_shared<snapshot>();
else if (type == message::REQUEST_RESYNC)
msg = std::make_shared<request_resync>();
else if (type == message::PEER_ROSTER)
msg = std::make_shared<peer_roster>();
else if (type == message::CHAT)
msg = std::make_shared<chat_message>();
if (!msg) {
// unknown message type; hand back a marker the peer handlers treat as a protocol error

View File

@@ -24,6 +24,8 @@ struct message
CLIENT_READY,
SNAPSHOT,
REQUEST_RESYNC,
PEER_ROSTER,
CHAT,
TYPE_MAX
};
@@ -49,6 +51,9 @@ struct client_hello : public message
// player who dropped out gets their seat and their peer id back. it is a reconnect
// token, not a credential - it says "this is the same session", nothing more
uint64_t session_token{ 0 };
// what the player would like to be called. a request, not a fact: the server tidies
// it up, makes it unique and hands back what it settled on
std::string nickname;
};
// sent by the server instead of SERVER_HELLO when the client cannot join;
@@ -140,6 +145,31 @@ struct request_resync : public message
virtual void deserialize(std::istream &stream) override;
};
// who is taking part, and what each of them is called. rebroadcast whenever somebody
// joins or leaves, so that no peer has to guess at a name
struct peer_roster : public message
{
peer_roster() : message(PEER_ROSTER) {}
std::vector<peer_entry> peers;
virtual void serialize(std::ostream &stream) const override;
virtual void deserialize(std::istream &stream) override;
};
// something a participant said. going up it carries only the text - who said it is
// decided by the connection it arrived on, never by what the sender put in the field
struct chat_message : public message
{
chat_message() : message(CHAT) {}
uint32_t peer{ PEER_NONE };
std::string text;
virtual void serialize(std::ostream &stream) const override;
virtual void deserialize(std::istream &stream) override;
};
// a peer asking the server to be put on the crew of a vehicle
struct claim_vehicle : public message
{

View File

@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "network/network.h"
#include "network/snapshot.h"
#include "network/chat.h"
#include "network/message.h"
#include "utilities/Logs.h"
#include "scene/sn_utils.h"
@@ -81,6 +82,7 @@ void network::connection::connected()
msg.start_packet = packet_counter;
msg.app_version = Global.asVersion;
msg.session_token = Global.network_session_token;
msg.nickname = local_nickname();
send_message(msg);
}
}
@@ -117,20 +119,35 @@ void report_rejected_command(network::PeerId Peer, user_command Command, network
void network::server::prune_clients()
{
bool dropped{false};
for (auto it = clients.begin(); it != clients.end(); ) {
if ((*it)->state == connection::DEAD) {
if ((*it)->peer_id != PEER_NONE) {
WriteLog("net: peer " + std::to_string((*it)->peer_id) + " disconnected", logtype::net);
WriteLog("net: " + Peers.name_of((*it)->peer_id) + " (peer " + std::to_string((*it)->peer_id) + ") disconnected", logtype::net);
// the seat is held for a while in case the peer comes straight back. a
// vehicle keeps running for whoever is left on board either way; it only
// falls back to the AI once the crew really is empty
Crews.suspend_peer((*it)->peer_id);
Peers.forget((*it)->peer_id);
dropped = true;
}
it = clients.erase(it);
continue;
}
it++;
}
if (dropped) {
// whoever is left learns who is left. sent from here rather than through
// push_message, which is what called us
peer_roster msg;
msg.peers = Peers.roster();
for (auto const &client : clients) {
if (client->state == connection::ACTIVE)
client->send_message(msg);
}
}
}
void network::server::push_delta(const frame_info &msg)
@@ -153,6 +170,13 @@ void network::server::push_message(const message &msg)
}
}
void network::server::publish_roster()
{
peer_roster msg;
msg.peers = Peers.roster();
push_message(msg);
}
command_queue::commands_map network::server::pop_commands()
{
command_queue::commands_map map(client_commands_queue);
@@ -203,6 +227,8 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
reply.session_token = cmd.session_token;
reply.peer_id = resolve_peer_identity(reply.session_token);
conn->peer_id = reply.peer_id;
// what the player asked to be called, tidied up and made unique within the session
auto const name = Peers.assign(conn->peer_id, cmd.nickname);
// the peer now goes and loads the scenario. it gets the state of the world as a
// snapshot when it reports back, instead of replaying the session frame by frame
conn->state = connection::AWAITING_READY;
@@ -210,7 +236,7 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
conn->send_message(reply);
WriteLog("net: peer " + std::to_string(conn->peer_id) + " connected, build \"" + cmd.app_version
WriteLog("net: peer " + std::to_string(conn->peer_id) + " connected as \"" + name + "\", build \"" + cmd.app_version
+ "\", scenario \"" + Global.SceneryFile + "\"", logtype::net);
}
else if (msg.type == message::CLIENT_READY) {
@@ -227,6 +253,11 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
return;
}
// who is taking part, so that the crew section of the snapshot has names to show
peer_roster people;
people.peers = Peers.roster();
conn->send_message(people);
// the roster first, so that the crew section of the snapshot resolves to names
vehicle_list roster;
roster.vehicles = Entities.entries();
@@ -241,12 +272,31 @@ void network::server::handle_message(std::shared_ptr<connection> conn, const mes
// from here on the peer receives the live stream; there is no backlog to catch up on
conn->state = connection::ACTIVE;
// and everybody already in the session learns who just walked in
publish_roster();
WriteLog("net: snapshot sent to peer " + std::to_string(conn->peer_id) + " at tick "
+ std::to_string(reply.tick), logtype::net);
// and it still has to learn who is where
Crews.mark_all_pending();
}
else if (msg.type == message::CHAT) {
const auto& cmd = dynamic_cast<const chat_message&>(msg);
// the sender is the connection this arrived on, never the field in the packet:
// otherwise anybody could put words in anybody's mouth
auto const text = tidy_chat(cmd.text);
if (text.empty())
return;
chat_message relay;
relay.peer = conn->peer_id;
relay.text = text;
note_chat(relay.peer, relay.text);
push_message(relay);
}
else if (msg.type == message::SNAPSHOT) {
const auto& cmd = dynamic_cast<const snapshot&>(msg);
@@ -474,6 +524,19 @@ void network::client::send_leave(NetworkEntityId entity_id)
conn->send_message(msg);
}
void network::client::send_chat(const std::string &text)
{
if (!conn || conn->state != connection::ACTIVE)
return;
chat_message msg;
// the peer field is filled in by the server from the connection; whatever we put here
// is ignored, which is the point
msg.peer = Global.network_peer_id;
msg.text = text;
conn->send_message(msg);
}
void network::client::handle_message(std::shared_ptr<connection> conn, const message &msg)
{
if (msg.type >= message::TYPE_MAX)

View File

@@ -101,6 +101,8 @@ namespace network
// sends an out of band message to every active peer. unlike push_delta this is not
// written to the backbuffer, so it does not become part of the replayed history
void push_message(const message &msg);
// tells every peer who is taking part and what each of them is called
void publish_roster();
command_queue::commands_map pop_commands();
};
@@ -165,6 +167,7 @@ namespace network
// lobby requests; the server is the one that decides
void send_claim(NetworkEntityId entity_id);
void send_leave(NetworkEntityId entity_id);
void send_chat(const std::string &text);
int get_frame_counter() {
return resume_frame_counter;
}