#[cfg(feature = "tokio")]
#[tokio::main]
async fn main() -> Result<(), netring::Error> {
use netring::Injector;
use netring::async_adapters::tokio_injector::AsyncInjector;
use std::time::Duration;
let iface = std::env::args().nth(1).unwrap_or_else(|| "lo".into());
let count: usize = std::env::args()
.nth(2)
.and_then(|s| s.parse().ok())
.unwrap_or(1000);
eprintln!("Injecting {count} broadcast frames on {iface} via AsyncInjector...");
let tx = Injector::builder()
.interface(&iface)
.frame_size(2048)
.frame_count(64)
.qdisc_bypass(true)
.build()?;
let mut atx = AsyncInjector::new(tx)?;
let mut sent = 0u64;
let started = std::time::Instant::now();
for i in 0..count {
let mut frame = [0u8; 64];
frame[0..6].copy_from_slice(&[0xff; 6]); frame[6..12].copy_from_slice(&[0; 6]); frame[12..14].copy_from_slice(&0x0800u16.to_be_bytes()); frame[14..22].copy_from_slice(&(i as u64).to_be_bytes());
atx.send(&frame).await?;
sent += 1;
if i % 32 == 0 {
atx.flush().await?;
}
}
atx.flush().await?;
atx.wait_drained(Duration::from_secs(1)).await?;
let elapsed = started.elapsed();
eprintln!(
"Sent {sent} frames in {elapsed:?} ({:.0} pkt/s)",
sent as f64 / elapsed.as_secs_f64()
);
Ok(())
}
#[cfg(not(feature = "tokio"))]
fn main() {
eprintln!(
"This example requires the 'tokio' feature: cargo run --example async_inject --features tokio"
);
}