Fix --wait timeout duration logic and iterator borrows
This commit is contained in:
1 parent
4c8cfaf7c9
commit
de3eda9200
1 file changed
+18
-3
+18
-3
@@ -9,13 +9,25 @@ fn main() {
|
|||||||
let mut port_str = String::new();
|
let mut port_str = String::new();
|
||||||
let mut payload = String::new();
|
let mut payload = String::new();
|
||||||
let mut positional_args = 0;
|
let mut positional_args = 0;
|
||||||
|
let mut timeout_secs = 30; // default 30s timeout
|
||||||
|
|
||||||
// 1. Iterate args safely, avoiding heavy Vec allocations
|
// 1. Iterate args safely, avoiding heavy Vec allocations
|
||||||
for arg in env::args().skip(1) {
|
let args: Vec<String> = env::args().skip(1).collect();
|
||||||
|
let mut args_iter = args.into_iter().peekable();
|
||||||
|
|
||||||
|
while let Some(arg) = args_iter.next() {
|
||||||
if arg == "--debug" {
|
if arg == "--debug" {
|
||||||
debug = true;
|
debug = true;
|
||||||
} else if arg == "--wait" {
|
} else if arg == "--wait" {
|
||||||
wait = true;
|
wait = true;
|
||||||
|
// Parse optional timeout if present, otherwise default to 30s
|
||||||
|
// Peek at next arg to see if it's a number
|
||||||
|
if let Some(next_arg) = args_iter.peek() {
|
||||||
|
if let Ok(secs) = next_arg.parse::<u64>() {
|
||||||
|
timeout_secs = secs;
|
||||||
|
args_iter.next(); // Consume the timeout arg
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if positional_args == 0 {
|
} else if positional_args == 0 {
|
||||||
port_str = arg;
|
port_str = arg;
|
||||||
positional_args += 1;
|
positional_args += 1;
|
||||||
@@ -61,8 +73,11 @@ fn main() {
|
|||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
} else if wait {
|
} else if wait {
|
||||||
// Set a timeout of 10 minutes for waiting
|
// Set a dynamic timeout for waiting
|
||||||
let _ = socket.set_read_timeout(Some(Duration::from_secs(600)));
|
if timeout_secs == 0 {
|
||||||
|
if debug { eprintln!("cannot set a 0 duration timeout"); process::exit(1); } else { process::exit(1); }
|
||||||
|
}
|
||||||
|
let _ = socket.set_read_timeout(Some(Duration::from_secs(timeout_secs)));
|
||||||
let mut buf = [0; 1024];
|
let mut buf = [0; 1024];
|
||||||
match socket.recv_from(&mut buf) {
|
match socket.recv_from(&mut buf) {
|
||||||
Ok((size, _src)) => {
|
Ok((size, _src)) => {
|
||||||
|
|||||||
Reference in new issue
Block a user