AsyncSession

Struct AsyncSession 

Source
pub struct AsyncSession<P: Protocol> { /* private fields */ }
Expand description

Async handle to a session running in a spawned tokio task.

Methods translate to commands sent over an internal channel and reply over a oneshot. Dropping the handle severs the connection from the consumer side; the spawned task exits on its next loop.

Drop is not graceful — for graceful shutdown call AsyncSession::disconnect. Drop just severs the connection from the consumer’s side; the reflector eventually times the link out via inactivity.

Implementations§

Source§

impl<P: Protocol> AsyncSession<P>

Source

pub fn spawn(session: Session<P, Connected>, socket: Arc<UdpSocket>) -> Self
where P: Send + 'static,

Spawn the session loop on the current tokio runtime and return a handle for controlling it.

The session must already be in the [Connected] state (typically via Session::<P, Connecting>::promote after observing [Event::Connected] from the handshake). The socket must be bound (typically via UdpSocket::bind).

The loop runs until the handle is dropped, the consumer’s command channel closes, or a fatal I/O error occurs.

§Example
use std::sync::Arc;
use dstar_gateway::tokio_shell::AsyncSession;
use dstar_gateway_core::session::client::{Connected, DExtra, Session};
use tokio::net::UdpSocket;

let sock = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
let mut shell = AsyncSession::spawn(connected, sock);
while let Some(event) = shell.next_event().await {
    println!("{event:?}");
}
Source

pub async fn next_event(&mut self) -> Option<Event<P>>

Pull the next event from the inbound stream.

Returns None once the session task has exited and the event channel has been fully drained.

§Cancellation safety

This method is cancel-safe. It only awaits a tokio::sync::mpsc receiver, which is documented as cancel-safe: dropping the future leaves the channel in a clean state and any undelivered events remain queued for the next call.

Source

pub async fn send_header( &mut self, header: DStarHeader, stream_id: StreamId, ) -> Result<(), ShellError>

Send a voice header and start a new outbound voice stream.

§Errors
§Cancellation safety

This method is cancel-safe. The method enqueues a Command on the command channel and awaits a oneshot reply. If the future is dropped before the enqueue completes no command is sent; if it is dropped after the enqueue the session task still executes the command and the (now-orphaned) oneshot reply is simply discarded. Either way the session state remains consistent.

Source

pub async fn send_voice( &mut self, stream_id: StreamId, seq: u8, frame: VoiceFrame, ) -> Result<(), ShellError>

Send a voice data frame.

§Errors
§Cancellation safety

This method is cancel-safe under the same rules as Self::send_header. Dropping the future either before the command is enqueued or after it has been dispatched leaves the session in a coherent state; orphaning the oneshot reply is harmless.

Source

pub async fn send_eot( &mut self, stream_id: StreamId, seq: u8, ) -> Result<(), ShellError>

Send a voice EOT and close the outbound stream.

§Errors
§Cancellation safety

This method is cancel-safe under the same rules as Self::send_header.

Source

pub async fn disconnect(&mut self) -> Result<(), ShellError>

Request a graceful disconnect.

Sends an UNLINK to the reflector and returns when the loop has enqueued it. The caller should continue polling Self::next_event until [Event::Disconnected] arrives, then drop the session.

§Errors
§Cancellation safety

This method is not cancel-safe. Cancelling the future drives a state-machine transition (ConnectedDisconnecting) that may be partially complete: the UNLINK may already be in the outbox even though the reply oneshot has been dropped. Callers that cancel disconnect() should treat the session as indeterminate and drop the handle rather than attempting further sends. For graceful shutdown, always await this method to completion before dropping the session.

Trait Implementations§

Source§

impl<P: Debug + Protocol> Debug for AsyncSession<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: Protocol> Drop for AsyncSession<P>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<P> Freeze for AsyncSession<P>

§

impl<P> RefUnwindSafe for AsyncSession<P>
where P: RefUnwindSafe,

§

impl<P> Send for AsyncSession<P>

§

impl<P> Sync for AsyncSession<P>

§

impl<P> Unpin for AsyncSession<P>
where P: Unpin,

§

impl<P> UnwindSafe for AsyncSession<P>
where P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more