use std::{
io,
net::{SocketAddr, UdpSocket},
task::{Context, Poll},
time::Duration,
};
use futures::{future::BoxFuture, FutureExt};
use crate::GenTransport;
pub type Transport = GenTransport<Provider>;
pub struct Provider;
impl super::Provider for Provider {
type IfWatcher = if_watch::tokio::IfWatcher;
fn runtime() -> super::Runtime {
super::Runtime::Tokio
}
fn new_if_watcher() -> io::Result<Self::IfWatcher> {
if_watch::tokio::IfWatcher::new()
}
fn poll_if_event(
watcher: &mut Self::IfWatcher,
cx: &mut Context<'_>,
) -> Poll<io::Result<if_watch::IfEvent>> {
watcher.poll_if_event(cx)
}
fn sleep(duration: Duration) -> BoxFuture<'static, ()> {
tokio::time::sleep(duration).boxed()
}
fn send_to<'a>(
udp_socket: &'a UdpSocket,
buf: &'a [u8],
target: SocketAddr,
) -> BoxFuture<'a, io::Result<usize>> {
Box::pin(async move {
tokio::net::UdpSocket::from_std(udp_socket.try_clone()?)?
.send_to(buf, target)
.await
})
}
}