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

multiplayer stage 4: server-authoritative input, permissions, per-source holds

Three things stood in the way of two people sharing a cab and of a client not
being able to reach past its own vehicle.

Vehicle commands were addressed by TTrain::id(), which every peer hands out
locally from its own counter, so the same number means a different vehicle on
a different peer. Commands now travel with the session wide NetworkEntityId
and are translated back to the local cab on arrival; outside of multiplayer
the registry is empty and both directions are a no-op, so singleplayer is
untouched.

A client could send any command it liked, including setweather, queueevent or
spawntrainset. The server now validates every incoming command: a player may
work the controls of the vehicle whose crew they are on and nothing else, and
world wide commands stay with the host until dispatcher roles exist. The
source is stamped by the server, never taken from what the peer sent, and
rejections are logged once per peer/command instead of every frame. Host input
runs through the very same validation, just without the transport.

Continuous commands were tracked per command and recipient only, so with two
people in one cab the release sent by one of them cancelled the other one's
hold. command_data now carries the source peer and the active set is keyed by
{source, recipient, command}.

Also: a client asking to quit no longer replicates quitsimulation, which would
have shut down everybody else's session.
This commit is contained in:
2026-09-06 18:24:21 +00:00
parent 3f62fd5927
commit c3bf261934
7 changed files with 229 additions and 17 deletions

View File

@@ -445,6 +445,12 @@ struct command_data {
glm::vec3 location;
std::string payload;
// network::PeerId of the participant the command came from. kept as a plain integer
// so that this header stays independent of the network layer. it matters wherever two
// people work the same vehicle: a continuous command held by one of them must not be
// cancelled by the other one letting go of the same key
uint32_t source { 0 };
};
// command_queues: collects and holds commands from input sources, for processing by their intended recipients
@@ -487,15 +493,29 @@ private:
void push_direct( command_data const &Command, uint32_t const Recipient );
// hash operator for m_active_continuous
struct command_set_hash {
uint64_t operator() (const std::pair<user_command, uint32_t> &pair) const {
return (uint64_t)pair.first << 32 | (uint64_t)pair.second;
}
// a continuous command is held per source, not just per recipient: with two people in
// one cab, the release sent by one of them may only cancel their own hold
struct continuous_key {
uint32_t source;
uint32_t recipient;
user_command command;
bool operator==( continuous_key const &Other ) const {
return source == Other.source
&& recipient == Other.recipient
&& command == Other.command; }
};
struct continuous_key_hash {
std::size_t operator() ( continuous_key const &Key ) const {
std::size_t hash = (std::size_t)Key.source * 2654435761u;
hash ^= (std::size_t)Key.recipient + 0x9e3779b9 + ( hash << 6 ) + ( hash >> 2 );
hash ^= (std::size_t)Key.command + 0x9e3779b9 + ( hash << 6 ) + ( hash >> 2 );
return hash; }
};
// currently pressed continuous commands
std::unordered_set<std::pair<user_command, uint32_t>, command_set_hash> m_active_continuous;
std::unordered_set<continuous_key, continuous_key_hash> m_active_continuous;
};
template<typename A, typename B>