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
use std::rc::Rc;
use std::cell::{ RefCell, RefMut };
use std::collections::VecDeque;
use std::net::SocketAddr;
use mio::{ Ready, PollOpt };
use bytes::{ BytesMut, BufMut };
use mio;
use std;
use std::io;
use std::ops::Deref;
use std::net::{ IpAddr, Ipv4Addr, Ipv6Addr };
use std::str::FromStr;
use std::io::ErrorKind;
use super::Token;

use callback::Callback;

use node::{ Loop, Core };

fn send_messages(pvt: &mut RefMut<SockPvt>) {
    loop {
        let st_ = pvt.send_queue.pop_front();
        if st_.is_none() { return; }
        let st = st_.unwrap();
        match pvt.s.as_ref().unwrap().send_to(&st.msg.buf, &st.msg.sa) {
            Ok(size) => {
                assert!(size == st.msg.buf.len());
                st.cb.call(Ok(()));
            },
            Err(e) => {
                if e.kind() == ErrorKind::WouldBlock {
                    pvt.can_send = false;
                    return;
                }
                st.cb.call(Err(e));
                //println!("err {:?}", e);
            }
        }
    }
}


struct SendTo {
    msg: Message,
    cb: Callback<io::Result<()>>
}
struct SockPvt {
    s: Option<Rc<mio::net::UdpSocket>>,

    can_send_event: Token,
    can_send: bool,
    can_recv_event: Token,

    send_queue: VecDeque<SendTo>,
    on_message: Vec<Callback<Message>>,
    core: Option<Core>,

    closed: bool
}

pub struct SockBuilder {
    pvt: Rc<RefCell<SockPvt>>,
    af: Af,
}

fn try_setup_core(pvt: &mut RefMut<SockPvt>, rc: &Rc<RefCell<SockPvt>>) {
    // can't do anything until we have the socket and core
    if pvt.s.is_none() || pvt.core.is_none() { return; }

    // done already
    if pvt.can_recv_event != Token(0) { return; }

    let c = pvt.core.as_ref().unwrap().clone();
    let s = pvt.s.as_ref().unwrap().clone();

    let can_send_cb = Callback::new(&c, rc.clone(), |pvt_,_|{
        let mut pvt = pvt_.borrow_mut();
        pvt.can_send = true;
        send_messages(&mut pvt);
    });
    pvt.can_send_event =
        c.register_event(s.clone(), can_send_cb, Ready::writable(), PollOpt::edge()).unwrap();

    let can_recv_cb = Callback::new(&c, rc.clone(), |pvt_,_| {
        let pvt = pvt_.borrow_mut();
        let s = pvt.s.as_ref().unwrap();
        loop {
            let mut buf = BytesMut::with_capacity(2048);
            let ret = unsafe { s.recv_from(buf.bytes_mut()) };
            match ret {
                Ok((count, sa)) => {
                    unsafe { buf.advance_mut(count); }
                    if pvt.on_message.len() == 1 {
                        pvt.on_message[0].call(Message { buf, sa });
                    } else {
                        for cb in &pvt.on_message {
                            cb.call(Message { buf: buf.clone(), sa: sa.clone() });
                        }
                    }
                },
                Err(e) => {
                    if e.kind() != ErrorKind::WouldBlock {
                        error!("dgram can_recv_cb {:?}", &e);
                    }
                    break;
                }
            }
        }
    });
    pvt.can_recv_event =
        c.register_event(s, can_recv_cb, Ready::readable(), PollOpt::edge()).unwrap();

    // you don't get a can_send event until you clog up the buffer first, so better send now.
    pvt.can_send = true;
    send_messages(&mut *pvt);
}

pub struct Sock {
    bldr: SockBuilder,
    s: Rc<mio::net::UdpSocket>,
}
impl Deref for Sock {
    type Target = mio::net::UdpSocket;
    fn deref(&self) -> &Self::Target { &self.s }
}
impl Sock {
    pub fn on_message<L:Loop<L>,F:'static+Fn(&mut L,Message)>(&self, l:&L, f:F) -> &Sock {
        self.bldr.on_message(l, f);
        self
    }
    pub fn send_to<L,F,A,B>(&self, l:&L, bm: B, a:A, f:F) -> &Sock where
        L: Loop<L>,
        F: 'static + Fn(&mut L, io::Result<()>),
        A: AddrLike,
        B: Into<BytesMut>
    {
        self.bldr.send_to(l, bm, a, f);
        self
    }
    pub fn close(&self) {
        debug!("close()");
        let mut pvt = self.bldr.pvt.borrow_mut();
        if pvt.s.is_none() { return; }
        if pvt.core.is_none() { return; }
        if pvt.closed { return; }
        pvt.closed = true;
        pvt.on_message.clear();
        pvt.send_queue.clear();
        let c = pvt.core.as_ref().unwrap();
        match c.deregister_event(&pvt.can_send_event) {_=>()}
        match c.deregister_event(&pvt.can_recv_event) {_=>()}
    }
}

