From b82ebe911c2427d5de089ba7e261131b25fe42b3 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 24 Dec 2022 20:08:15 -0800 Subject: [PATCH] fix: set streams to unblocking; catch those errors --- Cargo.toml | 2 -- src/main.rs | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09a94d5..5dcb57e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,5 +18,3 @@ derive_more = "0.99.17" id3 = "1.5.1" rodio = "0.16.0" tungstenite = "0.18.0" -workctl = "0.2.0" -crossbeam-channel = "0.5.6" diff --git a/src/main.rs b/src/main.rs index 74c6ac2..8ee30d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,15 +90,21 @@ fn main() { let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap(); let mut music_player = MusicPlayer::new(test_file[0].clone(), &stream_handle); - let server = TcpListener::bind("127.0.0.1:9001").unwrap(); + let tcp_listener = TcpListener::bind("127.0.0.1:9001").unwrap(); + tcp_listener.set_nonblocking(true).unwrap(); let mut sockets = Vec::>::new(); - println!("Listening on {}", server.local_addr().unwrap()); + println!("Listening on {}", tcp_listener.local_addr().unwrap()); + loop { - if let Ok((stream, addr)) = server.accept() { + if let Ok((stream, addr)) = tcp_listener.accept() { + stream.set_nonblocking(true).unwrap(); println!("New socket connected from: {}", addr); - //TODO: handle this error - sockets.push(accept(stream).unwrap()); + + match accept(stream) { + Ok(sck) => sockets.push(sck), + Err(_) => continue, + } } if sockets.len() == 0 { @@ -107,7 +113,6 @@ fn main() { // Need to get an asynchronous socket reader like tokio for i in 0..sockets.len() { - println!("{}", sockets.len()); match sockets[i].read_message() { Ok(mess) => { if mess.is_text() { @@ -126,13 +131,26 @@ fn main() { } } } - Err(error) => { - if error.to_string().starts_with("Connection closed normally") { - sockets.remove(i); - } else { - println!("a socket errored: {}", error.to_string()); + Err(error) => match error { + tungstenite::Error::ConnectionClosed => { + println!("dropping socket"); + let tmp = sockets.remove(i); + drop(tmp); } - } + tungstenite::Error::Io(_) => { + if error.to_string().ends_with("(os error 32)") { + sockets.remove(i); + } else if error.to_string().ends_with("(os error 11)") { + continue; + } else { + println!("There was an IO error: {}", error.to_string()); + } + + //panic!(); + //continue; + } + _ => println!("A socket errored: {}", error.to_string()), + }, } } }