pub struct SessionClient<C: Connection> { /* private fields */ }Expand description
A high-level, cloneable client for quic-reverse sessions.
SessionClient wraps a SessionHandle and provides a more convenient
API for working with quic-reverse sessions. It:
- Is cloneable, allowing use from multiple tasks
- Automatically processes incoming messages in a background task
- Delivers events via a channel for handling incoming requests
§Usage
For the relay (stream opener) side:
let client = SessionClient::new(handle);
let (send, recv) = client.open("echo", Metadata::Empty).await?;For the edge (stream acceptor) side:
let (client, mut events) = SessionClient::with_events(handle);
while let Some(event) = events.recv().await {
if let ClientEvent::OpenRequest { request_id, service, .. } = event {
let stream_id = 1;
client.accept_open(request_id, stream_id).await?;
let (send, recv) = connection.open_bi().await?;
// Handle the stream...
}
}Implementations§
Source§impl<C: Connection> SessionClient<C>
impl<C: Connection> SessionClient<C>
Sourcepub fn new(handle: SessionHandle<C>) -> Self
pub fn new(handle: SessionHandle<C>) -> Self
Creates a new session client from a session handle.
This spawns a background task to process incoming messages.
Use with_events if you need to handle
incoming stream requests (edge device role).
Sourcepub fn with_events(handle: SessionHandle<C>) -> (Self, Receiver<ClientEvent>)
pub fn with_events(handle: SessionHandle<C>) -> (Self, Receiver<ClientEvent>)
Creates a new session client with an event channel.
Returns the client and a receiver for incoming events. Use this when you need to handle incoming stream requests (edge device role).
Sourcepub fn connection(&self) -> &C
pub fn connection(&self) -> &C
Returns a reference to the underlying connection.
Sourcepub async fn open(
&self,
service: impl Into<ServiceId>,
metadata: Metadata,
) -> Result<(C::SendStream, C::RecvStream), Error>
pub async fn open( &self, service: impl Into<ServiceId>, metadata: Metadata, ) -> Result<(C::SendStream, C::RecvStream), Error>
Opens a stream to a service on the peer.
Sends an OpenRequest and waits for the peer to accept and
open a data stream back.
§Errors
Returns an error if:
- The session is not ready
- The request limit has been reached
- The peer rejects the request
- The request times out
Sourcepub async fn accept_open(
&self,
request_id: u64,
logical_stream_id: u64,
) -> Result<(), Error>
pub async fn accept_open( &self, request_id: u64, logical_stream_id: u64, ) -> Result<(), Error>
Accepts an incoming open request.
Call this in response to a ClientEvent::OpenRequest event.
After calling this, open a data stream back to the peer using
the underlying connection.
§Errors
Returns an error if sending the response fails.
Sourcepub async fn reject_open(
&self,
request_id: u64,
code: RejectCode,
reason: Option<String>,
) -> Result<(), Error>
pub async fn reject_open( &self, request_id: u64, code: RejectCode, reason: Option<String>, ) -> Result<(), Error>
Rejects an incoming open request.
Call this in response to a ClientEvent::OpenRequest event
when you cannot or do not want to handle the request.
§Errors
Returns an error if sending the response fails.
Sourcepub async fn bind_stream<S: AsyncWriteExt + Unpin>(
&self,
send: &mut S,
logical_stream_id: u64,
) -> Result<(), Error>
pub async fn bind_stream<S: AsyncWriteExt + Unpin>( &self, send: &mut S, logical_stream_id: u64, ) -> Result<(), Error>
Binds a data stream to a logical stream ID.
After accepting an open request with accept_open,
open a bidirectional stream and call this method to bind it to the
logical stream ID you provided. The peer will verify the binding before
using the stream.
§Errors
Returns an error if writing the bind frame fails.
§Example
// After receiving ClientEvent::OpenRequest { request_id, service, .. }
client.accept_open(request_id, stream_id).await?;
// Open the data stream and bind it
let (mut send, recv) = connection.open_bi().await?;
client.bind_stream(&mut send, stream_id).await?;
// Now the stream is ready for useSourcepub async fn ping(&self) -> Result<Duration, Error>
pub async fn ping(&self) -> Result<Duration, Error>
Sends a ping and waits for the pong response.
Returns the round-trip time on success.
§Errors
Returns an error if the session is closed, sending fails, or the ping times out.