karyon_net/stream_mux.rs
1use std::future::Future;
2
3use crate::{stream::ByteStream, Endpoint, Result};
4
5/// Multiplexed connection that yields multiple independent streams
6/// from a single underlying connection (e.g., QUIC).
7///
8/// Each stream is a `Box<dyn ByteStream>` that can be wrapped
9/// with `framed_conn()` for message framing.
10///
11/// # Example
12///
13/// ```no_run
14/// use karyon_net::{quic, StreamMux, Endpoint};
15///
16/// async {
17/// let ep: Endpoint = "quic://127.0.0.1:9000".parse().unwrap();
18/// // let mux = quic::QuicEndpoint::dial(&ep, config, "localhost").await?;
19/// // let stream = mux.open_stream().await?;
20/// // let mut conn = framed_conn(stream, codec);
21/// };
22/// ```
23pub trait StreamMux: Send + Sync {
24 /// Open a new bidirectional stream.
25 fn open_stream(&self) -> impl Future<Output = Result<Box<dyn ByteStream>>> + Send;
26 /// Accept a stream opened by the peer.
27 fn accept_stream(&self) -> impl Future<Output = Result<Box<dyn ByteStream>>> + Send;
28 /// Remote peer address.
29 fn peer_endpoint(&self) -> Option<Endpoint>;
30 /// Local address.
31 fn local_endpoint(&self) -> Option<Endpoint>;
32}