vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::NetSession Class Reference

One end of a game, whether it is the authority or a guest. More...

#include <net_session.h>

Classes

class  Drawn
 Holds the outstanding correction in the scene for one scope. More...
class  Rewind
 Puts every other player back where a shooter saw them, for one scope. More...

Public Member Functions

void setWorld (uint64_t fingerprint, const Scene &scene)
 Say which world this end loaded, as loaded, before anything in it has run.
bool host (uint16_t port, uint32_t maxPlayers, uint32_t tickRate)
 Take players on port.
bool connect (const NetAddress &server, uint32_t tickRate)
 Join the game at server.
void close ()
 Say goodbye and go back to Offline. Safe to call when already offline.
void onSpawn (NetServer::SpawnPlayer spawn, NetServer::DespawnPlayer despawn)
 What the game does when a player joins and leaves. Set before host().
bool isDisconnected () const
 True when a session was open and the other end has gone.
bool isPlaying () const
 True once this end can play: always for a server, and for a client from the Welcome until the connection ends.
const std::string & lastError () const
 Why the last attempt to join ended, or an empty string.
NetAddress localAddress () const
 The address this end is bound to; the real port after hosting on zero.
EntityId localEntity () const
 The entity this end drives; null on a server.
float roundTrip () const
 Round trip to the server, in seconds. Zero on a server or offline.
size_t playerCount () const
 How many players this end knows of.
bool simulates (const Scene &scene, EntityId entity) const
 Whether this end decides what happens to entity.
void lease (const std::vector< EntityId > &entities)
 Also simulate entities, for a while.
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.
size_t leaseCount () const
 How many bodies this end is simulating on top of its own.
bool isMine (EntityId entity) const
 Whether entity belongs to the player at this end.
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.
void receive (Scene &scene, ResourceManager &resources)
 Apply everything that has arrived.
void deliver (EventBus &events)
 Hand what players sent since the last call to events, as PlayerMessageEvent.
const std::vector< InputCommand > & replayCommands () const
 The ticks a correction wants run again, oldest first.
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.
bool replaying () const
 True while a tick that already happened is being run again.
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)
 Take the command this tick runs under.
void send (Scene &scene, uint32_t tick)
 Send what this frame produced. Called once a frame, after the ticks.
EntityId spawn (Scene &scene, ResourceManager &resources, const std::string &prefab, const Transform &at)
 Build prefab at at, here and on every client.
void advance (float seconds)
 Wall-clock time passing, for the timers that must run while the simulation does not - a paused editor still notices a peer leaving.
void interpolate (Scene &scene, float deltaTime)
 Draw what this end is only told about where it was a moment ago.
float renderDelay () const
 How far behind the server this end is drawing, in ticks; zero when it holds the present.
double worldTime (const Clock &clock) const
 The seconds of the authority's world this end is drawing.
float pacing () const
 How much faster than the wall clock this end should run its ticks.
float predictionError () const
 How far this end's prediction of its own entity was out, last time the server said.
std::string describe () const
 One line describing the session, for a log or a readout.

Detailed Description

One end of a game, whether it is the authority or a guest.

Not a System: it brackets the frame - what arrived is applied before the ticks that consume it, what happened is sent after them.

Offline is a real state: a single-player game's session answers every question as it would with no networking at all.

Member Function Documentation

◆ setWorld()

void Vkm::Engine::NetSession::setWorld ( uint64_t fingerprint,
const Scene & scene )

Say which world this end loaded, as loaded, before anything in it has run.

Slots name entities on the wire, so different scenes would agree on every name and mean different things: the fingerprint is compared at a join, and zero - no file behind this world - against nothing. What scene holds now is what both ends share; a client names those entities by their own slots, and makes whatever else the server names where it has room.

Parameters
fingerprintWhat fingerprintScene said of the entry scene.
sceneThe world, just opened.

◆ host()

bool Vkm::Engine::NetSession::host ( uint16_t port,
uint32_t maxPlayers,
uint32_t tickRate )

Take players on port.

A host referees and does not play; to play too, run a server and join it.

Parameters
portUDP port to bind; zero takes whatever is free.
maxPlayersSeats; a connection past the last is refused, and zero is refused.
tickRateThe clock's ticks a second; at most NET_MAX_TICK_RATE.
Returns
False when the port could not be bound, maxPlayers is zero, or tickRate is too fast.

◆ connect()

bool Vkm::Engine::NetSession::connect ( const NetAddress & server,
uint32_t tickRate )

Join the game at server.

Parameters
serverWhere the game is.
tickRateThe clock's ticks a second; at most NET_MAX_TICK_RATE.
Returns
False when the socket cannot be opened or tickRate is too fast. A refusal arrives later, as a state change.

◆ close()