impl SockBuilder {
    pub fn on_message<L:Loop<L>,F:'static+Fn(&mut L,Message)>(&self, l:&L, f:F) -> &SockBuilder {
        let c = l.core();
        let mut pvt = self.pvt.borrow_mut();
        if pvt.closed { error!("on_message() Socket already closed"); return self; }
        pvt.on_message.push(Callback::new(c, rec!{ l: l.as_rc(), f:f }, |ctx,msg|{
            (ctx.f)(&mut *ctx.l.borrow_mut(), msg);
        }));
        if pvt.core.is_none() { pvt.core = Some(c.clone()); }
        try_setup_core(&mut pvt, &self.pvt);
        self
    }
    pub fn send_to<L,F,A,B>(&self, l:&L, bm: B, a:A, f:F) -> &SockBuilder where
        L: Loop<L>,
        F: 'static + Fn(&mut L, io::Result<()>),
        A: AddrLike,
        B: Into<BytesMut>
    {
        let addr_str = a.to_string();
        let sa = match a.as_sockaddr(self.af) {
            Ok(sa) => sa,
            Err(_e) => {
                error!("send_to() Failed to parse address {}", &addr_str);
                return self;
            }
        };
        let c = l.core();
        let mut pvt = self.pvt.borrow_mut();
        if pvt.closed { error!("send_to() Socket already closed"); return self; }
        pvt.send_queue.push_back(SendTo {
            msg: bm.to_msg(sa),
            cb: Callback::new(c, rec!{ l: l.as_rc(), f:f }, |ctx,res|{
                (ctx.f)(&mut *ctx.l.borrow_mut(), res);
            })
        });
        if pvt.can_send {
            send_messages(&mut pvt);
        } else {
            if pvt.core.is_none() { pvt.core = Some(c.clone()); }
            try_setup_core(&mut pvt, &self.pvt);
        }
        self
    }
    pub fn _bind(self, addr: &SocketAddr) -> io::Result<Sock> {
        let s = mio::net::UdpSocket::bind(addr)?;
        let rc = Rc::new(s);
        {
            let mut pvt = self.pvt.borrow_mut();
            pvt.s = Some(rc.clone());
            try_setup_core(&mut pvt, &self.pvt);
        }
        Ok(Sock { s: rc, bldr: self })
    }
    pub fn bind<T:AddrLike>(self, t:T) -> io::Result<Sock> {
        let af = self.af;
        match t.as_sockaddr(af) {
            Ok(sa) => self._bind(&sa),
            Err(e) => Err(e)
        }
    }
}

pub fn create_socket(afs: &'static str) -> io::Result<SockBuilder> {
    let af = match afs {
        "udp4" => Af::Inet,
        "udp6" => Af::Inet6,
        _ => { return Err(io::Error::new(ErrorKind::InvalidInput, "expecting udp4 or udp6")); }
    };
    Ok(SockBuilder {
        af,
        pvt: Rc::new(RefCell::new(SockPvt {
            s: None,

            can_send_event: Token(0),
            can_send: false,
            can_recv_event: Token(0),

            send_queue: VecDeque::new(),
            on_message: Vec::new(),
            core: None,

            closed: false
        }))
    })
}

///

#[derive(Clone, Copy)]
pub enum Af { Inet, Inet6 }

pub trait AddrLike {
    fn as_sockaddr(self, af: Af) -> io::Result<SocketAddr>;
    fn to_string(&self) -> String;
}
impl AddrLike for u16 {
    fn to_string(&self) -> String { <Self as std::string::ToString>::to_string(self) }
    fn as_sockaddr(self, af: Af) -> io::Result<SocketAddr> {
        Ok(match af {
            Af::Inet => SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0,0,0,0)), self),
            Af::Inet6 => SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0,0,0,0,0,0,0,0)), self)
        })
    }
}
impl AddrLike for &'static str {
    fn to_string(&self) -> String { <Self as std::string::ToString>::to_string(self) }
    fn as_sockaddr(self, af: Af) -> io::Result<SocketAddr> { (0, self).as_sockaddr(af) }
}
impl AddrLike for (u16, &'static str) {
    fn to_string(&self) -> String { format!("({},{})", self.0, self.1) }
    fn as_sockaddr(self, _af: Af) -> io::Result<SocketAddr> {
        let (port, addr) = self;
        let res = match IpAddr::from_str(addr) {
            Ok(addr) => addr,
            Err(e) => { return Result::Err(io::Error::new(ErrorKind::InvalidInput, e)); }
        };
        Ok(SocketAddr::new(res, port))
    }
}


pub struct Message {
    pub sa: SocketAddr,
    pub buf: BytesMut
}
pub trait MsgLike {
    fn to_msg(self, sa: SocketAddr) -> Message;
}
impl<T> MsgLike for T where T: Into<BytesMut> {
    fn to_msg(self, sa: SocketAddr) -> Message {
        let buf = self.into();
        Message { sa, buf }
    }
}