From 9968bb00683477c18d4dc3f853214716c815fa1e Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Mon, 15 Aug 2022 23:17:44 +0200 Subject: [PATCH] implement proper ping --- Cargo.toml | 2 +- src/main.rs | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a0ecde2..bb84517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ clap = { version = "3.2.17", features = ["derive"] } [dependencies.async-minecraft-ping] git = "https://github.com/LordMZTE/async-minecraft-ping.git" -tag = "v0.3.0" +tag = "v0.4.0" [dependencies.tokio] version = "1.20.1" diff --git a/src/main.rs b/src/main.rs index b779f63..43d3ee9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use async_minecraft_ping::{ConnectionConfig, ServerDescription, StatusResponse}; use clap::Parser; use itertools::Itertools; use miette::{IntoDiagnostic, WrapErr}; -use time::{Duration, Instant}; +use time::Duration; use tokio::time; use mcstat::{ @@ -103,18 +103,13 @@ async fn main() -> miette::Result<()> { // create timeout for server connection let (raw_response, ping) = time::timeout(Duration::from_millis(opt.timeout), async { info!("Connecting to server"); - let start_time = Instant::now(); let mut con = config.connect().await.into_diagnostic()?; - // we end the timer here, because at this point, we've sent ONE request to the - // server, and we don't want to send 2, since then we get double the - // ping. the connect function may have some processing which may take - // some time, but it shouldn't make an impact since this code runs at rust - // speed. - let end_time = Instant::now(); info!("Requesting status"); let status = con.status_raw().await.into_diagnostic()?; - Result::<_, miette::Error>::Ok((status, end_time - start_time)) + let ping = con.ping().await.into_diagnostic()?; + + Result::<_, miette::Error>::Ok((status, ping)) }) .await .into_diagnostic()