![]() |
vkmEngine 1.0.0
A C++ game engine · vkmengine.com
|
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. | |
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.
|
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.
| fingerprint | What fingerprintScene said of the entry scene. |
| 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.
| port | UDP port to bind; zero takes whatever is free. |
| maxPlayers | Seats; a connection past the last is refused, and zero is refused. |
| tickRate | The clock's ticks a second; at most NET_MAX_TICK_RATE. |
maxPlayers is zero, or tickRate is too fast. | bool Vkm::Engine::NetSession::connect | ( | const NetAddress & | server, |
| uint32_t | tickRate ) |
Join the game at server.
| server | Where the game is. |
| tickRate | The clock's ticks a second; at most NET_MAX_TICK_RATE. |
tickRate is too fast. A refusal arrives later, as a state change. | 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.
|
inline |
The address this end is bound to; the real port after hosting on zero.
|
inline |
The entity this end drives; null on a server.
| 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.
| 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.
| entity | The entity asked about; a null id is never simulated by a client. |
entity.
|
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.
| entities | Bodies to simulate; null ids and this end's own entity are skipped. |
|
inline |
How many bodies this end is simulating on top of its own.
| bool Vkm::Engine::NetSession::isMine | ( | EntityId | entity | ) | const |
Whether entity belongs to the player at this end.
| entity | The entity asked about. |
| 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.
| entity | The entity about to be driven. |
| 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.
| scene | The world what arrived is applied to. |
| resources | Loads a spawned player's or prefab's assets. |
|
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.
| 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.
| command | The command that tick first ran under, from replayCommands(). |
|
inline |
True while a tick that already happened is being run again.
| 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.
| scene | The world as this tick left it. |
| tick | The tick that ran. |
| 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).
| tick | The tick about to run. |
| command | What the local player did on it. |
| actionCount | Action slots the project defines. |
| actionNames | InputMap::actionFingerprint, or zero to be compared against nothing. |
| 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.
| scene | World to build into. |
| resources | Resolves the prefab's asset names. |
| prefab | Project-relative path; on a server, one that names nothing outside the project - see isSayable. |
| at | Root pose; on a server, finite, with a rotation of some length and a bounded scale. |
| 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.
| seconds | Real seconds since the last call. |
| 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.
| scene | World to smooth. |
| deltaTime | Real seconds since the last frame. |
| float Vkm::Engine::NetSession::renderDelay | ( | ) | const |
How far behind the server this end is drawing, in ticks; zero when it holds the present.
| 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.
|
inline |
How far this end's prediction of its own entity was out, last time the server said.
| 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.