summaryrefslogtreecommitdiff
path: root/src/bin/server.rs
blob: 7c6a63e2ba050391fc8e6d71322f0cc17c22d8ec (plain)
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
use std::thread::sleep;
use std::time::Duration;
use std::net::TcpListener;

extern crate space;
use space::connection::Connection;

fn main() {
    let listener = TcpListener::bind("localhost:6000").unwrap();
    listener.set_nonblocking(true).unwrap();

    let mut ships = Vec::new();

    let mut connections = Vec::new();
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => connections.push(Connection::new(stream, &mut ships)),
            _ => {
                for i in 0..connections.len() {
                    connections[i].process(&mut ships);
                }
                connections.retain(|connection| connection.open );

                sleep(Duration::from_millis(100));
            }
        }
    }
}