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

multiplayer stage 8: state correction and surviving a dropped connection

Two things were still missing once a session ran for a while: a peer that had
drifted apart from the authority had no way back short of restarting, and a
connection that blinked cost the player their seat and handed their vehicle to
the AI while the other driver was still sitting in it.

- a client that disagrees with the authoritative digest for 120 steps in a row
  asks to be put back in line, and the server answers with a fresh snapshot.
  There is a cooldown between requests, so a correction that needs a moment to
  take hold does not turn into a stream of them
- the server hands out a session token with its hello and remembers it. Present
  it again on the next connection and you come back as the same peer, with the
  same id
- a lost connection no longer removes the peer from its crews: the seat is held
  for a minute. Come back in time and nothing changed; stay away and the seat
  is given up properly, handing the vehicle back to the AI only when that
  leaves the crew empty and the AI was driving it in the first place
- after applying a snapshot a peer that finds itself on a crew walks into that
  cab, which is what makes a reconnect land the player back where they were
- the vehicle roster is sent to a joining peer right before its snapshot, so
  the crew section resolves to names instead of waiting for the next broadcast
This commit is contained in:
2026-09-06 19:48:06 +00:00
parent 54d8b112b8
commit 30fa68cfd8
11 changed files with 209 additions and 5 deletions

View File

@@ -23,6 +23,7 @@ struct message
CREW_UPDATE,
CLIENT_READY,
SNAPSHOT,
REQUEST_RESYNC,
TYPE_MAX
};
@@ -44,6 +45,10 @@ struct client_hello : public message
uint32_t start_packet;
// build identification of the connecting simulator, informational only
std::string app_version;
// identity handed out by this server on an earlier connection, echoed back so that a
// 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 };
};
// sent by the server instead of SERVER_HELLO when the client cannot join;
@@ -70,6 +75,8 @@ struct server_hello : public message
std::string app_version;
// identity assigned to the joining peer for the rest of the session
uint32_t peer_id{ PEER_NONE };
// hand this back on a later connection to be recognised as the same participant
uint64_t session_token{ 0 };
virtual void serialize(std::ostream &stream) const override;
virtual void deserialize(std::istream &stream) override;
@@ -111,6 +118,19 @@ struct snapshot : public message
virtual void deserialize(std::istream &stream) override;
};
// a peer telling the server its world has drifted too far to carry on, and asking to be
// put back in line
struct request_resync : public message
{
request_resync() : message(REQUEST_RESYNC) {}
uint64_t tick{ 0 };
uint64_t state_hash{ 0 };
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
{