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
impl NetworkHandle
Sourcepub fn new(config: NetworkConfig) -> OpticResult<Self>
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).
Sourcepub fn poll(&mut self, out: &mut NetworkEvents)
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.
Sourcepub fn send(&self, peer: PeerId, bytes: &[u8]) -> OpticResult<()>
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.
Sourcepub fn send_all(&self, bytes: &[u8]) -> OpticResult<()>
pub fn send_all(&self, bytes: &[u8]) -> OpticResult<()>
Sends raw bytes to all connected peers.
Sourcepub fn send_all_except(&self, exclude: PeerId, bytes: &[u8]) -> OpticResult<()>
pub fn send_all_except(&self, exclude: PeerId, bytes: &[u8]) -> OpticResult<()>
Sends raw bytes to all connected peers except exclude.
Sourcepub fn disconnect(&self, peer: PeerId)
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.
Sourcepub fn peers(&self) -> Vec<PeerId>
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.
Sourcepub fn local_addr(&self) -> Option<SocketAddr>
pub fn local_addr(&self) -> Option<SocketAddr>
Returns the local socket address this handle is bound to, if known.
Sourcepub fn is_shutdown(&self) -> bool
pub fn is_shutdown(&self) -> bool
Returns true after the network thread has fully shut down.