rust/hedgewars-server/src/server/network.rs
changeset 15180 f1c2289d40bd
parent 15179 e705ac360785
child 15181 f6115638aa92
equal deleted inserted replaced
15179:e705ac360785 15180:f1c2289d40bd
   361         self.io.io_thread.register_rx(poll, utils::IO_TOKEN)?;
   361         self.io.io_thread.register_rx(poll, utils::IO_TOKEN)?;
   362 
   362 
   363         Ok(())
   363         Ok(())
   364     }
   364     }
   365 
   365 
   366     fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
   366     fn deregister_client(&mut self, poll: &Poll, id: ClientId, is_error: bool) {
   367         if let Some(ref mut client) = self.clients.get_mut(id) {
   367         if let Some(ref mut client) = self.clients.get_mut(id) {
   368             poll.deregister(client.socket.inner())
   368             poll.deregister(client.socket.inner())
   369                 .expect("could not deregister socket");
   369                 .expect("could not deregister socket");
   370             if client.has_pending_sends() {
   370             if client.has_pending_sends() && !is_error {
   371                 info!(
   371                 info!(
   372                     "client {} ({}) pending removal",
   372                     "client {} ({}) pending removal",
   373                     client.id, client.peer_addr
   373                     client.id, client.peer_addr
   374                 );
   374                 );
   375                 client.pending_close = true;
   375                 client.pending_close = true;
   437                 }
   437                 }
   438             }
   438             }
   439         }
   439         }
   440 
   440 
   441         for client_id in response.extract_removed_clients() {
   441         for client_id in response.extract_removed_clients() {
   442             self.deregister_client(poll, client_id);
   442             self.deregister_client(poll, client_id, false);
   443         }
   443         }
   444 
   444 
   445         #[cfg(feature = "official-server")]
   445         #[cfg(feature = "official-server")]
   446         {
   446         {
   447             let client_id = response.client_id();
   447             let client_id = response.client_id();
   465                         };
   465                         };
   466                         client.replace_timeout(timeout);
   466                         client.replace_timeout(timeout);
   467                     }
   467                     }
   468                 }
   468                 }
   469                 TimeoutEvent::DropClient => {
   469                 TimeoutEvent::DropClient => {
       
   470                     if let Some(ref mut client) = self.clients.get_mut(client_id) {
       
   471                         client.send_string(
       
   472                             &HwServerMessage::Bye("Ping timeout".to_string()).to_raw_protocol(),
       
   473                         );
       
   474                         client.write();
       
   475                     }
   470                     self.operation_failed(
   476                     self.operation_failed(
   471                         poll,
   477                         poll,
   472                         client_id,
   478                         client_id,
   473                         &ErrorKind::TimedOut.into(),
   479                         &ErrorKind::TimedOut.into(),
   474                         "No ping response",
   480                         "No ping response",
   614         match result {
   620         match result {
   615             Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
   621             Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
   616                 self.pending.insert((client_id, state));
   622                 self.pending.insert((client_id, state));
   617             }
   623             }
   618             Ok(((), state)) if state == NetworkClientState::Closed => {
   624             Ok(((), state)) if state == NetworkClientState::Closed => {
   619                 self.deregister_client(poll, client_id);
   625                 self.deregister_client(poll, client_id, false);
   620             }
   626             }
   621             Ok(_) => (),
   627             Ok(_) => (),
   622             Err(e) => {
   628             Err(e) => {
   623                 self.operation_failed(poll, client_id, &e, "Error while writing to client socket")?
   629                 self.operation_failed(poll, client_id, &e, "Error while writing to client socket")?
   624             }
   630             }
   626 
   632 
   627         Ok(())
   633         Ok(())
   628     }
   634     }
   629 
   635 
   630     pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
   636     pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
   631         self.deregister_client(poll, client_id);
   637         let pending_close = self.clients[client_id].pending_close;
   632         let mut response = handlers::Response::new(client_id);
   638         self.deregister_client(poll, client_id, true);
   633         handlers::handle_client_loss(&mut self.server, client_id, &mut response);
   639 
   634         self.handle_response(response, poll);
   640         if !pending_close {
       
   641             let mut response = handlers::Response::new(client_id);
       
   642             handlers::handle_client_loss(&mut self.server, client_id, &mut response);
       
   643             self.handle_response(response, poll);
       
   644         }
   635 
   645 
   636         Ok(())
   646         Ok(())
   637     }
   647     }
   638 
   648 
   639     pub fn has_pending_operations(&self) -> bool {
   649     pub fn has_pending_operations(&self) -> bool {