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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::io;
use std::net::SocketAddr;
use futures::{Poll, Async, Future};
use mio;
use internal::io_poll::{Interest, EventedHandle};
use sync::oneshot::Monitor;
use super::{into_io_error, Bind};
#[derive(Debug, Clone)]
pub struct UdpSocket {
handle: EventedHandle<mio::net::UdpSocket>,
}
impl UdpSocket {
pub fn bind(addr: SocketAddr) -> UdpSocketBind {
UdpSocketBind(Bind::Bind(addr, mio::net::UdpSocket::bind))
}
pub fn send_to<B: AsRef<[u8]>>(self, buf: B, target: SocketAddr) -> SendTo<B> {
SendTo(Some(SendToInner {
socket: self,
buf: buf,
target: target,
monitor: None,
}))
}
pub fn recv_from<B: AsMut<[u8]>>(self, buf: B) -> RecvFrom<B> {
RecvFrom(Some(RecvFromInner {
socket: self,
buf: buf,
monitor: None,
}))
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.handle.inner().local_addr()
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.handle.inner().take_error()
}
pub unsafe fn with_inner<F, T>(&self, f: F) -> T
where F: FnOnce(&mio::net::UdpSocket) -> T
{
f(&*self.handle.inner())
}
}
#[derive(Debug)]
pub struct UdpSocketBind(Bind<fn(&SocketAddr) -> io::Result<mio::net::UdpSocket>,
mio::net::UdpSocket>);
impl Future for UdpSocketBind {
type Item = UdpSocket;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(self.0
.poll()?
.map(|handle| UdpSocket { handle: handle }))
}
}
#[derive(Debug)]
pub struct SendTo<B>(Option<SendToInner<B>>);
impl<B: AsRef<[u8]>> Future for SendTo<B> {
type Item = (UdpSocket, B, usize);
type Error = (UdpSocket, B, io::Error);
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let mut state = self.0.take().expect("Cannot poll SendTo twice");
loop {
if let Some(mut monitor) = state.monitor.take() {
match monitor.poll() {
Err(e) => return Err((state.socket, state.buf, into_io_error(e))),
Ok(Async::NotReady) => {
state.monitor = Some(monitor);
self.0 = Some(state);
return Ok(Async::NotReady);
}
Ok(Async::Ready(())) => {}
}
} else {
let result = state
.socket
.handle
.inner()
.send_to(state.buf.as_ref(), &state.target);
match result {
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
state.monitor = Some(state.socket.handle.monitor(Interest::Write));
} else {
return Err((state.socket, state.buf, e));
}
}
Ok(size) => return Ok(Async::Ready((state.socket, state.buf, size))),
}
}
}
}
}
#[derive(Debug)]
struct SendToInner<B> {
socket: UdpSocket,
buf: B,
target: SocketAddr,
monitor: Option<Monitor<(), io::Error>>,
}
#[derive(Debug)]
pub struct RecvFrom<B>(Option<RecvFromInner<B>>);
impl<B: AsMut<[u8]>> Future for RecvFrom<B> {
type Item = (UdpSocket, B, usize, SocketAddr);
type Error = (UdpSocket, B, io::Error);
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let mut state = self.0.take().expect("Cannot poll RecvFrom twice");
loop {
if let Some(mut monitor) = state.monitor.take() {
match monitor.poll() {
Err(e) => return Err((state.socket, state.buf, into_io_error(e))),
Ok(Async::NotReady) => {
state.monitor = Some(monitor);
self.0 = Some(state);
return Ok(Async::NotReady);
}
Ok(Async::Ready(())) => {}
}
} else {
let mut buf = state.buf;
let result = state.socket.handle.inner().recv_from(buf.as_mut());
state.buf = buf;
match result {
Err(e) => {
if e.kind() == io::ErrorKind::WouldBlock {
state.monitor = Some(state.socket.handle.monitor(Interest::Read));
} else {
return Err((state.socket, state.buf, e));
}
}
Ok((size, addr)) => {
return Ok(Async::Ready((state.socket, state.buf, size, addr)))
}
}
}
}
}
}
#[derive(Debug)]
struct RecvFromInner<B> {
socket: UdpSocket,
buf: B,
monitor: Option<Monitor<(), io::Error>>,
}