use std::{
fmt::Debug,
future::Future,
io,
num::NonZeroUsize,
pin::Pin,
sync::Arc,
task::{Context, Poll, ready},
time::Instant,
};
use tokio::{
io::Interest,
time::{Sleep, sleep_until},
};
use super::{AsyncTimer, AsyncUdpSocket, Runtime, UdpSenderHelper, UdpSenderHelperSocket};
#[derive(Debug)]
pub struct TokioRuntime;
impl Runtime for TokioRuntime {
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> {
Box::pin(sleep_until(t.into()))
}
fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
tokio::spawn(future);
}
fn wrap_udp_socket(&self, sock: std::net::UdpSocket) -> io::Result<Box<dyn AsyncUdpSocket>> {
Ok(Box::new(UdpSocket {
inner: Arc::new(udp::UdpSocketState::new((&sock).into())?),
io: Arc::new(tokio::net::UdpSocket::from_std(sock)?),
}))
}
fn now(&self) -> Instant {
tokio::time::Instant::now().into_std()
}
}
impl AsyncTimer for Sleep {
fn reset(self: Pin<&mut Self>, t: Instant) {
Self::reset(self, t.into())
}
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
Future::poll(self, cx)
}
}
#[derive(Debug, Clone)]
struct UdpSocket {
io: Arc<tokio::net::UdpSocket>,
inner: Arc<udp::UdpSocketState>,
}
impl UdpSenderHelperSocket for UdpSocket {
fn max_transmit_segments(&self) -> NonZeroUsize {
self.inner.max_gso_segments()
}
fn try_send(&self, transmit: &udp::Transmit<'_>) -> io::Result<()> {
self.io.try_io(Interest::WRITABLE, || {
self.inner.send((&self.io).into(), transmit)
})
}
}
impl AsyncUdpSocket for UdpSocket {
fn create_sender(&self) -> Pin<Box<dyn super::UdpSender>> {
Box::pin(UdpSenderHelper::new(self.clone(), |socket: &Self| {
let socket = socket.clone();
async move { socket.io.writable().await }
}))
}
fn poll_recv(
&mut self,
cx: &mut Context<'_>,
bufs: &mut [std::io::IoSliceMut<'_>],
meta: &mut [udp::RecvMeta],
) -> Poll<io::Result<usize>> {
loop {
ready!(self.io.poll_recv_ready(cx))?;
if let Ok(res) = self.io.try_io(Interest::READABLE, || {
self.inner.recv((&self.io).into(), bufs, meta)
}) {
return Poll::Ready(Ok(res));
}
}
}
fn local_addr(&self) -> io::Result<std::net::SocketAddr> {
self.io.local_addr()
}
fn may_fragment(&self) -> bool {
self.inner.may_fragment()
}
fn max_receive_segments(&self) -> NonZeroUsize {
self.inner.gro_segments()
}
}