gameServer2/src/server/server.rs
author unc0rr
Wed, 25 Jan 2017 23:48:23 +0300
changeset 12149 589a2d7d3dc5
parent 12148 7e874846afe3
child 12151 8d8fb85bc09c
permissions -rw-r--r--
More refactoring
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;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     5
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     6
use utils;
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     7
use super::client::HWClient;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     8
use super::actions;
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,
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    14
    pub clients: Slab<HWClient>,
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    15
    pub rooms: Slab<HWRoom>,
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    16
    pub lobby_id: Token,
12131
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 {
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    21
        let mut rooms = Slab::with_capacity(rooms_limit);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    22
        let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby");
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    24
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    25
            clients: Slab::with_capacity(clients_limit),
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    26
            rooms: rooms,
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    27
            lobby_id: token,
12131
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
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    31
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    32
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    33
                      PollOpt::edge())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
    }
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
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12133
diff changeset
    37
        let (sock, addr) = self.listener.accept()?;
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    38
        info!("Connected: {}", addr);
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    40
        let client = HWClient::new(sock, &self.lobby_id);
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    41
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
            .ok().expect("could not add connection to slab");
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
        self.clients[token].register(poll, token);
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    45
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    46
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    47
    }
12132
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
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    50
                           token: Token) -> io::Result<()> {
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    51
        let actions;
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
            actions = self.clients[token].readable(poll);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    54
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    55
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    56
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    57
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    58
        Ok(())
12132
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
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    61
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    62
                           token: Token) -> io::Result<()> {
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    63
        self.clients[token].writable(poll)?;
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    64
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    65
        Ok(())
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    66
    }
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    67
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    68
    pub fn client_error(&mut self, poll: &Poll,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    69
                           token: Token) -> io::Result<()> {
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    70
        let actions;
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    71
        {
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    72
            actions = self.clients[token].error(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    73
        }
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    74
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    75
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    76
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    77
        Ok(())
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    78
    }
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    79
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    80
    pub fn send(&mut self, token: Token, msg: &String) {
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    81
        self.clients[token].send_string(msg);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    82
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    83
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    84
    pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<actions::Action>) {
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    85
        for action in actions {
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    86
            actions::run_action(self, token, poll, action);
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    87
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    88
    }
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    89
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    90
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    91
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    92
pub struct HWRoom {
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    93
    pub name: String,
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    94
}
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    95
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    96
impl HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    97
    pub fn new() -> HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    98
        HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    99
            name: String::new(),
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   100
        }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   101
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   102
}