use std::os::fd::AsFd;
use std::time::Duration;
use crate::afpacket::tx::TxSlot;
use crate::error::Error;
use crate::packet::PacketBatch;
use crate::stats::CaptureStats;
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be used as a packet source",
label = "this type does not implement `PacketSource`",
note = "consider using `Capture` or implementing this trait for your backend"
)]
pub trait PacketSource: AsFd {
fn next_batch(&mut self) -> Option<PacketBatch<'_>>;
fn next_batch_blocking(&mut self, timeout: Duration) -> Result<Option<PacketBatch<'_>>, Error>;
fn stats(&self) -> Result<CaptureStats, Error>;
fn cumulative_stats(&self) -> Result<CaptureStats, Error> {
self.stats()
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be used as a packet sink",
label = "this type does not implement `PacketSink`",
note = "consider using `Injector` or implementing this trait for your backend"
)]
pub trait PacketSink: AsFd {
fn allocate(&mut self, len: usize) -> Option<TxSlot<'_>>;
fn flush(&mut self) -> Result<usize, Error>;
}
#[cfg(feature = "tokio")]
pub trait AsyncPacketSource: AsFd {
fn next_batch(
&mut self,
) -> impl std::future::Future<Output = Result<PacketBatch<'_>, Error>> + Send;
}