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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use super::UnixStream;
use crate::driver::Handle;
use crate::util::PollEvented;
use futures_core::ready;
use futures_util::future::poll_fn;
use mio::Ready;
use mio_uds;
use std::convert::TryFrom;
use std::fmt;
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;
use std::task::{Context, Poll};
pub struct UnixListener {
io: PollEvented<mio_uds::UnixListener>,
}
impl UnixListener {
pub fn bind<P>(path: P) -> io::Result<UnixListener>
where
P: AsRef<Path>,
{
let listener = mio_uds::UnixListener::bind(path)?;
let io = PollEvented::new(listener);
Ok(UnixListener { io })
}
pub fn from_std(listener: net::UnixListener, handle: &Handle) -> io::Result<UnixListener> {
let listener = mio_uds::UnixListener::from_listener(listener)?;
let io = PollEvented::new_with_handle(listener, handle)?;
Ok(UnixListener { io })
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.io.get_ref().local_addr()
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.io.get_ref().take_error()
}
pub async fn accept(&mut self) -> io::Result<(UnixStream, SocketAddr)> {
poll_fn(|cx| self.poll_accept(cx)).await
}
pub(crate) fn poll_accept(
&mut self,
cx: &mut Context<'_>,
) -> Poll<io::Result<(UnixStream, SocketAddr)>> {
let (io, addr) = ready!(self.poll_accept_std(cx))?;
let io = mio_uds::UnixStream::from_stream(io)?;
Ok((UnixStream::new(io), addr)).into()
}
fn poll_accept_std(
&mut self,
cx: &mut Context<'_>,
) -> Poll<io::Result<(net::UnixStream, SocketAddr)>> {
ready!(self.io.poll_read_ready(cx, Ready::readable()))?;
match self.io.get_ref().accept_std() {
Ok(None) => {
self.io.clear_read_ready(cx, Ready::readable())?;
Poll::Pending
}
Ok(Some((sock, addr))) => Ok((sock, addr)).into(),
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => {
self.io.clear_read_ready(cx, Ready::readable())?;
Poll::Pending
}
Err(err) => Err(err).into(),
}
}
#[cfg(feature = "async-traits")]
pub fn incoming(self) -> super::Incoming {
super::Incoming::new(self)
}
}
impl TryFrom<UnixListener> for mio_uds::UnixListener {
type Error = io::Error;
fn try_from(value: UnixListener) -> Result<Self, Self::Error> {
value.io.into_inner()
}
}
impl TryFrom<net::UnixListener> for UnixListener {
type Error = io::Error;
fn try_from(stream: net::UnixListener) -> io::Result<Self> {
Self::from_std(stream, &Handle::default())
}
}
impl fmt::Debug for UnixListener {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.io.get_ref().fmt(f)
}
}
impl AsRawFd for UnixListener {
fn as_raw_fd(&self) -> RawFd {
self.io.get_ref().as_raw_fd()
}
}