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
use crate::{
    config::Config,
    error::{ErrorKind, Result},
    net::{connection::ActiveConnections, events::SocketEvent, link_conditioner::LinkConditioner},
    packet::{DeliveryGuarantee, Outgoing, Packet},
};
use crossbeam_channel::{self, unbounded, Receiver, Sender};
use log::error;
use std::{
    self, io,
    net::{SocketAddr, ToSocketAddrs, UdpSocket},
};

/// A reliable UDP socket implementation with configurable reliability and ordering guarantees.
pub struct Socket {
    socket: UdpSocket,
    config: Config,
    connections: ActiveConnections,
    recv_buffer: Vec<u8>,
    link_conditioner: Option<LinkConditioner>,
    event_sender: Sender<SocketEvent>,
    packet_receiver: Receiver<Packet>,
}

impl Socket {
    /// Binds to the socket and then sets up `ActiveConnections` to manage the "connections".
    /// Because UDP connections are not persistent, we can only infer the status of the remote
    /// endpoint by looking to see if they are still sending packets or not
    pub fn bind<A: ToSocketAddrs>(
        addresses: A,
    ) -> Result<(Self, Sender<Packet>, Receiver<SocketEvent>)> {
        Socket::bind_with_config(addresses, Config::default())
    }

    /// Binds to the socket and then sets up `ActiveConnections` to manage the "connections".
    /// Because UDP connections are not persistent, we can only infer the status of the remote
    /// endpoint by looking to see if they are still sending packets or not
    ///
    /// This function allows you to configure laminar with the passed configuration.
    pub fn bind_with_config<A: ToSocketAddrs>(
        addresses: A,
        config: Config,
    ) -> Result<(Self, Sender<Packet>, Receiver<SocketEvent>)> {
        let socket = UdpSocket::bind(addresses)?;
        socket.set_nonblocking(true)?;
        let (event_sender, event_receiver) = unbounded();
        let (packet_sender, packet_receiver) = unbounded();
        Ok((
            Socket {
                recv_buffer: vec![0; config.receive_buffer_max_size],
                socket,
                config,
                connections: ActiveConnections::new(),
                link_conditioner: None,
                event_sender,
                packet_receiver,
            },
            packet_sender,
            event_receiver,
        ))
    }

    /// Entry point to the run loop. This should run in a spawned thread since calls to `poll.poll`
    /// are blocking.
    pub fn start_polling(&mut self) -> Result<()> {
        // Nothing should break out of this loop!
        loop {
            // First we pull any newly arrived packets and handle them
            if let Err(e) = self.recv_from() {
                error!("Encountered an error receiving data: {:?}", e);
            };

            // Now grab all the packets waiting to be sent and send them
            while let Ok(p) = self.packet_receiver.try_recv() {
                if let Err(e) = self.send_to(p) {
                    error!("There was an error sending packet: {:?}", e);
                }
            }

            // Finally check for idle clients
            if let Err(e) = self.handle_idle_clients() {
                error!("Encountered an error when sending TimeoutEvent: {:?}", e);
            }
        }
    }

    /// Iterate through all of the idle connections based on `idle_connection_timeout` config and
    /// remove them from the active connections. For each connection removed, we will send a
    /// `SocketEvent::TimeOut` event to the `event_sender` channel.
    fn handle_idle_clients(&mut self) -> Result<()> {
        let idle_addresses = self
            .connections
            .idle_connections(self.config.idle_connection_timeout);
        for address in idle_addresses {
            self.connections.remove_connection(&address);
            self.event_sender.send(SocketEvent::Timeout(address))?;
        }

        Ok(())
    }

    // Serializes and sends a `Packet` on the socket. On success, returns the number of bytes written.
    fn send_to(&mut self, packet: Packet) -> Result<usize> {
        let connection = self
            .connections
            .get_or_insert_connection(packet.addr(), &self.config);

        let dropped = connection.gather_dropped_packets();
        let mut processed_packets: Vec<Outgoing> = dropped
            .iter()
            .flat_map(|waiting_packet| {
                connection.process_outgoing(
                    &waiting_packet.payload,
                    // Because a delivery guarantee is only sent with reliable packets
                    DeliveryGuarantee::Reliable,
                    // This is stored with the dropped packet because they could be mixed
                    waiting_packet.ordering_guarantee,
                )
            })
            .collect();

        let processed_packet = connection.process_outgoing(
            packet.payload(),
            packet.delivery_guarantee(),
            packet.order_guarantee(),
        )?;

        processed_packets.push(processed_packet);

        let mut bytes_sent = 0;

        for processed_packet in processed_packets {
            if self.should_send_packet() {
                match processed_packet {
                    Outgoing::Packet(outgoing) => {
                        bytes_sent += self.send_packet(&packet.addr(), &outgoing.contents())?;
                    }
                    Outgoing::Fragments(packets) => {
                        for outgoing in packets {
                            bytes_sent += self.send_packet(&packet.addr(), &outgoing.contents())?;
                        }
                    }
                }
            }
        }
        Ok(bytes_sent)
    }

