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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
extern crate futures;
extern crate future_utils;
extern crate tokio;
#[macro_use]
extern crate unwrap;
extern crate bytes;
extern crate void;
#[cfg(test)]
#[macro_use]
extern crate net_literals;
#[macro_use]
extern crate log;

use std::{mem, io};
use std::collections::{hash_map, HashMap};
use std::sync::{Arc, Mutex};
use std::net::SocketAddr;
use bytes::{BytesMut, Bytes};
use futures::{Async, AsyncSink, Stream, Sink};
use future_utils::mpsc::{self, UnboundedReceiver, UnboundedSender};
use tokio::net::UdpSocket;
use void::{ResultVoidExt};

/// A UDP socket that can easily be shared amongst a bunch of different futures.
pub struct SharedUdpSocket {
    inner: Arc<SharedUdpSocketInner>,
}

pub struct IncomingEndpoints {
    inner: Arc<SharedUdpSocketInner>,
    incoming_rx: UnboundedReceiver<UdpEndpoint>,
    buffer: BytesMut,
}

/// A `Sink`/`Stream` that can be used to send/receive packets to/from a particular address.
/// 
/// These can be created by calling `SharedUdpSocket::endpoint`. `SharedUdpSocket` will also
/// yield these (when used as a `Stream`) when it receives a packet from a new address.
pub struct UdpEndpoint {
    inner: Arc<SharedUdpSocketInner>,
    incoming_rx: UnboundedReceiver<Bytes>,
    addr: SocketAddr,
    buffer: BytesMut,
}

struct SharedUdpSocketInner {
    socket: Mutex<Option<UdpSocket>>,
    endpoints: Mutex<HashMap<SocketAddr, UnboundedSender<Bytes>>>,
    incoming_tx: Mutex<Option<UnboundedSender<UdpEndpoint>>>,
}

impl SharedUdpSocket {
    /// Create a new `SharedUdpSocket` from a `UdpSocket`.
    pub fn share(socket: UdpSocket) -> (SharedUdpSocket, IncomingEndpoints) {
        trace!("creating shared udp socket on address {:?}", socket.local_addr());
        let (tx, rx) = mpsc::unbounded();
        let inner = SharedUdpSocketInner {
            socket: Mutex::new(Some(socket)),
            endpoints: Mutex::new(HashMap::new()),
            incoming_tx: Mutex::new(Some(tx)),
        };
        let inner = Arc::new(inner);
        let shared = SharedUdpSocket {
            inner: inner.clone(),
        };
        let incoming = IncomingEndpoints {
            inner,
            incoming_rx: rx,
            buffer: BytesMut::new(),
        };
        (shared, incoming)
    }

    /// Creates a `UdpEndpoint` object which receives all packets that arrive from the given
    /// address. `UdpEndpoint` can also be used as a `Sink` to send packets. If another
    /// `UdpEndpoint` with the given address already exists then it will no longer receive packets
    /// since the newly created `UdpEndpoint` will take precedence.
    pub fn endpoint(&self, addr: SocketAddr) -> UdpEndpoint {
        let (tx, endpoint) = endpoint_new(&self.inner, addr);
        let mut endpoints = unwrap!(self.inner.endpoints.lock());
        let _ = endpoints.insert(addr, tx);
        endpoint
    }

    /// Creates a `UdpEndpoint` object which receives all packets that arrive from the given
    /// address. `UdpEndpoint` can also be used as a `Sink` to send packets. Unlike the `endpoint`
    /// method, this method will not replace any pre-existing `UdpEndpoint` associated with the
    /// given address and will instead return `None` if one exists.
    pub fn try_endpoint(&self, addr: SocketAddr) -> Option<UdpEndpoint> {
        let mut endpoints = unwrap!(self.inner.endpoints.lock());
        match endpoints.entry(addr) {
            hash_map::Entry::Occupied(..) => None,
            hash_map::Entry::Vacant(ve) => {
                let (tx, endpoint) = endpoint_new(&self.inner, addr);
                let _ = ve.insert(tx);
                Some(endpoint)
            },
        }
    }

    /// Steals the udp socket (if it hasn't already been stolen) causing all other
    /// `SharedUdpSocket` and `UdpEndpoint` streams to end.
    pub fn steal(self) -> Option<UdpSocket> {
        let mut socket_opt = unwrap!(self.inner.socket.lock());
        socket_opt.take()
    }

    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        self.inner.local_addr()
    }
}

