mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 15:39:20 +02: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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user