Expand description
Tokio async runtime UDP socket adapter for the sntpc SNTP client library.
This crate provides a wrapper around tokio::net::UdpSocket that implements
the NtpUdpSocket trait, enabling asynchronous SNTP requests using the Tokio runtime.
§Design Rationale
The network adapters are separated into their own crates to:
- Enable independent versioning (updating Tokio doesn’t require updating
sntpccore) - Allow version flexibility (works with any Tokio 1.x version)
- Keep the core SNTP protocol logic independent of async runtimes
- Simplify future compatibility (only this adapter needs updating for Tokio 2.x)
§Example
ⓘ
use sntpc::{get_time, NtpContext, StdTimestampGen};
use sntpc_net_tokio::UdpSocketWrapper;
use tokio::net::UdpSocket;
#[tokio::main]
async fn main() {
let socket = UdpSocket::bind("0.0.0.0:0")
.await
.expect("Failed to bind socket");
let socket = UdpSocketWrapper::from(socket);
let context = NtpContext::new(StdTimestampGen::default());
let result = get_time("pool.ntp.org:123".parse().unwrap(), &socket, context).await;
match result {
Ok(time) => println!("Received time: {}.{}", time.sec(), time.sec_fraction()),
Err(e) => eprintln!("Failed to get time: {:?}", e),
}
}For more examples, see the repository examples.
Structs§
- UdpSocket
Wrapper - A wrapper around
tokio::net::UdpSocketthat implementsNtpUdpSocket.