pub trait NetworkHandler:
Send
+ Sync
+ 'static {
// Required method
fn on_data(
&self,
ctx: &ServerContext,
conn_id: ConnectionId,
data: &[u8],
) -> Result<()>;
// Provided methods
fn on_event(&self, ctx: &ServerContext, event: NetworkEvent) -> Result<()> { ... }
fn on_connect(
&self,
ctx: &ServerContext,
conn_id: ConnectionId,
) -> Result<()> { ... }
fn on_disconnect(
&self,
ctx: &ServerContext,
conn_id: ConnectionId,
) -> Result<()> { ... }
fn on_writable(
&self,
ctx: &ServerContext,
conn_id: ConnectionId,
) -> Result<()> { ... }
fn on_error(
&self,
ctx: &ServerContext,
conn_id: Option<ConnectionId>,
error: NetworkError,
) { ... }
}Expand description
Handler for network events on TCP connections.
Implement this trait to define how your application responds to network events. All methods except on_data have default implementations that do nothing.
The handler is invoked by worker threads from the event loop’s thread pool, so implementations must be thread-safe (Send + Sync).
§Execution Context
Handler methods are called from worker threads in the thread pool. Multiple handlers may execute concurrently for different connections. Your implementation should be efficient to avoid blocking worker threads.
§Error Handling
Return errors from handler methods to indicate processing failures. The connection will be closed automatically when handlers return errors from on_data.
Required Methods§
Sourcefn on_data(
&self,
ctx: &ServerContext,
conn_id: ConnectionId,
data: &[u8],
) -> Result<()>
fn on_data( &self, ctx: &ServerContext, conn_id: ConnectionId, data: &[u8], ) -> Result<()>
Called when data is received
Provided Methods§
Sourcefn on_event(&self, ctx: &ServerContext, event: NetworkEvent) -> Result<()>
fn on_event(&self, ctx: &ServerContext, event: NetworkEvent) -> Result<()>
Called when a non-error network event occurs
Sourcefn on_connect(&self, ctx: &ServerContext, conn_id: ConnectionId) -> Result<()>
fn on_connect(&self, ctx: &ServerContext, conn_id: ConnectionId) -> Result<()>
Called when connection is established (TCP only)
Sourcefn on_disconnect(
&self,
ctx: &ServerContext,
conn_id: ConnectionId,
) -> Result<()>
fn on_disconnect( &self, ctx: &ServerContext, conn_id: ConnectionId, ) -> Result<()>
Called when connection is closed (TCP only)
Sourcefn on_writable(&self, ctx: &ServerContext, conn_id: ConnectionId) -> Result<()>
fn on_writable(&self, ctx: &ServerContext, conn_id: ConnectionId) -> Result<()>
Called on write readiness (for backpressure handling)
Sourcefn on_error(
&self,
ctx: &ServerContext,
conn_id: Option<ConnectionId>,
error: NetworkError,
)
fn on_error( &self, ctx: &ServerContext, conn_id: Option<ConnectionId>, error: NetworkError, )
Called on errors