From bf7b2a2ee3cfa1b1bd86f6efbe152c9c536d29ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Altu=C4=9F=20Bakan?= Date: Sun, 8 Oct 2023 15:16:28 +0200 Subject: [PATCH] Update duplicate packet behavior --- src/config.rs | 28 ++++++++++++++++++---------- src/server.rs | 2 +- src/worker.rs | 8 ++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7e7e1d2..eb744f9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -91,15 +91,20 @@ impl Config { println!(" -d, --directory \tSet the serving directory (default: Current Working Directory)"); println!(" -s, --single-port\t\tUse a single port for both sending and receiving (default: false)"); println!(" -r, --read-only\t\tRefuse all write requests, making the server read-only (default: false)"); - println!(" --duplicate-packets \tDuplicate all packets sent from the server (default: 1)"); + println!(" --duplicate-packets \tDuplicate all packets sent from the server (default: 0)"); println!(" -h, --help\t\t\tPrint help information"); process::exit(0); } "--duplicate-packets" => { if let Some(duplicate_packets_str) = args.next() { let duplicate_packets = duplicate_packets_str.parse::()?; - if duplicate_packets < 1 { - return Err("Duplicate packets must be greater than 0".into()); + + if duplicate_packets == u8::MAX { + return Err(format!( + "Duplicate packets should be less than {}", + u8::MAX + ) + .into()); } config.duplicate_packets = duplicate_packets; } else { @@ -178,13 +183,6 @@ mod tests { #[test] fn returns_error_on_invalid_duplicate_packets() { - assert!(Config::new( - ["/", "--duplicate-packets", "0"] - .iter() - .map(|s| s.to_string()), - ) - .is_err()); - assert!(Config::new( ["/", "--duplicate-packets", "-1"] .iter() @@ -192,4 +190,14 @@ mod tests { ) .is_err()); } + + #[test] + fn returns_error_on_max_duplicate_packets() { + assert!(Config::new( + ["/", "--duplicate-packets", format!("{}", u8::MAX).as_str()] + .iter() + .map(|s| s.to_string()), + ) + .is_err()); + } } diff --git a/src/server.rs b/src/server.rs index 436de8e..3ff390d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -179,7 +179,7 @@ impl Server { worker_options.block_size, worker_options.timeout, worker_options.window_size, - self.duplicate_packets, + self.duplicate_packets + 1, ); worker.send() } diff --git a/src/worker.rs b/src/worker.rs index 3e39e83..6fd4801 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -43,7 +43,7 @@ pub struct Worker { blk_size: usize, timeout: Duration, windowsize: u16, - duplicate_packets: u8, + repeat_amount: u8, } impl Worker { @@ -54,7 +54,7 @@ impl Worker { blk_size: usize, timeout: Duration, windowsize: u16, - duplicate_packets: u8, + repeat_amount: u8, ) -> Worker { Worker { socket, @@ -62,7 +62,7 @@ impl Worker { blk_size, timeout, windowsize, - duplicate_packets, + repeat_amount, } } @@ -241,7 +241,7 @@ impl Worker { } fn send_packet(&self, packet: &Packet) -> Result<(), Box> { - for _ in 0..self.duplicate_packets { + for _ in 0..self.repeat_amount { self.socket.send(packet)?; }