JFLib

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 Weather what the network does to a packet
    • loss: f64 -- the chance a datagram is lost (on a WebSocket: retransmitted)
    • dup: f64 -- the chance a datagram comes twice
    • reorder: f64 -- the chance of an extra delay of up to extra_us (packets overtake each other)
    • extra_us: u64 -- see reorder
    • delay_us: u64 -- the delay one way
    • jitter_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 else

  • const 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 later

  • const 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 +- 5

  • const 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 +- 20

  • enum Which which of a peer's links

    • Main -- the main channel
    • Fetch -- a fetch's WebSocket

impl Link

  • fn reliable(&self) -> bool a reliable stream (a WebSocket)

  • fn closed(&self) -> bool the stream ended or the core disconnected

  • struct PeerStats what went over a peer's links (netchan's packets, without IP and UDP headers)

    • up_bytes: u64 -- bytes to the server
    • up_packets: u64 -- packets to the server
    • down_bytes: u64 -- bytes from the server
    • down_packets: u64 -- packets from the server
  • struct Peer a client machine on the network

    • weather: Weather -- what its network does
    • main: Option<Link> -- the main channel
    • fetch: Option<Link> -- a fetch's WebSocket
    • stats: PeerStats -- what went over its links
  • struct SimNet a server and its peers on one simulated network (see the module header)

    • now_us: u64 -- the virtual clock (microseconds)
    • server: ServerCore -- netchan's server protocol
    • peers: Vec<Peer> -- the peers
    • down: bool -- the network carries nothing (a cut cable)

impl SimNet

  • fn new(seed: u64, cfg: ServerConfig) -> SimNet a server with these limits; seed drives the network and the server's secret
  • fn add_udp(&mut self, cfg: ClientConfig, w: Weather) -> usize a peer whose main channel is UDP -> its index (the handshake starts now)
  • fn add_ws(&mut self, cfg: ClientConfig, w: Weather) -> usize a peer whose main channel is a WebSocket -> its index
  • fn open_fetch(&mut self, i: usize, cfg: ClientConfig) a fetch's WebSocket for peer i (replacing an old one)
  • fn set_weather(&mut self, i: usize, w: Weather) the peer's network from now on
  • fn flush(&mut self) hand what the cores want sent to the network
  • fn step(&mut self, dt_us: u64) the clock moves on by dt_us: what is due arrives, the cores tick, what they send goes out
  • fn server_events(&mut self, out: &mut Vec<ServerEvent>) the server's events since the last call
  • fn server_send(&mut self, id: u32, data: &[u8]) -> Result<u16, Error> the server sends to a connection
  • fn server_disconnect(&mut self, id: u32) the server ends a connection
  • fn in_flight(&self) -> usize packets on their way

Source: docs/API.md in the JFLib repository.