rust/hedgewars-server/src/server/handlers.rs
author alfadur <mail@none>
Wed, 06 Feb 2019 22:29:02 +0300
changeset 14695 f61ce544d436
parent 14692 5122c584804e
child 14698 6a2e13e36b7f
permissions -rw-r--r--
Server action refactoring part N of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
     1
use mio;
13526
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13449
diff changeset
     2
use std::{io, io::Write};
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
     3
14695
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14692
diff changeset
     4
use super::{actions::Destination, core::HWServer, coretypes::ClientId};
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
     5
use crate::{
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
     6
    protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*},
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
     7
    server::actions::PendingMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
     8
};
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13803
diff changeset
     9
use log::*;
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13526
diff changeset
    10
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    11
mod checker;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    12
mod common;
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    13
mod inroom;
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    14
mod lobby;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    15
mod loggingin;
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    16
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    17
pub struct Response {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    18
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    19
    messages: Vec<PendingMessage>,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    20
}
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    21
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    22
impl Response {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    23
    pub fn new(client_id: ClientId) -> Self {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    24
        Self {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    25
            client_id,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    26
            messages: vec![],
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    27
        }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    28
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    29
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    30
    #[inline]
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    31
    pub fn is_empty(&self) -> bool {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    32
        self.messages.is_empty()
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    33
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    34
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    35
    #[inline]
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    36
    pub fn len(&self) -> usize {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    37
        self.messages.len()
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    38
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    39
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    40
    #[inline]
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    41
    pub fn client_id(&self) -> ClientId {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    42
        self.client_id
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    43
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    44
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    45
    #[inline]
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    46
    pub fn add(&mut self, message: PendingMessage) {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    47
        self.messages.push(message)
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    48
    }
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    49
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    50
    pub fn extract_messages<'a, 'b: 'a>(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    51
        &'b mut self,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    52
        server: &'a HWServer,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    53
    ) -> impl Iterator<Item = (Vec<ClientId>, HWServerMessage)> + 'a {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    54
        let client_id = self.client_id;
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    55
        self.messages.drain(..).map(move |m| {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    56
            let ids = get_recipients(server, client_id, &m.destination);
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    57
            (ids, m.message)
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    58
        })
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    59
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    60
}
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    61
14692
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    62
impl Extend<PendingMessage> for Response {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    63
    fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    64
        for msg in iter {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    65
            self.add(msg)
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    66
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    67
    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    68
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14678
diff changeset
    69
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    70
fn get_recipients(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    71
    server: &HWServer,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    72
    client_id: ClientId,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    73
    destination: &Destination,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    74
) -> Vec<ClientId> {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    75
    let mut ids = match *destination {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    76
        Destination::ToSelf => vec![client_id],
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    77
        Destination::ToId(id) => vec![id],
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    78
        Destination::ToAll {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    79
            room_id: Some(id), ..
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    80
        } => server.room_clients(id),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    81
        Destination::ToAll {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    82
            protocol: Some(proto),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    83
            ..
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    84
        } => server.protocol_clients(proto),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    85
        Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    86
    };
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    87
    if let Destination::ToAll {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    88
        skip_self: true, ..
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    89
    } = destination
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    90
    {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    91
        if let Some(index) = ids.iter().position(|id| *id == client_id) {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    92
            ids.remove(index);
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    93
        }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    94
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    95
    ids
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    96
}
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    97
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    98
pub fn handle(
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    99
    server: &mut HWServer,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   100
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   101
    response: &mut Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   102
    message: HWProtocolMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   103
) {
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   104
    match message {
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   105
        HWProtocolMessage::Ping => {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   106
            response.add(Pong.send_self());
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   107
        }
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   108
        HWProtocolMessage::Quit(Some(msg)) => {
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   109
            common::remove_client(server, response, "User quit: ".to_string() + &msg);
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   110
        }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   111
        HWProtocolMessage::Quit(None) => {
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   112
            common::remove_client(server, response, "User quit".to_string());
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   113
        }
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   114
        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   115
        HWProtocolMessage::Empty => warn!("Empty message"),
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   116
        _ => match server.clients[client_id].room_id {
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   117
            None => loggingin::handle(server, client_id, response, message),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   118
            Some(id) if id == server.lobby_id => {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   119
                lobby::handle(server, client_id, response, message)
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   120
            }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
   121
            Some(id) => inroom::handle(server, client_id, response, id, message),
12154
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   122
        },
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   123
    }
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   124
}
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   125
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   126
pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   127
    common::remove_client(server, response, "Connection reset".to_string());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   128
}