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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#![deny(missing_docs)]
//! A generic eventually-correct framework over unreliable connections, such as UDP.
//!
//! This crate uses a struct called `Connector` that eventually guarantees that all persistent messages
//! arrive at the other end, or it will disconnect.
//!
//! This crate differentiates between two message types:
//! * Confirmed: This is a message that is:
//! * * Guaranteed to arrive ***at some point***
//! * * Not guaranteed to arrive in the correct order
//! * Unconfirmed: This is a message that is not guaranteed to arrive
//!
//! Use cases can be:
//! * Sending player data does not always have to arrive, because the location is updated 10 times a second (unconfirmed)
//! * Login information should always arrive, but this can take a second (confirmed)

#[macro_use]
extern crate serde_derive;

mod packet;
mod param;

#[cfg(test)]
pub mod test;

/// The result that is used in this type. It is a simple wrapper around `Result<T, failure::Error>`
pub type Result<T> = std::result::Result<T, failure::Error>;

use self::packet::Packet;
pub use self::param::ConnectorParam;

use std::collections::HashMap;
use std::io::ErrorKind;
use std::net::{SocketAddr, UdpSocket};
use std::num::NonZeroU64;

/// Contains data about the sending half of this connector
#[derive(Debug)]
struct ConnectorSend<TParam: ConnectorParam> {
    /// Contains a list of messages that are send but are not confirmed yet.
    unconfirmed_message_cache: HashMap<NonZeroU64, CachedPacket<TParam::TSend>>,

    /// Contains the last Id that was send to the peer connector.
    next_message_id: Option<NonZeroU64>,

    /// Last time a ping was send
    last_ping_time: f64,
}

impl<TParam: ConnectorParam> Default for ConnectorSend<TParam> {
    fn default() -> Self {
        ConnectorSend {
            unconfirmed_message_cache: HashMap::new(),
            next_message_id: None,
            last_ping_time: 0.,
        }
    }
}

/// Contains data about the receiving half of this connector
#[derive(Debug)]
struct ConnectorReceive {
    /// Contains the last ID we've received from the peer.
    last_message_id: Option<NonZeroU64>,

    /// Contains the IDs that we are requesting from the peer.
    missing_message_id_list: Vec<MissingId>,

    /// Last time a ping was received
    last_ping_time: f64,
}

impl Default for ConnectorReceive {
    fn default() -> Self {
        ConnectorReceive {
            last_message_id: None,
            missing_message_id_list: Vec::new(),
            last_ping_time: 0.,
        }
    }
}

/// The connector is used to handle handshakes and timeouts with a different, remote connector
///
/// For client-side applications, we recommend calling `update_and_receive` at a frequent rate
///
/// For server-side applications, we recommend dealing with your own UdpSocket receiving logic, looking up the connector based on a SocketAddr, and then calling `handle_incoming_data`.
///
/// The connector struct has a lot of config settings. All these settings can be found in `ConnectorParam`
pub struct Connector<TParam: ConnectorParam> {
    /// Contains data about the sending half of this connector
    send: ConnectorSend<TParam>,

    /// Contains data about the receiving half of this connector
    receive: ConnectorReceive,

    /// The address that this connector is associated with
    peer_addr: SocketAddr,
    // /// Additional data stored in this Connector
    // data: TParam::TData,
}

#[derive(Debug)]
struct MissingId {
    pub id: NonZeroU64,
    pub last_request_time: f64,
}

#[derive(Debug)]
struct CachedPacket<TSend> {
    pub packet: Packet<TSend>,
    pub last_emit_time: f64,
}

/// The state of the connector. This is based on when the last ping was send or received. Changing your ConnectorParam will greatly affect the results of `Connector.state()`, returning this value.
#[derive(Debug, Eq, PartialEq)]
pub enum NetworkState {
    /// We received a ping a reasonable amount of time ago, so we're connected. See `ConnectorParam::PING_INTERVAL_S` for more info.
    Connected,

    /// We have not received a ping for a while, and we are not connecting at this point in time. See `ConnectorParam::RECEIVE_PING_TIMEOUT_S` for more info.
    Disconnected,

    /// We have not received a ping for a while but we did try to connect. See `ConnectorParam::SEND_PING_TIMEOUT_S` for more info.
    Connecting,
}

impl MissingId {
    pub fn new(id: NonZeroU64) -> MissingId {
        MissingId {
            id,
            last_request_time: 0.,
        }
    }
}

/// A generic trait over a socket. This is automatically implemented for `UdpSocket` but can be implemented for your own connector as well.
pub trait Socket {
    /// Receive data from any remote, returning the amount of bytes read, and the SocketAddr that the data was received from
    fn recv_from(&mut self, buffer: &mut [u8]) -> std::io::Result<(usize, SocketAddr)>;

    /// The local SocketAddr we're listening on
    fn local_addr(&self) -> SocketAddr;

    /// Send data to the given SocketAddr
    fn send_to(&mut self, buffer: &[u8], target: SocketAddr) -> Result<()>;
}

