vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
net_server.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <functional>
6#include <memory>
7#include <string>
8#include <vector>
9
10#include "ecs/component/core/transform.h"
11#include "ecs/entity.h"
12#include "net/prediction/command.h"
13#include "net/prediction/rewind.h"
14#include "net/replication/silence.h"
15#include "net/replication/snapshot.h"
16#include "net/transport/connection.h"
17#include "net/transport/join_cookie.h"
18#include "net/transport/messages.h"
19#include "net/player_message.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;
28
35class NetServer {
36 public:
42 using SpawnPlayer = std::function<EntityId(Scene&, ResourceManager&, PlayerId)>;
43
49 using DespawnPlayer = std::function<void(Scene&, ResourceManager&, PlayerId, EntityId)>;
50
51 public:
52 explicit NetServer(NetCore& core);
53 ~NetServer();
54
55 NetServer(const NetServer& other) = delete;
56 NetServer& operator=(const NetServer& other) = delete;
57
58 NetServer(NetServer && other) = delete;
59 NetServer& operator=(NetServer && other) = delete;
60
61 public:
62 void onSpawn(SpawnPlayer spawn, DespawnPlayer despawn);
63
65 void open(uint32_t maxPlayers);
66
68 void close();
69
70 void advance(float seconds);
71
73 void receive(Scene& scene, ResourceManager& resources);
74
76 void beginTick(uint32_t tick);
77
79 void endTick(const Scene& scene, uint32_t tick);
80
82 void send(Scene& scene, uint32_t tick);
83
90 const InputCommand* commandFor(EntityId entity) const;
91
99 void rewind(Scene& scene, EntityId shooter);
100
102 void restore(Scene& scene);
103
106
107 size_t playerCount() const { return m_peers.size(); }
108
109 std::string describe() const;
110
112 std::vector<PlayerMessageEvent> takeMessages();
113
114 private:
115 struct Peer;
116
118 struct Rewound {
119 EntityId entity;
120 Transform transform;
121 };
122
123 private:
133 NetSnapshotStats sendSnapshot(Scene& scene, const NetSchema& schema, Peer& peer, uint32_t tick);
134
143 void holdScalesToTheWire(Scene& scene);
144
160 bool vetHello(
161 const NetAddress& from,
162 uint64_t echoed,
163 BitReader& reader,
164 uint32_t& actions,
165 uint64_t& actionNames
166 );
167
176 void acceptCommands(Peer& peer, BitReader& reader);
177
191 void greet(
192 const NetAddress& from,
193 uint64_t echoed,
194 BitReader& hello,
195 Scene& scene,
196 ResourceManager& resources
197 );
198
199 void challenge(const NetAddress& to);
200 void refuse(const NetAddress& from, NetRefusal reason, uint64_t token);
201 void welcome(Peer& peer, const NetAddress& to);
202
210 PlayerId freePlayerId() const;
211 void dropPeer(Scene& scene, ResourceManager& resources, size_t index, const char* why);
212
213 Peer* findPeer(const NetAddress& address);
214
215 private:
216 NetCore& m_core;
217
218 std::vector<std::unique_ptr<Peer>> m_peers;
219
220 SpawnPlayer m_spawn;
221 DespawnPlayer m_despawn;
222
223 uint32_t m_maxPlayers = 0;
224 PlayerId m_nextPlayer = 1;
225
226 NetSnapshotStats m_lastSnapshot;
227 bool m_warnedScale = false;
228
229 NetRewind m_rewind;
230 std::vector<Rewound> m_rewound;
231
232 NetSilenceMap m_silence;
233
234 NetJoinCookie m_cookie;
235 double m_seconds = 0.0;
236
237 uint32_t m_refusedMismatch = 0;
238 uint32_t m_refusedFull = 0;
239
240 std::vector<InputCommand> m_received;
241 std::vector<PlayerMessageEvent> m_messages;
242
244 NetConnection m_unseated;
245};
246
247} // namespace Vkm::Engine
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
One end of one conversation: sequencing, acknowledgement and timing.
Definition connection.h:34
The token a server gives an address that asks to join, and takes back as proof that the address hears...
Definition join_cookie.h:33
Where every player was, for as long as it takes a shot to arrive.
Definition rewind.h:21
The ordered list of what replicates, agreed by both ends before play.
Definition schema.h:84
void rewind(Scene &scene, EntityId shooter)
Put every player but shooter where shooter saw them.
std::function< void(Scene &, ResourceManager &, PlayerId, EntityId)> DespawnPlayer
Undo what SpawnPlayer did.
Definition net_server.h:49
void receive(Scene &scene, ResourceManager &resources)
Take every datagram that has arrived, then give back the seats that are going.
std::function< EntityId(Scene &, ResourceManager &, PlayerId)> SpawnPlayer
What a joining player is given, decided by the game.
Definition net_server.h:42
void close()
Tell every player goodbye and forget them.
void open(uint32_t maxPlayers)
Start taking up to maxPlayers players, under a fresh join key.
void forgetWorld()
Drop everything naming an entity; see NetSession::forgetWorld.
const InputCommand * commandFor(EntityId entity) const
The command entity's player sent for this tick.
void send(Scene &scene, uint32_t tick)
Send every player who has answered its snapshot of this tick.
void beginTick(uint32_t tick)
Hand every player the command its entity runs this tick.
void endTick(const Scene &scene, uint32_t tick)
Remember where every player is, for judging a shot later.
void restore(Scene &scene)
Put back exactly what rewind moved.
std::vector< PlayerMessageEvent > takeMessages()
What players sent since the last call, in the order each sent it; taken, so said once.
netSilence for every entity of a world, classified in one pass.
Definition silence.h:65
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 one snapshot said, for the session's status line.
Definition snapshot.h:165
Local position, rotation and scale of an entity.
Definition transform.h:17