pub struct Host { /* private fields */ }Expand description
High-level host for managing connections and sending/receiving packets.
High-level host managing multiple peers over a single socket.
Implementations§
Source§impl Host
impl Host
Sourcepub fn bind<A: ToSocketAddrs>(addresses: A) -> Result<Self>
pub fn bind<A: ToSocketAddrs>(addresses: A) -> Result<Self>
Creates a new Host bound to the specified address with default configuration.
Sourcepub fn bind_any() -> Result<Self>
pub fn bind_any() -> Result<Self>
Creates a new Host bound to any available port on localhost with default configuration.
Sourcepub fn bind_any_with_config(config: Config) -> Result<Self>
pub fn bind_any_with_config(config: Config) -> Result<Self>
Creates a new Host bound to any available port on localhost with the specified configuration.
Sourcepub fn bind_with_config<A: ToSocketAddrs>(
addresses: A,
config: Config,
) -> Result<Self>
pub fn bind_with_config<A: ToSocketAddrs>( addresses: A, config: Config, ) -> Result<Self>
Creates a new Host bound to the specified address with custom configuration.
Sourcepub fn bind_with_config_and_clock(
socket: UdpSocket,
config: Config,
clock: Arc<dyn Clock>,
) -> Result<Self>
pub fn bind_with_config_and_clock( socket: UdpSocket, config: Config, clock: Arc<dyn Clock>, ) -> Result<Self>
Creates a new Host with a custom socket, configuration, and clock for testing.
Sourcepub fn bind_with_config_clock_and_interceptor(
socket: UdpSocket,
config: Config,
clock: Arc<dyn Clock>,
interceptor: Option<Box<dyn Interceptor>>,
) -> Result<Self>
pub fn bind_with_config_clock_and_interceptor( socket: UdpSocket, config: Config, clock: Arc<dyn Clock>, interceptor: Option<Box<dyn Interceptor>>, ) -> Result<Self>
Creates a new Host with custom socket, configuration, clock, and interceptor.
Sourcepub fn bind_with_interceptor<A: ToSocketAddrs>(
addresses: A,
config: Config,
interceptor: Box<dyn Interceptor>,
) -> Result<Self>
pub fn bind_with_interceptor<A: ToSocketAddrs>( addresses: A, config: Config, interceptor: Box<dyn Interceptor>, ) -> Result<Self>
Creates a Host with a custom interceptor for packet inspection/modification.
§Arguments
addresses- The address to bind toconfig- Configuration optionsinterceptor- Custom interceptor for packet interception
§Examples
use bitfold_host::Host;
use bitfold_core::{config::Config, interceptor::Interceptor};
use std::net::SocketAddr;
struct LoggingInterceptor;
impl Interceptor for LoggingInterceptor {
fn on_receive(&mut self, _addr: &SocketAddr, data: &mut [u8]) -> bool {
println!("Received {} bytes", data.len());
true
}
fn on_send(&mut self, _addr: &SocketAddr, data: &mut Vec<u8>) -> bool {
println!("Sending {} bytes", data.len());
true
}
}
let host = Host::bind_with_interceptor(
"127.0.0.1:8080",
Config::default(),
Box::new(LoggingInterceptor),
).unwrap();Sourcepub fn get_packet_sender(&self) -> Sender<Packet>
pub fn get_packet_sender(&self) -> Sender<Packet>
Returns a clone of the packet sender channel for sending packets to peers.
Sourcepub fn get_event_receiver(&self) -> Receiver<SocketEvent>
pub fn get_event_receiver(&self) -> Receiver<SocketEvent>
Returns a clone of the event receiver channel for receiving network events.
Sourcepub fn send(&mut self, packet: Packet) -> Result<()>
pub fn send(&mut self, packet: Packet) -> Result<()>
Sends a packet to a peer. The packet will be queued and sent during the next poll.
Sourcepub fn recv(&mut self) -> Option<SocketEvent>
pub fn recv(&mut self) -> Option<SocketEvent>
Receives the next available network event (connect, disconnect, packet, timeout).
Sourcepub fn start_polling(&mut self)
pub fn start_polling(&mut self)
Starts automatic polling in a loop with 1ms intervals (blocking call).
Sourcepub fn start_polling_with_duration(&mut self, sleep_duration: Option<Duration>)
pub fn start_polling_with_duration(&mut self, sleep_duration: Option<Duration>)
Starts automatic polling with custom sleep duration between polls (blocking call).
Sourcepub fn manual_poll(&mut self, time: Instant)
pub fn manual_poll(&mut self, time: Instant)
Manually polls the network for incoming/outgoing packets and updates peer states.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address this host is bound to.
Sourcepub fn disconnect(&mut self, addr: SocketAddr) -> Result<()>
pub fn disconnect(&mut self, addr: SocketAddr) -> Result<()>
Initiates a graceful disconnect from the specified peer.
Sourcepub fn broadcast(
&mut self,
channel_id: u8,
data: Vec<u8>,
delivery: DeliveryGuarantee,
ordering: OrderingGuarantee,
) -> Result<usize>
pub fn broadcast( &mut self, channel_id: u8, data: Vec<u8>, delivery: DeliveryGuarantee, ordering: OrderingGuarantee, ) -> Result<usize>
Broadcasts data to all established connections.
This is a convenience method that sends the same packet to all connected peers. Common use case: server broadcasting game state to all clients.
§Arguments
channel_id- Channel to send on (0-255)data- Payload data to broadcastdelivery- Delivery guarantee (Reliable/Unreliable)ordering- Ordering guarantee (Ordered/Sequenced/Unsequenced/None)
§Returns
Number of peers the packet was sent to
Sourcepub fn broadcast_reliable(
&mut self,
channel_id: u8,
data: Vec<u8>,
) -> Result<usize>
pub fn broadcast_reliable( &mut self, channel_id: u8, data: Vec<u8>, ) -> Result<usize>
Broadcasts data to all established connections with reliable delivery.
Equivalent to broadcast(channel_id, data, DeliveryGuarantee::Reliable, OrderingGuarantee::Ordered(None))
Sourcepub fn broadcast_unreliable(
&mut self,
channel_id: u8,
data: Vec<u8>,
) -> Result<usize>
pub fn broadcast_unreliable( &mut self, channel_id: u8, data: Vec<u8>, ) -> Result<usize>
Broadcasts data to all established connections with unreliable delivery.
Equivalent to broadcast(channel_id, data, DeliveryGuarantee::Unreliable, OrderingGuarantee::None)
Sourcepub fn established_connections_count(&self) -> usize
pub fn established_connections_count(&self) -> usize
Returns the number of established connections.