Expand description
The reference driver for the stream half of WebRTC.rs’s sans-I/O engine
(rtci_shared::stream): it owns the TCP sockets, on the async runtime it is given
(runtime), and does what the engine asks.
§Engine and driver
The engine (rtci::peer_connection::RTCPeerConnection) performs no I/O. For its datagrams
the application reads and writes its own UDP sockets (handle_read / poll_write). For
TCP it is the engine that decides — which ICE-TCP candidates to gather (RFC 6544), which
connections to open and close, the RFC 4571 framing, TURN over TCP, TLS and DTLS, relayed
TCP candidates (RFC 6062), server-reflexive gathering over TCP, the Happy Eyeballs race
(RFC 8305) — and names every listener and connection by id. This driver only carries
out what it is told:
The engine says (StreamCommand) | The driver |
|---|---|
Listen { local, reuse_address } | binds a listener (port 0: any, or one of DriverConfig::tcp_port_range), reports Listening or ListenFailed, then Accepted for each connection |
Connect { local, remote, reuse_address } | connects (within DriverConfig::connect_timeout), reports Connected or ConnectFailed |
Close / Reset | ends the connection with FIN (waiting DriverConfig::close_linger) or RST |
Resolve { host, port } | looks the name up (both families at once, or AAAA and A apart with the dns-records feature) |
ResolveRecords { name, kind } | SRV / NAPTR records with dns-records, none without |
Bytes go both ways untouched: what the engine transmits (StreamDriver::write) is
already framed and encrypted, and what the socket reads goes back in whatever pieces it
came (DriverInput::Read); each write is reported with StreamEvent::Written, the
engine’s back-pressure signal.
§The loop
while let Some(command) = pc.poll_stream_command() { driver.execute(command) }
while let Some(transmit) = pc.poll_stream_transmit() { driver.write(transmit) }
match driver.next().await { // or try_next() in a polling loop
DriverInput::Event(event) => pc.handle_stream_event(now, event)?,
DriverInput::Read { id, data } => pc.handle_stream_read(now, id, &data)?,
DriverInput::Accepted(accepted) => match pc.handle_stream_accepted(
now, accepted.listener, accepted.local, accepted.remote) {
Some(id) => driver.adopt(id, accepted),
None => drop(accepted), // refused: the socket closes
},
}A complete program, two peer connections over loopback TCP with a data channel, is the
stream-driver-loopback example of the rtci crate (cargo run --example stream-driver-loopback); the test environment runs it.
§Marking (DSCP)
Each TCP connection’s packets carry the codepoint of its transmits (StreamTransmit::dscp,
one per connection, RFC 8837 section 5), and peer::PeerDriver marks each UDP datagram
with the byte RTCPeerConnection::traffic_class gives it (RFC 8837 Table 1 with the ECN
bits). sockets::set_traffic_class says what each platform does: Windows leaves the
packets unmarked.
§Server choices
sockets has what a server decides and the engine does not: port ranges
(sockets::PortRange), which local addresses get candidates (sockets::AddressFilter),
one UDP port for many peer connections (sockets::UfragMux) and one passive ICE-TCP port
for many (DriverConfig::shared_passive, sockets::accept_by_ufrag).
§NAT-assisted candidates
nat_mapping (opt-in, no dependency) asks the gateway for a port mapping with PCP (RFC
6887), falling back to NAT-PMP (RFC 6886); the application adds the mapped address as a
server-reflexive candidate (nat_mapping::candidate_line, RFC 6544 section 5.3).
§A whole peer connection
peer::PeerDriver (feature peer, on by default) drives an rtci peer connection over
sockets it owns, its UDP host sockets included, and gathers for it: at the first local
description, and again on its own when a transport needs a new set of local candidates
(OnLocalCandidatesNeeded: a new DTLS association runs on a new 3-tuple, RFC 8842 section
5.1), closing the previous sockets once a pair on the new ones is selected.
Its addresses are the ones given, or chosen from the host’s interfaces at every gathering
(sockets::AddressSelection: RFC 8445 section 5.1.1.1’s rules for whoever owns the
sockets). STUN and TURN over UDP are the engine’s own: it sends from the driver’s sockets,
and peer::PeerDriver::shutdown waits for its TURN allocations to be deleted before the
sockets close.
task runs a peer::PeerDriver as a task driven through channels, from any runtime
or an OS thread; negotiation is W3C perfect negotiation over a peer connection.
§Runtime
The driver’s sockets, tasks, timers and name lookups belong to the runtime::Runtime it
is given (StreamDriver::with_runtime, peer::PeerDriver::with_runtime): there is no
global or default runtime. The runtime module says what is injected and what is not,
and how to bring your own.
§Features
runtime-tokio(default):runtime::TokioRuntime, andStreamDriver::new/defaultandPeerDriver::newon it.runtime-smol:runtime::SmolRuntime.runtime-mock:runtime::MockRuntime, virtual time and an in-memory network (delay, loss, jitter, reordering, a rate cap, interfaces for address selection).peer(default):peer, with a dependency onrtci.dns-records: SRV and NAPTR lookups (RFC 5928, RFC 8489 section 8) and AAAA / A answered apart (RFC 8305 section 3) inTokioRuntime, with hickory-resolver (MIT OR Apache-2.0); it turnsruntime-tokioon. Without it, names go to the runtime’s resolver and record queries are answered with none (the engine then uses the host and the scheme’s default port).
With default-features = false and no runtime feature the crate has no async runtime in
its dependency tree.
Each connection has a reader task and a writer task. With TokioRuntime::new() sockets are
created on the thread that calls StreamDriver::execute inside a tokio runtime, so a
current-thread runtime on a thread that entered a network namespace keeps them there.
Modules§
- nat_
mapping - NAT-assisted candidates (RFC 6544 section 5.3, RFC 8445 section 5.1.1.2): an opt-in client of
the Port Control Protocol (PCP, RFC 6887) with a fallback to NAT-PMP (RFC 6886), asking the
gateway for a port mapping. Nothing here runs unless the application calls it, and it
adds no dependency: one UDP socket of the driver’s
Runtime. - negotiation
- Perfect negotiation (feature
peer): the W3C pattern for two peers that may both offer at once (webrtc-pc section 10.7 “Perfect negotiation example”). One side is polite: when offers cross (“glare”), it rolls its own back and answers the peer’s; the impolite side ignores the peer’s colliding offer and waits for the answer to its own. Each side then renegotiates whenever its connection says so, and every collision settles by itself. - peer
- A whole peer connection driven over sockets the driver owns (feature
peer, on by default): its UDP sockets for the ICE host candidates, and the TCP sockets of the engine’s stream interface through aStreamDriver. - runtime
- The async runtime a driver runs on, injected per driver.
- sockets
- Server-side socket choices, which belong to the driver and not to the engine: port ranges, which local addresses get candidates, and one UDP port or one ICE-TCP listening port shared by many peer connections.
- task
- A
PeerDriveras a task of its own, driven through channels (featurepeer): what an application with its own threads or tasks uses instead of stepping the driver itself.
Structs§
- Accepted
- A connection accepted on a listener, waiting for its id.
- Driver
Config - The driver’s settings.
- Stream
Driver - Owns the sockets of one engine (see the crate documentation).
Enums§
- Driver
Input - What the driver has for the engine.