circuit::multi_stream

Function multi_stream

Source
pub async fn multi_stream(
    reader: Reader,
    writer: Writer,
    is_server: bool,
    streams_out: Sender<(Reader, Writer, Sender<Result<()>>)>,
    streams_in: Receiver<(Reader, Writer)>,
    stream_errors_out: UnboundedSender<Error>,
    remote_name: String,
) -> Result<()>
Expand description

Multiplexes multiple streams into one.

While running, this function serves a protocol on the reader and writer that multiplexes multiple streams into a single stream.

The protocol is a series of variable length frames each consisting of:

  • 4 bytes - A stream ID in little-endian.
  • 2 bytes - a LENGTH in little-endian.
  • LENGTH bytes - data.

To start a new stream, an endpoint simply begins sending frames with that stream ID. To close a stream, an endpoint can send a zero-length frame. Once a stream is closed that stream ID should never be used again.

We distinguish the two endpoints of the protocol as a client and a server. The only difference between the two is in which stream IDs they may initiate; Any new streams started by the client should have odd-numbered IDs, and any new streams initiated by the server should have even-numbered IDs. The is_server argument controls this behavior.

Any new streams initiated by the other end of the connection will be returned to us via the streams_out channel, and we may initiate a stream by sending a reader and writer to the streams_in channel.