Repository navigation
feat: add a network simulator and netcode overlay - #144
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NetSimulator is a threaded UDP relay that adds per-direction latency, jitter, loss and duplication beneath ENet, with live reconfiguration, seeded decisions and counters. Receivers block on their sockets and the delivery thread spins only near due times, so delays stay accurate on Windows systems with a 15.6 ms timer tick. NetStats collects RTT, traffic rates, snapshot jitter and reconciliation corrections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A debug setting or --netsim routes the next join through the relay; the menu keeps the real address. FlightSession feeds NetStats with snapshot arrivals, reconciliation corrections, accepted commands and ENet traffic. F8 shows them with live simulator sliders and a translucent ghost at the last authoritative position. Gameplay authority and packet contents are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 1 of 6 of the real-time netcode rework. This PR is the base of a stack; later phases branch from it.
Behavior
scripts/net_simulator.gd,NetSimulator). An in-process UDP relay sits between the client's ENet socket (on127.0.0.1:<listen_port>) and the real server. Each direction (client→server UP, server→client DOWN) has its own one-way latency, ± jitter (uniform, can reorder datagrams), loss % and duplication %. Conditions change live behind a mutex, the RNG can be seeded, and per-direction counters track received, forwarded, dropped, duplicated and queued datagrams.stop()flushes queued datagrams first, so ENet's disconnect notice still reaches the server. One relay serves one client.PacketPeerUDP.wait()and timestamps each datagram on arrival. A delivery thread measures how long its 1 ms sleeps really take and spins only when the next datagram is due sooner than that. On this machine, sleeps in some contexts lasted a full 15.6 ms Windows timer tick, which first quantized jittered delays. A fixed 60 ms now measures 60.1 ms, and 50 ± 20 ms spreads uniformly from 30 to 70 ms.start(address, port, bind_port := 0),stop(),is_running(),listen_port,configure(UP|DOWN|BOTH, latency_ms, jitter_ms, loss_percent, duplicate_percent),set_round_trip(rtt_ms, jitter_ms, loss, duplicate)(split evenly per direction: "150 ± 30 ms" adds 75 ± 15 ms each way),conditions(direction),symmetric(),stats(),reset_stats(),set_seed()andconfigure_from_args().--netsim,--netsim-rtt=MS,--netsim-jitter=MS,--netsim-loss=%,--netsim-duplicate=%or--netsim-seed=N, makes the nextjoin()start the relay. The connection menu still shows the real address and adds "Routed through the network simulator (debug)". Normal play never uses the relay. Tests setsession.use_net_simulatorand configuresession.net_simulatorbefore joining.scripts/net_stats.gd). A small client-side metrics object thatFlightSessionfeeds. It tracks:It is meant to be extended in later phases (buffer health, acks, per-client server bytes).
scripts/netcode_overlay.gd). It matches the F3 overlay's style and is a regular HUD layout panel, so it can be moved, resized and closed in the Ctrl+Alt editor. It shows:The sliders are mouse-only (
FOCUS_NONE,MOUSE_FILTER_PASS), so flight keys keep working, left clicks don't select targets, and right-drag steering still passes through. F8 is a fixed shortcut like F3. The overlay hides while the sector overview is open.NETWORK_SCHEMAstays 23. The only server-side addition is a per-pilot counter of accepted flight commands.Why a relay and not a
MultiplayerPeerExtensionwrapperA wrapper around
ENetMultiplayerPeerwould sit above ENet's reliability layer. Dropping a "packet" there would drop a whole reliable message that ENet has already delivered and acknowledged, and delaying it would delay delivery to the game, not the wire. ENet's resends, acks, RTT estimate, sequencing and packet throttle would never see the loss or latency, so reliable traffic would lose data (never realistic) and unreliable traffic would skip ENet's real behavior. A UDP relay degrades the actual datagrams, so ENet reacts exactly as on a bad network. The baseline shows why that matters: at 200 ms, ENet's own throttle discards unreliable traffic. The relay also needs no engine changes and works headlessly on Linux and Windows.Baseline (
tests/netcode_benchmark.gd, also indocs/netcode-baseline.md)One dedicated server, one measured client through the relay and three direct clients, all in one headless process (129–139 fps), flying a mirrored scripted pattern at about 30 m/s. Each condition gets a 15 s warm-up, then 20 s of measurement. Added RTT is split evenly per direction; loss applies both ways.
What these show:
Usage:
godot --headless --path . --script res://tests/netcode_benchmark.gd -- [--scenario=baseline|degraded] [--condition=RTT,JITTER,LOSS[,DUP]]... [--seconds=20] [--extra-clients=3] [--seed=1]. Every metric is defined in the script header, and it also prints aNETCODE_BENCHMARK=JSON line, so phases 2–4 can rerun it unchanged. The benchmark is not in the check suites.Validation
./tools/dev.ps1 check(Windows, 2 workers): all 73 checks pass in 530 s, including the newnet_simulatorgroup and the HUD customization test, which covers the new panel's editor lifecycle.net_simulatorgroup in both the Windows and Linux suites:tests/net_simulator_test.gd(rawPacketPeerUDP) covers per-direction latency within tolerance, jitter bounds and spread, seeded loss (20% → 1590/2000 arrived) and duplication (10% → 2210/2000 delivered) with exact counters, live reconfiguration, flush on stop, and argument parsing.tests/netsim_network_test.gdcovers an authenticated join to a dedicated server through the relay at 80 ± 10 ms with 5% loss and 5% duplication. It also checks snapshots, RTT, traffic rates, corrections and command counts, plus F8 toggling, live round-trip and per-direction sliders, the ghost and its toggle, a prompt server-side leave on disconnect (flush), and a direct reconnect that bypasses the relay.python -m unittest tests/ci_performance_test.pypasses (suite layout and port blocks).--netsim-rtt=150 --netsim-jitter=30 --netsim-loss=5to a local headless dedicated server. The connection menu showed the routing note, and F8 opened the overlay with live readings. Dragging the Added RTT slider changed it to 405 ms and clicking the Loss track changed loss, without selecting targets. RTT then visibly rose and ENet traffic fell. The automation tool couldn't hold flight keys, so the ghost was captured with a scratch script that renders a relayed client while flying. The overlay and the cyan ghost render correctly, and the ghost sits close to the ship, matching the "dragged onto the server path" finding. Screenshots stay out of the repository.Limitations
🤖 Generated with Claude Code