use std::net::IpAddr; use std::time::Duration; use tokio::sync::mpsc::Sender; use tokio::task; pub enum PingResult { NoPingErr { id: u32 }, Ping { rtt: u64, id: u32 }, } pub fn spawn_ping(target: IpAddr, tx: Sender, id: u32) { task::spawn_blocking(move || { let result = do_ping(target, id); let _ = tx.try_send(result); }); } fn do_ping(target: IpAddr, id: u32) -> PingResult { let payload: [u8; 24] = [0u8; 24]; let result = ping::new(target) .timeout(Duration::from_secs(3)) .payload(&payload) .send(); match result { Ok(reply) => PingResult::Ping { rtt: reply.rtt.as_millis() as u64, id, }, Err(_) => PingResult::NoPingErr { id }, } }