From 6a1f512db4c7efcbb38daf0a8a7ccd6b31ca900f Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Sun, 2 Jan 2022 19:02:26 -0800 Subject: [PATCH] error catching for sockets --- src/modules/socket_handler.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/modules/socket_handler.rs b/src/modules/socket_handler.rs index ae27309..8c5c519 100644 --- a/src/modules/socket_handler.rs +++ b/src/modules/socket_handler.rs @@ -82,16 +82,29 @@ impl Socket { pub fn send(&self, message: String) { let mut streams = self.socket_txs.lock().unwrap(); + let mut removes = Vec::::new(); if streams.len() == 0 {return} for i in 0..streams.len(){ - let mut tx = streams.get(i).unwrap().as_ref(); - - match tx.write(message.clone().as_bytes()) { - Err(_) => {streams.remove(i); println!("removed a socket"); continue;}, - Ok(_) => {}, + match streams.get(i) { + None => { + removes.push(i); + }, + Some(strm) => { + let mut tx = strm.as_ref(); + match tx.write(message.clone().as_bytes()) { + Err(_) => { + removes.push(i) + }, + Ok(_) => { + tx.write(b"\n").unwrap(); + tx.flush().unwrap(); + } + } + } } - tx.write(b"\n").unwrap(); - tx.flush().unwrap(); + } + for i in removes.iter() { + streams.remove(*i); } } } \ No newline at end of file