Skip to content

Commit

Permalink
reconstruct files
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Dec 8, 2023
1 parent bd0c6c9 commit d7ab90c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 138 deletions.
95 changes: 3 additions & 92 deletions quinn/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,96 +98,7 @@ mod tokio;
#[cfg(feature = "runtime-tokio")]
pub use self::tokio::TokioRuntime;

#[cfg(feature = "runtime-async-std")]
mod async_std;
#[cfg(feature = "runtime-async-std")]
pub use self::async_std::AsyncStdRuntime;

#[cfg(feature = "runtime-smol")]
mod smol;
#[cfg(feature = "runtime-smol")]
pub use self::smol::SmolRuntime;

#[cfg(feature = "async-io")]
mod sealed {
use async_io::{Async, Timer};
use std::{
future::Future,
io,
pin::Pin,
task::{Context, Poll},
time::Instant,
};

use super::{AsyncTimer, AsyncUdpSocket};

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)]
pub(super) struct AsyncIOUdpSocket {
pub(super) io: Async<std::net::UdpSocket>,
pub(super) inner: udp::UdpSocketState,
}

impl AsyncIOUdpSocket {
pub(super) 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()
}
}
}
mod async_io;
#[cfg(feature = "async-io")]
pub use self::async_io::*;
133 changes: 133 additions & 0 deletions quinn/src/runtime/async_io.rs
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()
}
}
23 changes: 0 additions & 23 deletions quinn/src/runtime/async_std.rs

This file was deleted.

23 changes: 0 additions & 23 deletions quinn/src/runtime/smol.rs

This file was deleted.

0 comments on commit d7ab90c

Please sign in to comment.