Working on server side stuff

This commit is contained in:
Vladislav Khorev committed 2026-11-17 15:28:21 +06:00
1 parent a784da9fb4
commit 542fc191fa
12 files changed
+757 -64

No files matched your search

+71 -1
View File
@@ -62,6 +62,75 @@ configured player slot connects or disconnects.
locations against the replica and presents completed-tick events. It never
advances simulation from a render-frame delta.
## Delayed Ticks and Connection Loss
- **Delay.** The client never advances simulation from wall-clock time. If tick N
is missing it stalls: the world looks frozen, but input is still sent (the
server schedules it two ticks after it arrives). When the missing ticks arrive
the client applies all contiguous buffered ticks in one `update()` call, so
the game visibly fast-forwards. There is no timeout or resynchronization.
- **Connection lost.** `WebsocketGameClient::connectionFailed()` becomes true
on any socket error, any close after connecting (including a clean server
close), a `server_full`/`incompatible` rejection, or a compatibility
fingerprint mismatch. `Game` then unloads the campaign and returns to the main
menu / campaign selection (checked once loading has completed).
- **Server side.** A read error, close, or write error releases the slot, closes
the socket, and schedules `SetCharacterEnabledCommand(enabled=false)` for the
next tick, which is broadcast so every replica disables that character.
Inputs queued from a released slot in the same update are discarded.
- There is no application-level heartbeat; a silently dead peer is detected by
Beast's idle timeout.
## Message Signing and Rate Limiting
- **Session secret.** On connect the server generates 32 random bytes and sends
them hex-encoded as `sessionSecret` in `initial_state`. The client must receive
a valid 64-hex-character secret or the message is rejected.
- **Per-message key.** `key = SHA-256(sessionSecret || appPepper || bigEndian(clientSequence))`.
The pepper is rebuilt at run time from three masked fragments in separate
functions (`MessageAuth.h`); it is never one contiguous constant. This raises
the cost of extraction but cannot stop a determined reverse engineer, since the
client must hold it. Change the fragments for each release if desired.
- **Signature.** Every client input envelope carries `signature =
hex(HMAC-SHA256(key, "type|clientSequence|payload"))`, where `payload` is the
canonical JSON dump. `clientSequence` starts at 1 per connection and must
strictly increase (replay protection).
- **Validation order (server, per session, on the network thread).** rate limit,
then signature, then sequence, then payload parsing.
- **Rate limit.** More than `kMaxCommandsPerSecond` (20) messages inside a
sliding one-second window counts every message, valid or not.
- **Kick.** A rate-limit, signature or replay failure makes the server send a
final `error` (`rate_limited`, `invalid_signature` or `replayed_message`),
ignore the session, and close it. The slot is released and
`SetCharacterEnabledCommand(enabled=false)` is broadcast like any disconnect.
The client treats any error other than `invalid_input` as a lost connection and
returns to the menu. A malformed but correctly signed payload only yields
`invalid_input` and does not kick.
- **Primitives.** SHA-256/HMAC are implemented in the header-only
`src/game_server/MessageAuth.h`, because the standalone server and the web
client do not link OpenSSL. It is checked against the standard test vectors.
- **Limits.** Over plain `ws://` the secret travels in cleartext, so use `wss://`
(the reverse proxy). Only client inputs are signed; the server's tick messages
are not.
## Multiple Commands per Frame
A client may send several inputs in one frame; each is a separate message. The
server keeps, per player and per scheduled tick, only the **last order**, where
an order is `destination_input`, `set_target`, or `clear_attack_target`.
`try_attack_combo` and presence commands are never dropped. Orders are applied
by the shared `applyScheduledCommand()` (`LockstepApply.h`) on the server and on
every replica:
- destination: clear the attack target, then walk to the point;
- `set_target`: stop the current walk, then attack (the chase path is planned by
the simulation);
- `clear_attack_target`: clear the attack target only.
So "walk to A, then attack NPC 1" attacks, and "attack NPC 1, then walk to A"
walks. The client therefore sends the destination before `set_target` when a
hostile NPC is clicked, and no longer sends redundant trailing clears.
## Campaign Routing
The graphical game uses stable router objects for `IGameServer` and
@@ -116,7 +185,8 @@ Message types are:
- `tick` (server to client): tick ID, globally sequenced destination,
character-action, and character-presence commands, and post-tick diagnostic
state hash;
- `error` (server to client): invalid input, incompatibility, or full server.
- `error` (server to client): invalid input, incompatibility, full server, or a
kick (`rate_limited`, `invalid_signature`, `replayed_message`).
Protocol v1 is not compatible. Malformed payloads, non-finite or out-of-range
coordinates, duplicate client sequences, empty action IDs, and unsupported