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
use std::thread;
use std::io::Error;
use std::net::{SocketAddr, ToSocketAddrs};
use std::collections::HashMap;
use shared::{Config, Connection, ConnectionID, Handler, Socket};

/// A multi-client server that uses a reliable UDP connection for
/// unreliable packet transmission.
pub struct Server {
    closed: bool,
    config: Config
}

impl Server {

    /// Creates a new server with the given connection configuration.
    pub fn new(config: Config) -> Server {
        Server {
            closed: false,
            config: config
        }
    }

    /// Tries to bind a reliable UDP based server to the specified local
    /// address which actively listens and manages incoming client connections.
    ///
    /// The clients must use a compatible connection configuration in order for
    /// connections to be actually established.
    ///
    /// The `handler` is a struct that implements the `Handler` trait in order
    /// to handle events from the server and its connections.
    pub fn bind<T: ToSocketAddrs>(
        &mut self, handler: &mut Handler<Server>, address: T
    ) -> Result<(), Error> {

        // Internal ticker for send rate control
        let mut tick = 0;
        let tick_delay = 1000 / self.config.send_rate;

        // Create the UDP socket
        let mut socket = try!(Socket::new(
            address,
            self.config.packet_max_size
        ));

        // Extract packet reader
        let reader = socket.reader().unwrap();

        // Create connection management collections
        let mut dropped: Vec<ConnectionID> = Vec::new();
        let mut addresses: HashMap<ConnectionID, SocketAddr> = HashMap::new();
        let mut connections: HashMap<ConnectionID, Connection> = HashMap::new();

        // Invoke handler
        handler.bind(self);

        // Receive and send until we shut down.
        while !self.closed {

            // Receive all incoming UDP packets to our local address
            while let Ok((addr, packet)) = reader.try_recv() {

                // Try to extract the connection id from the packet
                match Connection::id_from_packet(&self.config, &packet) {
                    Some(id) => {

                        // In case of a unknown connection id create a new
                        // connection and insert it into out hash map
                        if !connections.contains_key(&id) {
                            connections.insert(
                                id, Connection::new(self.config)
                            );
                        }

                        // In addition map the sender address to the connection
                        // id, this is done in order to reliable track the
                        // connection in case of address re-assignments in some
                        // NAT.
                        addresses.insert(id, addr);

                        // Then feed the packet into the connection object for
                        // parsing
                        connections.get_mut(&id).unwrap().receive(
                            packet, self, handler
                        );

                    },
                    None => { /* Ignore any invalid packets */ }
                }

            }

            // Invoke handler
            handler.tick_connections(self, &mut connections);

            // Create outgoing packets for all connections
            for (id, conn) in connections.iter_mut() {

                // If not congested send at full rate otherwise send
                // at reduced rate
                if !conn.is_congested() ||
                    tick % self.config.congestion_divider == 0 {

                    // Resolve the last known remote address for this
                    // connection and send the data
                    let addr = addresses.get(id).unwrap();

                    // Then invoke the connection to send a outgoing packet
                    conn.send(&mut socket, addr, self, handler);

                }

                // Collect all lost / closed connections
                if conn.is_open() == false {
                    dropped.push(*id);
                }

            }

            // Remove any dropped connections and their address mappings
            for id in dropped.iter() {
                connections.remove(id).unwrap();
                addresses.remove(id).unwrap();
            }

            dropped.clear();

            // Next Tick
            thread::sleep_ms(tick_delay);
            tick += 1;

            if tick == self.config.send_rate {
                tick = 0;
            }

        }

        // Invoke handler
        handler.shutdown(self);

        // Reset all connection states
        for (_, conn) in connections.iter_mut() {
            conn.reset();
        }

        // Close the UDP socket
        socket.shutdown();

        Ok(())

    }

    /// Shutsdown the server, closing all its active connections.
    pub fn shutdown(&mut self) {
        self.closed = true;
    }

}