gameServer2/src/server/handlers.rs
author alfadur
Fri, 07 Dec 2018 18:32:36 +0300
changeset 14396 cc99f7c673c7
parent 13810 gameServer2/src/server/handlers/mod.rs@0463a4221327
permissions -rw-r--r--
try again 🙃
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
     1
use mio;
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     2
use std::{io, io::Write};
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
     3
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     4
use super::{
14396
cc99f7c673c7 try again 🙃
alfadur
parents: 13810
diff changeset
     5
    core::HWServer,
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     6
    actions::{Action, Action::*},
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     7
    coretypes::ClientId
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     8
};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
     9
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
    10
    protocol::messages::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
    11
        HWProtocolMessage,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
    12
        HWServerMessage::*
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
    13
    }
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    14
};
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13771
diff changeset
    15
use log::*;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
    16
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    17
mod loggingin;
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    18
mod lobby;
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    19
mod inroom;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13492
diff changeset
    20
mod common;
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    21
mod checker;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    22
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    23
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    24
    match message {
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    25
        HWProtocolMessage::Ping =>
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    26
            server.react(client_id, vec![Pong.send_self().action()]),
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    27
        HWProtocolMessage::Quit(Some(msg)) =>
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    28
            server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)]),
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    29
        HWProtocolMessage::Quit(None) =>
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    30
            server.react(client_id, vec![ByeClient("User quit".to_string())]),
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    31
        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    32
        HWProtocolMessage::Empty => warn!("Empty message"),
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    33
        _ => {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    34
            match server.clients[client_id].room_id {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    35
                None =>
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    36
                    loggingin::handle(server, client_id, message),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    37
                Some(id) if id == server.lobby_id =>
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    38
                    lobby::handle(server, client_id, message),
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    39
                Some(id) =>
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    40
                    inroom::handle(server, client_id, id, message)
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    41
            }
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    42
        },
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    43
    }
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    44
}