rust/hedgewars-server/src/server/handlers.rs
author alfadur <mail@none>
Thu, 07 Feb 2019 17:17:42 +0300
changeset 14694 25c564f77b7d
parent 14693 6a2e13e36b7f
child 14696 8a45c90f4580
permissions -rw-r--r--
remove lobby room
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
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
     4
use super::{
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
     5
    actions::{Destination, DestinationRoom},
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
     6
    core::HWServer,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
     7
    coretypes::ClientId,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
     8
};
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
     9
use crate::{
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    10
    protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*},
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    11
    server::actions::PendingMessage,
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    12
    utils,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    13
};
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    14
use base64::encode;
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13798
diff changeset
    15
use log::*;
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    16
use rand::{thread_rng, RngCore};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13521
diff changeset
    17
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    18
mod checker;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    19
mod common;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    20
mod inroom;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    21
mod lobby;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    22
mod loggingin;
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
    23
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    24
use self::loggingin::LoginResult;
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    25
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    26
pub struct Response {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    27
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    28
    messages: Vec<PendingMessage>,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    29
}
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    30
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    31
impl Response {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    32
    pub fn new(client_id: ClientId) -> Self {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    33
        Self {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    34
            client_id,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    35
            messages: vec![],
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
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    39
    #[inline]
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    40
    pub fn is_empty(&self) -> bool {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    41
        self.messages.is_empty()
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    42
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    43
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    44
    #[inline]
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    45
    pub fn len(&self) -> usize {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    46
        self.messages.len()
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    47
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    48
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    49
    #[inline]
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    50
    pub fn client_id(&self) -> ClientId {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    51
        self.client_id
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    52
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    53
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    54
    #[inline]
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    55
    pub fn add(&mut self, message: PendingMessage) {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    56
        self.messages.push(message)
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    57
    }
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    58
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    59
    pub fn extract_messages<'a, 'b: 'a>(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    60
        &'b mut self,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    61
        server: &'a HWServer,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    62
    ) -> impl Iterator<Item = (Vec<ClientId>, HWServerMessage)> + 'a {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    63
        let client_id = self.client_id;
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    64
        self.messages.drain(..).map(move |m| {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    65
            let ids = get_recipients(server, client_id, &m.destination);
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    66
            (ids, m.message)
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    67
        })
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    68
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    69
}
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    70
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    71
impl Extend<PendingMessage> for Response {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    72
    fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    73
        for msg in iter {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    74
            self.add(msg)
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    75
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    76
    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    77
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14673
diff changeset
    78
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    79
fn get_recipients(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    80
    server: &HWServer,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    81
    client_id: ClientId,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    82
    destination: &Destination,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    83
) -> Vec<ClientId> {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    84
    let mut ids = match *destination {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    85
        Destination::ToSelf => vec![client_id],
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    86
        Destination::ToId(id) => vec![id],
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    87
        Destination::ToAll {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
    88
            room_id: DestinationRoom::Lobby,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
    89
            ..
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
    90
        } => server.lobby_clients(),
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
    91
        Destination::ToAll {
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
    92
            room_id: DestinationRoom::Room(id),
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14693
diff changeset
    93
            ..
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    94
        } => server.room_clients(id),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    95
        Destination::ToAll {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    96
            protocol: Some(proto),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    97
            ..
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    98
        } => server.protocol_clients(proto),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    99
        Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   100
    };
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   101
    if let Destination::ToAll {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   102
        skip_self: true, ..
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   103
    } = destination
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   104
    {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   105
        if let Some(index) = ids.iter().position(|id| *id == client_id) {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   106
            ids.remove(index);
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   107
        }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   108
    }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   109
    ids
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   110
}
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   111
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   112
pub fn handle(
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   113
    server: &mut HWServer,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   114
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   115
    response: &mut Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   116
    message: HWProtocolMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   117
) {
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   118
    match message {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   119
        HWProtocolMessage::Ping => response.add(Pong.send_self()),
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   120
        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   121
        HWProtocolMessage::Empty => warn!("Empty message"),
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   122
        _ => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   123
            if server.anteroom.clients.contains(client_id) {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   124
                match loggingin::handle(&mut server.anteroom, client_id, response, message) {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   125
                    LoginResult::Unchanged => (),
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   126
                    LoginResult::Complete => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   127
                        if let Some(client) = server.anteroom.remove_client(client_id) {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   128
                            server.add_client(client_id, client);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   129
                        }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   130
                    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   131
                    LoginResult::Exit => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   132
                        server.anteroom.remove_client(client_id);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   133
                    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   134
                }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   135
            } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   136
                match message {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   137
                    HWProtocolMessage::Quit(Some(msg)) => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   138
                        common::remove_client(server, response, "User quit: ".to_string() + &msg);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   139
                    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   140
                    HWProtocolMessage::Quit(None) => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   141
                        common::remove_client(server, response, "User quit".to_string());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   142
                    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   143
                    _ => match server.clients[client_id].room_id {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   144
                        None => lobby::handle(server, client_id, response, message),
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   145
                        Some(room_id) => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   146
                            inroom::handle(server, client_id, response, room_id, message)
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   147
                        }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   148
                    },
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   149
                }
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   150
            }
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   151
        }
12149
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   152
    }
44b06731278b Fix handlers module
unc0rr
parents:
diff changeset
   153
}
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   154
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   155
pub fn handle_client_accept(server: &mut HWServer, client_id: ClientId, response: &mut Response) {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   156
    let mut salt = [0u8; 18];
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   157
    thread_rng().fill_bytes(&mut salt);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   158
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   159
    server.anteroom.add_client(client_id, encode(&salt));
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   160
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   161
    response.add(HWServerMessage::Connected(utils::PROTOCOL_VERSION).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   162
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   163
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   164
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: 14672
diff changeset
   165
    common::remove_client(server, response, "Connection reset".to_string());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   166
}