rust/hedgewars-server/src/server/handlers.rs
changeset 14701 8a45c90f4580
parent 14699 25c564f77b7d
child 14784 f43ab2bd76ae
equal deleted inserted replaced
14700:216d39de1a44 14701:8a45c90f4580
    24 use self::loggingin::LoginResult;
    24 use self::loggingin::LoginResult;
    25 
    25 
    26 pub struct Response {
    26 pub struct Response {
    27     client_id: ClientId,
    27     client_id: ClientId,
    28     messages: Vec<PendingMessage>,
    28     messages: Vec<PendingMessage>,
       
    29     removed_clients: Vec<ClientId>,
    29 }
    30 }
    30 
    31 
    31 impl Response {
    32 impl Response {
    32     pub fn new(client_id: ClientId) -> Self {
    33     pub fn new(client_id: ClientId) -> Self {
    33         Self {
    34         Self {
    34             client_id,
    35             client_id,
    35             messages: vec![],
    36             messages: vec![],
       
    37             removed_clients: vec![],
    36         }
    38         }
    37     }
    39     }
    38 
    40 
    39     #[inline]
    41     #[inline]
    40     pub fn is_empty(&self) -> bool {
    42     pub fn is_empty(&self) -> bool {
    41         self.messages.is_empty()
    43         self.messages.is_empty() && self.removed_clients.is_empty()
    42     }
    44     }
    43 
    45 
    44     #[inline]
    46     #[inline]
    45     pub fn len(&self) -> usize {
    47     pub fn len(&self) -> usize {
    46         self.messages.len()
    48         self.messages.len()
    63         let client_id = self.client_id;
    65         let client_id = self.client_id;
    64         self.messages.drain(..).map(move |m| {
    66         self.messages.drain(..).map(move |m| {
    65             let ids = get_recipients(server, client_id, &m.destination);
    67             let ids = get_recipients(server, client_id, &m.destination);
    66             (ids, m.message)
    68             (ids, m.message)
    67         })
    69         })
       
    70     }
       
    71 
       
    72     pub fn remove_client(&mut self, client_id: ClientId) {
       
    73         self.removed_clients.push(client_id);
       
    74     }
       
    75 
       
    76     pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ {
       
    77         self.removed_clients.drain(..)
    68     }
    78     }
    69 }
    79 }
    70 
    80 
    71 impl Extend<PendingMessage> for Response {
    81 impl Extend<PendingMessage> for Response {
    72     fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) {
    82     fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) {
   128                             server.add_client(client_id, client);
   138                             server.add_client(client_id, client);
   129                         }
   139                         }
   130                     }
   140                     }
   131                     LoginResult::Exit => {
   141                     LoginResult::Exit => {
   132                         server.anteroom.remove_client(client_id);
   142                         server.anteroom.remove_client(client_id);
       
   143                         response.remove_client(client_id);
   133                     }
   144                     }
   134                 }
   145                 }
   135             } else {
   146             } else {
   136                 match message {
   147                 match message {
   137                     HWProtocolMessage::Quit(Some(msg)) => {
   148                     HWProtocolMessage::Quit(Some(msg)) => {
   160 
   171 
   161     response.add(HWServerMessage::Connected(utils::PROTOCOL_VERSION).send_self());
   172     response.add(HWServerMessage::Connected(utils::PROTOCOL_VERSION).send_self());
   162 }
   173 }
   163 
   174 
   164 pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) {
   175 pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) {
   165     common::remove_client(server, response, "Connection reset".to_string());
   176     if server.anteroom.remove_client(client_id).is_none() {
       
   177         common::remove_client(server, response, "Connection reset".to_string());
       
   178     }
   166 }
   179 }