Protocol

Trait Protocol 

Source
pub trait Protocol:
    Clone
    + Send
    + Sync
    + 'static {
    type Transport: Transport;
    type Stream: Stream;
    type Message: Message;
    type Context: RequestContext;

    // Required methods
    fn role(&self) -> ProtocolRole;
    fn detect(initial_bytes: &[u8]) -> bool
       where Self: Sized;
    fn handle<'life0, 'async_trait>(
        &'life0 mut self,
        reader: TcpReader,
        writer: TcpWriter,
        app: Arc<App>,
    ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
}
Expand description

User-defined protocol handler.

This is the main trait that protocols implement to handle connections. It brings together Transport, Stream, and Message abstractions.

Protocols must be Clone because each connection gets its own instance to maintain per-connection state (like keep-alive, request count, etc.).

Required Associated Types§

Source

type Transport: Transport

The protocol’s connection-level abstraction.

Source

type Stream: Stream

The protocol’s stream abstraction (use () if no streams).

Source

type Message: Message

The protocol’s message format.

Source

type Context: RequestContext

The protocol’s request context type.

Required Methods§

Source

fn role(&self) -> ProtocolRole

Returns the role of this protocol handler.

Source

fn detect(initial_bytes: &[u8]) -> bool
where Self: Sized,

Detects if this protocol can handle the connection.

Source

fn handle<'life0, 'async_trait>( &'life0 mut self, reader: TcpReader, writer: TcpWriter, app: Arc<App>, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Handles a connection with this protocol.

This is where all protocol logic lives. The implementation should check self.role() to determine whether to act as client or server.

The method receives TcpReader/TcpWriter which provide buffered I/O and connection metadata (socket addresses).

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.

Implementors§