vkmEngine 1.0.0
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)
 Say which world this end loaded, so both can agree they match.
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 (EntityId entity) const
 Whether this end decides what happens to entity.
void lease (const std::vector< EntityId > &entities)
 Also simulate entities, for a while.
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.
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.
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=0)
 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.
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)
inline

Say which world this end loaded, so both can agree they match.

Slots name entities on the wire, so different scenes would agree on every name and mean different things. Zero means no file behind this world and is compared against nothing.

Parameters
fingerprintWhat fingerprintScene said of the entry scene.

◆ 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.

◆ 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 ( EntityId entity) const

Whether this end decides what happens to entity.

A server says yes to everything, offline too; a client only to what it owns or leases. A client that loses or is refused by its server ends Disconnected and answers no to everything. Ask the role, not isPlaying(), which is false for both Disconnected and Offline.

Parameters
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.

◆ 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.

◆ 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.

◆ 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 = 0 )

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, or zero to be compared against nothing.

◆ 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: a client-only entity would be corrected out of existence.

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.

◆ 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: