fixed potential tcp errors that may occur when packets are split
parent
e64ddafeb6
commit
2f894d7729
36
src/main.rs
36
src/main.rs
|
|
@ -187,27 +187,37 @@ 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 {
|
||||
Ok(r) => {
|
||||
match r {
|
||||
//this is a read exact function. The buffer passed should be the exact size wanted
|
||||
|
||||
Ok(n) if n == 0 => {
|
||||
let num_to_read = buf.len();
|
||||
|
||||
return Err(format!("{}", "no bytes read"))?
|
||||
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 => {
|
||||
|
||||
Err(e) => {return Err(format!("{}", e.to_string()))?}
|
||||
return Err(format!("{}", "no bytes read"))?
|
||||
|
||||
},
|
||||
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<RefCell<Client>>, mut socket: TcpStream, clients: Rc<RefCell<HashMap<u32, Rc<RefCell<Client>>>>>, rooms: Rc<RefCell<HashMap<String, Rc<RefCell<Room>>>>>, duration: u64){
|
||||
|
|
|
|||
Loading…
Reference in New Issue