UDP example
examples/udp/ is a small Rust host that uses std::net::UdpSocket. It shows
the intended division of labour: UDP provides datagrams, predsync provides the
frame.
# deterministic CI-friendly self-test on loopback
cargo run -p predsync-udp-example -- self-test
# send frames to a receiver
cargo run -p predsync-udp-example -- send 127.0.0.1:9000 10
# receive frames from a local port
cargo run -p predsync-udp-example -- receive 127.0.0.1:9000 10
The schema is parsed at runtime with predsync_compiler::parse_schema, using a
schema embedded in the example crate. Both sides therefore use the same field
order and predictor configuration.
The self-test mode binds two loopback sockets, encodes a deterministic
telemetry sequence, sends each frame as one datagram, feeds it to a fresh
decoder and asserts the decoded values. It needs no external network and is the
mode run by pixi run examples.
send installs a SIGINT handler so Ctrl-C stops the loop and prints an encode
summary. The same summary is printed when the requested frame count finishes.
It reports median, mean and a 95% bootstrap confidence interval for predicted
fields per frame, byte savings and percentage size reduction. A maximum/mean
message-size line precedes the overall figure. The baseline is a fresh encoder
at the same tick, so the savings are the cost of sending every true value every
time.
Notes for real deployments:
- Use one
Decoderper remote sender; a UDP socket may receive from many peers. - Check
Decoded::fields[*].validitybefore acting on a predicted value. - UDP can reorder or duplicate datagrams. Sequence handling only covers frames that reach the decoder; it does not request retransmission.
- Keep datagrams below the path MTU. The example uses a 1500-byte receive buffer.