1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::io;
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};
use hyper::server::accept::Accept;
pub use hyper::Server;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use super::Listener;
use crate::addr::SocketAddr;
use crate::transport::Transport;
#[cfg(unix)]
pub struct UnixListener {
incoming: tokio::net::UnixListener,
}
#[cfg(unix)]
impl UnixListener {
#[inline]
pub fn bind(path: impl AsRef<Path>) -> UnixListener {
Self::try_bind(path).unwrap()
}
#[inline]
pub fn try_bind(path: impl AsRef<Path>) -> io::Result<UnixListener> {
Ok(UnixListener {
incoming: tokio::net::UnixListener::bind(path)?,
})
}
}
#[cfg(unix)]
impl Listener for UnixListener {}
#[cfg(unix)]
impl Accept for UnixListener {
type Conn = UnixStream;
type Error = io::Error;
fn poll_accept(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
match self.incoming.poll_accept(cx) {
Poll::Ready(Ok((stream, remote_addr))) => {
Poll::Ready(Some(Ok(UnixStream::new(stream, remote_addr.into()))))
}
Poll::Ready(Err(err)) => Poll::Ready(Some(Err(err))),
Poll::Pending => Poll::Pending,
}
}
}
pub struct UnixStream {
inner_stream: tokio::net::UnixStream,
remote_addr: SocketAddr,
}
impl Transport for UnixStream {
fn remote_addr(&self) -> Option<SocketAddr> {
Some(self.remote_addr.clone())
}
}
impl UnixStream {
fn new(inner_stream: tokio::net::UnixStream, remote_addr: SocketAddr) -> Self {
UnixStream {
inner_stream,
remote_addr,
}
}
}
impl AsyncRead for UnixStream {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut ReadBuf) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner_stream).poll_read(cx, buf)
}
}
impl AsyncWrite for UnixStream {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().inner_stream).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner_stream).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner_stream).poll_shutdown(cx)
}
}