Skip to main content

VirtualUdpSocket

Struct VirtualUdpSocket 

Source
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

Source

pub fn new(physical: Arc<UdpSocket>) -> Arc<Self>

Build a virtual socket against physical with DEFAULT_INBOUND_CAPACITY.

Source

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.

Source

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.

Source

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.

Source

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

Source

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) when Self::close has been called and the queue has been fully drained — the caller can treat this as a clean end-of-stream.
  • Poll::Pending otherwise; the waker from cx is registered and woken on the next Self::enqueue_inbound / Self::close.
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn is_closed(&self) -> bool

Whether Self::close has been called.

Source

pub fn inbound_len(&self) -> usize

Inbound queue length. Useful for metrics / diagnostics.

Trait Implementations§

Source§

impl Debug for VirtualUdpSocket

Source§

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

Formats the value using the given formatter. 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> 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<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