vkmEngine 1.0.15
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 NetNames;
14class NetSilenceMap;
15class Scene;
16
30class NetBaseline {
31 public:
33 struct Confirmed {
34 std::vector<uint8_t> bytes;
35 uint16_t sequence = 0;
36 bool held = false;
37 bool folded = false;
38 };
39
41 struct Pending {
42 struct Change {
43 EntityId entity;
44 uint32_t slot = 0;
45 uint32_t type = 0;
46 std::vector<uint8_t> bytes;
47 bool removed = false;
48 };
49
50 uint16_t sequence = 0;
51 std::vector<Change> changes;
52 std::vector<EntityId> forgotten;
53 };
54
55 public:
56 NetBaseline() = default;
57 ~NetBaseline() = default;
58
59 NetBaseline(const NetBaseline& other) = delete;
60 NetBaseline& operator=(const NetBaseline& other) = delete;
61
62 NetBaseline(NetBaseline && other) = delete;
63 NetBaseline& operator=(NetBaseline && other) = delete;
64
65 public:
76 const Confirmed* find(EntityId entity, uint32_t type) const;
77
79 void hold(Pending && pending);
80
89 void confirm(uint16_t sequence);
90
99 void expire(uint16_t sequence);
100
110 void gatherLost(const Scene& scene, std::vector<EntityId>& out) const;
111
118 void clear() {
119 m_entities.clear();
120 m_pending.clear();
121 }
122
123 size_t knownEntities() const { return m_entities.size(); }
124 size_t pendingSnapshots() const { return m_pending.size(); }
125
126 private:
127 struct EntityRecord {
128 EntityId entity;
129 std::vector<Confirmed> components;
130 };
131
132 private:
133 std::unordered_map<uint32_t, EntityRecord> m_entities;
134 std::vector<Pending> m_pending;
135};
136
143struct NetBudget {
149 uint32_t bytes = static_cast<uint32_t>(NetConnection::MAX_PAYLOAD);
150
157};
158
166 uint32_t entitiesConsidered = 0;
167 uint32_t entitiesWritten = 0;
168 uint32_t componentsWritten = 0;
169 uint32_t entitiesDeferred = 0;
170 uint32_t entitiesLost = 0;
171 uint32_t bytesWritten = 0;
172};
173
191NetSnapshotStats writeSnapshot(
192 const Scene& scene,
193 const NetSchema& schema,
194 const NetSilenceMap& silence,
195 uint16_t sequence,
196 const NetBudget& budget,
197 NetBaseline& baseline,
198 BitWriter& out
199);
200
216bool readSnapshot(
217 Scene& scene,
218 const NetSchema& schema,
219 BitReader& in,
220 NetNames& names,
221 std::vector<EntityId>* touched = nullptr,
222 std::vector<EntityId>* lost = nullptr
223);
224
225} // 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:30
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:118
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
What the server's slots name on this end, so a client can make entities of its own.
Definition names.h:23
The ordered list of what replicates, agreed by both ends before play.
Definition schema.h:84
netSilence for every entity of a world, classified in one pass.
Definition silence.h:65
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:33
bool folded
A change, a removal included, is confirmed.
Definition snapshot.h:37
bool held
The receiver holds the component.
Definition snapshot.h:36
uint16_t sequence
Which snapshot established it.
Definition snapshot.h:35
What a written snapshot claimed, held until it is acknowledged.
Definition snapshot.h:41
std::vector< EntityId > forgotten
Occupants reported as gone.
Definition snapshot.h:52
How much of the world one snapshot may describe.
Definition snapshot.h:143
EntityId owner
The connection's own entity, which is never the one deferred.
Definition snapshot.h:156
uint32_t bytes
Bytes one snapshot body may occupy.
Definition snapshot.h:149
What one snapshot said, for the session's status line.
Definition snapshot.h:165
uint32_t entitiesWritten
Of those, the ones that differed from the baseline.
Definition snapshot.h:167
uint32_t bytesWritten
The body's size, headers excluded.
Definition snapshot.h:171
uint32_t entitiesLost
Announced as gone: the receiver holds them, the scene does not.
Definition snapshot.h:170
uint32_t componentsWritten
Component records inside them.
Definition snapshot.h:168
uint32_t entitiesDeferred
Left out for want of budget; first in line next snapshot.
Definition snapshot.h:169
uint32_t entitiesConsidered
Entities in the world walked.
Definition snapshot.h:166