vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
snapshot.h
1#pragma once
2
3#include <cstdint>
4#include <unordered_map>
5#include <vector>
6
7#include "ecs/entity.h"
8#include "net/transport/connection.h"
9#include "net/wire/schema.h"
10
11namespace Vkm::Engine {
12
13class NetSilenceMap;
14class Scene;
15
28class NetBaseline {
29 public:
31 struct Confirmed {
32 std::vector<uint8_t> bytes;
33 uint16_t sequence = 0;
34 bool held = false;
35 };
36
38 struct Pending {
39 struct Change {
40 EntityId entity;
41 uint32_t slot = 0;
42 uint32_t type = 0;
43 std::vector<uint8_t> bytes;
44 };
45
46 uint16_t sequence = 0;
47 std::vector<Change> changes;
48 std::vector<EntityId> forgotten;
49 };
50
51 public:
52 NetBaseline() = default;
53 ~NetBaseline() = default;
54
55 NetBaseline(const NetBaseline& other) = delete;
56 NetBaseline& operator=(const NetBaseline& other) = delete;
57
58 NetBaseline(NetBaseline && other) = delete;
59 NetBaseline& operator=(NetBaseline && other) = delete;
60
61 public:
72 const Confirmed* find(EntityId entity, uint32_t type) const;
73
75 void hold(Pending && pending);
76
85 void confirm(uint16_t sequence);
86
95 void expire(uint16_t sequence);
96
106 void gatherLost(const Scene& scene, std::vector<EntityId>& out) const;
107
114 void clear() {
115 m_entities.clear();
116 m_pending.clear();
117 }
118
119 size_t knownEntities() const { return m_entities.size(); }
120 size_t pendingSnapshots() const { return m_pending.size(); }
121
122 private:
123 struct EntityRecord {
124 EntityId entity;
125 std::vector<Confirmed> components;
126 };
127
128 private:
129 std::unordered_map<uint32_t, EntityRecord> m_entities;
130 std::vector<Pending> m_pending;
131};
132
139struct NetBudget {
145 uint32_t bytes = static_cast<uint32_t>(NetConnection::MAX_PAYLOAD);
146
153};
154
162 uint32_t entitiesConsidered = 0;
163 uint32_t entitiesWritten = 0;
164 uint32_t componentsWritten = 0;
165 uint32_t entitiesDeferred = 0;
166 uint32_t entitiesLost = 0;
167 uint32_t bytesWritten = 0;
168};
169
187NetSnapshotStats writeSnapshot(
188 const Scene& scene,
189 const NetSchema& schema,
190 const NetSilenceMap& silence,
191 uint16_t sequence,
192 const NetBudget& budget,
193 NetBaseline& baseline,
194 BitWriter& out
195);
196
211bool readSnapshot(
212 Scene& scene,
213 const NetSchema& schema,
214 BitReader& in,
215 std::vector<uint32_t>* touched = nullptr
216);
217
218} // namespace Vkm::Engine
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
Packs values to the bit into a byte buffer.
Definition bit_stream.h:19
What one connection is known to hold, so the next snapshot says less.
Definition snapshot.h:28
const Confirmed * find(EntityId entity, uint32_t type) const
What is confirmed for entity's component type, or null.
void confirm(uint16_t sequence)
Fold the snapshot sequence said into what is confirmed.
void expire(uint16_t sequence)
Give up on everything older than sequence by more than the window an acknowledgement can still arrive...
void clear()
Forget every entity and every unconfirmed snapshot.
Definition snapshot.h:114
void gatherLost(const Scene &scene, std::vector< EntityId > &out) const
Entities this connection was told about that are no longer alive.
void hold(Pending &&pending)
Hold what a snapshot said until it is confirmed or given up on.
static constexpr size_t MAX_PAYLOAD
The most one packet carries past its header: what a datagram leaves.
Definition connection.h:43
The ordered list of what replicates, agreed by both ends before play.
Definition schema.h:39
netSilence for every entity of a world, classified in one pass.
Definition silence.h:64
Central registry managing entities and an open set of component types.
Definition scene.h:29
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
One component of one entity, as this connection last confirmed it.
Definition snapshot.h:31
uint16_t sequence
Which snapshot established it.
Definition snapshot.h:33
What a written snapshot claimed, held until it is acknowledged.
Definition snapshot.h:38
std::vector< EntityId > forgotten
Occupants reported as gone.
Definition snapshot.h:48
How much of the world one snapshot may describe.
Definition snapshot.h:139
EntityId owner
The connection's own entity, which is never the one deferred.
Definition snapshot.h:152
uint32_t bytes
Bytes one snapshot body may occupy.
Definition snapshot.h:145
What one snapshot said, for the session's status line.
Definition snapshot.h:161
uint32_t entitiesWritten
Of those, the ones that differed from the baseline.
Definition snapshot.h:163
uint32_t bytesWritten
The body's size, headers excluded.
Definition snapshot.h:167
uint32_t entitiesLost
Announced as gone: the receiver holds them, the scene does not.
Definition snapshot.h:166
uint32_t componentsWritten
Component records inside them.
Definition snapshot.h:164
uint32_t entitiesDeferred
Left out for want of budget; first in line next snapshot.
Definition snapshot.h:165
uint32_t entitiesConsidered
Entities in the world walked.
Definition snapshot.h:162