From 2a95f11d304d20dca0741ebc9fbc5502fbbf4acb Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Wed, 6 May 2020 00:14:43 +0200 Subject: [PATCH] Handle IO error from `peer_addr()` call (fixes #8) Presumably, if a client disconnects immediately after connecting (maybe even while we're still busy setting up the previous one), then `peer_addr()` can return an error. This commit handles any such error by logging them. Except for the NotConnected kind which we now know are expected during normal operation. --- src/main.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ac248a4..752862c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use mumble_protocol::control::RawControlPacket; use mumble_protocol::Clientbound; use std::convert::Into; use std::convert::TryInto; +use std::io::ErrorKind; use std::net::Ipv4Addr; use std::net::Ipv6Addr; use std::net::ToSocketAddrs; @@ -121,7 +122,15 @@ async fn main() -> Result<(), Error> { let mut server = TcpListener::bind(&socket_addr).await?; loop { let (client, _) = server.accept().await?; - let addr = client.peer_addr().expect("peer to have an address"); + let addr = match client.peer_addr() { + Ok(addr) => addr, + Err(err) => { + if err.kind() != ErrorKind::NotConnected { + println!("Error getting address of new connection: {:?}", err); + } + continue; + } + }; println!("New connection from {}", addr); // Connect to server