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§
Sourcetype Context: RequestContext
type Context: RequestContext
The protocol’s request context type.
Required Methods§
Sourcefn role(&self) -> ProtocolRole
fn role(&self) -> ProtocolRole
Returns the role of this protocol handler.
Sourcefn detect(initial_bytes: &[u8]) -> boolwhere
Self: Sized,
fn detect(initial_bytes: &[u8]) -> boolwhere
Self: Sized,
Detects if this protocol can handle the connection.
Sourcefn 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,
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.