gameServer2/src/server/server.rs
author unc0rr
Wed, 18 Jan 2017 22:15:55 +0300
changeset 12139 f3121d7dedec
parent 12138 e0bf51609062
child 12141 78925eff02c2
permissions -rw-r--r--
- Handle errors - Client removal routine - Handle QUIT 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>,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    18
    rooms: Slab<HWRoom>
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    19
}
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
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
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),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    26
            rooms: Slab::with_capacity(rooms_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    27
        }
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
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    31
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    32
                      PollOpt::edge())
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 accept(&mut self, poll: &Poll) -> io::Result<()> {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    36
        let (sock, addr) = self.listener.accept()?;
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    37
        info!("Connected: {}", addr);
12126
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
        let client = HWClient::new(sock);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    40
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    41
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    43
        self.clients[token].register(poll, token);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    45
        Ok(())
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
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    48
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    49
                           token: Token) -> io::Result<()> {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    50
        let actions;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    51
        {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    52
            actions = self.clients[token].readable(poll);
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    53
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    54
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    55
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    56
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    57
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    58
    }
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
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    61
                           token: Token) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    62
        self.clients[token].writable(poll)?;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    63
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    64
        Ok(())
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    65
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    66
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    67
    pub fn client_error(&mut self, poll: &Poll,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    68
                           token: Token) -> io::Result<()> {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    69
        let actions;
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    70
        {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    71
            actions = self.clients[token].error(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    72
        }
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    73
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    74
        self.react(token, poll, actions);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    75
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    76
        Ok(())
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    77
    }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    78
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    79
    fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    80
        for action in actions {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    81
            match action {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    82
                SendMe(msg) => self.clients[token].send_string(&msg),
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    83
                ByeClient(msg) => {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    84
                    self.react(token, poll, vec![
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    85
                        SendMe(Bye(&msg).to_raw_protocol()),
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    86
                        RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    87
                    ]);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    88
                },
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    89
                RemoveClient => {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    90
                    self.clients[token].deregister(poll);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    91
                    self.clients.remove(token);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    92
                },
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    93
                //_ => unimplemented!(),
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    94
            }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    95
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    96
    }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    97
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    98
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    99
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   100
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   101
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   102
}