improve resyncing

This commit is contained in:
Daniella / Tove 2023-10-04 15:24:10 +02:00
parent 2c61f1b9c9
commit 63287f17fd
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
2 changed files with 7 additions and 6 deletions

View file

@ -79,6 +79,7 @@ fn connect(params: &ClientParams) -> Connection {
fn resync(tcp: &mut SocketAdapter) { fn resync(tcp: &mut SocketAdapter) {
println!(); println!();
eprintln!("Server version mismatch or broken connection. Re-syncing in case of the latter..."); eprintln!("Server version mismatch or broken connection. Re-syncing in case of the latter...");
tcp.set_nonblocking(true);
tcp.internal.set_print(false); tcp.internal.set_print(false);
tcp.write_now().unwrap(); tcp.write_now().unwrap();
tcp.write(&[PacketType::Resync.ordinal() as u8]).unwrap(); tcp.write(&[PacketType::Resync.ordinal() as u8]).unwrap();
@ -86,14 +87,13 @@ fn resync(tcp: &mut SocketAdapter) {
eprintln!( eprintln!(
"Sent resync packet. Server should now wait 8 seconds and then send a resync-echo packet." "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]; let mut buf = [0; 4096];
// read all packets that are still pending. // 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 // wait 5 seconds
thread::sleep(Duration::from_secs(5)); thread::sleep(Duration::from_secs(5));
// read all packets that are still pending. // 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 // server should now have stopped sending packets. waiting 5 more seconds so the server has time to
// send the resync packet. // send the resync packet.
thread::sleep(Duration::from_secs(5)); thread::sleep(Duration::from_secs(5));

View file

@ -13,6 +13,7 @@ use crate::{io_sync, Connection, PacketType, SocketAdapter};
fn resync(tcp: &mut SocketAdapter) { fn resync(tcp: &mut SocketAdapter) {
println!(); println!();
eprintln!("Client version mismatch or broken connection. Re-syncing in case of the latter..."); eprintln!("Client version mismatch or broken connection. Re-syncing in case of the latter...");
tcp.set_nonblocking(true);
tcp.internal.set_print(false); tcp.internal.set_print(false);
tcp.write_now().unwrap(); tcp.write_now().unwrap();
tcp.write(&[PacketType::Resync.ordinal() as u8]).unwrap(); tcp.write(&[PacketType::Resync.ordinal() as u8]).unwrap();
@ -22,11 +23,11 @@ fn resync(tcp: &mut SocketAdapter) {
); );
let mut buf = [0; 4096]; let mut buf = [0; 4096];
// read all packets that are still pending. // 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 // wait 5 seconds
thread::sleep(Duration::from_secs(5)); thread::sleep(Duration::from_secs(5));
// read all packets that are still pending. // 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 // server should now have stopped sending packets. waiting 5 more seconds so the client has time to
// send the resync packet. // send the resync packet.
thread::sleep(Duration::from_secs(5)); 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); tcp.set_nonblocking(false);
match pt { match pt {
PacketType::NewClient => unreachable!(), PacketType::NewClient => resync(&mut tcp),
PacketType::CloseClient => { PacketType::CloseClient => {
tcp.internal.read_exact(&mut buf8).unwrap(); tcp.internal.read_exact(&mut buf8).unwrap();