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.
This commit is contained in:
Jonas Herzig 2020-05-06 00:14:43 +02:00
parent 69451531bd
commit 2a95f11d30

View file

@ -14,6 +14,7 @@ use mumble_protocol::control::RawControlPacket;
use mumble_protocol::Clientbound; use mumble_protocol::Clientbound;
use std::convert::Into; use std::convert::Into;
use std::convert::TryInto; use std::convert::TryInto;
use std::io::ErrorKind;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::net::Ipv6Addr; use std::net::Ipv6Addr;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
@ -121,7 +122,15 @@ async fn main() -> Result<(), Error> {
let mut server = TcpListener::bind(&socket_addr).await?; let mut server = TcpListener::bind(&socket_addr).await?;
loop { loop {
let (client, _) = server.accept().await?; 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); println!("New connection from {}", addr);
// Connect to server // Connect to server