gameServer2/src/server.rs
author unc0rr
Sun, 01 Jan 2017 22:13:35 +0300
changeset 12126 4348997e502b
child 12127 36ac9c075d0d
permissions -rw-r--r--
Refactor code to add more structure to it
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     1
use slab::*;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     2
use mio::tcp::*;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     3
use mio::*;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     4
use mio;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     5
use std::io::Write;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     6
use std::io;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     7
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     8
use utils;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     9
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    10
pub struct HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    11
    listener: TcpListener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    12
    clients: Slab<HWClient>,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    13
    rooms: Slab<HWRoom>
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    16
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    17
    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    18
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    19
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    20
            clients: Slab::with_capacity(clients_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    21
            rooms: Slab::with_capacity(rooms_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
        }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    24
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    25
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    26
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    27
                      PollOpt::edge())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    28
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    29
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    30
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    31
        let (sock, addr) = self.listener.accept().unwrap();
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    32
        println!("Connected: {}", addr);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    33
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
        let client = HWClient::new(sock);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    35
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    36
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    37
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    38
        self.clients[token].send_raw_msg(
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    40
            , utils::PROTOCOL_VERSION).as_bytes());
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    41
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
        self.clients[token].uid = Some(token);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    43
        poll.register(&self.clients[token].sock, mio::Token(token), Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
                      PollOpt::edge() | PollOpt::oneshot())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    45
            .ok().expect("could not register socket with event loop");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    46
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    47
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    48
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    49
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    50
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    51
struct HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    52
    sock: TcpStream,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    53
    uid: Option<usize>
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    54
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    55
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    56
impl HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    57
    fn new(sock: TcpStream) -> HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    58
        HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    59
            sock: sock,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    60
            uid: None
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    61
        }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    62
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    63
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    64
    fn send_raw_msg(&mut self, msg: &[u8]) {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    65
        self.sock.write_all(msg).unwrap();
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    66
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    67
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    68
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    69
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    70
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    71
}