pub struct ChannelExtcapControlReader {
pub join_handle: JoinHandle<Result<(), ControlChannelError>>,
pub read_channel: Receiver<ControlPacket<'static>>,
}
Expand description
A reader for an Extcap Control using a Channel
. This is
the easier to use, but higher overhead way to read control packets. When the
reader is spawned, a thread is spawned to continuously read messages and
writes them into a bounded channel
. This allows reading the control
messages without worrying about spawning async tasks, by calling
try_read_packet
every once in a while.
Assuming the extcap capture
implementation uses a loop to read or generate
the packets, it can repeatedly call try_read_packet
to read and handle the
control packets until there are no more buffered messages before starting
the main capturing logic.
For example:
fn capture(reader: &ChannelExtcapControlReader) -> Result<()> {
let pcap_header = ...;
let mut pcap_writer = PcapWriter::with_header(fifo, pcap_header)?;
loop {
while let Some(packet) = reader.try_read_packet().await {
// Handle the control packet
}
pcap_writer.write_packet(...)?;
}
Ok(())
}
Fields§
§join_handle: JoinHandle<Result<(), ControlChannelError>>
The join handle for the spawned thread. In most cases there is no need to use this, as the control fifo is expected to run for the whole duration of the capture.
read_channel: Receiver<ControlPacket<'static>>
The channel to receive control packets from.
Implementations§
Source§impl ChannelExtcapControlReader
impl ChannelExtcapControlReader
Sourcepub fn spawn(in_path: PathBuf) -> Self
pub fn spawn(in_path: PathBuf) -> Self
Create a ChannelExtcapControlReader
and spawns the underlying thread
it uses to start reading the control packets from the pipe given in
in_path
.
Sourcepub async fn try_read_packet(&mut self) -> Option<ControlPacket<'static>>
pub async fn try_read_packet(&mut self) -> Option<ControlPacket<'static>>
Try to read a buffered control packet, or return None
if there are no
incoming control packets.
Sourcepub async fn read_packet(&mut self) -> Option<ControlPacket<'static>>
pub async fn read_packet(&mut self) -> Option<ControlPacket<'static>>
Reads a control packet. If the incoming channel is empty, this will
block and wait until an incoming packet comes in. This is typically used
when the extcap capture starts to wait for the Initialized
packet from
the control channel.
If you are only using this method and not using try_read_packet
,
consider whether you can use ExtcapControlReader
directly for lower
overhead.