Skip to content

Commit

Permalink
fix: fix while to loop with match cause of pcap-rs's recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
SkuldNorniern committed Mar 13, 2024
1 parent 9deecb1 commit e4d7c3d
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/net/packet_pcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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());
Expand Down

0 comments on commit e4d7c3d

Please sign in to comment.