|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::{ |
|
|
io::Error, |
|
|
net::{SocketAddr, UdpSocket}, |
|
|
task::{Context, Poll}, |
|
|
}; |
|
|
|
|
|
|
|
|
#[allow(unreachable_pub)] |
|
|
pub trait AsyncSocket: Unpin + Send + 'static { |
|
|
|
|
|
fn from_std(socket: UdpSocket) -> std::io::Result<Self> |
|
|
where |
|
|
Self: Sized; |
|
|
|
|
|
|
|
|
fn poll_read( |
|
|
&mut self, |
|
|
_cx: &mut Context, |
|
|
_buf: &mut [u8], |
|
|
) -> Poll<Result<(usize, SocketAddr), Error>>; |
|
|
|
|
|
|
|
|
fn poll_write( |
|
|
&mut self, |
|
|
_cx: &mut Context, |
|
|
_packet: &[u8], |
|
|
_to: SocketAddr, |
|
|
) -> Poll<Result<(), Error>>; |
|
|
} |
|
|
|
|
|
#[cfg(feature = "async-io")] |
|
|
pub(crate) mod asio { |
|
|
use super::*; |
|
|
use async_io::Async; |
|
|
use futures::FutureExt; |
|
|
|
|
|
|
|
|
pub(crate) type AsyncUdpSocket = Async<UdpSocket>; |
|
|
impl AsyncSocket for AsyncUdpSocket { |
|
|
fn from_std(socket: UdpSocket) -> std::io::Result<Self> { |
|
|
Async::new(socket) |
|
|
} |
|
|
|
|
|
fn poll_read( |
|
|
&mut self, |
|
|
cx: &mut Context, |
|
|
buf: &mut [u8], |
|
|
) -> Poll<Result<(usize, SocketAddr), Error>> { |
|
|
|
|
|
futures::ready!(self.poll_readable(cx))?; |
|
|
match self.recv_from(buf).now_or_never() { |
|
|
Some(data) => Poll::Ready(data), |
|
|
None => Poll::Pending, |
|
|
} |
|
|
} |
|
|
|
|
|
fn poll_write( |
|
|
&mut self, |
|
|
cx: &mut Context, |
|
|
packet: &[u8], |
|
|
to: SocketAddr, |
|
|
) -> Poll<Result<(), Error>> { |
|
|
futures::ready!(self.poll_writable(cx))?; |
|
|
match self.send_to(packet, to).now_or_never() { |
|
|
Some(Ok(_)) => Poll::Ready(Ok(())), |
|
|
Some(Err(err)) => Poll::Ready(Err(err)), |
|
|
None => Poll::Pending, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(feature = "tokio")] |
|
|
pub(crate) mod tokio { |
|
|
use super::*; |
|
|
use ::tokio::{io::ReadBuf, net::UdpSocket as TkUdpSocket}; |
|
|
|
|
|
|
|
|
pub(crate) type TokioUdpSocket = TkUdpSocket; |
|
|
impl AsyncSocket for TokioUdpSocket { |
|
|
fn from_std(socket: UdpSocket) -> std::io::Result<Self> { |
|
|
socket.set_nonblocking(true)?; |
|
|
TokioUdpSocket::from_std(socket) |
|
|
} |
|
|
|
|
|
fn poll_read( |
|
|
&mut self, |
|
|
cx: &mut Context, |
|
|
buf: &mut [u8], |
|
|
) -> Poll<Result<(usize, SocketAddr), Error>> { |
|
|
let mut rbuf = ReadBuf::new(buf); |
|
|
match self.poll_recv_from(cx, &mut rbuf) { |
|
|
Poll::Pending => Poll::Pending, |
|
|
Poll::Ready(Err(err)) => Poll::Ready(Err(err)), |
|
|
Poll::Ready(Ok(addr)) => Poll::Ready(Ok((rbuf.filled().len(), addr))), |
|
|
} |
|
|
} |
|
|
|
|
|
fn poll_write( |
|
|
&mut self, |
|
|
cx: &mut Context, |
|
|
packet: &[u8], |
|
|
to: SocketAddr, |
|
|
) -> Poll<Result<(), Error>> { |
|
|
match self.poll_send_to(cx, packet, to) { |
|
|
Poll::Pending => Poll::Pending, |
|
|
Poll::Ready(Err(err)) => Poll::Ready(Err(err)), |
|
|
Poll::Ready(Ok(_len)) => Poll::Ready(Ok(())), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|