void Vkm::Engine::NetSession::close ( )

Say goodbye and go back to Offline. Safe to call when already offline.

What the session made goes at the next receive().

◆ isPlaying()

bool Vkm::Engine::NetSession::isPlaying ( ) const

True once this end can play: always for a server, and for a client from the Welcome until the connection ends.

Returns
Whether this end can play.

◆ localAddress()

NetAddress Vkm::Engine::NetSession::localAddress ( ) const
inline

The address this end is bound to; the real port after hosting on zero.

Returns
The bound address.

◆ localEntity()

EntityId Vkm::Engine::NetSession::localEntity ( ) const
inline

The entity this end drives; null on a server.

Returns
The entity, or a null id.

◆ playerCount()

size_t Vkm::Engine::NetSession::playerCount ( ) const

How many players this end knows of.

Every seat on a server; only its own on a client, since who the others are never crosses the wire - a scoreboard replicates its own component keyed by PlayerId.

Returns
Seats known to this end.

◆ simulates()

bool Vkm::Engine::NetSession::simulates ( const Scene & scene,
EntityId entity ) const

Whether this end decides what happens to entity.

A server says yes to everything, offline too. A client says yes to the character it plays and what it leases, and, outside a replay, to what no slot of the server's names: its own entities, and those at or below a NetLocal. A replay re-runs only what the client predicts. A client that loses or is refused by its server ends Disconnected, and answers no for the server's world it holds still. Ask the role, not isPlaying(), which is false for both Disconnected and Offline.

Parameters
sceneThe world entity is in.
entityThe entity asked about; a null id is never simulated by a client.
Returns
Whether this end decides what happens to entity.

◆ lease()

void Vkm::Engine::NetSession::lease ( const std::vector< EntityId > & entities)
inline

Also simulate entities, for a while.

For the pile this end's character is touching (see PhysicsSystem::leaseContacts); without it a pushed crate is predicted as a wall. A lease lapses a fixed number of ticks after its last report, not when contact breaks, so a bouncing body is not handed back and forth. Re-reporting refreshes, so pass the whole island every tick.

Parameters
entitiesBodies to simulate; null ids and this end's own entity are skipped.

◆ sendMessage()

bool Vkm::Engine::NetSession::sendMessage ( const uint8_t * bytes,
size_t size )
inline

Send the server something it must have: a move, a name, a choice in a lobby.

Delivered at most once and in the order sent, as a PlayerMessageEvent at the server's next event flush; what is still on its way when this end leaves goes with it. It rides every packet this end sends until the server says it has it, so a lost datagram costs a packet interval, not the message. Input for a tick is not a message: the command carries that, and setPayload what is held beside it. Offline there is no server to tell: the game is its own authority and applies the choice where it makes it.

Parameters
bytesThe message; the game's own bytes.
size1 to NET_MESSAGE_MAX_BYTES of them.
Returns
False, sending nothing, unless this end is a client playing, the message fits, and fewer than NET_MESSAGE_WINDOW are still unacknowledged.

◆ leaseCount()

size_t Vkm::Engine::NetSession::leaseCount ( ) const
inline

How many bodies this end is simulating on top of its own.

Returns
The number of live leases.

◆ isMine()

bool Vkm::Engine::NetSession::isMine ( EntityId entity) const

Whether entity belongs to the player at this end.

Parameters
entityThe entity asked about.
Returns
True for everything offline, for this end's own entity on a client, and for nothing on a server.

◆ entityFor()

EntityId Vkm::Engine::NetSession::entityFor ( const Scene & scene,
uint32_t slot ) const

What the server's slot names on this end.

An entity's name on the wire is the server's slot. A client makes what the server names where it has room, so a replicated component that names an entity sends the server's slot, and a client looks it up here. On a server or offline it is the slot.

Parameters
sceneThe world it is in.
slotThe server's slot.
Returns
The entity, or a null id when this end has nothing for it.

◆ commandFor()

const InputCommand & Vkm::Engine::NetSession::commandFor ( EntityId entity) const

The input driving entity on the tick about to run.

Offline and on the owning client, the local command; on a server, the one that entity's player sent, consumed once. An entity no player owns gets a zeroed command.

Parameters
entityThe entity about to be driven.
Returns
The command it runs under this tick.

◆ receive()

void Vkm::Engine::NetSession::receive ( Scene & scene,
ResourceManager & resources )

Apply everything that has arrived.

Once a frame, before the ticks. A join spawns a player on a server and a client builds spawned prefabs, hence the resource manager.

Parameters
sceneThe world what arrived is applied to.
resourcesLoads a spawned player's or prefab's assets.

◆ deliver()

void Vkm::Engine::NetSession::deliver ( EventBus & events)

Hand what players sent since the last call to events, as PlayerMessageEvent.

