pub trait LinkConn: Send + Sync {
// Required methods
fn peer(&self) -> PeerId;
fn remote_addr(&self) -> SocketAddr;
fn open_uni(&self) -> BoxFuture<'_, LinkResult<Box<dyn LinkSendStream>>>;
fn open_bi(
&self,
) -> BoxFuture<'_, LinkResult<(Box<dyn LinkSendStream>, Box<dyn LinkRecvStream>)>>;
fn send_datagram(&self, data: Bytes) -> LinkResult<()>;
fn recv_datagrams(&self) -> BoxStream<'_, Bytes>;
fn close(&self, error_code: u64, reason: &str);
fn is_open(&self) -> bool;
fn stats(&self) -> ConnectionStats;
}Expand description
A connection to a remote peer.
This trait abstracts a single QUIC connection, providing methods to
open streams and send/receive datagrams. Connections are obtained via
LinkTransport::dial or LinkTransport::accept.
§Stream Types
- Bidirectional streams (
open_bi): Both endpoints can send and receive. Use for request/response patterns. - Unidirectional streams (
open_uni): Only the opener can send. Use for notifications or one-way data transfer. - Datagrams (
send_datagram): Unreliable, unordered messages. Use for real-time data where latency > reliability.
§Connection Lifecycle
- Connection established (via dial or accept)
- Open streams as needed
- Close gracefully with
close()or let it drop
Required Methods§
Sourcefn peer(&self) -> PeerId
fn peer(&self) -> PeerId
Get the remote peer’s cryptographic identity.
This is stable across reconnections and network changes.
Sourcefn remote_addr(&self) -> SocketAddr
fn remote_addr(&self) -> SocketAddr
Get the remote peer’s current network address.
Note: This may change during the connection lifetime due to NAT rebinding or connection migration.
Sourcefn open_uni(&self) -> BoxFuture<'_, LinkResult<Box<dyn LinkSendStream>>>
fn open_uni(&self) -> BoxFuture<'_, LinkResult<Box<dyn LinkSendStream>>>
Open a unidirectional stream (send only).
The remote peer will receive this stream via their accept_uni().
Use for one-way messages like notifications or log streams.
§Example
let mut stream = conn.open_uni().await?;
stream.write_all(b"notification").await?;
stream.finish()?; // Signal end of streamSourcefn open_bi(
&self,
) -> BoxFuture<'_, LinkResult<(Box<dyn LinkSendStream>, Box<dyn LinkRecvStream>)>>
fn open_bi( &self, ) -> BoxFuture<'_, LinkResult<(Box<dyn LinkSendStream>, Box<dyn LinkRecvStream>)>>
Open a bidirectional stream for request/response communication.
Returns a (send, recv) pair. Both sides can write and read. Use for RPC, file transfers, or any interactive protocol.
§Example
let (mut send, mut recv) = conn.open_bi().await?;
send.write_all(b"request").await?;
send.finish()?;
let response = recv.read_to_end(4096).await?;Sourcefn send_datagram(&self, data: Bytes) -> LinkResult<()>
fn send_datagram(&self, data: Bytes) -> LinkResult<()>
Send an unreliable datagram to the peer.
Datagrams are:
- Unreliable: May be dropped without notification
- Unordered: May arrive out of order
- Size-limited: Must fit in a single QUIC packet (~1200 bytes)
Use for heartbeats, metrics, or real-time data where occasional loss is acceptable.
Sourcefn recv_datagrams(&self) -> BoxStream<'_, Bytes>
fn recv_datagrams(&self) -> BoxStream<'_, Bytes>
Receive datagrams from the peer.
Returns a stream of datagrams. Each datagram is delivered as-is (no framing). The stream ends when the connection closes.
Sourcefn close(&self, error_code: u64, reason: &str)
fn close(&self, error_code: u64, reason: &str)
Close the connection gracefully.
§Parameters
error_code: Application-defined error code (0 = normal close)reason: Human-readable reason for debugging
Sourcefn is_open(&self) -> bool
fn is_open(&self) -> bool
Check if the connection is still open.
Returns false after the connection has been closed (locally or remotely) or if a fatal error occurred.
Sourcefn stats(&self) -> ConnectionStats
fn stats(&self) -> ConnectionStats
Get current connection statistics.
Useful for monitoring connection health and debugging performance.