avoid crashing server in the event loop
authoralfadur
Sun, 12 May 2019 00:29:47 +0300
changeset 14916 8750530bf7e7
parent 14915 a3ad06ac390e
child 14917 522b452390c4
avoid crashing server in the event loop
rust/hedgewars-server/src/main.rs
rust/hedgewars-server/src/server/network.rs
--- a/rust/hedgewars-server/src/main.rs	Sat May 11 22:39:55 2019 +0200
+++ b/rust/hedgewars-server/src/main.rs	Sun May 12 00:29:47 2019 +0300
@@ -70,27 +70,43 @@
             if event.readiness() & Ready::readable() == Ready::readable() {
                 match event.token() {
                     token @ utils::SERVER_TOKEN | token @ utils::SECURE_SERVER_TOKEN => {
-                        hw_network.accept_client(&poll, token).unwrap()
+                        match hw_network.accept_client(&poll, token) {
+                            Ok(()) => (),
+                            Err(e) => debug!("Error accepting client: {}", e),
+                        }
                     }
-                    utils::TIMER_TOKEN => hw_network.handle_timeout(&poll).unwrap(),
+                    utils::TIMER_TOKEN => match hw_network.handle_timeout(&poll) {
+                        Ok(()) => (),
+                        Err(e) => debug!("Error in timer event: {}", e),
+                    },
                     #[cfg(feature = "official-server")]
-                    utils::IO_TOKEN => hw_network.handle_io_result(),
-                    Token(tok) => hw_network.client_readable(&poll, tok).unwrap(),
+                    utils::IO_TOKEN => match hw_network.handle_io_result() {
+                        Ok(()) => (),
+                        Err(e) => debug!("Error in IO task: {}", e),
+                    },
+                    Token(token) => match hw_network.client_readable(&poll, token) {
+                        Ok(()) => (),
+                        Err(e) => debug!("Error reading from client socket {}: {}", token, e),
+                    },
                 }
             }
             if event.readiness() & Ready::writable() == Ready::writable() {
                 match event.token() {
-                    utils::SERVER_TOKEN | utils::TIMER_TOKEN | utils::IO_TOKEN => unreachable!(),
-                    Token(tok) => hw_network.client_writable(&poll, tok).unwrap(),
+                    utils::SERVER_TOKEN
+                    | utils::SECURE_SERVER_TOKEN
+                    | utils::TIMER_TOKEN
+                    | utils::IO_TOKEN => unreachable!(),
+                    Token(token) => match hw_network.client_writable(&poll, token) {
+                        Ok(()) => (),
+                        Err(e) => debug!("Error writing to client socket {}: {}", token, e),
+                    },
                 }
             }
-            //            if event.kind().is_hup() || event.kind().is_error() {
-            //                match event.token() {
-            //                    utils::SERVER => unreachable!(),
-            //                    Token(tok) => server.client_error(&poll, tok).unwrap(),
-            //                }
-            //            }
         }
-        hw_network.on_idle(&poll).unwrap();
+
+        match hw_network.on_idle(&poll) {
+            Ok(()) => (),
+            Err(e) => debug!("Error in idle handler: {}", e),
+        };
     }
 }
--- a/rust/hedgewars-server/src/server/network.rs	Sat May 11 22:39:55 2019 +0200
+++ b/rust/hedgewars-server/src/server/network.rs	Sun May 12 00:29:47 2019 +0300
@@ -368,7 +368,7 @@
         poll: &Poll,
         client_socket: ClientSocket,
         addr: SocketAddr,
-    ) -> ClientId {
+    ) -> io::Result<ClientId> {
         let entry = self.clients.vacant_entry();
         let client_id = entry.key();
 
@@ -377,8 +377,7 @@
             Token(client_id),
             Ready::readable() | Ready::writable(),
             PollOpt::edge(),
-        )
-        .expect("could not register socket with event loop");
+        )?;
 
         let client = NetworkClient::new(
             client_id,
@@ -389,7 +388,7 @@
         info!("client {} ({}) added", client.id, client.peer_addr);
         entry.insert(client);
 
-        client_id
+        Ok(client_id)
     }
 
     fn handle_response(&mut self, mut response: handlers::Response, poll: &Poll) {
@@ -453,11 +452,12 @@
     }
 
     #[cfg(feature = "official-server")]
-    pub fn handle_io_result(&mut self) {
+    pub fn handle_io_result(&mut self) -> io::Result<()> {
         if let Some((client_id, result)) = self.io.try_recv() {
             let mut response = handlers::Response::new(client_id);
             handlers::handle_io_result(&mut self.server, client_id, &mut response, result);
         }
+        Ok(())
     }
 
     fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> {
@@ -497,14 +497,14 @@
                 let (client_socket, addr) = self.listener.accept()?;
                 info!("Connected(plaintext): {}", addr);
                 let client_id =
-                    self.register_client(poll, self.create_client_socket(client_socket)?, addr);
+                    self.register_client(poll, self.create_client_socket(client_socket)?, addr)?;
                 self.init_client(poll, client_id);
             }
             #[cfg(feature = "tls-connections")]
             utils::SECURE_SERVER_TOKEN => {
                 let (client_socket, addr) = self.ssl.listener.accept()?;
                 info!("Connected(TLS): {}", addr);
-                self.register_client(poll, self.create_client_secure_socket(client_socket)?, addr);
+                self.register_client(poll, self.create_client_secure_socket(client_socket)?, addr)?;
             }
             _ => unreachable!(),
         }