From e4d7c3dd1f91d3351d7027ff0ffd364f7d78441b Mon Sep 17 00:00:00 2001 From: SkuldNorniern Date: Tue, 12 Mar 2024 10:40:47 +0900 Subject: [PATCH] fix: fix while to loop with match cause of `pcap-rs`'s recommendation --- src/net/packet_pcap.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/net/packet_pcap.rs b/src/net/packet_pcap.rs index 82138de..0f2776c 100644 --- a/src/net/packet_pcap.rs +++ b/src/net/packet_pcap.rs @@ -20,7 +20,7 @@ pub async fn pcap_capture(args: Args) { let cap = &mut cap_device.capture; let file_dir = "./output"; - let mut packet_count = 0; + // let mut packet_count = 0; match fs::create_dir_all(<&str>::clone(&file_dir)) { Ok(_) => debug!("Created directory: {}", file_dir), Err(error) => panic!("Problem creating directory: {:?}", error), @@ -33,21 +33,29 @@ pub async fn pcap_capture(args: Args) { }; let start = Instant::now(); - while let Ok(packet) = cap.next_packet() { - trace!("received packet"); - //println!("packet: {:?}", packet); - file.write(&packet); - - packet_count += 1; - // slow down the loop for windows to avoid random shutdown - // if packet_count % sleep_windows == 0 && cfg!(target_os = "windows") { - // println!("Slow down the loop for windows"); - // sleep(Duration::from_millis(0)).await; - // } - - // Check if the duration has been reached - if start.elapsed() >= Duration::from_millis(duration) && duration != 0 { - break; + + loop { + match cap.next_packet() { + Err(_) => { + continue; + } + Ok(packet) => { + trace!("received packet"); + //println!("packet: {:?}", packet); + file.write(&packet); + + // packet_count += 1; + // slow down the loop for windows to avoid random shutdown + // if packet_count % sleep_windows == 0 && cfg!(target_os = "windows") { + // println!("Slow down the loop for windows"); + // sleep(Duration::from_millis(0)).await; + // } + + // Check if the duration has been reached + if start.elapsed() >= Duration::from_millis(duration) && duration != 0 { + break; + } + } } } debug!("Captured in {:?}", start.elapsed());