gameServer2/src/server/server.rs
author Wuzzy <almikes@aol.com>
Thu, 27 Apr 2017 03:11:50 +0200
changeset 12352 7f09d01ecb45
parent 12148 8591375271b8
child 12852 bd35cb2302b3
permissions -rw-r--r--
Fix false victory in Teamwork/Teamwork 2 when one of your hogs die in same turn as enemy
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;
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     7
use super::client::HWClient;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     8
use super::actions;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     9
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    10
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    11
12126
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,
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    14
    pub clients: Slab<HWClient>,
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    15
    pub rooms: Slab<HWRoom>,
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    16
    pub lobby_id: Token,
12126
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 {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    21
        let mut rooms = Slab::with_capacity(rooms_limit);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    22
        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
    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),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    26
            rooms: rooms,
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    27
            lobby_id: token,
12126
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<()> {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    37
        let (sock, addr) = self.listener.accept()?;
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    38
        info!("Connected: {}", addr);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12144
diff changeset
    40
        let client = HWClient::new(sock);
12126
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
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12144
diff changeset
    44
        self.clients[token].id = token;
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
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    85
    pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<actions::Action>) {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    86
        for action in actions {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    87
            actions::run_action(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 {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    94
    pub id: Token,
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    95
    pub name: String,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    96
    pub password: Option<String>,
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    97
    pub protocol_number: u32,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    98
    pub ready_players_number: u8,
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    99
}
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   100
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   101
impl HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   102
    pub fn new() -> HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   103
        HWRoom {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   104
            id: Token(0),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   105
            name: String::new(),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   106
            password: None,
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   107
            protocol_number: 0,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   108
            ready_players_number: 0,
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   109
        }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   110
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   111
}