fn pump(inner: &Arc<SharedUdpSocketInner>, buffer: &mut BytesMut) -> io::Result<()> {
    let mut socket_opt = unwrap!(inner.socket.lock());
    let socket = match *socket_opt {
        Some(ref mut socket) => socket,
        None => return Ok(()),
    };

    loop {
        let min_capacity = 64 * 1024 + 1;
        let capacity = buffer.capacity();
        if capacity < min_capacity {
            buffer.reserve(min_capacity - capacity);
        }
        let capacity = buffer.capacity();
        unsafe {
            buffer.set_len(capacity)
        }
        match socket.poll_recv_from(&mut *buffer) {
            Ok(Async::Ready((n, addr))) => {
                if n == buffer.len() {
                    return Err(io::Error::new(
                        io::ErrorKind::Other,
                        "failed to recv entire dgram",
                    ));
                }
                let data = buffer.split_to(n).freeze();
                let mut endpoints = unwrap!(inner.endpoints.lock());
                let drop_after_unlock = match endpoints.entry(addr) {
                    hash_map::Entry::Occupied(mut oe) => {
                        match oe.get().unbounded_send(data) {
                            Ok(()) => None,
                            Err(send_error) => {
                                if let Some(ref incoming_tx) = *unwrap!(inner.incoming_tx.lock()) {
                                    let (tx, endpoint) = endpoint_new(inner, addr);

                                    unwrap!(tx.unbounded_send(send_error.into_inner()));
                                    let _ = mem::replace(oe.get_mut(), tx);
                                    match incoming_tx.unbounded_send(endpoint) {
                                        Ok(()) => None,
                                        Err(send_error) => Some(send_error.into_inner()),
                                    }
                                } else {
                                    None
                                }
                            },
                        }
                    },
                    hash_map::Entry::Vacant(ve) => {
                        if let Some(ref incoming_tx) = *unwrap!(inner.incoming_tx.lock()) {
                            let (tx, endpoint) = endpoint_new(inner, addr);

                            unwrap!(tx.unbounded_send(data));
                            ve.insert(tx);
                            match incoming_tx.unbounded_send(endpoint) {
                                Ok(()) => None,
                                Err(send_error) => Some(send_error.into_inner()),
                            }
                        } else {
                            None
                        }
                    },
                };
                drop(endpoints);
                drop(drop_after_unlock);
            },
            Ok(Async::NotReady) => return Ok(()),
            Err(e) => {
                match e.kind() {
                    io::ErrorKind::WouldBlock => return Ok(()),
                    io::ErrorKind::ConnectionReset => continue,
                    _ => return Err(e),
                }
            },
        }
    }
}

fn endpoint_new(inner: &Arc<SharedUdpSocketInner>, addr: SocketAddr) -> (UnboundedSender<Bytes>, UdpEndpoint) {
    let (tx, rx) = mpsc::unbounded();
    let inner = inner.clone();
    let endpoint = UdpEndpoint {
        inner: inner,
        incoming_rx: rx,
        addr: addr,
        buffer: BytesMut::new(),
    };
    (tx, endpoint)
}

impl UdpEndpoint {
    /// Get the remote address that this `UdpEndpoint` sends/receives packets to/from.
    pub fn remote_addr(&self) -> SocketAddr {
        self.addr
    }

    /// Steals the udp socket (if it hasn't already been stolen) causing all other
    /// `SharedUdpSocket` and `UdpEndpoint` streams to end.
    pub fn steal(self) -> Option<UdpSocket> {
        let mut socket_opt = unwrap!(self.inner.socket.lock());
        socket_opt.take()
    }

    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        self.inner.local_addr()
    }
}

impl SharedUdpSocketInner {
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        let socket_opt = unwrap!(self.socket.lock());
        match *socket_opt {
            Some(ref socket) => socket.local_addr(),
            None => Err(io::Error::new(io::ErrorKind::Other, "socket has been stolen")),
        }
    }
}

impl Stream for IncomingEndpoints {
    type Item = UdpEndpoint;
    type Error = io::Error;

    fn poll(&mut self) -> io::Result<Async<Option<UdpEndpoint>>> {
        pump(&self.inner, &mut self.buffer)?;

        Ok(self.incoming_rx.poll().void_unwrap())
    }
}

impl Stream for UdpEndpoint {
    type Item = Bytes;
    type Error = io::Error;

    fn poll(&mut self) -> io::Result<Async<Option<Bytes>>> {
        pump(&self.inner, &mut self.buffer)?;

        Ok(self.incoming_rx.poll().void_unwrap())
    }
}

impl Sink for UdpEndpoint {
    type SinkItem = Bytes;
    type SinkError = io::Error;

