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 {
31constexpr float NET_SNAPSHOT_RATE = 64.0f;
39constexpr float NET_COMMAND_RATE = 64.0f;
49constexpr uint32_t NET_MAX_TICK_RATE =
static_cast<uint32_t
>(2.0f * NET_COMMAND_RATE);
67 uint32_t actionCount = 0;
80 std::string lastError;
82 std::vector<uint8_t> scratch;
85 std::vector<uint16_t> acknowledged;
141 Drawn& operator=(
const Drawn& other) =
delete;
147 NetSession& m_session;
182 NetSession& m_session;
190 NetSession(
const NetSession& other) =
delete;
191 NetSession& operator=(
const NetSession& other) =
delete;
193 NetSession(NetSession && other) =
delete;
194 NetSession& operator=(NetSession && other) =
delete;
223 bool host(uint16_t port, uint32_t maxPlayers, uint32_t tickRate);
246 NetRole role()
const {
return m_role; }
247 bool isOffline()
const {
return m_role == NetRole::Offline; }
261 const std::string&
lastError()
const {
return m_core.lastError; }
263 PlayerId localPlayer()
const {
return m_client.localPlayer(); }
323 void lease(
const std::vector<EntityId>& entities) { m_client.lease(entities); }
341 return m_role == NetRole::Client && m_client.sendMessage(bytes, size);
414 const std::vector<InputCommand>&
replayCommands()
const {
return m_client.replayCommands(); }
461 uint32_t actionCount,
489 const std::string& prefab,
565 static constexpr uint64_t NO_WORLD = UINT64_MAX;
574 bool acceptsTickRate(uint32_t tickRate);
597 void endRewind(
Scene& scene);
603 NetRole m_role = NetRole::Offline;
609 float m_sendAccumulator = 1.0f;
610 bool m_tickOpen =
false;
612 uint32_t m_rewindDepth = 0;
617 uint64_t m_worldEpoch = NO_WORLD;
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
The engine's frame clock: real + simulation time and the fixed-step accumulator.
Definition clock.h:18
Typed pub/sub event dispatcher.
Definition event_bus.h:22
A guest's half of a NetSession: joining, predicting what it owns, being corrected,...
Definition net_client.h:44
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:35
std::function< void(Scene &, ResourceManager &, PlayerId, EntityId)> DespawnPlayer
Undo what SpawnPlayer did.
Definition net_server.h:49
std::function< EntityId(Scene &, ResourceManager &, PlayerId)> SpawnPlayer
What a joining player is given, decided by the game.
Definition net_server.h:42
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 receive(Scene &scene, ResourceManager &resources)
Apply everything that has arrived.
bool isMine(EntityId entity) const
Whether entity belongs to the player at this end.
void beginTick(uint32_t tick, const InputCommand &command, uint32_t actionCount, uint64_t actionNames)
Take the command this tick runs under.
void setWorld(uint64_t fingerprint, const Scene &scene)
Say which world this end loaded, as loaded, before anything in it has run.
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:414
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 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:277
void lease(const std::vector< EntityId > &entities)
Also simulate entities, for a while.
Definition net_session.h:323
double worldTime(const Clock &clock) const
The seconds of the authority's world this end is drawing.
void beginReplayTick(const InputCommand &command)
Take command for a tick being run a second time.
bool sendMessage(const uint8_t *bytes, size_t size)
Send the server something it must have: a move, a name, a choice in a lobby.
Definition net_session.h:340
void endReplay(Scene &scene)
Finish the replay and clear the request.
Definition net_session.h:426
NetAddress localAddress() const
The address this end is bound to; the real port after hosting on zero.
Definition net_session.h:270
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:261
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:349
bool simulates(const Scene &scene, EntityId entity) const
Whether this end decides what happens to entity.
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:250
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:433
EntityId entityFor(const Scene &scene, uint32_t slot) const
What the server's slot names on this end.
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.
void deliver(EventBus &events)
Hand what players sent since the last call to events, as PlayerMessageEvent.
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:552
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:55
uint64_t world
Which world this end loaded, or zero for none.
Definition net_session.h:74
uint32_t tickRate
Fixed for the session.
Definition net_session.h:65
static constexpr size_t MAX_READS_PER_FRAME
Datagrams read in one frame.
Definition net_session.h:62
uint64_t actionNames
Definition net_session.h:71
bool ticked
A tick has run, so the count is real.
Definition net_session.h:72
std::vector< uint8_t > payload
What an outgoing message is written into.
Definition net_session.h:83
std::vector< uint8_t > datagram
The datagram last received, or last framed to send.
Definition net_session.h:84
uint32_t serverTick
Definition net_session.h:78