gameServer2/src/server/server.rs
author unc0rr
Sun, 15 Jan 2017 00:34:36 +0300
changeset 12143 e0bf51609062
parent 12142 193dfdcb0620
child 12144 f3121d7dedec
permissions -rw-r--r--
Introduce actions, just like in the old server
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;
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
     9
use server::actions::Action;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    10
use server::actions::Action::*;
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    11
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    12
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    13
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
pub struct HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
    listener: TcpListener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    16
    clients: Slab<HWClient>,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    17
    rooms: Slab<HWRoom>
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
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    20
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    21
    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
    22
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    24
            clients: Slab::with_capacity(clients_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    25
            rooms: Slab::with_capacity(rooms_limit),
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
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    29
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    30
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    31
                      PollOpt::edge())
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
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12133
diff changeset
    35
        let (sock, addr) = self.listener.accept()?;
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    36
        info!("Connected: {}", addr);
12131
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
        let client = HWClient::new(sock);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    40
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    41
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    42
        self.clients[token].register(poll, token);
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    43
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    45
    }
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    46
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    47
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    48
                           token: Token) -> io::Result<()> {
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    49
        let actions;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    50
        {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    51
            actions = self.clients[token].readable(poll);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    52
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    53
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    54
        for action in actions {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    55
            self.react(token, action);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    56
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    57
        Ok(())
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    58
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    59
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    60
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    61
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    62
        self.clients[token].writable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    63
    }
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    64
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    65
    pub fn client_error(&mut self, poll: &Poll,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    66
                           token: Token) -> io::Result<()> {
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    67
        self.clients[token].error(poll)
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    68
    }
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    69
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    70
    fn react(&mut self, token: Token, action: Action) {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    71
        match action {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    72
            SendMe(msg) => self.clients[token].send_string(&msg),
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    73
            //_ => unimplemented!(),
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    74
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    75
    }
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    76
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    77
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    78
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    79
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    80
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    81
}