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