Host

Struct Host 

Source
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

Source

pub fn bind<A: ToSocketAddrs>(addresses: A) -> Result<Self>

Creates a new Host bound to the specified address with default configuration.

Source

pub fn bind_any() -> Result<Self>

Creates a new Host bound to any available port on localhost with default configuration.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 to
  • config - Configuration options
  • interceptor - 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();
Source

pub fn get_packet_sender(&self) -> Sender<Packet>

Returns a clone of the packet sender channel for sending packets to peers.

Source

pub fn get_event_receiver(&self) -> Receiver<SocketEvent>

Returns a clone of the event receiver channel for receiving network events.

Source

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.

Source

pub fn recv(&mut self) -> Option<SocketEvent>

Receives the next available network event (connect, disconnect, packet, timeout).

Source

pub fn start_polling(&mut self)

Starts automatic polling in a loop with 1ms intervals (blocking call).

Source

pub fn start_polling_with_duration(&mut self, sleep_duration: Option<Duration>)

Starts automatic polling with custom sleep duration between polls (blocking call).

Source

pub fn manual_poll(&mut self, time: Instant)

Manually polls the network for incoming/outgoing packets and updates peer states.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the local socket address this host is bound to.

Source

pub fn disconnect(&mut self, addr: SocketAddr) -> Result<()>

Initiates a graceful disconnect from the specified peer.

Source

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 broadcast
  • delivery - Delivery guarantee (Reliable/Unreliable)
  • ordering - Ordering guarantee (Ordered/Sequenced/Unsequenced/None)
§Returns

Number of peers the packet was sent to

Source

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))

Source

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)

Source

pub fn established_connections_count(&self) -> usize

Returns the number of established connections.

Trait Implementations§

Source§

impl Debug for Host

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Host

§

impl !RefUnwindSafe for Host

§

impl Send for Host

§

impl !Sync for Host

§

impl Unpin for Host

§

impl !UnwindSafe for Host

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more