17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 14:29:20 +02: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

@@ -184,6 +184,73 @@ bool crew_registry::leave(PeerId Peer, NetworkEntityId Id)
return true;
}
PeerId resolve_peer_identity(uint64_t &Token)
{
static std::unordered_map<uint64_t, PeerId> known;
if (Token != 0)
{
auto const lookup = known.find(Token);
if (lookup != known.end())
{
WriteLog("net: peer " + std::to_string(lookup->second) + " recognised, reconnecting", logtype::net);
Crews.resume_peer(lookup->second);
return lookup->second;
}
}
auto const peer = allocate_peer_id();
// only has to be hard to collide with, not hard to guess: it decides who is who
// across a dropped connection, nothing more
Token = ((uint64_t)Global.local_random_engine() << 32) ^ (uint64_t)Global.local_random_engine() ^ ((uint64_t)peer << 8);
if (Token == 0)
Token = peer;
known.emplace(Token, peer);
return peer;
}
void crew_registry::suspend_peer(PeerId Peer)
{
if (vehicle_of(Peer) == ENTITY_NONE)
return;
m_absent[Peer] = Timer::GetTime() + RECONNECT_GRACE_SECONDS;
WriteLog("net: peer " + std::to_string(Peer) + " lost its connection, holding its seat for " + std::to_string((int)RECONNECT_GRACE_SECONDS) + " s", logtype::net);
}
void crew_registry::resume_peer(PeerId Peer)
{
m_absent.erase(Peer);
}
bool crew_registry::is_absent(PeerId Peer) const
{
return m_absent.find(Peer) != m_absent.end();
}
void crew_registry::expire_absences()
{
auto const now = Timer::GetTime();
for (auto it = m_absent.begin(); it != m_absent.end();)
{
if (now < it->second)
{
++it;
continue;
}
WriteLog("net: peer " + std::to_string(it->first) + " did not come back, giving up its seat", logtype::net);
auto const peer = it->first;
it = m_absent.erase(it);
drop_peer(peer);
}
}
void crew_registry::drop_peer(PeerId Peer)
{
for (auto &pair : m_vehicles)