Use std::time for server ping, getting closer to eliminating use of 'time' crate, https://github.com/iceiix/steven/issues/3

This commit is contained in:
ice_iix 2018-09-29 22:57:55 -07:00
parent 2f861f815a
commit 9677f8ae9c
2 changed files with 10 additions and 8 deletions

View File

@ -31,7 +31,7 @@ use std::convert;
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt}; use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
use flate2::read::{ZlibDecoder, ZlibEncoder}; use flate2::read::{ZlibDecoder, ZlibEncoder};
use flate2; use flate2;
use time; use std::time::{Instant, Duration};
use shared::Position; use shared::Position;
pub const SUPPORTED_PROTOCOL: i32 = 315; pub const SUPPORTED_PROTOCOL: i32 = 315;
@ -866,7 +866,7 @@ impl Conn {
self.compression_threshold = threshold; 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 serde_json::Value;
use self::packet::status::serverbound::*; use self::packet::status::serverbound::*;
use self::packet::handshake::serverbound::Handshake; use self::packet::handshake::serverbound::Handshake;
@ -889,7 +889,7 @@ impl Conn {
return Err(Error::Err("Wrong packet".to_owned())); return Err(Error::Err("Wrong packet".to_owned()));
}; };
let start = time::now(); let start = Instant::now();
try!(self.write_packet(StatusPing { ping: 42 })); try!(self.write_packet(StatusPing { ping: 42 }));
if let Packet::StatusPong(_) = try!(self.read_packet()) { if let Packet::StatusPong(_) = try!(self.read_packet()) {
@ -897,7 +897,7 @@ impl Conn {
return Err(Error::Err("Wrong packet".to_owned())); 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) { let val: Value = match serde_json::from_str(&status) {
Ok(val) => val, Ok(val) => val,

View File

@ -25,7 +25,7 @@ use format::{Component, TextComponent};
use protocol; use protocol;
use serde_json; use serde_json;
use time; use std::time::{Duration};
use image; use image;
use rustc_serialize::base64::FromBase64; use rustc_serialize::base64::FromBase64;
use rand; use rand;
@ -69,7 +69,7 @@ struct Server {
struct PingInfo { struct PingInfo {
motd: format::Component, motd: format::Component,
ping: time::Duration, ping: Duration,
exists: bool, exists: bool,
online: i32, online: i32,
max: i32, max: i32,
@ -288,7 +288,7 @@ impl ServerList {
msg.modifier.color = Some(format::Color::Red); msg.modifier.color = Some(format::Color::Red);
let _ = send.send(PingInfo { let _ = send.send(PingInfo {
motd: Component::Text(msg), motd: Component::Text(msg),
ping: time::Duration::seconds(99999), ping: Duration::new(99999, 0),
exists: false, exists: false,
online: 0, online: 0,
max: 0, max: 0,
@ -465,7 +465,9 @@ impl super::Screen for ServerList {
s.done_ping = true; s.done_ping = true;
s.motd.borrow_mut().set_text(res.motd); s.motd.borrow_mut().set_text(res.motd);
// Selects the icon for the given ping range // 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 @ 0 ... 75 => 16.0 / 256.0,
_x @ 76 ... 150 => 24.0 / 256.0, _x @ 76 ... 150 => 24.0 / 256.0,
_x @ 151 ... 225 => 32.0 / 256.0, _x @ 151 ... 225 => 32.0 / 256.0,