rust/hedgewars-server/src/server/handlers.rs
author alfadur <mail@none>
Sat, 02 Feb 2019 15:06:39 +0300
changeset 14671 455865ccd36c
parent 14457 98ef2913ec73
child 14672 6e6632068a33
permissions -rw-r--r--
Server action refactoring part 2 of N
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
};
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
     9
use crate::{
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    10
    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    11
    server::actions::PendingMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    12
};
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13798
diff changeset
    13
use log::*;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
    14
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    15
mod checker;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    16
mod common;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    17
mod inroom;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    18
mod lobby;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    19
mod loggingin;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    20
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    21
pub struct Response {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    22
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    23
    messages: Vec<PendingMessage>,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    24
}
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    25
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    26
impl Response {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    27
    pub fn new(client_id: ClientId) -> Self {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    28
        Self {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    29
            client_id,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    30
            messages: vec![],
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    31
        }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    32
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    33
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    34
    pub fn client_id(&self) -> ClientId {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    35
        self.client_id
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    36
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    37
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    38
    pub fn add(&mut self, message: PendingMessage) {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    39
        self.messages.push(message)
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    40
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    41
}
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    42
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    43
pub fn handle(
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    44
    server: &mut HWServer,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    45
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    46
    response: &mut Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    47
    message: HWProtocolMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    48
) {
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    49
    match message {
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    50
        HWProtocolMessage::Ping => {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    51
            response.add(Pong.send_self());
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    52
            server.react(client_id, vec![Pong.send_self().action()])
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    53
        }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    54
        HWProtocolMessage::Quit(Some(msg)) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    55
            server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    56
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    57
        HWProtocolMessage::Quit(None) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    58
            server.react(client_id, vec![ByeClient("User quit".to_string())])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    59
        }
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    60
        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    61
        HWProtocolMessage::Empty => warn!("Empty message"),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    62
        _ => match server.clients[client_id].room_id {
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    63
            None => loggingin::handle(server, client_id, response, message),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    64
            Some(id) if id == server.lobby_id => {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    65
                lobby::handle(server, client_id, response, message)
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    66
            }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    67
            Some(id) => inroom::handle(server, client_id, response, id, message),
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    68
        },
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    69
    }
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    70
}