impl Socket for UdpSocket {
    fn recv_from(&mut self, buffer: &mut [u8]) -> std::io::Result<(usize, SocketAddr)> {
        UdpSocket::recv_from(self, buffer)
    }
    fn local_addr(&self) -> SocketAddr {
        UdpSocket::local_addr(self).unwrap()
    }
    fn send_to(&mut self, buffer: &[u8], target: SocketAddr) -> Result<()> {
        UdpSocket::send_to(self, buffer, target)?;
        Ok(())
    }
}

impl<TParam: ConnectorParam> Connector<TParam> {
    /// Create a Connector that is bound to the given remote SocketAddr
    pub fn bound_to(peer_addr: SocketAddr) -> Self {
        Connector {
            send: Default::default(),
            receive: Default::default(),
            peer_addr,
        }
    }

    /// Get the socket address that this connector is paired with
    pub fn bound_addr(&self) -> SocketAddr {
        self.peer_addr
    }

    /// Connect to the `bound_addr`. This will reset the internal state of the connector, and start up the connection handshake
    pub fn connect(&mut self, socket: &mut Socket) -> Result<()> {
        self.send = Default::default();
        self.receive = Default::default();
        self.send_ping(socket)
    }

    /// Get the current state of this connector. This is dependent on a couple of settings in ConnectorParam:
    /// * If we have received a ping since `ConnectorParam::RECEIVE_PING_TIMEOUT_S` ago, we're connected
    /// * If we have send a ping since `ConnectorParam::SEND_PING_TIMEOUT_S` ago, we're connecting
    /// * Else we're disconnected
    pub fn state(&self) -> NetworkState {
        if self.receive.last_ping_time + TParam::RECEIVE_PING_TIMEOUT_S < time::precise_time_s() {
            if self.send.last_ping_time + TParam::SEND_PING_TIMEOUT_S > time::precise_time_s() {
                NetworkState::Connecting
            } else {
                NetworkState::Disconnected
            }
        } else {
            NetworkState::Connected
        }
    }

    /// Receive data from the other connector. This will call `handle_incoming_data` internally.
    ///
    /// Ideally you would never need this function. Use `update_and_receive` on clients, and `handle_incoming_data` on servers.
    pub fn receive_from(&mut self, socket: &mut Socket) -> Result<Vec<TParam::TReceive>> {
        let mut buffer = [0u8; 1024];
        let mut result = Vec::new();
        let mut had_message = false;
        loop {
            let receive_result = socket.recv_from(&mut buffer);
            let count = match receive_result {
                Ok((_, addr)) if addr != self.peer_addr => continue, // ignored
                Ok((count, _)) if count == 0 => {
                    if !had_message {
                        return Err(std::io::Error::from(ErrorKind::BrokenPipe).into());
                    } else {
                        return Ok(result);
                    }
                }
                Ok((count, _)) => count,
                Err(ref e) if e.kind() == ErrorKind::WouldBlock => return Ok(result),
                Err(e) => return Err(e.into()),
            };
            had_message = true;
            if let Some(msg) = self.handle_incoming_data(socket, &buffer[..count])? {
                result.push(msg);
            }
        }
    }

    /// Update this connector and receive data from the remote connector.
    pub fn update_and_receive(&mut self, socket: &mut Socket) -> Result<Vec<TParam::TReceive>> {
        self.update(socket)?;
        self.receive_from(socket)
    }

    /// Update this connector. This will make sure the connection is still intact and requests any potentially missing packets.
    pub fn update(&mut self, socket: &mut Socket) -> Result<()> {
        if NetworkState::Disconnected == self.state() {
            return Ok(());
        }
        if self.send.last_ping_time + TParam::PING_INTERVAL_S < time::precise_time_s() {
            self.send_ping(socket)?;
        }
        for missing_packet in &mut self.receive.missing_message_id_list {
            if missing_packet.last_request_time + TParam::REQUEST_MISSING_PACKET_INTERVAL_S
                < time::precise_time_s()
            {
                send_packet_to::<TParam::TSend>(
                    self.peer_addr,
                    socket,
                    &Packet::RequestPacket {
                        id: missing_packet.id,
                    },
                )?;
                missing_packet.last_request_time = time::precise_time_s();
            }
        }
        for unconfirmed_packet in self.send.unconfirmed_message_cache.values_mut() {
            if unconfirmed_packet.last_emit_time + TParam::EMIT_UNCONFIRMED_PACKET_INTERVAL_S
                < time::precise_time_s()
            {
                unconfirmed_packet.last_emit_time = time::precise_time_s();
                send_packet_to(self.peer_addr, socket, &unconfirmed_packet.packet)?;
            }
        }
        Ok(())
    }

