Skip to main content

NetworkHandler

Trait NetworkHandler 

Source
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§

Source

fn on_data( &self, ctx: &ServerContext, conn_id: ConnectionId, data: &[u8], ) -> Result<()>

Called when data is received

Provided Methods§

Source

fn on_event(&self, ctx: &ServerContext, event: NetworkEvent) -> Result<()>

Called when a non-error network event occurs

Source

fn on_connect(&self, ctx: &ServerContext, conn_id: ConnectionId) -> Result<()>

Called when connection is established (TCP only)

Source

fn on_disconnect( &self, ctx: &ServerContext, conn_id: ConnectionId, ) -> Result<()>

Called when connection is closed (TCP only)

Source

fn on_writable(&self, ctx: &ServerContext, conn_id: ConnectionId) -> Result<()>

Called on write readiness (for backpressure handling)

Source

fn on_error( &self, ctx: &ServerContext, conn_id: Option<ConnectionId>, error: NetworkError, )

Called on errors

Implementors§