From fb4ed9cc37755f898847bf7cc880de8cc0b5c1e7 Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Fri, 17 Dec 2021 18:27:00 -0800 Subject: [PATCH] refactoring --- src/modules/socket_handler.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/modules/socket_handler.rs b/src/modules/socket_handler.rs index c6128f9..f6c1a9d 100644 --- a/src/modules/socket_handler.rs +++ b/src/modules/socket_handler.rs @@ -7,10 +7,6 @@ use std::time::Duration; use super::message_handler::StateMessage; -trait SocketCallback { - fn handle_message(message: String); -} - pub struct Socket{ } @@ -21,13 +17,14 @@ impl Socket { TcpListener::bind(address).unwrap() } - pub fn handle_connections(listener: TcpListener, update_tx: Sender) -> (sync_flag::SyncFlagTx, JoinHandle<()>){ + pub fn handle_connections(listener: TcpListener, messenger_tx: Sender) -> (sync_flag::SyncFlagTx, JoinHandle<()>){ let (tx, thread_stop_flag) = sync_flag::new_syncflag(true); + let handle = thread::spawn(move || { listener.set_nonblocking(true).unwrap(); while thread_stop_flag.get() { for (stream, addr) in listener.accept() { - Socket::handle_client(stream, update_tx.clone()); + Socket::handle_client(stream, messenger_tx.clone()); } thread::sleep(Duration::from_millis(100)); } @@ -37,18 +34,17 @@ impl Socket { } pub fn handle_client(mut stream: TcpStream, update_tx: Sender) { - let mut buffer = [0; 1024]; let read_size = stream.read(&mut buffer).unwrap(); + //Tcp is supposed to have a 0 byte read if closed by client if read_size == 0 { stream.shutdown(Shutdown::Both).unwrap(); return } let output = String::from_utf8_lossy(&buffer[0..read_size]); - println!("recieved: {}", output); update_tx.send(output.into_owned()).unwrap(); } } \ No newline at end of file