Crate async_smux
source ·Expand description
A lightweight and fast asynchronous smux (Simple MUltipleXing) library for Tokio async runtime.
Quickstart
ⓘ
# Server
// Initialize a stream with `AsyncRead + AsyncWrite`, e.g. TcpStream
let tcp_connection = ...
// Spawn a smux server to multiplexing the tcp stream using `MuxBuilder`
let connector, acceptor, worker = MuxBuilder::server().with_connection(tcp_connection).build();
// Spawn the smux worker (or a worker `future`, more precisely)
// The worker keeps running and dispatch smux frames until you drop (or close) all streams, acceptors and connectors
tokio::spawn(worker);
// Now we are ready to go!
// Both client and server can spawn and accept bi-directional streams
let outgoing_stream = connector.connect().unwrap();
let incoming_stream = acceptor.accept().await.unwrap();
// Just use these smux streams like normal tcp streams :)
incoming_stream.read(...).await.unwrap();
incoming_stream.write_all(...).await.unwrap();
Client
ⓘ
let tcp_connection = ...
// Just like what we do at the server side, except that we are calling the `client()` function this time
let (connector, acceptor, worker) = MuxBuilder::client().with_connection(tcp_connection).build();
tokio::spawn(worker);
let outgoing_stream1 = connector.connect().unwrap();
...
Re-exports
pub use builder::MuxBuilder;
pub use config::MuxConfig;
pub use config::StreamIdType;