    fn start_send(&mut self, item: Bytes) -> io::Result<AsyncSink<Bytes>> {
        let mut socket_opt = unwrap!(self.inner.socket.lock());
        let socket = match *socket_opt {
            Some(ref mut socket) => socket,
            None => return Err(io::ErrorKind::NotConnected.into()),
        };

        match socket.poll_send_to(&item, &self.addr) {
            Ok(Async::Ready(n)) => {
                if n != item.len() {
                    return Err(io::Error::new(
                        io::ErrorKind::Other,
                        "failed to send entire dgram",
                    ));
                }
                return Ok(AsyncSink::Ready);
            },
            Ok(Async::NotReady) => return Ok(AsyncSink::NotReady(item)),
            Err(e) => {
                if e.kind() == io::ErrorKind::WouldBlock {
                    return Ok(AsyncSink::NotReady(item));
                }
                return Err(e);
            },
        }
    }

    fn poll_complete(&mut self) -> io::Result<Async<()>> {
        Ok(Async::Ready(()))
    }
}

impl Drop for SharedUdpSocket {
    fn drop(&mut self) {
        let mut incoming_tx = unwrap!(self.inner.incoming_tx.lock());
        *incoming_tx = None;
    }
}

impl Drop for UdpEndpoint {
    fn drop(&mut self) {
        let mut endpoints = unwrap!(self.inner.endpoints.lock());
        let _ = endpoints.remove(&self.addr);
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use futures::Future;

    #[test]
    fn test() {
        let sock0 = unwrap!(UdpSocket::bind(&addr!("127.0.0.1:0")));
        let addr0 = unwrap!(sock0.local_addr());
        let sock1 = unwrap!(UdpSocket::bind(&addr!("127.0.0.1:0")));
        let addr1 = unwrap!(sock1.local_addr());

        let shared = unwrap!(UdpSocket::bind(&addr!("127.0.0.1:0")));
        let shared_addr = unwrap!(shared.local_addr());
        let (_shared, incoming) = SharedUdpSocket::share(shared);

        tokio::run({
            sock0
            .send_dgram(b"qqqq", &shared_addr)
            .map_err(|e| panic!("{}", e))
            .and_then(move |(sock0, _)| {
                incoming
                .into_future()
                .map_err(|(e, _)| panic!("{}", e))
                .and_then(move |(opt, shared)| {
                    let endpoint_0 = unwrap!(opt);
                    assert_eq!(endpoint_0.remote_addr(), addr0);

                    endpoint_0
                    .into_future()
                    .map_err(|(e, _)| panic!("{}", e))
                    .and_then(move |(opt, endpoint_0)| {
                        let data = unwrap!(opt);
                        assert_eq!(&data[..], b"qqqq");

                        sock0
                        .send_dgram(b"wwww", &shared_addr)
                        .map_err(|e| panic!("{}", e))
                        .and_then(move |(sock0, _)| {
                            sock1
                            .send_dgram(b"eeee", &shared_addr)
                            .map_err(|e| panic!("{}", e))
                            .and_then(move |_sock1| {
                                shared
                                .into_future()
                                .map_err(|(e, _)| panic!("{}", e))
                                .and_then(move |(opt, shared)| {
                                    let endpoint_1 = unwrap!(opt);
                                    assert_eq!(endpoint_1.remote_addr(), addr1);
                                    drop(shared);

                                    endpoint_1
                                    .into_future()
                                    .map_err(|(e, _)| panic!("{}", e))
                                    .and_then(move |(opt, _endpoint_1)| {
                                        let data = unwrap!(opt);
                                        assert_eq!(&data[..], b"eeee");

                                        endpoint_0
                                        .into_future()
                                        .map_err(|(e, _)| panic!("{}", e))
                                        .and_then(move |(opt, endpoint_0)| {
                                            let data = unwrap!(opt);
                                            assert_eq!(&data[..], b"wwww");

                                            endpoint_0
                                            .send(Bytes::from(&b"rrrr"[..]))
                                            .map_err(|e| panic!("{}", e))
                                            .and_then(move |endpoint_0| {
                                                let buff = [0; 10];

                                                sock0
                                                .recv_dgram(buff)
                                                .map_err(|e| panic!("{}", e))
                                                .map(move |(_sock0, data, len, addr)| {
                                                    assert_eq!(addr, shared_addr);
                                                    assert_eq!(&data[..len], b"rrrr");
                                                    assert!(endpoint_0.steal().is_some());
                                                })
                                            })
                                        })
                                    })
                                })
                            })
                        })
                    })
                })
            })
        });
    }
}