From 63287f17fdd9b253a5d1bda2a70109eed1db9575 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Wed, 4 Oct 2023 15:24:10 +0200 Subject: [PATCH] improve resyncing --- src/client.rs | 6 +++--- src/server.rs | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 6e29870..82a765c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -79,6 +79,7 @@ fn connect(params: &ClientParams) -> Connection { fn resync(tcp: &mut SocketAdapter) { println!(); eprintln!("Server version mismatch or broken connection. Re-syncing in case of the latter..."); + tcp.set_nonblocking(true); tcp.internal.set_print(false); tcp.write_now().unwrap(); tcp.write(&[PacketType::Resync.ordinal() as u8]).unwrap(); @@ -86,14 +87,13 @@ fn resync(tcp: &mut SocketAdapter) { eprintln!( "Sent resync packet. Server should now wait 8 seconds and then send a resync-echo packet." ); - tcp.set_nonblocking(true); let mut buf = [0; 4096]; // read all packets that are still pending. - while Some(Some(4096)) == tcp.poll(&mut buf).ok() {} + while let Some(Some(_x @ 1..)) = tcp.poll(&mut buf).ok() {} // wait 5 seconds thread::sleep(Duration::from_secs(5)); // read all packets that are still pending. - while Some(Some(4096)) == tcp.poll(&mut buf).ok() {} + while let Some(Some(_x @ 1..)) = tcp.poll(&mut buf).ok() {} // server should now have stopped sending packets. waiting 5 more seconds so the server has time to // send the resync packet. thread::sleep(Duration::from_secs(5)); diff --git a/src/server.rs b/src/server.rs index 351c74c..9634c9e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -13,6 +13,7 @@ use crate::{io_sync, Connection, PacketType, SocketAdapter}; fn resync(tcp: &mut SocketAdapter) { println!(); eprintln!("Client version mismatch or broken connection. Re-syncing in case of the latter..."); + tcp.set_nonblocking(true); tcp.internal.set_print(false); tcp.write_now().unwrap(); tcp.write(&[PacketType::Resync.ordinal() as u8]).unwrap(); @@ -22,11 +23,11 @@ fn resync(tcp: &mut SocketAdapter) { ); let mut buf = [0; 4096]; // read all packets that are still pending. - while Some(Some(4096)) == tcp.poll(&mut buf).ok() {} + while let Some(Some(_x @ 1..)) = tcp.poll(&mut buf).ok() {} // wait 5 seconds thread::sleep(Duration::from_secs(5)); // read all packets that are still pending. - while Some(Some(4096)) == tcp.poll(&mut buf).ok() {} + while let Some(Some(_x @ 1..)) = tcp.poll(&mut buf).ok() {} // server should now have stopped sending packets. waiting 5 more seconds so the client has time to // send the resync packet. thread::sleep(Duration::from_secs(5)); @@ -143,7 +144,7 @@ pub fn server(port: u16, key: &str, sleep_delay_ms: u64) { }; tcp.set_nonblocking(false); match pt { - PacketType::NewClient => unreachable!(), + PacketType::NewClient => resync(&mut tcp), PacketType::CloseClient => { tcp.internal.read_exact(&mut buf8).unwrap();