ProtocolHandler

Trait ProtocolHandler 

Source
pub trait ProtocolHandler:
    Send
    + Sync
    + Debug
    + 'static {
    // Required method
    fn accept(
        &self,
        connection: Connection,
    ) -> impl Future<Output = Result<(), AcceptError>> + Send;

    // Provided methods
    fn on_accepting(
        &self,
        accepting: Accepting,
    ) -> impl Future<Output = Result<Connection, AcceptError>> + Send { ... }
    fn shutdown(&self) -> impl Future<Output = ()> + Send { ... }
}
Expand description

Handler for incoming connections.

A router accepts connections for arbitrary ALPN protocols.

With this trait, you can handle incoming connections for any protocol.

Implement this trait on a struct that should handle incoming connections. The protocol handler must then be registered on the endpoint for an ALPN protocol with crate::protocol::RouterBuilder::accept.

See the module documentation for an example.

Required Methods§

Source

fn accept( &self, connection: Connection, ) -> impl Future<Output = Result<(), AcceptError>> + Send

Handle an incoming connection.

Can be implemented as async fn accept(&self, connection: Connection) -> Result<()>.

The returned future runs on a freshly spawned tokio task so it can be long-running. Once accept() returns, the connection is dropped. This means that it will be closed if there are no other clones of the connection. If there is a protocol error, you can use Connection::close to send an error code to the remote peer. Returning an Err<AcceptError> will also drop the connection and log a warning, but no dedicated error code will be sent to the peer, so it’s recommended to explicitly close the connection within your accept handler.

When Router::shutdown is called, no further connections will be accepted, and the futures returned by Self::accept will be aborted after the future returned from ProtocolHandler::shutdown completes.

Provided Methods§

Source

fn on_accepting( &self, accepting: Accepting, ) -> impl Future<Output = Result<Connection, AcceptError>> + Send

Optional interception point to handle the Accepting state.

Can be implemented as async fn on_accepting(&self, accepting: Accepting) -> Result<Connection>.

Typically, this method is used as an early interception point to accept or reject a connection.

However, this method can also be used to implement the accept side of a 0-RTT connection.

§0-RTT

ProtocolHandler::on_accepting allows you to take over the connection state machine early in the handshake processes, by calling Accepting::into_0rtt.

When working with 0-RTT, you may want to implement all of your protocol logic in on_accepting. This is fine because on_accepting can handle long-running processes. In this case, the ProtocolHandler::accept method can simply return Ok(()).

Source

fn shutdown(&self) -> impl Future<Output = ()> + Send

Called when the router shuts down.

Can be implemented as async fn shutdown(&self).

This is called from Router::shutdown. The returned future is awaited before the router closes the endpoint.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: ProtocolHandler> ProtocolHandler for Box<T>

Source§

async fn on_accepting( &self, accepting: Accepting, ) -> Result<Connection, AcceptError>

Source§

async fn accept(&self, conn: Connection) -> Result<(), AcceptError>

Source§

async fn shutdown(&self)

Source§

impl<T: ProtocolHandler> ProtocolHandler for Arc<T>

Source§

async fn on_accepting( &self, accepting: Accepting, ) -> Result<Connection, AcceptError>

Source§

async fn accept(&self, conn: Connection) -> Result<(), AcceptError>

Source§

async fn shutdown(&self)

Implementors§