gameServer2/src/server/server.rs
author unc0rr
Sat, 14 Jan 2017 00:46:52 +0300
changeset 12141 e25a82ce2374
parent 12134 07972a8c2433
child 12142 193dfdcb0620
permissions -rw-r--r--
- Render messages to string - Respond to PING messages
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;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     6
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     7
use utils;
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents: 12132
diff changeset
     8
use server::client::HWClient;
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     9
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    10
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    11
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    12
pub struct HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    13
    listener: TcpListener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
    clients: Slab<HWClient>,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
    rooms: Slab<HWRoom>
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    16
}
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
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    19
    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
    20
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    21
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
            clients: Slab::with_capacity(clients_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
            rooms: Slab::with_capacity(rooms_limit),
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
    }
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
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    28
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    29
                      PollOpt::edge())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    30
    }
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
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12133
diff changeset
    33
        let (sock, addr) = self.listener.accept()?;
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
        println!("Connected: {}", addr);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    35
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    36
        let client = HWClient::new(sock);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    37
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    38
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    40
        self.clients[token].register(poll, token);
12131
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
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    43
    }
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    44
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    45
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    46
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    47
        self.clients[token].readable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    48
    }
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
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    51
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    52
        self.clients[token].writable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    53
    }
12131
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
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    56
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    57
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    58
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    59
}