Skip to main content

Module stream

Module stream 

Source
Expand description

Stream transports (TCP, and TLS over TCP) for a sans-I/O engine.

A datagram engine is driven with one call per received datagram and one per datagram to send. A stream has more to it: it is opened (connect, or accept on a listener), it carries bytes in whatever pieces the network delivers, it can only take so much at a time, and it ends, cleanly or not. This module puts all of that behind ids, so the engine keeps every decision and the application (the driver) keeps only the sockets:

DirectionWhatType
engine → driverlisten on / stop listening on an address; connect to an address from a local one; close; resetStreamCommand
driver → enginelistening (with the bound address); connected (with the local address); failed; written; closed by the peer; resetStreamEvent
driver → enginea connection accepted on a listener: the engine gives it an idStreamTable::accept
driver → enginebytes read from a connection, in any segmentationStreamTable::read
engine → driverbytes to write on a connection, in orderStreamTransmit

StreamTable is the engine’s half: it allocates the ids, frames outgoing messages (Framing::Rfc4571 for ICE-TCP, RFC 4571 / RFC 6544; Framing::Turn for TURN over TCP, RFC 8656 §12.5, whose messages delimit themselves), splits incoming bytes back into messages whatever the read sizes, optionally runs a StreamLayer (TLS) between the two, and applies back-pressure.

§Back-pressure

The driver reports with StreamEvent::Written how many bytes of a connection have left for the socket. The table hands out at most StreamConfig::window bytes a connection that are not yet reported written (one transmit is always allowed, so progress never stops), and keeps what does not fit queued, up to StreamConfig::max_queued bytes. Beyond that a message is dropped whole, before framing or encryption, exactly as a congested datagram path would lose it: a byte stream is never cut inside a frame or a TLS record. StreamTable::send says which happened.

§Order of commands and transmits

A StreamCommand::Close is issued only after the last StreamTransmit of its connection has been handed out, so a driver that writes each transmit as it takes it closes after the last byte. StreamCommand::Reset discards whatever the driver still holds.

§Example: one framed message each way over a connection the engine asked for

use rtci_shared::stream::*;

let mut engine = StreamTable::new(StreamConfig::default());
let local = "10.0.0.1:0".parse().unwrap();
let remote = "10.0.0.2:5000".parse().unwrap();
let id = engine.connect(local, remote, Framing::Rfc4571, 0, false, None).unwrap();
assert_eq!(
    engine.poll_command(),
    Some(StreamCommand::Connect { id, local, remote, reuse_address: false })
);
// The engine may queue data before the connection is up.
assert!(engine.send(id, b"hello").unwrap());
assert!(engine.poll_transmit().is_none());

// The driver connected from an ephemeral port.
let bound = "10.0.0.1:40001".parse().unwrap();
engine.handle_event(StreamEvent::Connected { id, local: bound }).unwrap();
let transmit = engine.poll_transmit().unwrap();
assert_eq!(transmit.data, b"\x00\x05hello");
engine.handle_event(StreamEvent::Written { id, bytes: transmit.data.len() }).unwrap();

// The answer arrives one byte at a time.
let mut messages = vec![];
for byte in b"\x00\x03bye" {
    messages.extend(engine.read(id, &[*byte]).unwrap());
}
assert_eq!(messages, vec![b"bye".to_vec()]);

Modules§

happy_eyeballs
Happy Eyeballs (RFC 8305) for a sans-I/O engine: the order in which a server’s addresses are tried and when the next attempt starts. The driver resolves names (StreamCommand::Resolve), answering both families at once (StreamEvent::Resolved) or each as it arrives (StreamEvent::ResolvedFamily); the engine sorts and staggers the attempts with HappyEyeballs and keeps the first that succeeds. With answers per family the race starts on the first one (RFC 8305 section 3): at once on IPv6, after the Resolution Delay on IPv4 while IPv6 is outstanding, and a late answer joins the addresses not yet tried.

Structs§

ListenerId
A listening socket, as the engine names it in StreamCommand::Listen.
StreamConfig
Limits of a StreamTable.
StreamDecoder
Splits a byte stream of one Framing back into its messages. Bytes can be fed in any segmentation; memory stays bounded by two messages whatever the read size, because input is taken a message’s worth at a time and drained in between.
StreamId
A connection, as the engine names it. Every id is allocated by the engine: for a connection it asks for in StreamCommand::Connect, and for one the driver accepted, by StreamTable::accept. Ids are never reused.
StreamInfo
A connection, as StreamTable::info describes it.
StreamTable
The engine’s side of its stream transports: listeners, connections, framing, the optional security layer and back-pressure. See the module documentation.
StreamTransmit
Bytes to write on a connection, after every earlier transmit of the same connection.

Enums§

DnsRecord
A DNS record, as the driver reports it (StreamEvent::ResolvedRecords).
Framing
How messages are delimited on a connection.
RecordKind
A DNS record type the engine asks for (StreamCommand::ResolveRecords).
StreamCommand
What the engine asks the driver to do with its sockets.
StreamEvent
What the driver tells the engine about its sockets.
StreamNotice
What a StreamTable reports to its owner, in the order it happened.
StreamOrigin
How a connection came to be.
StreamState
Where a connection is in its life.

Constants§

RECORDS_TIMEOUT
How long the engine waits for a StreamCommand::ResolveRecords answer before it takes the name’s addresses instead (a driver that does not look records up).

Traits§

StreamLayer
A security layer between the framing and the socket: TLS for TURN over TLS, in its sans-I/O form. Ciphertext comes in with receive, plaintext goes out with send once the handshake is done, and the table collects what the layer has to write after every call.