Skip to content

Commit 97bc15e

Browse files
authored
Make blocking configurable, defaulting to non-blocking (#23)
1 parent a4ce26b commit 97bc15e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct Config {
77
buffer_size: u32,
88
bpf: Option<String>,
99
buffer_for: std::time::Duration,
10+
blocking: bool,
1011
}
1112

1213
impl Config {
@@ -55,19 +56,30 @@ impl Config {
5556
self
5657
}
5758

59+
pub fn blocking(&self) -> bool {
60+
self.blocking
61+
}
62+
63+
pub fn with_blocking(&mut self, blocking: bool) -> &mut Self {
64+
self.blocking = blocking;
65+
self
66+
}
67+
5868
pub fn new(
5969
max_packets_read: usize,
6070
snaplen: u32,
6171
buffer_size: u32,
6272
bpf: Option<String>,
6373
buffer_for: std::time::Duration,
74+
blocking: bool,
6475
) -> Config {
6576
Config {
6677
max_packets_read,
6778
snaplen,
6879
buffer_size,
6980
bpf,
7081
buffer_for,
82+
blocking,
7183
}
7284
}
7385
}
@@ -80,6 +92,7 @@ impl Default for Config {
8092
buffer_size: 16777216,
8193
bpf: None,
8294
buffer_for: std::time::Duration::from_millis(100),
95+
blocking: false,
8396
}
8497
}
8598
}

src/stream.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ impl PacketStream {
3333
.set_promiscuous()?
3434
.set_buffer_size(config.buffer_size())?
3535
.activate()?;
36-
h.set_non_block()?;
36+
if !config.blocking() {
37+
h.set_non_block()?;
38+
}
3739

3840
if let Some(bpf) = config.bpf() {
3941
let bpf = handle.compile_bpf(bpf)?;

0 commit comments

Comments
 (0)