mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 15:39:20 +02:00
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
368 lines
10 KiB
C++
368 lines
10 KiB
C++
#include "stdafx.h"
|
|
#include "network/message.h"
|
|
#include "scene/sn_utils.h"
|
|
|
|
void network::client_hello::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_int32(stream, version);
|
|
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)
|
|
{
|
|
version = sn_utils::ld_int32(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
|
|
{
|
|
sn_utils::s_str(stream, reason);
|
|
}
|
|
|
|
void network::server_reject::deserialize(std::istream &stream)
|
|
{
|
|
reason = sn_utils::d_str(stream);
|
|
}
|
|
|
|
void network::server_hello::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, seed);
|
|
sn_utils::ls_int64(stream, timestamp);
|
|
sn_utils::ls_int64(stream, config);
|
|
sn_utils::s_str(stream, scenario);
|
|
sn_utils::s_str(stream, app_version);
|
|
sn_utils::ls_uint32(stream, peer_id);
|
|
sn_utils::ls_uint64(stream, session_token);
|
|
sn_utils::ls_float64(stream, sync_running);
|
|
sn_utils::ls_float64(stream, sync_stop);
|
|
}
|
|
|
|
void network::server_hello::deserialize(std::istream &stream)
|
|
{
|
|
seed = sn_utils::ld_uint32(stream);
|
|
timestamp = sn_utils::ld_int64(stream);
|
|
config = sn_utils::ld_int64(stream);
|
|
scenario = sn_utils::d_str(stream);
|
|
app_version = sn_utils::d_str(stream);
|
|
peer_id = sn_utils::ld_uint32(stream);
|
|
session_token = sn_utils::ld_uint64(stream);
|
|
sync_running = sn_utils::ld_float64(stream);
|
|
sync_stop = sn_utils::ld_float64(stream);
|
|
}
|
|
|
|
void network::vehicle_list::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, (uint32_t)vehicles.size());
|
|
for (auto const &entry : vehicles)
|
|
{
|
|
sn_utils::ls_uint32(stream, entry.id);
|
|
sn_utils::s_str(stream, entry.name);
|
|
sn_utils::ls_uint32(stream, entry.consist_id);
|
|
sn_utils::s_uint8(stream, entry.consist_size);
|
|
sn_utils::s_uint8(stream, entry.crew_count);
|
|
sn_utils::s_uint8(stream, entry.crew_capacity);
|
|
sn_utils::s_uint8(stream, (uint8_t)((entry.ai_active ? 1 : 0) | (entry.claimable ? 2 : 0) | (entry.drivable ? 4 : 0) | (entry.consist_lead ? 8 : 0)));
|
|
}
|
|
}
|
|
|
|
void network::vehicle_list::deserialize(std::istream &stream)
|
|
{
|
|
vehicles.clear();
|
|
|
|
uint32_t const count = sn_utils::ld_uint32(stream);
|
|
vehicles.reserve(count);
|
|
|
|
for (uint32_t i = 0; i < count; i++)
|
|
{
|
|
vehicle_entry entry;
|
|
entry.id = sn_utils::ld_uint32(stream);
|
|
entry.name = sn_utils::d_str(stream);
|
|
entry.consist_id = sn_utils::ld_uint32(stream);
|
|
entry.consist_size = sn_utils::d_uint8(stream);
|
|
entry.crew_count = sn_utils::d_uint8(stream);
|
|
entry.crew_capacity = sn_utils::d_uint8(stream);
|
|
uint8_t const flags = sn_utils::d_uint8(stream);
|
|
entry.ai_active = (flags & 1) != 0;
|
|
entry.claimable = (flags & 2) != 0;
|
|
entry.drivable = (flags & 4) != 0;
|
|
entry.consist_lead = (flags & 8) != 0;
|
|
vehicles.emplace_back(entry);
|
|
}
|
|
}
|
|
|
|
void network::client_ready::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::s_str(stream, scenario);
|
|
}
|
|
|
|
void network::client_ready::deserialize(std::istream &stream)
|
|
{
|
|
scenario = sn_utils::d_str(stream);
|
|
}
|
|
|
|
void network::snapshot::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint64(stream, tick);
|
|
sn_utils::s_uint8(stream, mode);
|
|
sn_utils::ls_uint32(stream, (uint32_t)blob.size());
|
|
stream.write(blob.data(), blob.size());
|
|
}
|
|
|
|
void network::snapshot::deserialize(std::istream &stream)
|
|
{
|
|
tick = sn_utils::ld_uint64(stream);
|
|
mode = sn_utils::d_uint8(stream);
|
|
|
|
uint32_t const size = sn_utils::ld_uint32(stream);
|
|
blob.assign(size, '\0');
|
|
stream.read(blob.data(), size);
|
|
}
|
|
|
|
void network::request_resync::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint64(stream, tick);
|
|
sn_utils::ls_uint64(stream, state_hash);
|
|
}
|
|
|
|
void network::request_resync::deserialize(std::istream &stream)
|
|
{
|
|
tick = sn_utils::ld_uint64(stream);
|
|
state_hash = sn_utils::ld_uint64(stream);
|
|
}
|
|
|
|
void network::claim_vehicle::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, entity_id);
|
|
}
|
|
|
|
void network::claim_vehicle::deserialize(std::istream &stream)
|
|
{
|
|
entity_id = sn_utils::ld_uint32(stream);
|
|
}
|
|
|
|
void network::claim_granted::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, entity_id);
|
|
}
|
|
|
|
void network::claim_granted::deserialize(std::istream &stream)
|
|
{
|
|
entity_id = sn_utils::ld_uint32(stream);
|
|
}
|
|
|
|
void network::claim_denied::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, entity_id);
|
|
sn_utils::s_str(stream, reason);
|
|
}
|
|
|
|
void network::claim_denied::deserialize(std::istream &stream)
|
|
{
|
|
entity_id = sn_utils::ld_uint32(stream);
|
|
reason = sn_utils::d_str(stream);
|
|
}
|
|
|
|
void network::leave_vehicle::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, entity_id);
|
|
}
|
|
|
|
void network::leave_vehicle::deserialize(std::istream &stream)
|
|
{
|
|
entity_id = sn_utils::ld_uint32(stream);
|
|
}
|
|
|
|
void network::crew_update::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, entity_id);
|
|
sn_utils::ls_uint32(stream, (uint32_t)crew.size());
|
|
for (PeerId const peer : crew)
|
|
sn_utils::ls_uint32(stream, peer);
|
|
}
|
|
|
|
void network::crew_update::deserialize(std::istream &stream)
|
|
{
|
|
entity_id = sn_utils::ld_uint32(stream);
|
|
|
|
crew.clear();
|
|
uint32_t const count = sn_utils::ld_uint32(stream);
|
|
crew.reserve(count);
|
|
for (uint32_t i = 0; i < count; i++)
|
|
crew.emplace_back(sn_utils::ld_uint32(stream));
|
|
}
|
|
|
|
void ::network::request_command::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint64(stream, tick);
|
|
sn_utils::ls_uint32(stream, commands.size());
|
|
for (auto const &kv : commands)
|
|
{
|
|
sn_utils::ls_uint32(stream, kv.first);
|
|
sn_utils::ls_uint32(stream, kv.second.size());
|
|
for (command_data const &data : kv.second)
|
|
{
|
|
sn_utils::ls_uint32(stream, (uint32_t)data.command);
|
|
sn_utils::ls_int32(stream, data.action);
|
|
sn_utils::ls_float64(stream, data.param1);
|
|
sn_utils::ls_float64(stream, data.param2);
|
|
sn_utils::ls_float64(stream, data.time_delta);
|
|
|
|
sn_utils::s_bool(stream, data.freefly);
|
|
sn_utils::s_vec3(stream, data.location);
|
|
|
|
sn_utils::s_str(stream, data.payload);
|
|
sn_utils::ls_uint32(stream, data.source);
|
|
}
|
|
}
|
|
}
|
|
|
|
void network::request_command::deserialize(std::istream &stream)
|
|
{
|
|
tick = sn_utils::ld_uint64(stream);
|
|
|
|
uint32_t commands_size = sn_utils::ld_uint32(stream);
|
|
for (uint32_t i = 0; i < commands_size; i++)
|
|
{
|
|
uint32_t recipient = sn_utils::ld_uint32(stream);
|
|
uint32_t sequence_size = sn_utils::ld_uint32(stream);
|
|
|
|
command_queue::commanddata_sequence sequence;
|
|
for (uint32_t i = 0; i < sequence_size; i++)
|
|
{
|
|
command_data data;
|
|
data.command = (user_command)sn_utils::ld_uint32(stream);
|
|
data.action = sn_utils::ld_int32(stream);
|
|
data.param1 = sn_utils::ld_float64(stream);
|
|
data.param2 = sn_utils::ld_float64(stream);
|
|
data.time_delta = sn_utils::ld_float64(stream);
|
|
|
|
data.freefly = sn_utils::d_bool(stream);
|
|
data.location = sn_utils::d_vec3(stream);
|
|
|
|
data.payload = sn_utils::d_str(stream);
|
|
data.source = sn_utils::ld_uint32(stream);
|
|
|
|
sequence.emplace_back(data);
|
|
}
|
|
|
|
commands.emplace(recipient, sequence);
|
|
}
|
|
}
|
|
|
|
void network::frame_info::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_float64(stream, render_dt);
|
|
sn_utils::ls_float64(stream, dt);
|
|
sn_utils::ls_uint64(stream, state_hash);
|
|
sn_utils::ls_uint32(stream, state_hash_version);
|
|
|
|
request_command::serialize(stream);
|
|
}
|
|
|
|
void network::frame_info::deserialize(std::istream &stream)
|
|
{
|
|
render_dt = sn_utils::ld_float64(stream);
|
|
dt = sn_utils::ld_float64(stream);
|
|
state_hash = sn_utils::ld_uint64(stream);
|
|
state_hash_version = sn_utils::ld_uint32(stream);
|
|
|
|
request_command::deserialize(stream);
|
|
}
|
|
|
|
std::shared_ptr<network::message> network::deserialize_message(std::istream &stream)
|
|
{
|
|
message::type_e type = (message::type_e)sn_utils::ld_uint16(stream);
|
|
|
|
std::shared_ptr<message> msg;
|
|
|
|
if (type == message::CLIENT_HELLO)
|
|
msg = std::make_shared<client_hello>();
|
|
else if (type == message::SERVER_HELLO)
|
|
msg = std::make_shared<server_hello>();
|
|
else if (type == message::FRAME_INFO)
|
|
msg = std::make_shared<frame_info>();
|
|
else if (type == message::REQUEST_COMMAND)
|
|
msg = std::make_shared<request_command>();
|
|
else if (type == message::SERVER_REJECT)
|
|
msg = std::make_shared<server_reject>();
|
|
else if (type == message::VEHICLE_LIST)
|
|
msg = std::make_shared<vehicle_list>();
|
|
else if (type == message::CLAIM_VEHICLE)
|
|
msg = std::make_shared<claim_vehicle>();
|
|
else if (type == message::CLAIM_GRANTED)
|
|
msg = std::make_shared<claim_granted>();
|
|
else if (type == message::CLAIM_DENIED)
|
|
msg = std::make_shared<claim_denied>();
|
|
else if (type == message::LEAVE_VEHICLE)
|
|
msg = std::make_shared<leave_vehicle>();
|
|
else if (type == message::CREW_UPDATE)
|
|
msg = std::make_shared<crew_update>();
|
|
else if (type == message::CLIENT_READY)
|
|
msg = std::make_shared<client_ready>();
|
|
else if (type == message::SNAPSHOT)
|
|
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
|
|
return std::make_shared<message>(message::TYPE_MAX);
|
|
}
|
|
|
|
msg->deserialize(stream);
|
|
|
|
return msg;
|
|
}
|
|
|
|
void network::serialize_message(const message &msg, std::ostream &stream)
|
|
{
|
|
sn_utils::ls_uint16(stream, (uint16_t)msg.type);
|
|
msg.serialize(stream);
|
|
}
|