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 tokio, 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.
§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.
§Features
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), with hickory-resolver (MIT OR Apache-2.0). Without it, names go to the system resolver and record queries are answered with none (the engine then uses the host and the scheme’s default port).
Each connection has a reader task and a writer task. 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 on tokio.
- 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. - 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.
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.