mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-18 04:29:18 +02:00
Joining meant replaying backbuffer.bin from the very first frame of the session, so the longer a session ran, the longer it took to get in - and the whole history had to be kept for it. - network/snapshot: a chunked container with a version of its own. Every section carries its length, so a peer built before a section existed reads a newer snapshot and simply walks past what it does not know. Version 1 carries the session (tick, clock, weather, the state of the random engine), the vehicles (position on the track, speed, direction, controls, brake pressures, whether the AI is driving) and the crew roster. Switches, memcells, signals and the event queue are the next sections - joining is now: handshake -> load the scenario -> CLIENT_READY -> the server takes a snapshot on the spot and sends it -> the peer goes live. Because the snapshot is taken at the moment the peer becomes active there is no backlog to replay at all; the frame by frame catch-up is gone - the server refuses a peer that loaded a different scenario, with a readable reason instead of a silent desync - reconnecting into a session that is already loaded asks for a fresh snapshot straight away rather than waiting for a loader that will not run again - a vehicle is only put back on the rails when it really is somewhere else; the arithmetic that turns the recorded bogie offset back into the nose distance place_on_track() expects mirrors what that function itself does - the session log is still written out - it is useful when picking a desync apart - but nothing depends on replaying it any more
43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
namespace network
|
|
{
|
|
|
|
// layout of the snapshot container. a reader refuses a blob it does not understand
|
|
constexpr uint32_t SNAPSHOT_VERSION = 1;
|
|
|
|
// the sections a snapshot is built from. a reader skips over any id it does not know,
|
|
// which is what makes the format extensible: a peer built before a section existed can
|
|
// still read a newer snapshot and simply ignores what it has no use for
|
|
enum snapshot_chunk : uint16_t
|
|
{
|
|
// tick, clock, weather and the state of the random engine
|
|
SNAPSHOT_SESSION = 1,
|
|
// where every vehicle is and what its controls and brakes are doing
|
|
SNAPSHOT_VEHICLES = 2,
|
|
// who is working which vehicle
|
|
SNAPSHOT_CREWS = 3
|
|
// candidates for the next versions: switches, memcells, the event queue, signals
|
|
};
|
|
|
|
// packs the current state of the world. only ever called on the authority
|
|
std::string take_snapshot();
|
|
|
|
// restores a snapshot handed to us by the authority. returns false when the blob cannot
|
|
// be used at all - a version we do not know, or damaged content
|
|
bool apply_snapshot(std::string const &Blob);
|
|
|
|
} // namespace network
|