2019-07-23 03:17:29 +00:00
|
|
|
use std::io;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2020-10-17 13:49:45 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
2019-07-23 03:17:29 +00:00
|
|
|
use tokio::net::TcpStream;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use tokio::net::UnixStream;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Inner {
|
|
|
|
Tcp(TcpStream),
|
2019-07-29 22:17:45 +00:00
|
|
|
#[cfg(unix)]
|
2019-07-23 03:17:29 +00:00
|
|
|
Unix(UnixStream),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The standard stream type used by the crate.
|
|
|
|
///
|
|
|
|
/// Requires the `runtime` Cargo feature (enabled by default).
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Socket(Inner);
|
|
|
|
|
|
|
|
impl Socket {
|
|
|
|
pub(crate) fn new_tcp(stream: TcpStream) -> Socket {
|
|
|
|
Socket(Inner::Tcp(stream))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub(crate) fn new_unix(stream: UnixStream) -> Socket {
|
|
|
|
Socket(Inner::Unix(stream))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for Socket {
|
|
|
|
fn poll_read(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
2020-10-17 13:49:45 +00:00
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
) -> Poll<io::Result<()>> {
|
2019-07-23 03:17:29 +00:00
|
|
|
match &mut self.0 {
|
|
|
|
Inner::Tcp(s) => Pin::new(s).poll_read(cx, buf),
|
|
|
|
#[cfg(unix)]
|
|
|
|
Inner::Unix(s) => Pin::new(s).poll_read(cx, buf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncWrite for Socket {
|
|
|
|
fn poll_write(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
buf: &[u8],
|
|
|
|
) -> Poll<io::Result<usize>> {
|
|
|
|
match &mut self.0 {
|
|
|
|
Inner::Tcp(s) => Pin::new(s).poll_write(cx, buf),
|
|
|
|
#[cfg(unix)]
|
|
|
|
Inner::Unix(s) => Pin::new(s).poll_write(cx, buf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
match &mut self.0 {
|
|
|
|
Inner::Tcp(s) => Pin::new(s).poll_flush(cx),
|
|
|
|
#[cfg(unix)]
|
|
|
|
Inner::Unix(s) => Pin::new(s).poll_flush(cx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
match &mut self.0 {
|
|
|
|
Inner::Tcp(s) => Pin::new(s).poll_shutdown(cx),
|
|
|
|
#[cfg(unix)]
|
|
|
|
Inner::Unix(s) => Pin::new(s).poll_shutdown(cx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|