From 2f894d77296640aa4fef9b29d6c8d2946504f389 Mon Sep 17 00:00:00 2001 From: Kyle Johnsen Date: Sun, 20 Mar 2022 16:29:28 -0400 Subject: [PATCH] fixed potential tcp errors that may occur when packets are split --- src/main.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index b4932b7..4037adb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -187,27 +187,37 @@ async fn process_client(socket: TcpStream, udp_socket: Rc>, c async fn read_timeout(mut socket: &TcpStream, buf: &mut [u8], duration: u64) -> Result> { - match future::timeout(Duration::from_millis(duration), socket.read(buf)).await { - Ok(r) => { - match r { + //this is a read exact function. The buffer passed should be the exact size wanted - Ok(n) if n == 0 => { - - return Err(format!("{}", "no bytes read"))? + let num_to_read = buf.len(); + + let mut num_read = 0; - }, - Ok(n) => { - return Ok(n); - }, - Err(e) => {return Err(format!("{}", e.to_string()))?} - } + while num_read < num_to_read { + match future::timeout(Duration::from_millis(duration), socket.read(&mut buf[num_read..])).await { + Ok(r) => { + match r { - }, + Ok(n) if n == 0 => { + + return Err(format!("{}", "no bytes read"))? - Err(e) => {return Err(format!("{}", e.to_string()))?} + }, + Ok(n) => { + num_read += n; + }, + Err(e) => {return Err(format!("{}", e.to_string()))?} + } + }, + + Err(e) => {return Err(format!("{}", e.to_string()))?} + + } } + return Ok(num_read); + } async fn client_read(client: Rc>, mut socket: TcpStream, clients: Rc>>>>, rooms: Rc>>>>, duration: u64){