Interceptor

Trait Interceptor 

Source
pub trait Interceptor: Send {
    // Required methods
    fn on_receive(&mut self, addr: &SocketAddr, data: &mut [u8]) -> bool;
    fn on_send(&mut self, addr: &SocketAddr, data: &mut Vec<u8>) -> bool;
}
Expand description

Trait for intercepting packets before/after processing.

Interceptors can inspect, modify, or drop packets at the raw UDP level. This is useful for implementing custom encryption, logging, analytics, or packet inspection.

§Examples

use std::net::SocketAddr;
use bitfold_core::interceptor::Interceptor;

struct LoggingInterceptor;

impl Interceptor for LoggingInterceptor {
    fn on_receive(&mut self, _addr: &SocketAddr, data: &mut [u8]) -> bool {
        println!("Received {} bytes", data.len());
        true // Continue processing
    }

    fn on_send(&mut self, _addr: &SocketAddr, data: &mut Vec<u8>) -> bool {
        println!("Sending {} bytes", data.len());
        true // Continue sending
    }
}

Required Methods§

Source

fn on_receive(&mut self, addr: &SocketAddr, data: &mut [u8]) -> bool

Called when a packet is received from the network, before protocol processing.

§Arguments
  • addr - The source address of the packet
  • data - The raw packet data (mutable, can be modified)
§Returns
  • true - Continue processing the packet
  • false - Drop the packet (do not process)
Source

fn on_send(&mut self, addr: &SocketAddr, data: &mut Vec<u8>) -> bool

Called when a packet is about to be sent to the network, after protocol encoding.

§Arguments
  • addr - The destination address of the packet
  • data - The raw packet data (mutable, can be modified or resized)
§Returns
  • true - Continue sending the packet
  • false - Drop the packet (do not send)

Implementors§