Skip to main content

NetworkHandle

Struct NetworkHandle 

Source
pub struct NetworkHandle { /* private fields */ }
Expand description

Main-thread handle to the network subsystem.

Spawns a background thread running a tokio runtime on construction. All send methods are non-blocking (try_send on an unbounded channel). poll() is the only way to drain received data into NetworkEvents.

Implementations§

Source§

impl NetworkHandle

Source

pub fn new(config: NetworkConfig) -> OpticResult<Self>

Creates a new NetworkHandle, spawning a background network thread.

Blocks until the UDP socket is bound (host mode) or the thread is spawned (client mode). For Host mode, local_addr() returns the actual bound address (including OS-assigned port when port=0).

Source

pub fn poll(&mut self, out: &mut NetworkEvents)

Drains all available network events into out. Called once per frame from the game loop. Never blocks — returns in microseconds.

Source

pub fn send(&self, peer: PeerId, bytes: &[u8]) -> OpticResult<()>

Sends raw bytes to a specific peer by ID.

Returns OpticError if the outbound channel is closed (network thread has exited). The packet is silently dropped in that case.

Source

pub fn send_all(&self, bytes: &[u8]) -> OpticResult<()>

Sends raw bytes to all connected peers.

Source

pub fn send_all_except(&self, exclude: PeerId, bytes: &[u8]) -> OpticResult<()>

Sends raw bytes to all connected peers except exclude.

Source

pub fn disconnect(&self, peer: PeerId)

Disconnects a specific peer. For host mode, this removes the peer from the forwarding table and fires a Disconnected event. For client mode, this shuts down the connection to the server.

Source

pub fn peers(&self) -> Vec<PeerId>

Returns a snapshot of currently-connected peer IDs.

For a client, this returns &[PeerId::SERVER] if connected, or empty. Note: this is a best-effort view based on the latest lifecycle events; for accurate peer tracking drain poll() each frame.

Source

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

Returns the local socket address this handle is bound to, if known.

Source

pub fn shutdown(&mut self)

Gracefully shuts down the network thread, waiting for it to exit.

Source

pub fn is_shutdown(&self) -> bool

Returns true after the network thread has fully shut down.

Trait Implementations§

Source§

impl Drop for NetworkHandle

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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, 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.