gameServer2/src/server/server.rs
author Wuzzy <Wuzzy2@mail.ru>
Sat, 17 Mar 2018 04:37:13 +0100
changeset 13235 9f5023a5f9e1
parent 13119 1e39b8749072
child 13416 cdf69667593b
permissions -rw-r--r--
Fix speech bubble briefly rendering at (0,0) after spawning
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;
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
     2
use mio::net::*;
12126
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;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
     7
use super::client::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
     8
use super::room::*;
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     9
use super::actions;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    10
use protocol::messages::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    11
use super::handlers;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    12
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    13
type Slab<T> = slab::Slab<T>;
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    14
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    15
pub enum Destination {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    16
    ToSelf(ClientId),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    17
    ToOthers(ClientId)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    18
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    19
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    20
pub struct PendingMessage(pub Destination, pub HWServerMessage);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    21
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
pub struct HWServer {
12143
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    23
    pub clients: Slab<HWClient>,
7e874846afe3 Toss code around
unc0rr
parents: 12142
diff changeset
    24
    pub rooms: Slab<HWRoom>,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    25
    pub lobby_id: RoomId,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    26
    pub output: Vec<PendingMessage>,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    27
    pub removed_clients: Vec<ClientId>,
12126
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 {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
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: 12852
diff changeset
    32
        let rooms = Slab::with_capacity(rooms_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    33
        let clients = Slab::with_capacity(clients_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    34
        let mut server = HWServer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    35
            clients, rooms,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    36
            lobby_id: 0,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    37
            output: vec![],
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    38
            removed_clients: vec![]
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    39
        };
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    40
        server.lobby_id = server.add_room();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    41
        server
12126
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
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    44
    pub fn add_client(&mut self) -> ClientId {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    45
        let key: ClientId;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    46
        {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    47
            let entry = self.clients.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    48
            key = entry.key();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    49
            let client = HWClient::new(entry.key());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    50
            entry.insert(client);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    51
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
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: 12852
diff changeset
    53
        key
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    54
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    55
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
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: 12852
diff changeset
    57
        actions::run_action(self, client_id,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    58
                            actions::Action::ByeClient("Connection reset".to_string()));
12127
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
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    61
    pub fn add_room(&mut self) -> RoomId {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    62
        let entry = self.rooms.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    63
        let key = entry.key();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    64
        let room = HWRoom::new(entry.key());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    65
        entry.insert(room);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    66
        key
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    67
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    68
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
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: 12852
diff changeset
    70
        handlers::handle(self, client_id, msg);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    71
    }
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    72
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
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: 12852
diff changeset
    74
        self.output.push(PendingMessage(
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    75
            Destination::ToSelf(client_id), msg));
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    76
    }
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    77
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
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: 12852
diff changeset
    79
        self.output.push(PendingMessage(
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    80
            Destination::ToOthers(client_id), msg));
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    81
    }
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    82
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    83
    pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) {
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    84
        for action in actions {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    85
            actions::run_action(self, client_id, action);
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    86
        }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    87
    }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    88
}