-
-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use async_io::{Async, Timer}; | ||
use std::{ | ||
future::Future, | ||
io, | ||
pin::Pin, | ||
sync::Arc, | ||
task::{Context, Poll}, | ||
time::Instant, | ||
}; | ||
|
||
use super::{AsyncTimer, AsyncUdpSocket, Runtime}; | ||
|
||
#[cfg(feature = "smol")] | ||
pub use self::smol::SmolRuntime; | ||
|
||
#[cfg(feature = "smol")] | ||
mod smol { | ||
use super::*; | ||
/// A Quinn runtime for smol | ||
#[derive(Debug)] | ||
pub struct SmolRuntime; | ||
|
||
impl Runtime for SmolRuntime { | ||
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> { | ||
Box::pin(Timer::at(t)) | ||
} | ||
|
||
fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) { | ||
::smol::spawn(future).detach(); | ||
} | ||
|
||
fn wrap_udp_socket( | ||
&self, | ||
sock: std::net::UdpSocket, | ||
) -> io::Result<Arc<dyn AsyncUdpSocket>> { | ||
Ok(Arc::new(AsyncIOUdpSocket::new(sock)?)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use self::async_std::AsyncStdRuntime; | ||
|
||
#[cfg(feature = "async-std")] | ||
mod async_std { | ||
use super::*; | ||
/// A Quinn runtime for async-std | ||
#[derive(Debug)] | ||
pub struct AsyncStdRuntime; | ||
|
||
impl Runtime for AsyncStdRuntime { | ||
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> { | ||
Box::pin(Timer::at(t)) | ||
} | ||
|
||
fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) { | ||
::async_std::task::spawn(future); | ||
} | ||
|
||
fn wrap_udp_socket( | ||
&self, | ||
sock: std::net::UdpSocket, | ||
) -> io::Result<Arc<dyn AsyncUdpSocket>> { | ||
Ok(Arc::new(AsyncIOUdpSocket::new(sock)?)) | ||
} | ||
} | ||
} | ||
|
||
impl AsyncTimer for Timer { | ||
fn reset(mut self: Pin<&mut Self>, t: Instant) { | ||
self.set_at(t) | ||
} | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> { | ||
Future::poll(self, cx).map(|_| ()) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct AsyncIOUdpSocket { | ||
io: Async<std::net::UdpSocket>, | ||
inner: udp::UdpSocketState, | ||
} | ||
|
||
impl AsyncIOUdpSocket { | ||
fn new(sock: std::net::UdpSocket) -> io::Result<Self> { | ||
Ok(Self { | ||
inner: udp::UdpSocketState::new((&sock).into())?, | ||
io: Async::new(sock)?, | ||
}) | ||
} | ||
} | ||
|
||
impl AsyncUdpSocket for AsyncIOUdpSocket { | ||
fn poll_send(&self, cx: &mut Context, transmits: &[udp::Transmit]) -> Poll<io::Result<usize>> { | ||
loop { | ||
ready!(self.io.poll_writable(cx))?; | ||
if let Ok(res) = self.inner.send((&self.io).into(), transmits) { | ||
return Poll::Ready(Ok(res)); | ||
} | ||
} | ||
} | ||
|
||
fn poll_recv( | ||
&self, | ||
cx: &mut Context, | ||
bufs: &mut [io::IoSliceMut<'_>], | ||
meta: &mut [udp::RecvMeta], | ||
) -> Poll<io::Result<usize>> { | ||
loop { | ||
ready!(self.io.poll_readable(cx))?; | ||
if let Ok(res) = self.inner.recv((&self.io).into(), bufs, meta) { | ||
return Poll::Ready(Ok(res)); | ||
} | ||
} | ||
} | ||
|
||
fn local_addr(&self) -> io::Result<std::net::SocketAddr> { | ||
self.io.as_ref().local_addr() | ||
} | ||
|
||
fn may_fragment(&self) -> bool { | ||
self.inner.may_fragment() | ||
} | ||
|
||
fn max_transmit_segments(&self) -> usize { | ||
self.inner.max_gso_segments() | ||
} | ||
|
||
fn max_receive_segments(&self) -> usize { | ||
self.inner.gro_segments() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.