Skip to main content

duplex

Function duplex 

Source
pub fn duplex(capacity: usize, clock: &Clock) -> (Duplex, Duplex)
Expand description

Creates two connected streams with capacity bytes of buffering per direction.

Both measure their deadlines on clock, and so does any transport on them. Reads wait for data and writes wait for buffer space, bounded by the deadlines the transport installs. Partial progress never refreshes a deadline. A timeout leaves the connection reusable and preserves any bytes already accepted. Flush checks its deadline but does not wait for the peer to consume output. Reads deliver available bytes, EOF and empty reads even after their deadline; the deadline only limits waiting for input. Writes and flushes refuse expired deadlines even if buffer space is available. The transport enforces its own deadlines before calling either half.

Closing or dropping an endpoint wakes blocked I/O on both sides. Its unread input is discarded, while the peer can still drain the endpoint’s accepted output before receiving EOF. Further nonempty writes on either side fail with io::ErrorKind::BrokenPipe. So do flushes on the closed endpoint. The peer’s flush fails the same way only if the endpoint closed with some of the peer’s output unread. Output the endpoint had consumed before closing flushes fine afterwards.

Both peers must run concurrently when exchanging data. Allow enough capacity for the handshake’s initial output; 64 KiB (64 * 1024) is a useful starting point. Small buffers can cause handshake backpressure, just as a real stream can.

§Panics

Panics if capacity is zero.