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