    // On success the packet will be send on the `event_sender`
    fn recv_from(&mut self) -> Result<()> {
        match self.socket.recv_from(&mut self.recv_buffer) {
            Ok((recv_len, address)) => {
                if recv_len == 0 {
                    return Err(ErrorKind::ReceivedDataToShort)?;
                }
                let received_payload = &self.recv_buffer[..recv_len];

                if !self.connections.exists(&address) {
                    self.event_sender.send(SocketEvent::Connect(address))?;
                }

                let connection = self
                    .connections
                    .get_or_insert_connection(address, &self.config);

                connection.process_incoming(received_payload, &self.event_sender)?;
            }
            Err(e) => {
                if e.kind() != io::ErrorKind::WouldBlock {
                    error!("Encountered an error receiving data: {:?}", e);
                    return Err(e.into());
                }
            }
        }
        Ok(())
    }

    // Send a single packet over the UDP socket.
    fn send_packet(&self, addr: &SocketAddr, payload: &[u8]) -> Result<usize> {
        let bytes_sent = self.socket.send_to(payload, addr)?;
        Ok(bytes_sent)
    }

    // In the presence of a link conditioner, we would like it to determine whether or not we should
    // send a packet.
    fn should_send_packet(&self) -> bool {
        if let Some(link_conditioner) = &self.link_conditioner {
            link_conditioner.should_send()
        } else {
            true
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        net::constants::{ACKED_PACKET_HEADER, FRAGMENT_HEADER_SIZE, STANDARD_HEADER_SIZE},
        Config, Packet, Socket, SocketEvent,
    };
    use std::net::SocketAddr;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn can_send_and_receive() {
        let (mut server, _, packet_receiver) =
            Socket::bind("127.0.0.1:12342".parse::<SocketAddr>().unwrap()).unwrap();
        let (mut client, packet_sender, _) =
            Socket::bind("127.0.0.1:12341".parse::<SocketAddr>().unwrap()).unwrap();

        thread::spawn(move || client.start_polling());
        thread::spawn(move || server.start_polling());

        for _ in 0..3 {
            packet_sender
                .send(Packet::unreliable(
                    "127.0.0.1:12342".parse::<SocketAddr>().unwrap(),
                    vec![1, 2, 3, 4, 5, 6, 7, 8, 9],
                ))
                .unwrap();
        }

        let mut iter = packet_receiver.iter();

        assert!(iter.next().is_some());
        assert!(iter.next().is_some());
        assert!(iter.next().is_some());
    }

    #[test]
    fn sending_large_unreliable_packet_should_fail() {
        let (mut server, _, _) =
            Socket::bind("127.0.0.1:12370".parse::<SocketAddr>().unwrap()).unwrap();

        assert_eq!(
            server
                .send_to(Packet::unreliable(
                    "127.0.0.1:12360".parse().unwrap(),
                    vec![1; 5000]
                ))
                .is_err(),
            true
        );
    }

    #[test]
    fn send_returns_right_size() {
        let (mut server, _, _) =
            Socket::bind("127.0.0.1:12371".parse::<SocketAddr>().unwrap()).unwrap();

        assert_eq!(
            server
                .send_to(Packet::unreliable(
                    "127.0.0.1:12361".parse().unwrap(),
                    vec![1; 1024]
                ))
                .unwrap(),
            1024 + STANDARD_HEADER_SIZE as usize
        );
    }

    #[test]
    fn fragmentation_send_returns_right_size() {
        let (mut server, _, _) =
            Socket::bind("127.0.0.1:12372".parse::<SocketAddr>().unwrap()).unwrap();

        let fragment_packet_size = STANDARD_HEADER_SIZE + FRAGMENT_HEADER_SIZE;

        // the first fragment of an sequence of fragments contains also the acknowledgment header.
        assert_eq!(
            server
                .send_to(Packet::reliable_unordered(
                    "127.0.0.1:12362".parse().unwrap(),
                    vec![1; 4000]
                ))
                .unwrap(),
            4000 + (fragment_packet_size * 4 + ACKED_PACKET_HEADER) as usize
        );
    }

    #[test]
    fn connect_event_occurs() {
        let (mut server, _, packet_receiver) =
            Socket::bind("127.0.0.1:12345".parse::<SocketAddr>().unwrap()).unwrap();
        let (mut client, packet_sender, _) =
            Socket::bind("127.0.0.1:12344".parse::<SocketAddr>().unwrap()).unwrap();

        thread::spawn(move || client.start_polling());
        thread::spawn(move || server.start_polling());

        packet_sender
            .send(Packet::unreliable(
                "127.0.0.1:12345".parse().unwrap(),
                vec![0, 1, 2],
            ))
            .unwrap();
        assert_eq!(
            packet_receiver.recv().unwrap(),
            SocketEvent::Connect("127.0.0.1:12344".parse().unwrap())
        );
    }

    #[test]
    fn disconnect_event_occurs() {
        let mut config = Config::default();
        config.idle_connection_timeout = Duration::from_millis(1);

        let (mut server, _, packet_receiver) =
            Socket::bind("127.0.0.1:12347".parse::<SocketAddr>().unwrap()).unwrap();
        let (mut client, packet_sender, _) =
            Socket::bind("127.0.0.1:12346".parse::<SocketAddr>().unwrap()).unwrap();

        thread::spawn(move || client.start_polling());
        thread::spawn(move || server.start_polling());

        packet_sender
            .send(Packet::unreliable(
                "127.0.0.1:12347".parse().unwrap(),
                vec![0, 1, 2],
            ))
            .unwrap();

        assert_eq!(
            packet_receiver.recv().unwrap(),
            SocketEvent::Connect("127.0.0.1:12346".parse().unwrap())
        );
        assert_eq!(
            packet_receiver.recv().unwrap(),
            SocketEvent::Packet(Packet::unreliable(
                "127.0.0.1:12346".parse().unwrap(),
                vec![0, 1, 2]
            ))
        );
        assert_eq!(
            packet_receiver.recv().unwrap(),
            SocketEvent::Timeout("127.0.0.1:12346".parse().unwrap())
        );
    }

    const LOCAL_ADDR: &str = "127.0.0.1:13000";
    const REMOTE_ADDR: &str = "127.0.0.1:14000";

    fn create_test_packet(id: u8, addr: &str) -> Packet {
        let payload = vec![id];
        Packet::reliable_unordered(addr.parse().unwrap(), payload)
    }

    #[test]
    fn multiple_sends_should_start_sending_dropped() {
        // Start up a server and a client.
        let (mut server, server_sender, server_receiver) =
            Socket::bind(REMOTE_ADDR.parse::<SocketAddr>().unwrap()).unwrap();
        thread::spawn(move || server.start_polling());

        let (mut client, client_sender, client_receiver) =
            Socket::bind(LOCAL_ADDR.parse::<SocketAddr>().unwrap()).unwrap();
        thread::spawn(move || client.start_polling());

        // Send enough packets to ensure that we must have dropped packets.
        for i in 0..35 {
            client_sender
                .send(create_test_packet(i, REMOTE_ADDR))
                .unwrap();
        }

        let mut events = Vec::new();

        loop {
            if let Ok(event) = server_receiver.recv_timeout(Duration::from_millis(500)) {
                events.push(event);
            } else {
                break;
            }
        }

        // Ensure that we get the correct number of events to the server.
        // 1 connect event plus the 35 messages
        assert_eq!(events.len(), 36);

        // Finally the server decides to send us a message back. This necessarily will include
        // the ack information for 33 of the sent 35 packets.
        server_sender
            .send(create_test_packet(0, LOCAL_ADDR))
            .unwrap();

        // Block to ensure that the client gets the server message before moving on.
        client_receiver.recv().unwrap();

        // This next sent message should end up sending the 2 unacked messages plus the new messages
        // with payload 35
        events.clear();
        client_sender
            .send(create_test_packet(35, REMOTE_ADDR))
            .unwrap();

        loop {
            if let Ok(event) = server_receiver.recv_timeout(Duration::from_millis(500)) {
                events.push(event);
            } else {
                break;
            }
        }

        let sent_events: Vec<u8> = events
            .iter()
            .flat_map(|e| match e {
                SocketEvent::Packet(p) => Some(p.payload()[0]),
                _ => None,
            })
            .collect();
        assert_eq!(sent_events, vec![0, 1, 35]);
    }
}