    /// Handles incoming data. This will perform internal logic to make sure data is being transmitted correctly,
    /// and requests missing packets.
    ///
    /// Any actual data that was received, will be returned from this function.
    pub fn handle_incoming_data(
        &mut self,
        socket: &mut Socket,
        data: &[u8],
    ) -> Result<Option<TParam::TReceive>> {
        let packet: Packet<_> = bincode::deserialize(data)?;
        println!("Received {:?}", packet);
        // TODO: This should probably be simplified. Currently it's just a big match statement
        Ok(match packet {
            Packet::Ping {
                last_send_message_id,
            } => {
                if let Some(last_send_message_id) = last_send_message_id {
                    self.request_message_up_to(last_send_message_id.get());
                }
                self.receive.last_ping_time = time::precise_time_s();
                send_packet_to::<TParam::TSend>(
                    self.peer_addr,
                    socket,
                    &Packet::Pong {
                        last_send_message_id: self.send.next_message_id,
                    },
                )?;
                None
            }
            Packet::RequestPacket { id } => {
                if let Some(packet) = self.send.unconfirmed_message_cache.get_mut(&id) {
                    packet.last_emit_time = time::precise_time_s();
                    send_packet_to(self.peer_addr, socket, &packet.packet)?;
                } else {
                    send_packet_to::<TParam::TSend>(
                        self.peer_addr,
                        socket,
                        &Packet::PacketNotFound { id },
                    )?;
                }
                None
            }
            Packet::ConfirmPacket { id } => {
                self.send.unconfirmed_message_cache.remove(&id);
                None
            }
            Packet::PacketNotFound { id } => {
                self.receive.missing_message_id_list.retain(|i| i.id != id);
                None
            }
            Packet::Pong {
                last_send_message_id,
            } => {
                if let Some(last_send_message_id) = last_send_message_id {
                    self.request_message_up_to(last_send_message_id.get());
                }
                self.receive.last_ping_time = time::precise_time_s();
                None
            }
            Packet::Data { message_id, data } => {
                if let Some(message_id) = message_id {
                    self.request_message_up_to(message_id.get() - 1);
                    send_packet_to::<TParam::TSend>(
                        self.peer_addr,
                        socket,
                        &Packet::ConfirmPacket { id: message_id },
                    )?;
                }
                self.receive.last_message_id = message_id;
                Some(data)
            }
        })
    }

    fn send_ping(&mut self, socket: &mut Socket) -> Result<()> {
        self.send.last_ping_time = time::precise_time_s();
        send_packet_to::<TParam::TSend>(
            self.peer_addr,
            socket,
            &Packet::Ping {
                last_send_message_id: self
                    .send
                    .next_message_id
                    .map(|id| unsafe { NonZeroU64::new_unchecked(id.get() - 1) }),
            },
        )
    }

    fn request_message_up_to(&mut self, id: u64) {
        let mut start = if let Some(id) = self.receive.last_message_id {
            id
        } else {
            unsafe { NonZeroU64::new_unchecked(1) }
        };
        while start.get() <= id {
            if self
                .receive
                .missing_message_id_list
                .iter()
                .find(|id| id.id == start)
                .is_none()
            {
                self.receive
                    .missing_message_id_list
                    .push(MissingId::new(start));
            }
            start = unsafe { NonZeroU64::new_unchecked(start.get() + 1) };
        }
        self.receive.last_message_id = NonZeroU64::new(id);
    }

    /// Send an unconfirmed message to the other connector. It is not guaranteed that this message will ever arrive.
    ///
    /// This is useful for data that does not have to arrive. Think of things like player movements, frames of a lossy video stream, etc.
    pub fn send_unconfirmed(&mut self, socket: &mut Socket, msg: TParam::TSend) -> Result<()> {
        send_packet_to(
            self.peer_addr,
            socket,
            &Packet::Data {
                data: msg,
                message_id: None,
            },
        )?;
        Ok(())
    }

    /// Send a confirmed message to the other connector. The connector will try to make sure this message arrives. It is not guaranteed that messages will arrive in the same order at the other side.
    pub fn send_confirmed(&mut self, socket: &mut Socket, msg: TParam::TSend) -> Result<()> {
        let sending_id = if let Some(id) = self.send.next_message_id {
            id
        } else {
            unsafe { NonZeroU64::new_unchecked(1) }
        };
        let data = Packet::Data {
            data: msg,
            message_id: Some(sending_id),
        };
        send_packet_to(self.peer_addr, socket, &data)?;
        self.send.unconfirmed_message_cache.insert(
            sending_id,
            CachedPacket {
                packet: data,
                last_emit_time: time::precise_time_s(),
            },
        );
        self.send.next_message_id = NonZeroU64::new(sending_id.get() + 1);
        Ok(())
    }
}

fn send_packet_to<TSend: serde::Serialize>(
    peer_addr: SocketAddr,
    socket: &mut Socket,
    packet: &Packet<TSend>,
) -> Result<()> {
    let bytes = bincode::serialize(packet)?;
    println!(
        "Sending {:?} from {:?} to {:?}",
        bytes,
        socket.local_addr(),
        peer_addr
    );
    socket.send_to(&bytes, peer_addr)?;
    Ok(())
}