gameServer2/src/server/server.rs
author unc0rr
Wed, 18 Jan 2017 22:54:02 +0300
changeset 12141 78925eff02c2
parent 12139 f3121d7dedec
child 12142 4d7d41be1993
permissions -rw-r--r--
Basic support for NICK message
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::Write;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     5
use std::io;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     6
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     7
use utils;
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents: 12127
diff changeset
     8
use server::client::HWClient;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
     9
use server::actions::Action;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    10
use server::actions::Action::*;
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    11
use protocol::messages::HWProtocolMessage::*;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    12
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    13
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    14
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
pub struct HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    16
    listener: TcpListener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    17
    clients: Slab<HWClient>,
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    18
    rooms: Slab<HWRoom>,
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    19
    lobbyId: Token,
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    20
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    21
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    24
        let mut rooms = Slab::with_capacity(rooms_limit);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    25
        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
    26
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    27
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    28
            clients: Slab::with_capacity(clients_limit),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    29
            rooms: rooms,
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    30
            lobbyId: token,
12126
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
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    33
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    35
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    36
                      PollOpt::edge())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    37
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    38
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    40
        let (sock, addr) = self.listener.accept()?;
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    41
        info!("Connected: {}", addr);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    43
        let client = HWClient::new(sock, &self.lobbyId);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    45
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    46
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    47
        self.clients[token].register(poll, token);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    48
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    49
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    50
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    51
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    52
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    53
                           token: Token) -> io::Result<()> {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    54
        let actions;
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
            actions = self.clients[token].readable(poll);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    57
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    58
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    59
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    60
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    61
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    62
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    63
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    64
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    65
                           token: Token) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    66
        self.clients[token].writable(poll)?;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    67
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    68
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    69
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    70
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    71
    pub fn client_error(&mut self, poll: &Poll,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    72
                           token: Token) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    73
        let actions;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    74
        {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    75
            actions = self.clients[token].error(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    76
        }
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    77
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    78
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    79
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    80
        Ok(())
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    81
    }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    82
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    83
    fn send(&mut self, token: Token, msg: &String) {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    84
        self.clients[token].send_string(msg);
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    85
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    86
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    87
    fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    88
        for action in actions {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    89
            match action {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    90
                SendMe(msg) => self.send(token, &msg),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    91
                ByeClient(msg) => {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    92
                    self.react(token, poll, vec![
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    93
                        SendMe(Bye(&msg).to_raw_protocol()),
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    94
                        RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    95
                    ]);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    96
                },
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    97
                SetNick(nick) => {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    98
                    self.send(token, &Nick(&nick).to_raw_protocol());
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    99
                    self.clients[token].nick = nick;
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   100
                }
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   101
                RemoveClient => {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   102
                    self.clients[token].deregister(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   103
                    self.clients.remove(token);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   104
                },
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   105
                //_ => unimplemented!(),
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   106
            }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
   107
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
   108
    }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   109
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   110
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   111
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   112
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   113
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   114
}
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   115
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   116
impl HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   117
    pub fn new() -> HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   118
        HWRoom {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   119
            name: String::new(),
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   120
        }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   121
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
   122
}