gameServer2/src/server/server.rs
author unc0rr
Mon, 02 Jan 2017 00:16:22 +0300
changeset 12133 f50876f3eff8
parent 12132 gameServer2/src/server.rs@36ac9c075d0d
child 12134 07972a8c2433
permissions -rw-r--r--
Refactor modules layout
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
     1
use slab;
12131
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 std::io::Write;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     5
use std::io;
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
     6
use netbuf;
12131
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;
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents: 12132
diff changeset
     9
use server::client::HWClient;
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    10
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    11
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    12
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    13
pub struct HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
    listener: TcpListener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
    clients: Slab<HWClient>,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    16
    rooms: Slab<HWRoom>
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    17
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    18
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    19
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    20
    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
    21
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
            clients: Slab::with_capacity(clients_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    24
            rooms: Slab::with_capacity(rooms_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    25
        }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    26
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    27
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    28
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    29
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    30
                      PollOpt::edge())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    31
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    32
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    33
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
        let (sock, addr) = self.listener.accept().unwrap();
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    35
        println!("Connected: {}", addr);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    36
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    37
        let client = HWClient::new(sock);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    38
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    40
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    41
        self.clients[token].register(poll, token);
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    43
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
    }
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    45
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    46
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    47
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    48
        self.clients[token].readable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    49
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    50
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    51
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    52
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    53
        self.clients[token].writable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    54
    }
12131
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
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    57
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    58
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    59
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    60
}