vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
net_client.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <memory>
6#include <string>
7#include <vector>
8
9#include <glm/glm.hpp>
10
11#include "ecs/component/core/transform.h"
12#include "ecs/component/physics/rigidbody.h"
13#include "ecs/entity.h"
14#include "net/prediction/command.h"
15#include "net/prediction/interpolation.h"
16#include "net/prediction/pacing.h"
17#include "net/replication/names.h"
18#include "net/transport/connection.h"
19#include "net/transport/messages.h"
20#include "net/wire/bit_stream.h"
21#include "net/wire/protocol.h"
22
23namespace Vkm::Engine {
24
25class Scene;
26class ResourceManager;
27struct NetCore;
28struct NetSpawn;
29
36constexpr uint32_t NET_SPAWN_BUILDS_PER_FRAME = 8;
37
44class NetClient {
45 public:
46 explicit NetClient(NetCore& core);
47 ~NetClient();
48
49 NetClient(const NetClient& other) = delete;
50 NetClient& operator=(const NetClient& other) = delete;
51
52 NetClient(NetClient && other) = delete;
53 NetClient& operator=(NetClient && other) = delete;
54
55 public:
57 void open(const NetAddress& server);
58
60 void close();
61
63 void endSession() { m_names.endSession(); }
64
65 void advance(float seconds);
66
75 bool receive(Scene& scene, ResourceManager& resources);
76
78 void send();
79
81 bool simulates(const Scene& scene, EntityId entity) const;
82
84 void lease(const std::vector<EntityId>& entities);
85
87 void ageLeases();
88
90 void beginTick(const InputCommand& command);
91
93 void endTick(const Scene& scene, uint32_t tick);
94
95 void beginReplayTick() { m_prediction.replaying = true; }
96
98 void endReplay(const Scene& scene);
99
100 void interpolate(Scene& scene, float deltaTime);
101
103 void applyDrawCorrection(Scene& scene, float deltaTime);
104
107
110
112 EntityId entityFor(const Scene& scene, uint32_t slot) const { return m_names.find(scene, slot); }
113
115 void adoptWorld(const Scene& scene) { m_names.adoptWorld(scene); }
116
118 void sweepEnded(Scene& scene) { m_names.sweepEnded(scene); }
119
121 bool sendMessage(const uint8_t* bytes, size_t size);
122
123 std::string describe() const;
124
125 public:
126 PlayerId localPlayer() const { return m_localPlayer; }
127 EntityId localEntity() const { return m_localEntity; }
128
130 bool isPlaying() const { return m_localPlayer != NO_PLAYER; }
131
132 float roundTrip() const { return m_connection ? m_connection->roundTrip() : 0.0f; }
133
134 size_t leaseCount() const { return m_prediction.leases.size(); }
135
136 const std::vector<InputCommand>& replayCommands() const { return m_prediction.replay; }
137
138 bool replaying() const { return m_prediction.replaying; }
139
140 float predictionError() const { return m_prediction.error; }
141
142 float renderDelay() const { return m_interpolation.behindTicks(); }
143
145 bool drawingServerTicks() const { return m_interpolation.started(); }
146
147 double drawnTick() const { return m_interpolation.renderTick(); }
148
149 float pacing() const { return m_prediction.pacing.scale(); }
150
151 private:
155 struct Predicted {
156 uint32_t tick = 0;
157 glm::vec3 position{0.0f};
158 };
159
164 struct Lease {
165 EntityId entity;
166 uint32_t ticks = 0;
167 };
168
175 struct DrawCorrection {
176 EntityId entity;
177 glm::vec3 offset;
178 };
179
187 struct Held {
188 EntityId entity;
189 Transform transform;
190 Rigidbody body;
191 bool hasBody = false;
192 };
193
199 struct Prediction {
200 std::vector<Predicted> predicted;
201 float error = 0.0f;
202 std::vector<InputCommand> unacknowledged;
203 std::vector<InputCommand> replay;
204 bool replaying = false;
205
206 uint32_t heard = 0;
207 uint32_t sent = 0;
208
210 NetPacing pacing;
211
216 std::vector<Lease> leases;
217 std::vector<EntityId> acquired;
218 std::vector<EntityId> released;
219
221 std::vector<Held> held;
222 std::vector<DrawCorrection> corrections;
223
224 bool warnedNoReplay = false;
225
226 void clear() { *this = Prediction{}; }
227 };
228
233 struct DrawnFrom {
234 EntityId entity;
235 glm::vec3 drawn{0.0f};
236 };
237
238 private:
239 bool onWelcome(Scene& scene, BitReader& reader);
240 bool onRefuse(BitReader& reader);
241 bool onGoodbye();
242 void onSnapshot(Scene& scene, BitReader& reader);
243
254 void holdPredicted(Scene& scene, std::vector<DrawnFrom>& takenFrom);
255
266 void buildSpawned(Scene& scene, ResourceManager& resources);
267
279 void buildInto(Scene& scene, ResourceManager& resources, EntityId root, const NetSpawn& spawn);
280
290 void reconcile(Scene& scene, uint32_t confirmedTick);
291
301 void restoreHeld(Scene& scene);
302 void correctDraw(EntityId entity, const glm::vec3& drawn, const glm::vec3& simulated);
303 void offsetDrawn(Scene& scene, EntityId entity, const glm::vec3& by);
304 glm::vec3 drawOffsetFor(EntityId entity) const;
305
312 bool adoptServerTick(uint32_t serverTick);
313
314 private:
315 NetCore& m_core;
316
317 std::unique_ptr<NetConnection> m_connection;
318
319 PlayerId m_localPlayer = NO_PLAYER;
320 EntityId m_localEntity;
321 uint64_t m_token = 0;
322
323 bool m_warnedTickJump = false;
324
326 Prediction m_prediction;
327
328 NetInterpolation m_interpolation;
329 std::vector<EntityId> m_touched;
330 std::vector<EntityId> m_lost;
331 NetNames m_names;
332 NetOutbox m_outbox;
333};
334
335} // namespace Vkm::Engine
bool drawingServerTicks() const
Whether a snapshot has arrived, so drawnTick() is the server's.
Definition net_client.h:145
bool isPlaying() const
From the Welcome until the session ends.
Definition net_client.h:130
void ageLeases()
Count every lease down a tick, outside a replay, and hand back the lapsed.
void close()
Tell the server goodbye and forget everything this session held but what it made.
void adoptWorld(const Scene &scene)
The world as loaded; see NetSession::setWorld.
Definition net_client.h:115
void sweepEnded(Scene &scene)
Destroy what the session last closed had made (NetNames::sweepEnded).
Definition net_client.h:118
void endReplay(const Scene &scene)
Finish the replay and measure what the picture owes for it.
void endTick(const Scene &scene, uint32_t tick)
Record where this end's own entity ended tick.
void removeDrawCorrection(Scene &scene)
Take back what applyDrawCorrection applied.
void beginTick(const InputCommand &command)
Keep command until the server says it ran it.
void endSession()
Mark what this session made to go at the next sweepEnded: the session is over.
Definition net_client.h:63
void forgetWorld()
Drop everything naming an entity; see NetSession::forgetWorld.
bool simulates(const Scene &scene, EntityId entity) const
See NetSession::simulates.
bool receive(Scene &scene, ResourceManager &resources)
Take what has arrived, one datagram at a time, then build what was spawned.
void open(const NetAddress &server)
Start talking to server. Nothing is sent until the first tick has run.
bool sendMessage(const uint8_t *bytes, size_t size)
See NetSession::sendMessage.
void send()
Send a Hello until welcomed, then the commands the server has not heard.
void applyDrawCorrection(Scene &scene, float deltaTime)
Work a little of every outstanding correction off, and apply the rest.
EntityId entityFor(const Scene &scene, uint32_t slot) const
See NetSession::entityFor.
Definition net_client.h:112
void lease(const std::vector< EntityId > &entities)
See NetSession::lease.
double renderTick() const
Where the clock that decides what is drawn currently stands, in ticks.
Definition interpolation.h:154
float scale() const
How much faster than wall time to run ticks.
Definition pacing.h:76
Type-erased resource registry with typed handles and generational lifetimes.
Definition resource_manager.h:46
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
Everything one simulation tick was told to do, as a value.
Definition input_command.h:28
Where a datagram came from, or is going to: an IPv4 host and a port.
Definition net_address.h:13
What a session's server and client halves share: the socket, what the two ends agreed,...
Definition net_session.h:55
What a server built at this root after the scene loaded, so every client can build the same.
Definition net_spawn.h:19