diff options
| author | ache <ache@ache.one> | 2026-08-04 09:43:58 +0200 |
|---|---|---|
| committer | ache <ache@ache.one> | 2026-08-04 09:43:58 +0200 |
| commit | aecb7ac234ddb38772f3a968c3fa43ec3b507315 (patch) | |
| tree | 8752022ffab608719305103b53209910801f20b4 /src/pinger.rs | |
Init commit
Diffstat (limited to 'src/pinger.rs')
| -rw-r--r-- | src/pinger.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/pinger.rs b/src/pinger.rs new file mode 100644 index 0000000..ef87cbf --- /dev/null +++ b/src/pinger.rs @@ -0,0 +1,35 @@ +use std::net::IpAddr; +use std::time::Duration; +use tokio::sync::mpsc::Sender; +use tokio::task; + +pub enum PingResult { + NoPingErr { id: u32, error: String }, + Ping { rrt: u64, ttl: u32, id: u32 }, +} + +pub fn spawn_ping(target: IpAddr, tx: Sender<PingResult>, id: u32) { + // let _ = tx.try_send(PingResult::NoPingErr { id, error: "Test Ping".to_string() }); + 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 { + rrt: reply.rtt.as_millis() as u64, + ttl: reply.ttl.unwrap_or(0) as u32, + id, + }, + Err(e) => PingResult::NoPingErr { id, error: format!("Ping failed: {}", e) }, + } +} |