Skip to content

About

Server-authoritative multiplayer netcode: client-side prediction, server reconciliation, and snapshot interpolation, tested under injected latency and packet loss (.NET core).

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

netcode-arena

Server-authoritative multiplayer netcode in C#: client-side prediction, server reconciliation and snapshot interpolation, tested under injected latency and packet loss.

CI

Why

Naive multiplayer sends the local position and renders whatever arrived last. With real internet latency the local player feels their own character lag behind their input, and remote players teleport between updates. The standard fix is to predict locally, let the server stay the single source of truth, and reconcile the two: the local player feels instant, the remote player looks smooth, and errors correct themselves instead of accumulating.

netcode-arena is a 2-player top-down arena (movement plus one hitscan weapon) that implements that loop end to end, with a headless UDP dedicated server and an offline test harness that simulates a lossy network deterministically.

Features

  • Client-side prediction: the local player moves on input, before any server round trip.
  • Server reconciliation: on each authoritative snapshot the client snaps to the server state and replays the inputs the server has not yet acknowledged.
  • Snapshot interpolation: the remote player is rendered 100 ms in the past by interpolating between two buffered snapshots, and holds its last position rather than extrapolating when the buffer runs dry.
  • Shared deterministic simulation: the same ArenaSimulation.Step runs in the client predictor and the server tick.
  • Server-only damage: health changes only when an authoritative snapshot says so; the client predicts movement and weapon cooldown, never hits.
  • Headless UDP server: fixed 30 tick/s loop, 20 snapshot/s broadcast, no game engine required.
  • Deterministic network simulator: SimulatedNetworkChannel<T> injects seeded latency, jitter and packet loss.

Architecture

sequenceDiagram
    participant P as Player input
    participant C as Client<br/>ClientPredictor
    participant N as Network<br/>latency and loss
    participant S as Server<br/>ServerWorld
    participant R as Remote render<br/>SnapshotInterpolationBuffer

    P->>C: move, aim, fire (tick T)
    C->>C: ApplyInput: predict locally and render now
    C->>N: PlayerInput(seq, T)
    N-->>S: deliver after simulated latency or drop
    S->>S: Tick: authoritative ArenaSimulation.Step
    S->>N: WorldSnapshot(tick, A, B)
    N-->>C: deliver snapshot
    C->>C: Reconcile: snap to server state,<br/>replay un-acked inputs
    N-->>R: deliver snapshot (remote player)
    R->>R: buffer, sample at now minus interpolation delay
Loading

NetcodeArena.Core is pure C# with no engine or socket dependencies. Because the client and the server run the identical ArenaSimulation.Step on the same (state, input), reconciliation converges: a prediction error can only come from inputs the client has not yet had acknowledged. The local player runs the predict, send, reconcile loop; the remote player is fed from the same snapshots through the interpolation buffer.

Quick start

Prerequisite: .NET 7 SDK or newer. No Docker, game engine or network access is needed to build or test.

git clone https://github.com/SlateGitOrg/netcode-arena.git
cd netcode-arena

dotnet build NetcodeArena.sln -c Release
dotnet test tests/NetcodeArena.Core.Tests -c Release

Run the dedicated server (UDP port 7777 by default; pass another port as the argument):

dotnet run --project src/NetcodeArena.Server -c Release -- 7777

Or in Docker:

docker build -t netcode-arena-server .
docker run --rm -p 7777:7777/udp netcode-arena-server

Usage

Server output on startup, with no clients connected yet:

[netcode-arena] headless server starting on udp/7777 (30 tick/s, 20 snapshot/s)
[netcode-arena] tick=150 uptime=00:04 seats=0/2 A.hp=100 B.hp=100

The first two distinct UDP endpoints that send a packet take seats A and B. Packets use the layout in WireCodec.

Using the core library directly:

var predictor = new ClientPredictor(initialState);

// each client tick: predict now and send the input to the server
uint clientTick = 42;
var move = new Vector2F(1f, 0f);
var aimDirection = new Vector2F(1f, 0f);
PlayerInput input = predictor.ApplyInput(clientTick, move, aimDirection, fire: true);

// when an authoritative state for this player arrives
bool mispredicted = predictor.Reconcile(authoritativeState);

How it works

  • Prediction. ClientPredictor.ApplyInput steps the local state and keeps the input in a pending list, tagged with a sequence number.
  • Reconciliation. Reconcile takes the server's state, drops pending inputs up to the last sequence the server processed (Step tracks it per player), then replays the remainder on top. MispredictionCount and ReconciliationCount expose how often the correction was non-trivial.
  • Interpolation. SnapshotInterpolationBuffer keeps snapshots sorted by tick, ignores duplicates, tolerates out-of-order arrival, and lerps between the two snapshots that bracket now - InterpolationDelaySeconds.
  • Starvation. If a player's input queue is empty on a server tick, the server holds that player in place rather than guessing a movement.
  • Weapon. A hitscan blaster with 14 unit range, an aim cone, a 0.35 s cooldown and 18 damage against 100 health. All constants live in SimulationConfig, the single source of truth shared by client and server.

Testing

dotnet test tests/NetcodeArena.Core.Tests -c Release

42 xUnit tests, fully offline (seeded RNG, no wall-clock waits, no sockets):

  • ReconciliationUnderNetworkConditionsTests drives a full predict, send, reconcile loop through SimulatedNetworkChannel<T> at several loss and latency settings (up to 30% loss and 3 to 12 ticks of latency) and asserts the client converges to the server position once the network drains.
  • SnapshotInterpolationBufferTests cover interpolation, the configured delay, out-of-order and duplicate snapshots, and holding on underrun.
  • ClientPredictorTests, ArenaSimulationTests, WeaponSystemTests, PlayerMovementTests and SimulatedNetworkChannelTests cover the pieces individually, including determinism.

Project layout

src/NetcodeArena.Core/       simulation, prediction, reconciliation, interpolation,
                             simulated network channel
src/NetcodeArena.Server/     headless UDP host (HeadlessHost, WireCodec, Program)
tests/NetcodeArena.Core.Tests/  xUnit suite and network harness
Dockerfile                   headless server image

Limitations

  • No game client or engine front end is included; NetcodeArena.Core is designed so one can be a thin layer over ClientPredictor and SnapshotInterpolationBuffer.
  • No lag-compensated hit detection: hits are resolved against the target's current server position.
  • No matchmaking: one match per server process, seats go to the first two UDP endpoints seen.
  • The UDP wire format has no authentication, encryption or reliability layer.

License

MIT — see LICENSE.

About

Server-authoritative multiplayer netcode: client-side prediction, server reconciliation, and snapshot interpolation, tested under injected latency and packet loss (.NET core).

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages