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
use std::io;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};

use futures::{Future, Poll, Async};
use tokio::net::UdpSocket;

use crate::UdpDatagram;

/// A future representing a UDP datagram currently being sent.
#[derive(Debug)]
pub struct SendTo<'a> {
    socket: Arc<Mutex<UdpSocket>>,
    buffer: &'a [u8],
    addr: SocketAddr,
}

impl<'a> SendTo<'a> {
    pub(crate) fn new(socket: Arc<Mutex<UdpSocket>>, buffer: &'a [u8], addr: SocketAddr) -> SendTo {
        SendTo { socket, buffer, addr }
    }
}

impl<'a> Future for SendTo<'a> {
    type Item = ();
    type Error = io::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let _ = try_ready!{
            self.socket.lock().unwrap().poll_send_to(&self.buffer, &self.addr)
        };

        Ok(Async::Ready(()))
    }
}

/// A future representing a UDP datagram currently being sent.
#[derive(Debug)]
pub struct Send {
    socket: Arc<Mutex<UdpSocket>>,
    datagram: UdpDatagram,
}

impl Send {
    pub(crate) fn new(socket: Arc<Mutex<UdpSocket>>, datagram: UdpDatagram) -> Send {
        Send { socket, datagram }
    }
}

impl Future for Send {
    type Item = ();
    type Error = io::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let _ = try_ready!{
            self.socket.lock().unwrap().poll_send_to(&self.datagram.data, &self.datagram.peer)
        };

        Ok(Async::Ready(()))
    }
}