Procházet zdrojové kódy

Maybe experimenting with .map and .map_err was a bit overkill for now.

Michele Orrù před 8 roky
rodič
revize
cc17bcdd10
1 změnil soubory, kde provedl 11 přidání a 8 odebrání
  1. 11 8
      src/main.rs

+ 11 - 8
src/main.rs

@@ -42,10 +42,11 @@ fn malformed_packet(err: std::io::Error) {
 
 fn handle(mut stream: tcp::TcpStream) {
     let _ = stream.write(&"go fuck you kthxbye".as_bytes());
-    let _ = stream.local_addr()
-        .map(|addr|
-             info!("somebody opened a tcp connection to {}:{}", addr.ip(), addr.port()))
-        .map_err(malformed_packet);
+    match stream.local_addr() {
+        Ok(addr) =>
+            info!("somebody opened a tcp connection to {}:{}", addr.ip(), addr.port()),
+        Err(err) => malformed_packet(err),
+    }
 }
 
 fn main() {
@@ -80,11 +81,13 @@ fn main() {
         for event in events.iter() {
             match (event.token(), event.kind().is_readable()) {
                 (SERVER, true) => {
-                    let (stream, _) = server.accept().unwrap();
-                    handle(stream);
-                }
+                    match server.accept() {
+                        Err(err) => malformed_packet(err),
+                        Ok((stream, _)) => handle(stream),
+                        }
+                    },
 
-                _ => {
+                    _ => {
                     info!("something happened: {:?}", event.kind());
                     // unreachable!(),
                 }