rust/hedgewars-server/src/server/handlers.rs
changeset 14672 6e6632068a33
parent 14671 455865ccd36c
child 14673 08a8605bafaf
equal deleted inserted replaced
14671:455865ccd36c 14672:6e6632068a33
     1 use mio;
     1 use mio;
     2 use std::{io, io::Write};
     2 use std::{io, io::Write};
     3 
     3 
     4 use super::{
     4 use super::{
     5     actions::{Action, Action::*},
     5     actions::{Action, Action::*, Destination},
     6     core::HWServer,
     6     core::HWServer,
     7     coretypes::ClientId,
     7     coretypes::ClientId,
     8 };
     8 };
     9 use crate::{
     9 use crate::{
    10     protocol::messages::{HWProtocolMessage, HWServerMessage::*},
    10     protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*},
    11     server::actions::PendingMessage,
    11     server::actions::PendingMessage,
    12 };
    12 };
    13 use log::*;
    13 use log::*;
    14 
    14 
    15 mod checker;
    15 mod checker;
    29             client_id,
    29             client_id,
    30             messages: vec![],
    30             messages: vec![],
    31         }
    31         }
    32     }
    32     }
    33 
    33 
       
    34     #[inline]
       
    35     pub fn is_empty(&self) -> bool {
       
    36         self.messages.is_empty()
       
    37     }
       
    38 
       
    39     #[inline]
       
    40     pub fn len(&self) -> usize {
       
    41         self.messages.len()
       
    42     }
       
    43 
       
    44     #[inline]
    34     pub fn client_id(&self) -> ClientId {
    45     pub fn client_id(&self) -> ClientId {
    35         self.client_id
    46         self.client_id
    36     }
    47     }
    37 
    48 
       
    49     #[inline]
    38     pub fn add(&mut self, message: PendingMessage) {
    50     pub fn add(&mut self, message: PendingMessage) {
    39         self.messages.push(message)
    51         self.messages.push(message)
    40     }
    52     }
       
    53 
       
    54     pub fn extract_messages<'a, 'b: 'a>(
       
    55         &'b mut self,
       
    56         server: &'a HWServer,
       
    57     ) -> impl Iterator<Item = (Vec<ClientId>, HWServerMessage)> + 'a {
       
    58         let client_id = self.client_id;
       
    59         self.messages.drain(..).map(move |m| {
       
    60             let ids = get_recipients(server, client_id, &m.destination);
       
    61             (ids, m.message)
       
    62         })
       
    63     }
       
    64 }
       
    65 
       
    66 fn get_recipients(
       
    67     server: &HWServer,
       
    68     client_id: ClientId,
       
    69     destination: &Destination,
       
    70 ) -> Vec<ClientId> {
       
    71     let mut ids = match *destination {
       
    72         Destination::ToSelf => vec![client_id],
       
    73         Destination::ToId(id) => vec![id],
       
    74         Destination::ToAll {
       
    75             room_id: Some(id), ..
       
    76         } => server.room_clients(id),
       
    77         Destination::ToAll {
       
    78             protocol: Some(proto),
       
    79             ..
       
    80         } => server.protocol_clients(proto),
       
    81         Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(),
       
    82     };
       
    83     if let Destination::ToAll {
       
    84         skip_self: true, ..
       
    85     } = destination
       
    86     {
       
    87         if let Some(index) = ids.iter().position(|id| *id == client_id) {
       
    88             ids.remove(index);
       
    89         }
       
    90     }
       
    91     ids
    41 }
    92 }
    42 
    93 
    43 pub fn handle(
    94 pub fn handle(
    44     server: &mut HWServer,
    95     server: &mut HWServer,
    45     client_id: ClientId,
    96     client_id: ClientId,
    47     message: HWProtocolMessage,
    98     message: HWProtocolMessage,
    48 ) {
    99 ) {
    49     match message {
   100     match message {
    50         HWProtocolMessage::Ping => {
   101         HWProtocolMessage::Ping => {
    51             response.add(Pong.send_self());
   102             response.add(Pong.send_self());
    52             server.react(client_id, vec![Pong.send_self().action()])
       
    53         }
   103         }
    54         HWProtocolMessage::Quit(Some(msg)) => {
   104         HWProtocolMessage::Quit(Some(msg)) => {
    55             server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)])
   105             //ByeClient("User quit: ".to_string() + &msg)
    56         }
   106         }
    57         HWProtocolMessage::Quit(None) => {
   107         HWProtocolMessage::Quit(None) => {
    58             server.react(client_id, vec![ByeClient("User quit".to_string())])
   108             //ByeClient("User quit".to_string())
    59         }
   109         }
    60         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
   110         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
    61         HWProtocolMessage::Empty => warn!("Empty message"),
   111         HWProtocolMessage::Empty => warn!("Empty message"),
    62         _ => match server.clients[client_id].room_id {
   112         _ => match server.clients[client_id].room_id {
    63             None => loggingin::handle(server, client_id, response, message),
   113             None => loggingin::handle(server, client_id, response, message),