pub struct VirtualUdpSocket { /* private fields */ }Expand description
One virtual UDP socket sharing the physical socket given at
construction time. Inbound datagrams arrive only via
VirtualUdpSocket::enqueue_inbound (called by the router that
owns the physical socket); outbound datagrams pass through to the
physical socket unchanged.
Cloning the Arc<VirtualUdpSocket> shares state — both clones see
the same inbound queue, the same physical socket, and the same
closed flag. This is intended: typical use installs one clone in
the router’s dispatch table and hands another to the consumer
task.
Implementations§
Source§impl VirtualUdpSocket
impl VirtualUdpSocket
Sourcepub fn new(physical: Arc<UdpSocket>) -> Arc<Self>
pub fn new(physical: Arc<UdpSocket>) -> Arc<Self>
Build a virtual socket against physical with
DEFAULT_INBOUND_CAPACITY.
Sourcepub fn new_with_capacity(physical: Arc<UdpSocket>, capacity: usize) -> Arc<Self>
pub fn new_with_capacity(physical: Arc<UdpSocket>, capacity: usize) -> Arc<Self>
Build a virtual socket against physical with a custom
inbound queue capacity. capacity of zero is allowed but
drops every enqueue.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Local address of the underlying physical socket.
§Errors
Surfaces the OS error from tokio::net::UdpSocket::local_addr.
Sourcepub fn enqueue_inbound(&self, peer: SocketAddr, datagram: Bytes)
pub fn enqueue_inbound(&self, peer: SocketAddr, datagram: Bytes)
Push one datagram onto this virtual socket’s inbound queue.
Called by the router that owns the physical socket. Drops the
datagram silently when the queue is full or the socket has
been closed — UDP is lossy by design and stalling the router
would block every other virtual socket sharing the physical
endpoint. A tracing::warn! records each drop.
Sourcepub fn try_dequeue(&self) -> Option<(SocketAddr, Bytes)>
pub fn try_dequeue(&self) -> Option<(SocketAddr, Bytes)>
Pop the head of the inbound queue without blocking. Returns
None when the queue is empty (regardless of closed state).
Sourcepub fn poll_dequeue(
&self,
cx: &mut Context<'_>,
) -> Poll<Option<(SocketAddr, Bytes)>>
pub fn poll_dequeue( &self, cx: &mut Context<'_>, ) -> Poll<Option<(SocketAddr, Bytes)>>
Poll for the next inbound datagram.
Poll::Ready(Some((peer, datagram)))when a datagram is available.Poll::Ready(None)whenSelf::closehas been called and the queue has been fully drained — the caller can treat this as a clean end-of-stream.Poll::Pendingotherwise; the waker fromcxis registered and woken on the nextSelf::enqueue_inbound/Self::close.
Sourcepub fn try_send_to(&self, buf: &[u8], target: SocketAddr) -> Result<usize>
pub fn try_send_to(&self, buf: &[u8], target: SocketAddr) -> Result<usize>
Forward an outbound datagram to the physical socket without
blocking. Surfaces WouldBlock to the caller — wait via
Self::poll_send_ready before retrying.
§Errors
Surfaces the OS error from
tokio::net::UdpSocket::try_send_to.
Sourcepub fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>>
pub fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>>
Wait until the physical socket is writable. Proxies to
tokio::net::UdpSocket::poll_send_ready.
§Errors
Surfaces the OS error from poll_send_ready.
Sourcepub fn physical(&self) -> &Arc<UdpSocket>
pub fn physical(&self) -> &Arc<UdpSocket>
Borrow the underlying physical socket. Useful when callers need to invoke socket-level methods (e.g. setting buffer sizes) that virtual-socket does not surface.
Sourcepub fn close(&self)
pub fn close(&self)
Mark this virtual socket closed. New
Self::enqueue_inbound calls are dropped; existing queued
datagrams remain drainable. Once the queue is empty,
Self::poll_dequeue returns Poll::Ready(None) so consumer
tasks can exit cleanly. Idempotent.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Whether Self::close has been called.
Sourcepub fn inbound_len(&self) -> usize
pub fn inbound_len(&self) -> usize
Inbound queue length. Useful for metrics / diagnostics.