Update duplicate packet behavior

This commit is contained in:
Altuğ Bakan 2023-10-08 15:16:28 +02:00
parent e8f8c8e8c1
commit bf7b2a2ee3
3 changed files with 23 additions and 15 deletions

View file

@ -91,15 +91,20 @@ impl Config {
println!(" -d, --directory <DIRECTORY>\tSet the serving directory (default: Current Working Directory)"); println!(" -d, --directory <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!(" -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!(" -r, --read-only\t\tRefuse all write requests, making the server read-only (default: false)");
println!(" --duplicate-packets <NUM>\tDuplicate all packets sent from the server (default: 1)"); println!(" --duplicate-packets <NUM>\tDuplicate all packets sent from the server (default: 0)");
println!(" -h, --help\t\t\tPrint help information"); println!(" -h, --help\t\t\tPrint help information");
process::exit(0); process::exit(0);
} }
"--duplicate-packets" => { "--duplicate-packets" => {
if let Some(duplicate_packets_str) = args.next() { if let Some(duplicate_packets_str) = args.next() {
let duplicate_packets = duplicate_packets_str.parse::<u8>()?; let duplicate_packets = duplicate_packets_str.parse::<u8>()?;
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; config.duplicate_packets = duplicate_packets;
} else { } else {
@ -178,13 +183,6 @@ mod tests {
#[test] #[test]
fn returns_error_on_invalid_duplicate_packets() { fn returns_error_on_invalid_duplicate_packets() {
assert!(Config::new(
["/", "--duplicate-packets", "0"]
.iter()
.map(|s| s.to_string()),
)
.is_err());
assert!(Config::new( assert!(Config::new(
["/", "--duplicate-packets", "-1"] ["/", "--duplicate-packets", "-1"]
.iter() .iter()
@ -192,4 +190,14 @@ mod tests {
) )
.is_err()); .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());
}
} }

View file

@ -179,7 +179,7 @@ impl Server {
worker_options.block_size, worker_options.block_size,
worker_options.timeout, worker_options.timeout,
worker_options.window_size, worker_options.window_size,
self.duplicate_packets, self.duplicate_packets + 1,
); );
worker.send() worker.send()
} }

View file

@ -43,7 +43,7 @@ pub struct Worker<T: Socket + ?Sized> {
blk_size: usize, blk_size: usize,
timeout: Duration, timeout: Duration,
windowsize: u16, windowsize: u16,
duplicate_packets: u8, repeat_amount: u8,
} }
impl<T: Socket + ?Sized> Worker<T> { impl<T: Socket + ?Sized> Worker<T> {
@ -54,7 +54,7 @@ impl<T: Socket + ?Sized> Worker<T> {
blk_size: usize, blk_size: usize,
timeout: Duration, timeout: Duration,
windowsize: u16, windowsize: u16,
duplicate_packets: u8, repeat_amount: u8,
) -> Worker<T> { ) -> Worker<T> {
Worker { Worker {
socket, socket,
@ -62,7 +62,7 @@ impl<T: Socket + ?Sized> Worker<T> {
blk_size, blk_size,
timeout, timeout,
windowsize, windowsize,
duplicate_packets, repeat_amount,
} }
} }
@ -241,7 +241,7 @@ impl<T: Socket + ?Sized> Worker<T> {
} }
fn send_packet(&self, packet: &Packet) -> Result<(), Box<dyn Error>> { fn send_packet(&self, packet: &Packet) -> Result<(), Box<dyn Error>> {
for _ in 0..self.duplicate_packets { for _ in 0..self.repeat_amount {
self.socket.send(packet)?; self.socket.send(packet)?;
} }