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):
- Hardware (SO_TIMESTAMPING with RAW_HARDWARE) – requires NIC support
- Kernel software (SO_TIMESTAMPING or SO_TIMESTAMPNS)
- Userspace (NtpTimestamp::now() immediately after syscall)
Implementations§
Source§impl TimestampedSocket
impl TimestampedSocket
Sourcepub async fn bind(addr: &str) -> Result<Self, Error>
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”).
Sourcepub fn from_socket(socket: UdpSocket) -> Self
pub fn from_socket(socket: UdpSocket) -> Self
Wrap an already-bound tokio UdpSocket.
Sourcepub async fn send_to(
&self,
buf: &[u8],
addr: SocketAddr,
) -> Result<NtpTimestamp, Error>
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.
Sourcepub async fn recv_from(&self, buf: &mut [u8]) -> Result<ReceivedPacket, Error>
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.
Sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Get the local address this socket is bound to.
Sourcepub fn timestamp_mode(&self) -> TimestampMode
pub fn timestamp_mode(&self) -> TimestampMode
Current timestamping mode.
Sourcepub fn enable_software_timestamps(&mut self) -> Result<()>
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.
Sourcepub fn enable_hardware_timestamps(&mut self) -> Result<bool>
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.