gameServer2/src/server/server.rs
author unc0rr
Tue, 24 Jan 2017 20:28:16 +0300
changeset 12143 7e874846afe3
parent 12142 4d7d41be1993
child 12144 589a2d7d3dc5
permissions -rw-r--r--
Toss code around
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
     1
use slab;
12126
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;
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents: 12127
diff changeset
     7
use server::client::HWClient;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
     8
use server::actions::Action;
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
     9
use super::handlers;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    10
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    11
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    12
12126
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,
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    15
    pub clients: Slab<HWClient>,
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    16
    pub rooms: Slab<HWRoom>,
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    17
    pub lobby_id: Token,
12126
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 {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    22
        let mut rooms = Slab::with_capacity(rooms_limit);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    23
        let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby");
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    24
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    25
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    26
            clients: Slab::with_capacity(clients_limit),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    27
            rooms: rooms,
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    28
            lobby_id: token,
12126
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
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    32
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    33
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
                      PollOpt::edge())
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
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    37
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    38
        let (sock, addr) = self.listener.accept()?;
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    39
        info!("Connected: {}", addr);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    40
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    41
        let client = HWClient::new(sock, &self.lobby_id);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    43
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    45
        self.clients[token].register(poll, token);
12126
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
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    49
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    50
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    51
                           token: Token) -> io::Result<()> {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    52
        let actions;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    53
        {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    54
            actions = self.clients[token].readable(poll);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    55
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    56
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    57
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    58
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    59
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    60
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    61
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    62
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    63
                           token: Token) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    64
        self.clients[token].writable(poll)?;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    65
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    66
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    67
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    68
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    69
    pub fn client_error(&mut self, poll: &Poll,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    70
                           token: Token) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    71
        let actions;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    72
        {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    73
            actions = self.clients[token].error(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    74
        }
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    75
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    76
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    77
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    78
        Ok(())
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    79
    }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    80
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    81
    pub fn send(&mut self, token: Token, msg: &String) {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    82
        self.clients[token].send_string(msg);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    83
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    84
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    85
    pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    86
        for action in actions {
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    87
            handlers::handle(self, token, poll, action);
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    88
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    89
    }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    90
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    91
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    92
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    93
pub struct HWRoom {
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    94
    pub name: String,
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    95
}
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    96
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    97
impl HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    98
    pub fn new() -> HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    99
        HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   100
            name: String::new(),
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   101
        }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   102
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   103
}