gameServer2/src/server/server.rs
author Wuzzy <Wuzzy2@mail.ru>
Mon, 26 Feb 2018 21:17:36 +0100
changeset 13076 bcb205281f38
parent 12857 bd35cb2302b3
child 13124 1e39b8749072
permissions -rw-r--r--
Some minor convenience improvements in Basic Flying Saucer Training
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;
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
     2
use mio::net::*;
12131
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
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    10
type Slab<T> = slab::Slab<T>;
12132
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>,
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    16
    pub lobby_id: usize,
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);
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    22
        let token = rooms.insert(HWRoom::new());
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
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12149
diff changeset
    40
        let client = HWClient::new(sock);
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    41
        let token = self.clients.insert(client);
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12149
diff changeset
    43
        self.clients[token].id = token;
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    44
        self.clients[token].register(poll, Token(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,
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    50
                           token: usize) -> 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,
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    62
                           token: usize) -> 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,
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    69
                           token: usize) -> 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
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    80
    pub fn send(&mut self, token: usize, 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
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    84
    pub fn react(&mut self, token: usize, 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 {
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    93
    pub id: usize,
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    94
    pub name: String,
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    95
    pub password: Option<String>,
12153
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    96
    pub protocol_number: u32,
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    97
    pub ready_players_number: u8,
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    98
}
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    99
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   100
impl HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   101
    pub fn new() -> HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   102
        HWRoom {
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
   103
            id: 0,
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   104
            name: String::new(),
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   105
            password: None,
12153
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
   106
            protocol_number: 0,
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   107
            ready_players_number: 0,
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   108
        }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   109
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
   110
}