fixed potential tcp errors that may occur when packets are split

asyncversion
Kyle Johnsen 2022-03-20 16:29:28 -04:00
parent e64ddafeb6
commit 2f894d7729
1 changed files with 24 additions and 14 deletions

View File

@ -187,7 +187,14 @@ async fn process_client(socket: TcpStream, udp_socket: Rc<RefCell<UdpSocket>>, c
async fn read_timeout(mut socket: &TcpStream, buf: &mut [u8], duration: u64) -> Result<usize,Box<dyn Error>> {
match future::timeout(Duration::from_millis(duration), socket.read(buf)).await {
//this is a read exact function. The buffer passed should be the exact size wanted
let num_to_read = buf.len();
let mut num_read = 0;
while num_read < num_to_read {
match future::timeout(Duration::from_millis(duration), socket.read(&mut buf[num_read..])).await {
Ok(r) => {
match r {
@ -197,7 +204,7 @@ async fn read_timeout(mut socket: &TcpStream, buf: &mut [u8], duration: u64) ->
},
Ok(n) => {
return Ok(n);
num_read += n;
},
Err(e) => {return Err(format!("{}", e.to_string()))?}
}
@ -207,6 +214,9 @@ async fn read_timeout(mut socket: &TcpStream, buf: &mut [u8], duration: u64) ->
Err(e) => {return Err(format!("{}", e.to_string()))?}
}
}
return Ok(num_read);
}