Once a frame, after receive. Only a server has any.

Parameters
eventsWhere they are queued, for the next flush.

◆ replayCommands()

const std::vector< InputCommand > & Vkm::Engine::NetSession::replayCommands ( ) const
inline

The ticks a correction wants run again, oldest first.

Empty except in the frame a snapshot disagreed with the prediction. The caller drives them; the session cannot run a System.

Returns
The commands to run again, oldest first.

◆ beginReplayTick()

void Vkm::Engine::NetSession::beginReplayTick ( const InputCommand & command)

Take command for a tick being run a second time.

Not beginTick, which would queue it for sending again.

Parameters
commandThe command that tick first ran under, from replayCommands().

◆ replaying()

bool Vkm::Engine::NetSession::replaying ( ) const
inline

True while a tick that already happened is being run again.

Returns
Whether a replay is in progress.

◆ endTick()

void Vkm::Engine::NetSession::endTick ( Scene & scene,
uint32_t tick )

Record what this end predicted for the tick that just ran.

Inside the fixed step, after the systems. Kept to compare against a later snapshot, which describes a moment this end has moved past.

Parameters
sceneThe world as this tick left it.
tickThe tick that ran.

◆ beginTick()

void Vkm::Engine::NetSession::beginTick ( uint32_t tick,
const InputCommand & command,
uint32_t actionCount,
uint64_t actionNames )

Take the command this tick runs under.

Inside the fixed step, after InputMap::beginTick. Both ends must agree on the action count (it sizes the encoding) and on which action has which slot (the fingerprint).

Parameters
tickThe tick about to run.
commandWhat the local player did on it.
actionCountAction slots the project defines.
actionNamesInputMap::actionFingerprint.

◆ spawn()

EntityId Vkm::Engine::NetSession::spawn ( Scene & scene,
ResourceManager & resources,
const std::string & prefab,
const Transform & at )

Build prefab at at, here and on every client.

On a server the root gets a NetSpawn, which replicates so every client, and every later joiner, builds the same file into it. Offline it is an ordinary instantiate. Undone by destroying the root's subtree (HierarchyOperations::destroyHierarchy or Behavior::destroy). Authority only: refused on a client, as the server would never hear of what it built; a client's own entity is an ordinary Prefab::instantiate.

Parameters
sceneWorld to build into.
resourcesResolves the prefab's asset names.
prefabProject-relative path; on a server, one that names nothing outside the project - see isSayable.
atRoot pose; on a server, finite, with a rotation of some length and a bounded scale.
Returns
The instance root, or a null id when nothing was built.

◆ advance()

void Vkm::Engine::NetSession::advance ( float seconds)

Wall-clock time passing, for the timers that must run while the simulation does not - a paused editor still notices a peer leaving.

Parameters
secondsReal seconds since the last call.

◆ interpolate()

void Vkm::Engine::NetSession::interpolate ( Scene & scene,
float deltaTime )

Draw what this end is only told about where it was a moment ago.

Once a frame; a no-op except on a client. Also works off the step a body takes when its lease lapses and it drops back to being drawn.

Parameters
sceneWorld to smooth.
deltaTimeReal seconds since the last frame.

◆ renderDelay()

float Vkm::Engine::NetSession::renderDelay ( ) const

How far behind the server this end is drawing, in ticks; zero when it holds the present.

Returns
The delay, in ticks.

◆ worldTime()

double Vkm::Engine::NetSession::worldTime ( const Clock & clock) const

The seconds of the authority's world this end is drawing.

What moves by the clock and must look alike at every end is moved by these. Offline and on a server, the clock's ticks and the fraction past the last, so it stops with the game, and inside a tick that tick's own, getTick() * getFixedStep(), so what a tick decides by it it decides the same way each time. On a playing client, from the first snapshot on, the server tick it draws others at, so what the server floats is drawn on the sea it floated on; the two clocks are otherwise unrelated, and a client that has lost its server goes back to its own.

Parameters
clockThe engine's clock.
Returns
Seconds of the authority's simulated time.

◆ pacing()

float Vkm::Engine::NetSession::pacing ( ) const

How much faster than the wall clock this end should run its ticks.

A playing client's answer from NetPacing; exactly one in every other role and state. See Clock::setPacing.

Returns
The rate scale; one outside a playing client.

◆ predictionError()

float Vkm::Engine::NetSession::predictionError ( ) const
inline

How far this end's prediction of its own entity was out, last time the server said.

Returns
The error in metres; zero on a server and offline.

◆ describe()

std::string Vkm::Engine::NetSession::describe ( ) const

One line describing the session, for a log or a readout.

Connection, peers, their distance and prediction health; built on demand.

Returns
The line.

The documentation for this class was generated from the following file: