From 9677f8ae9ca4ebd2b91ef27ad986486dfe59da7f Mon Sep 17 00:00:00 2001 From: ice_iix Date: Sat, 29 Sep 2018 22:57:55 -0700 Subject: [PATCH] Use std::time for server ping, getting closer to eliminating use of 'time' crate, https://github.com/iceiix/steven/issues/3 --- src/protocol/mod.rs | 8 ++++---- src/screen/server_list.rs | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index ba6da37..4ada321 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -31,7 +31,7 @@ use std::convert; use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt}; use flate2::read::{ZlibDecoder, ZlibEncoder}; use flate2; -use time; +use std::time::{Instant, Duration}; use shared::Position; pub const SUPPORTED_PROTOCOL: i32 = 315; @@ -866,7 +866,7 @@ impl Conn { self.compression_threshold = threshold; } - pub fn do_status(mut self) -> Result<(Status, time::Duration), Error> { + pub fn do_status(mut self) -> Result<(Status, Duration), Error> { use serde_json::Value; use self::packet::status::serverbound::*; use self::packet::handshake::serverbound::Handshake; @@ -889,7 +889,7 @@ impl Conn { return Err(Error::Err("Wrong packet".to_owned())); }; - let start = time::now(); + let start = Instant::now(); try!(self.write_packet(StatusPing { ping: 42 })); if let Packet::StatusPong(_) = try!(self.read_packet()) { @@ -897,7 +897,7 @@ impl Conn { return Err(Error::Err("Wrong packet".to_owned())); }; - let ping = time::now() - start; + let ping = start.elapsed(); let val: Value = match serde_json::from_str(&status) { Ok(val) => val, diff --git a/src/screen/server_list.rs b/src/screen/server_list.rs index 9c073d2..5a7579c 100644 --- a/src/screen/server_list.rs +++ b/src/screen/server_list.rs @@ -25,7 +25,7 @@ use format::{Component, TextComponent}; use protocol; use serde_json; -use time; +use std::time::{Duration}; use image; use rustc_serialize::base64::FromBase64; use rand; @@ -69,7 +69,7 @@ struct Server { struct PingInfo { motd: format::Component, - ping: time::Duration, + ping: Duration, exists: bool, online: i32, max: i32, @@ -288,7 +288,7 @@ impl ServerList { msg.modifier.color = Some(format::Color::Red); let _ = send.send(PingInfo { motd: Component::Text(msg), - ping: time::Duration::seconds(99999), + ping: Duration::new(99999, 0), exists: false, online: 0, max: 0, @@ -465,7 +465,9 @@ impl super::Screen for ServerList { s.done_ping = true; s.motd.borrow_mut().set_text(res.motd); // Selects the icon for the given ping range - let y = match res.ping.num_milliseconds() { + // TODO: switch to as_millis() experimental duration_as_u128 #50202 once available? + let ping_ms = (res.ping.subsec_nanos() as f64)/1000000.0 + (res.ping.as_secs() as f64)*1000.0; + let y = match ping_ms.round() as u64 { _x @ 0 ... 75 => 16.0 / 256.0, _x @ 76 ... 150 => 24.0 / 256.0, _x @ 151 ... 225 => 32.0 / 256.0,