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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use {SendDgram, RecvDgram};
use tokio_reactor::{Handle, PollEvented};
use futures::{Async, Poll};
use mio::Ready;
use mio_uds;
use std::fmt;
use std::io;
use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, RawFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;
pub struct UnixDatagram {
io: PollEvented<mio_uds::UnixDatagram>,
}
impl UnixDatagram {
pub fn bind<P>(path: P) -> io::Result<UnixDatagram>
where
P: AsRef<Path>,
{
let socket = mio_uds::UnixDatagram::bind(path)?;
Ok(UnixDatagram::new(socket))
}
pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
let (a, b) = mio_uds::UnixDatagram::pair()?;
let a = UnixDatagram::new(a);
let b = UnixDatagram::new(b);
Ok((a, b))
}
pub fn from_std(datagram: net::UnixDatagram, handle: &Handle) -> io::Result<UnixDatagram> {
let socket = mio_uds::UnixDatagram::from_datagram(datagram)?;
let io = PollEvented::new_with_handle(socket, handle)?;
Ok(UnixDatagram { io })
}
fn new(socket: mio_uds::UnixDatagram) -> UnixDatagram {
let io = PollEvented::new(socket);
UnixDatagram { io }
}
pub fn unbound() -> io::Result<UnixDatagram> {
let socket = mio_uds::UnixDatagram::unbound()?;
Ok(UnixDatagram::new(socket))
}
pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.io.get_ref().connect(path)
}
pub fn poll_read_ready(&self, ready: Ready) -> Poll<Ready, io::Error> {
self.io.poll_read_ready(ready)
}
pub fn poll_write_ready(&self) -> Poll<Ready, io::Error> {
self.io.poll_write_ready()
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.io.get_ref().local_addr()
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.io.get_ref().peer_addr()
}
pub fn poll_recv_from(&self, buf: &mut [u8]) -> Poll<(usize, SocketAddr), io::Error> {
if self.io.poll_read_ready(Ready::readable())?.is_not_ready() {
return Ok(Async::NotReady);
}
let r = self.io.get_ref().recv_from(buf);
if is_wouldblock(&r) {
self.io.clear_read_ready(Ready::readable())?;
return Ok(Async::NotReady);
}
r.map(Async::Ready)
}
pub fn poll_recv(&self, buf: &mut [u8]) -> Poll<usize, io::Error> {
if self.io.poll_read_ready(Ready::readable())?.is_not_ready() {
return Ok(Async::NotReady);
}
let r = self.io.get_ref().recv(buf);
if is_wouldblock(&r) {
self.io.clear_read_ready(Ready::readable())?;
return Ok(Async::NotReady);
}
r.map(Async::Ready)
}
pub fn recv_dgram<T>(self, buf: T) -> RecvDgram<T>
where
T: AsMut<[u8]>,
{
RecvDgram::new(self, buf)
}
pub fn poll_send_to<P>(&self, buf: &[u8], path: P) -> Poll<usize, io::Error>
where
P: AsRef<Path>,
{
if self.io.poll_write_ready()?.is_not_ready() {
return Ok(Async::NotReady);
}
let r = self.io.get_ref().send_to(buf, path);
if is_wouldblock(&r) {
self.io.clear_write_ready()?;
}
r.map(Async::Ready)
}
pub fn poll_send(&self, buf: &[u8]) -> Poll<usize, io::Error> {
if self.io.poll_write_ready()?.is_not_ready() {
return Ok(Async::NotReady);
}
let r = self.io.get_ref().send(buf);
if is_wouldblock(&r) {
self.io.clear_write_ready()?;
}
r.map(Async::Ready)
}
pub fn send_dgram<T, P>(self, buf: T, path: P) -> SendDgram<T, P>
where
T: AsRef<[u8]>,
P: AsRef<Path>,
{
SendDgram::new(self, buf, path)
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.io.get_ref().take_error()
}
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
self.io.get_ref().shutdown(how)
}
}
impl fmt::Debug for UnixDatagram {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.io.get_ref().fmt(f)
}
}
impl AsRawFd for UnixDatagram {
fn as_raw_fd(&self) -> RawFd {
self.io.get_ref().as_raw_fd()
}
}
fn is_wouldblock<T>(r: &io::Result<T>) -> bool {
match *r {
Ok(_) => false,
Err(ref e) => e.kind() == io::ErrorKind::WouldBlock,
}
}