![]() |
vkmEngine 1.0.15
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, 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. | |
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.
| 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.
| fingerprint | What fingerprintScene said of the entry scene. |
| scene | The world, just opened. |
| 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. | 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().
| 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.
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.
| scene | The world entity is in. |
| 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 |
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.
| bytes | The message; the game's own bytes. |
| size | 1 to NET_MESSAGE_MAX_BYTES of them. |
|
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. |
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.
| scene | The world it is in. |
| slot | The server's slot. |
| 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. |
| 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.
| events | Where they are queued, for the next flush. |
|
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 ) |
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. |
| 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.
| 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.
| 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.
| clock | The engine's clock. |
| 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.