8#include "ecs/component/core/transform.h"
10#include "net/net_client.h"
11#include "net/net_server.h"
12#include "net/prediction/command.h"
13#include "net/transport/connection.h"
14#include "net/wire/bit_stream.h"
15#include "net/wire/protocol.h"
16#include "platform/net/udp_socket.h"
18namespace Vkm::Engine {
29constexpr float NET_SNAPSHOT_RATE = 64.0f;
37constexpr float NET_COMMAND_RATE = 64.0f;
47constexpr uint32_t NET_MAX_TICK_RATE =
static_cast<uint32_t
>(2.0f * NET_COMMAND_RATE);
65 uint32_t actionCount = 0;
78 std::string lastError;
80 std::vector<uint8_t> scratch;
83 std::vector<uint16_t> acknowledged;
139 Drawn& operator=(
const Drawn& other) =
delete;
145 NetSession& m_session;
180 NetSession& m_session;
188 NetSession(
const NetSession& other) =
delete;
189 NetSession& operator=(
const NetSession& other) =
delete;
191 NetSession(NetSession && other) =
delete;
192 NetSession& operator=(NetSession && other) =
delete;
204 void setWorld(uint64_t fingerprint) { m_core.world = fingerprint; }
218 bool host(uint16_t port, uint32_t maxPlayers, uint32_t tickRate);
237 NetRole role()
const {
return m_role; }
238 bool isOffline()
const {
return m_role == NetRole::Offline; }
252 const std::string&
lastError()
const {
return m_core.lastError; }
254 PlayerId localPlayer()
const {
return m_client.localPlayer(); }
311 void lease(
const std::vector<EntityId>& entities) { m_client.lease(entities); }
361 const std::vector<InputCommand>&
replayCommands()
const {
return m_client.replayCommands(); }
409 uint32_t actionCount,
410 uint64_t actionNames = 0
436 const std::string& prefab,
502 bool acceptsTickRate(uint32_t tickRate);
521 void endRewind(
Scene& scene);
527 NetRole m_role = NetRole::Offline;
533 float m_sendAccumulator = 1.0f;
535 uint32_t m_rewindDepth = 0;
538 uint64_t m_worldEpoch = 0;
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
A guest's half of a NetSession: joining, predicting what it owns, being corrected,...
Definition net_client.h:42
One end of one conversation: sequencing, acknowledgement and timing.
Definition connection.h:34
The authority's half of a NetSession: seats, the join handshake, commands in and snapshots out.
Definition net_server.h:33
std::function< void(Scene &, ResourceManager &, PlayerId, EntityId)> DespawnPlayer
Undo what SpawnPlayer did.
Definition net_server.h:47
std::function< EntityId(Scene &, ResourceManager &, PlayerId)> SpawnPlayer
What a joining player is given, decided by the game.
Definition net_server.h:40
Drawn(NetSession &session, Scene &scene, float deltaTime)
Works a little of the correction off and applies the rest.
Rewind(NetSession &session, Scene &scene, EntityId shooter)
Rewinds to the moment shooter was drawing when the running command was made.
void setWorld(uint64_t fingerprint)
Say which world this end loaded, so both can agree they match.
Definition net_session.h:204
void receive(Scene &scene, ResourceManager &resources)
Apply everything that has arrived.
bool isMine(EntityId entity) const
Whether entity belongs to the player at this end.
float pacing() const
How much faster than the wall clock this end should run its ticks.
size_t playerCount() const
How many players this end knows of.
bool host(uint16_t port, uint32_t maxPlayers, uint32_t tickRate)
Take players on port.
const std::vector< InputCommand > & replayCommands() const
The ticks a correction wants run again, oldest first.
Definition net_session.h:361
EntityId spawn(Scene &scene, ResourceManager &resources, const std::string &prefab, const Transform &at)
Build prefab at at, here and on every client.
void endTick(Scene &scene, uint32_t tick)
Record what this end predicted for the tick that just ran.
void beginTick(uint32_t tick, const InputCommand &command, uint32_t actionCount, uint64_t actionNames=0)
Take the command this tick runs under.
void interpolate(Scene &scene, float deltaTime)
Draw what this end is only told about where it was a moment ago.
EntityId localEntity() const
The entity this end drives; null on a server.
Definition net_session.h:268
void lease(const std::vector< EntityId > &entities)
Also simulate entities, for a while.
Definition net_session.h:311
void beginReplayTick(const InputCommand &command)
Take command for a tick being run a second time.
void endReplay(Scene &scene)
Finish the replay and clear the request.
Definition net_session.h:373
NetAddress localAddress() const
The address this end is bound to; the real port after hosting on zero.
Definition net_session.h:261
float renderDelay() const
How far behind the server this end is drawing, in ticks; zero when it holds the present.
bool connect(const NetAddress &server, uint32_t tickRate)
Join the game at server.
const std::string & lastError() const
Why the last attempt to join ended, or an empty string.
Definition net_session.h:252
void onSpawn(NetServer::SpawnPlayer spawn, NetServer::DespawnPlayer despawn)
What the game does when a player joins and leaves. Set before host().
size_t leaseCount() const
How many bodies this end is simulating on top of its own.
Definition net_session.h:318
void advance(float seconds)
Wall-clock time passing, for the timers that must run while the simulation does not - a paused editor...
bool isDisconnected() const
True when a session was open and the other end has gone.
Definition net_session.h:241
bool simulates(EntityId entity) const
Whether this end decides what happens to entity.
void send(Scene &scene, uint32_t tick)
Send what this frame produced. Called once a frame, after the ticks.
void close()
Say goodbye and go back to Offline. Safe to call when already offline.
bool replaying() const
True while a tick that already happened is being run again.
Definition net_session.h:380
const InputCommand & commandFor(EntityId entity) const
The input driving entity on the tick about to run.
std::string describe() const
One line describing the session, for a log or a readout.
bool isPlaying() const
True once this end can play: always for a server, and for a client from the Welcome until the connect...
float roundTrip() const
Round trip to the server, in seconds. Zero on a server or offline.
float predictionError() const
How far this end's prediction of its own entity was out, last time the server said.
Definition net_session.h:483
Open 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
A non-blocking UDP socket, the same on Windows and Linux.
Definition udp_socket.h:17
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
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:53
uint64_t world
Which world this end loaded, or zero for none.
Definition net_session.h:72
uint32_t tickRate
Fixed for the session.
Definition net_session.h:63
static constexpr size_t MAX_READS_PER_FRAME
Datagrams read in one frame.
Definition net_session.h:60
uint64_t actionNames
Definition net_session.h:69
bool ticked
A tick has run, so the count is real.
Definition net_session.h:70
std::vector< uint8_t > payload
What an outgoing message is written into.
Definition net_session.h:81
std::vector< uint8_t > datagram
The datagram last received, or last framed to send.
Definition net_session.h:82
uint32_t serverTick
Definition net_session.h:76