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§
Sourcefn accept(
&self,
connection: Connection,
) -> impl Future<Output = Result<(), AcceptError>> + Send
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§
Sourcefn on_accepting(
&self,
accepting: Accepting,
) -> impl Future<Output = Result<Connection, AcceptError>> + Send
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(()).
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.