Skip to main content

ProtocolListener

Struct ProtocolListener 

Source
pub struct ProtocolListener { /* private fields */ }
Expand description

Accept inbound WebSocket peers and drive each one through the protocol-client state machine. Construct via ProtocolClientBuilder::listen.

Sendspin’s protocol-client/server roles are independent of who initiates the TCP connection — the protocol-client always sends client/hello first. This listener handles the server-initiated case; the ProtocolClient returned by Self::accept is indistinguishable in shape from one returned by ProtocolClientBuilder::connect.

Self::accept drives the full protocol handshake before returning, so it serves one inbound connection at a time — a slow handshake blocks the next accept(). If you need concurrent handshakes, or want to own the transport (custom TLS, HTTP routing, …), accept your own streams and drive ProtocolClientBuilder::accept on each, tokio::spawn-ing per peer.

The Sendspin spec allows multiple servers to initiate connections to the same client. The keep-or-switch policy belongs to the application: read ProtocolClient::server_hello for server_id and connection_reason, compare against your persisted last-played server, and send GoodbyeReason::AnotherServer to the loser. This listener does not enforce a policy.

Practical notes:

  • Disconnect consumes the handle. Call ProtocolClient::disconnect pre-split, or connection.guard.disconnect(...) post-split.
  • Self::accept is serial. If the loser’s goodbye is on the critical path, run it on a spawned task so the next inbound peer can handshake while the previous one is tearing down.

Implementations§

Source§

impl ProtocolListener

Source

pub fn path(self, path: impl Into<String>) -> Self

Restrict accepted connections to a specific HTTP path. Mismatches are rejected with HTTP 404 during the WebSocket handshake; the listener stays bound. Defaults to accepting any path.

Matching is exact: /sendspin does not match /sendspin/. A missing leading slash is added, so "sendspin" and "/sendspin" are equivalent — request paths always start with /, and the raw form would otherwise reject every connection.

Source

pub async fn accept(&self) -> Result<(ProtocolClient, SocketAddr), Error>

Accept the next inbound connection, returning the driven ProtocolClient and the peer’s address. Performs the WebSocket handshake and protocol-client hello/state exchange.

Per-peer failures surface as Error without affecting the listener; callers typically call accept() in a loop.

Not cancel-safe: dropping the returned future mid-handshake tears down that connection. A peer that connects but stalls the handshake will block this future indefinitely, so wrap it in a timeout if untrusted peers can reach the socket.

Source

pub fn local_addr(&self) -> Result<SocketAddr, Error>

Local bound address.

Trait Implementations§

Source§

impl Debug for ProtocolListener

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V