Skip to main content

TimestampedSocket

Struct TimestampedSocket 

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

A UDP socket wrapper that captures timestamps on send/receive.

Supports three timestamping modes (best to worst accuracy):

  1. Hardware (SO_TIMESTAMPING with RAW_HARDWARE) – requires NIC support
  2. Kernel software (SO_TIMESTAMPING or SO_TIMESTAMPNS)
  3. Userspace (NtpTimestamp::now() immediately after syscall)

Implementations§

Source§

impl TimestampedSocket

Source

pub async fn bind(addr: &str) -> Result<Self, Error>

Bind to the given address (e.g. “0.0.0.0:123” or “127.0.0.1:0”).

Source

pub fn from_socket(socket: UdpSocket) -> Self

Wrap an already-bound tokio UdpSocket.

Source

pub async fn send_to( &self, buf: &[u8], addr: SocketAddr, ) -> Result<NtpTimestamp, Error>

Send data to the given address and return the transmit timestamp.

The timestamp is taken as close to the actual send as possible. With software timestamping this is immediately after the syscall returns.

Source

pub async fn recv_from(&self, buf: &mut [u8]) -> Result<ReceivedPacket, Error>

Receive data into the provided buffer.

Returns a ReceivedPacket containing the data (truncated to actual received length), the sender’s address, and the receive timestamp. The timestamp is taken as close to the actual receive as possible.

Source

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

Get the local address this socket is bound to.

Source

pub fn inner(&self) -> &UdpSocket

Get a reference to the inner tokio UdpSocket.

Source

pub fn timestamp_mode(&self) -> TimestampMode

Current timestamping mode.

Source

pub fn enable_software_timestamps(&mut self) -> Result<()>

Enable kernel software timestamps.

On Linux, uses SO_TIMESTAMPNS for nanosecond-resolution software timestamps. On FreeBSD, uses SO_TIMESTAMP for microsecond-resolution timestamps. The kernel timestamps packets in the network stack, which is more accurate than userspace timestamps taken after the syscall returns.

Source

pub fn enable_hardware_timestamps(&mut self) -> Result<bool>

Enable hardware timestamps via SO_TIMESTAMPING (Linux).

Attempts to enable hardware timestamping using SOF_TIMESTAMPING_TX_HARDWARE, SOF_TIMESTAMPING_RX_HARDWARE, and SOF_TIMESTAMPING_RAW_HARDWARE. If hardware timestamping is not supported by the NIC, falls back to software timestamping via SO_TIMESTAMPING with the software flags.

Returns Ok(true) if hardware timestamping is active, Ok(false) if it fell back to software timestamping.

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