rust/hedgewars-server/src/server/handlers.rs
author unc0rr
Sun, 16 Dec 2018 00:12:29 +0100
changeset 14457 98ef2913ec73
parent 14415 06672690d71b
child 14671 455865ccd36c
permissions -rw-r--r--
Apply rustfmt to all files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
     1
use mio;
13521
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
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     4
use super::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     5
    actions::{Action, Action::*},
14375
cc99f7c673c7 try again 🙃
alfadur
parents: 13805
diff changeset
     6
    core::HWServer,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     7
    coretypes::ClientId,
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
     8
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     9
use crate::protocol::messages::{HWProtocolMessage, HWServerMessage::*};
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13798
diff changeset
    10
use log::*;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
    11
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    12
mod checker;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    13
mod common;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    14
mod inroom;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    15
mod lobby;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    16
mod loggingin;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    17
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13444
diff changeset
    18
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    19
    match message {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    20
        HWProtocolMessage::Ping => server.react(client_id, vec![Pong.send_self().action()]),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    21
        HWProtocolMessage::Quit(Some(msg)) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    22
            server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    23
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    24
        HWProtocolMessage::Quit(None) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    25
            server.react(client_id, vec![ByeClient("User quit".to_string())])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    26
        }
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    27
        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    28
        HWProtocolMessage::Empty => warn!("Empty message"),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    29
        _ => match server.clients[client_id].room_id {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    30
            None => loggingin::handle(server, client_id, message),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    31
            Some(id) if id == server.lobby_id => lobby::handle(server, client_id, message),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    32
            Some(id) => inroom::handle(server, client_id, id, message),
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    33
        },
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    34
    }
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    35
}