gameServer2/src/server/server.rs
author unc0rr
Wed, 06 Dec 2017 23:46:17 +0100
changeset 12852 bd35cb2302b3
parent 12148 8591375271b8
child 13119 1e39b8749072
permissions -rw-r--r--
Quick dirty fix for building
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;
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
     2
use mio::net::*;
12126
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
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    10
type Slab<T> = slab::Slab<T>;
12127
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>,
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    16
    pub lobby_id: usize,
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);
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    22
        let token = rooms.insert(HWRoom::new());
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);
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    41
        let token = self.clients.insert(client);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12144
diff changeset
    43
        self.clients[token].id = token;
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    44
        self.clients[token].register(poll, Token(token));
12126
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
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    48
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    49
    pub fn client_readable(&mut self, poll: &Poll,
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    50
                           token: usize) -> io::Result<()> {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    51
        let actions;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    52
        {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    53
            actions = self.clients[token].readable(poll);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    54
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    55
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    56
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    57
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    58
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    59
    }
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
    pub fn client_writable(&mut self, poll: &Poll,
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    62
                           token: usize) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    63
        self.clients[token].writable(poll)?;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    64
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    65
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    66
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    67
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    68
    pub fn client_error(&mut self, poll: &Poll,
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    69
                           token: usize) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    70
        let actions;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    71
        {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    72
            actions = self.clients[token].error(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    73
        }
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    74
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    75
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    76
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    77
        Ok(())
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    78
    }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    79
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    80
    pub fn send(&mut self, token: usize, msg: &String) {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    81
        self.clients[token].send_string(msg);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    82
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    83
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    84
    pub fn react(&mut self, token: usize, poll: &Poll, actions: Vec<actions::Action>) {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    85
        for action in actions {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    86
            actions::run_action(self, token, poll, action);
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    87
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    88
    }
12126
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
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    91
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    92
pub struct HWRoom {
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    93
    pub id: usize,
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    94
    pub name: String,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    95
    pub password: Option<String>,
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    96
    pub protocol_number: u32,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    97
    pub ready_players_number: u8,
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    98
}
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    99
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   100
impl HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   101
    pub fn new() -> HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   102
        HWRoom {
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
   103
            id: 0,
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   104
            name: String::new(),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   105
            password: None,
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   106
            protocol_number: 0,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   107
            ready_players_number: 0,
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   108
        }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   109
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   110
}