-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
1 parent
7ef7a5c
commit c12d09c
Showing
19 changed files
with
441 additions
and
65 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
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
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,51 @@ | ||
use super::{AsyncWrappedUdpSocket, Runtime}; | ||
use async_io::Async; | ||
use std::io; | ||
use std::task::{Context, Poll}; | ||
|
||
impl AsyncWrappedUdpSocket for Async<std::net::UdpSocket> { | ||
fn poll_read_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> { | ||
Async::poll_readable(self, cx) | ||
} | ||
|
||
fn poll_write_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> { | ||
Async::poll_writable(self, cx) | ||
} | ||
|
||
fn clear_read_ready(&self, _cx: &mut Context) { | ||
// async-std doesn't need this | ||
} | ||
|
||
fn clear_write_ready(&self, _cx: &mut Context) { | ||
// async-std doesn't need this | ||
} | ||
|
||
fn try_recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, std::net::SocketAddr)> { | ||
self.get_ref().recv_from(buf) | ||
} | ||
|
||
fn try_send_to(&self, buf: &[u8], target: std::net::SocketAddr) -> io::Result<usize> { | ||
self.get_ref().send_to(buf, target) | ||
} | ||
|
||
fn local_addr(&self) -> io::Result<std::net::SocketAddr> { | ||
self.get_ref().local_addr() | ||
} | ||
|
||
#[cfg(unix)] | ||
fn get_ref(&self) -> &std::net::UdpSocket { | ||
Async::get_ref(self) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct AsyncStdRuntime; | ||
|
||
impl Runtime for AsyncStdRuntime { | ||
fn wrap_udp_socket( | ||
&self, | ||
t: std::net::UdpSocket, | ||
) -> io::Result<Box<dyn AsyncWrappedUdpSocket>> { | ||
Ok(Box::new(Async::new(t)?)) | ||
} | ||
} |
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,39 @@ | ||
#[cfg(feature = "runtime-tokio")] | ||
mod tokio_runtime; | ||
#[cfg(feature = "runtime-tokio")] | ||
pub use tokio_runtime::*; | ||
|
||
#[cfg(feature = "runtime-async-std")] | ||
mod async_std_runtime; | ||
#[cfg(feature = "runtime-async-std")] | ||
pub use async_std_runtime::*; | ||
|
||
use std::fmt::Debug; | ||
use std::io; | ||
use std::task::{Context, Poll}; | ||
|
||
pub trait AsyncWrappedUdpSocket: Send + Debug { | ||
fn poll_read_ready(&self, cx: &mut Context) -> Poll<io::Result<()>>; | ||
|
||
fn poll_write_ready(&self, cx: &mut Context) -> Poll<io::Result<()>>; | ||
|
||
fn clear_read_ready(&self, cx: &mut Context); | ||
|
||
fn clear_write_ready(&self, cx: &mut Context); | ||
|
||
fn try_recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, std::net::SocketAddr)>; | ||
|
||
fn try_send_to(&self, buf: &[u8], target: std::net::SocketAddr) -> io::Result<usize>; | ||
|
||
fn local_addr(&self) -> io::Result<std::net::SocketAddr>; | ||
|
||
// On Unix we expect to be able to access the underlying std UdpSocket | ||
// to be able to implement more advanced features | ||
#[cfg(unix)] | ||
fn get_ref(&self) -> &std::net::UdpSocket; | ||
} | ||
|
||
pub trait Runtime: Send + Sync + Debug + 'static { | ||
fn wrap_udp_socket(&self, t: std::net::UdpSocket) | ||
-> io::Result<Box<dyn AsyncWrappedUdpSocket>>; | ||
} |
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,99 @@ | ||
use super::{AsyncWrappedUdpSocket, Runtime}; | ||
use std::io; | ||
use std::task::{Context, Poll}; | ||
#[cfg(unix)] | ||
use tokio::io::unix::AsyncFd; | ||
|
||
#[cfg(unix)] | ||
impl AsyncWrappedUdpSocket for AsyncFd<std::net::UdpSocket> { | ||
fn poll_read_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> { | ||
AsyncFd::poll_read_ready(self, cx).map(|x| x.map(|_| ())) | ||
} | ||
|
||
fn poll_write_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> { | ||
AsyncFd::poll_write_ready(self, cx).map(|x| x.map(|_| ())) | ||
} | ||
|
||
fn clear_read_ready(&self, cx: &mut Context) { | ||
match self.poll_read_ready(cx) { | ||
Poll::Pending => {} | ||
Poll::Ready(Err(_)) => {} | ||
Poll::Ready(Ok(mut guard)) => guard.clear_ready(), | ||
} | ||
} | ||
|
||
fn clear_write_ready(&self, cx: &mut Context) { | ||
match self.poll_write_ready(cx) { | ||
Poll::Pending => {} | ||
Poll::Ready(Err(_)) => {} | ||
Poll::Ready(Ok(mut guard)) => guard.clear_ready(), | ||
} | ||
} | ||
|
||
fn try_recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, std::net::SocketAddr)> { | ||
self.get_ref().recv_from(buf) | ||
} | ||
|
||
fn try_send_to(&self, buf: &[u8], target: std::net::SocketAddr) -> io::Result<usize> { | ||
self.get_ref().send_to(buf, target) | ||
} | ||
|
||
fn local_addr(&self) -> io::Result<std::net::SocketAddr> { | ||
self.get_ref().local_addr() | ||
} | ||
|
||
fn get_ref(&self) -> &std::net::UdpSocket { | ||
AsyncFd::get_ref(self) | ||
} | ||
} | ||
|
||
#[cfg(not(unix))] | ||
impl AsyncWrappedUdpSocket for tokio::net::UdpSocket { | ||
fn poll_read_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> { | ||
tokio::net::UdpSocket::poll_recv_ready(self, cx) | ||
} | ||
|
||
fn poll_write_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> { | ||
tokio::net::UdpSocket::poll_send_ready(self, cx) | ||
} | ||
|
||
fn clear_read_ready(&self, _cx: &mut Context) { | ||
// not necessary because tokio::net::UdpSocket::try_recv_from already uses try_io | ||
} | ||
|
||
fn clear_write_ready(&self, _cx: &mut Context) { | ||
// not necessary because tokio::net::UdpSocket::try_send_from already uses try_io | ||
} | ||
|
||
fn try_recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, std::net::SocketAddr)> { | ||
tokio::net::UdpSocket::try_recv_from(self, buf) | ||
} | ||
|
||
fn try_send_to(&self, buf: &[u8], target: std::net::SocketAddr) -> io::Result<usize> { | ||
tokio::net::UdpSocket::try_send_to(self, buf, target) | ||
} | ||
|
||
fn local_addr(&self) -> io::Result<std::net::SocketAddr> { | ||
tokio::net::UdpSocket::local_addr(self) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct TokioRuntime; | ||
|
||
impl Runtime for TokioRuntime { | ||
fn wrap_udp_socket( | ||
&self, | ||
t: std::net::UdpSocket, | ||
) -> io::Result<Box<dyn AsyncWrappedUdpSocket>> { | ||
t.set_nonblocking(true)?; | ||
#[cfg(unix)] | ||
{ | ||
Ok(Box::new(AsyncFd::new(t)?)) | ||
} | ||
#[cfg(not(unix))] | ||
{ | ||
Ok(Box::new(tokio::net::UdpSocket::from_std(t)?)) | ||
} | ||
} | ||
} |
Oops, something went wrong.