game/src/net/sim.rs#
Source: lib/game/src/net/sim.rs
sim -- a simulated network in the process: netchan's protocol (endpoint::ServerCore, ClientCore) on a
virtual clock, with delay, jitter, loss, duplicates and reordering per peer, from a seed
Weather what the network does to a packet: loss, duplicates, a chance of extra delay (packets
overtake each other), the delay and its jitter; CLEAR, TYPICAL, ROUGH, STORM
SimNet one server and its peers; a peer has a main channel (UDP or a WebSocket) and maybe a
fetch's WebSocket. UDP datagrams meet the weather; a WebSocket is reliable and ordered
(a loss is a retransmission: two delays and 20 ms later, and nothing behind it overtakes)
step(dt_us) the clock moves on, what is due arrives, the cores tick, what they send goes out
Nothing here reads a clock or a socket: the same seed gives the same packets on every target, so a whole
networked session can be checked by a checksum (apps/gametest --netsum). The template was apps/nettest's
simulation of G7d.
struct Weatherwhat the network does to a packetloss: f64-- the chance a datagram is lost (on a WebSocket: retransmitted)dup: f64-- the chance a datagram comes twicereorder: f64-- the chance of an extra delay of up to extra_us (packets overtake each other)extra_us: u64-- see reorderdelay_us: u64-- the delay one wayjitter_us: u64-- up to this much more, evenly spread
impl Weather
const CLEAR: Weather = Weather { loss: 0.0, dup: 0.0, reorder: 0.0, extra_us: 0, ...20 ms each way, nothing elseconst TYPICAL: Weather = Weather { loss: 0.01, dup: 0.0, reorder: 0.02, extra_us: ...a good home connection: 30 ms +- 5 each way, 1 % loss, 2 % a little laterconst ROUGH: Weather = Weather { loss: 0.2, dup: 0.1, reorder: 0.2, extra_us: 30 ...nettest's rough weather: 20 % loss, 10 % twice, 20 % up to 30 ms later, 20 ms +- 5const STORM: Weather = Weather { loss: 0.5, dup: 0.2, reorder: 0.3, extra_us: 60 ...nettest's storm: 50 % loss, 20 % twice, 30 % up to 60 ms later, 40 ms +- 20enum Whichwhich of a peer's linksMain-- the main channelFetch-- a fetch's WebSocket
struct Linkone link of a peer: a netchan client core and how it reaches the servercore: ClientCore-- netchan's client protocol
impl Link
fn reliable(&self) -> boola reliable stream (a WebSocket)fn closed(&self) -> boolthe stream ended or the core disconnectedstruct PeerStatswhat went over a peer's links (netchan's packets, without IP and UDP headers)up_bytes: u64-- bytes to the serverup_packets: u64-- packets to the serverdown_bytes: u64-- bytes from the serverdown_packets: u64-- packets from the server
struct Peera client machine on the networkweather: Weather-- what its network doesmain: Option<Link>-- the main channelfetch: Option<Link>-- a fetch's WebSocketstats: PeerStats-- what went over its links
struct SimNeta server and its peers on one simulated network (see the module header)now_us: u64-- the virtual clock (microseconds)server: ServerCore-- netchan's server protocolpeers: Vec<Peer>-- the peersdown: bool-- the network carries nothing (a cut cable)
impl SimNet
fn new(seed: u64, cfg: ServerConfig) -> SimNeta server with these limits;seeddrives the network and the server's secretfn add_udp(&mut self, cfg: ClientConfig, w: Weather) -> usizea peer whose main channel is UDP -> its index (the handshake starts now)fn add_ws(&mut self, cfg: ClientConfig, w: Weather) -> usizea peer whose main channel is a WebSocket -> its indexfn open_fetch(&mut self, i: usize, cfg: ClientConfig)a fetch's WebSocket for peeri(replacing an old one)fn set_weather(&mut self, i: usize, w: Weather)the peer's network from now onfn flush(&mut self)hand what the cores want sent to the networkfn step(&mut self, dt_us: u64)the clock moves on by dt_us: what is due arrives, the cores tick, what they send goes outfn server_events(&mut self, out: &mut Vec<ServerEvent>)the server's events since the last callfn link_events(&mut self, i: usize, which: Which, out: &mut Vec<ClientEvent>)a link's events since the last callfn server_send(&mut self, id: u32, data: &[u8]) -> Result<u16, Error>the server sends to a connectionfn server_disconnect(&mut self, id: u32)the server ends a connectionfn link_send(&mut self, i: usize, which: Which, data: &[u8]) -> Result<u16, Error>a link sends to the serverfn link_close(&mut self, i: usize, which: Which)a link ends (DISCONNECT goes out; a WebSocket's stream closes behind it)fn link_state(&self, i: usize, which: Which) -> Option<State>a link's netchan statefn in_flight(&self) -> usizepackets on their way