gameServer2/src/server/server.rs
author nemo
Fri, 13 Apr 2018 13:03:51 -0400
changeset 13327 b77a9380dd0f
parent 13124 1e39b8749072
child 13421 cdf69667593b
permissions -rw-r--r--
QT for some reason messes with XCompose causing broken input (Qt 5 only - Qt 4 did not break anything). In Qt 5.2 and 5.3 this was causing an invalid conversion in chat messages containing these resulting in the bad bytes being stripped. In Qt 5.9 it is still broken, but you at least get a string with something in it. This checks for non-zero converted strings for room creation and chat lines.
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;
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
     7
use super::client::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
     8
use super::room::*;
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     9
use super::actions;
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    10
use protocol::messages::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    11
use super::handlers;
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    12
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    13
type Slab<T> = slab::Slab<T>;
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    14
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    15
pub enum Destination {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    16
    ToSelf(ClientId),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    17
    ToOthers(ClientId)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    18
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    19
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    20
pub struct PendingMessage(pub Destination, pub HWServerMessage);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    21
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
pub struct HWServer {
12148
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    23
    pub clients: Slab<HWClient>,
7e874846afe3 Toss code around
unc0rr
parents: 12147
diff changeset
    24
    pub rooms: Slab<HWRoom>,
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    25
    pub lobby_id: RoomId,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    26
    pub output: Vec<PendingMessage>,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    27
    pub removed_clients: Vec<ClientId>,
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
impl HWServer {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    31
    pub fn new(clients_limit: usize, rooms_limit: usize) -> HWServer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    32
        let rooms = Slab::with_capacity(rooms_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    33
        let clients = Slab::with_capacity(clients_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    34
        let mut server = HWServer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    35
            clients, rooms,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    36
            lobby_id: 0,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    37
            output: vec![],
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    38
            removed_clients: vec![]
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    39
        };
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    40
        server.lobby_id = server.add_room();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    41
        server
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    43
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    44
    pub fn add_client(&mut self) -> ClientId {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    45
        let key: ClientId;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    46
        {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    47
            let entry = self.clients.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    48
            key = entry.key();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    49
            let client = HWClient::new(entry.key());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    50
            entry.insert(client);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    51
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    52
        self.send_self(key, HWServerMessage::Connected(utils::PROTOCOL_VERSION));
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    53
        key
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    54
    }
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    55
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    56
    pub fn client_lost(&mut self, client_id: ClientId) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    57
        actions::run_action(self, client_id,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    58
                            actions::Action::ByeClient("Connection reset".to_string()));
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
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    61
    pub fn add_room(&mut self) -> RoomId {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    62
        let entry = self.rooms.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    63
        let key = entry.key();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    64
        let room = HWRoom::new(entry.key());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    65
        entry.insert(room);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    66
        key
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    67
    }
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    68
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    69
    pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    70
        handlers::handle(self, client_id, msg);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    71
    }
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    72
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    73
    pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    74
        self.output.push(PendingMessage(
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    75
            Destination::ToSelf(client_id), msg));
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    76
    }
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    77
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    78
    pub fn send_others(&mut self, client_id: ClientId, msg: HWServerMessage) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    79
        self.output.push(PendingMessage(
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    80
            Destination::ToOthers(client_id), msg));
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    81
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    82
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    83
    pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) {
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    84
        for action in actions {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    85
            actions::run_action(self, client_id, action);
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    86
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    87
    }
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    88
}