From 246131857606eee1ee63742d5e21883106279699 Mon Sep 17 00:00:00 2001 From: Shane Handley Date: Tue, 22 Oct 2019 12:46:12 +0900 Subject: [PATCH] Address code review points: - Clarify caffeine fueled comment - Be better at comparing Instant's, and catch the 0 seconds case to say Goodbye to the user - Switch println for 'info!' --- chat-cli/src/main.rs | 13 +++++++++---- client/src/lib.rs | 14 +++++++++----- server-cli/src/main.rs | 2 +- voxygen/src/session.rs | 7 ++++++- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index f8caff4de7..2f288c6500 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -77,12 +77,17 @@ fn main() { match event { Event::Chat { message, .. } => println!("{}", message), Event::Disconnect => {} // TODO - Event::DisconnectionNotification(time) => println!( - "{}", - format!("Connection lost. Kicking in {} seconds", time) - ), + Event::DisconnectionNotification(time) => { + let message = match time { + 0 => String::from("Goodbye!"), + _ => format!("Connection lost. Kicking in {} seconds", time), + }; + + println!("{}", message) + } } } + // Clean up the server after a tick. client.cleanup(); diff --git a/client/src/lib.rs b/client/src/lib.rs index b3c49ebfb5..460c19e170 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -502,15 +502,19 @@ impl Client { let mut frontend_events = Vec::new(); // Check that we have an valid connection. - // Limit this to the ping time (1s), since that's the max resolution we have + // Use the last ping time as a 1s rate limiter, we only notify the user once per second if Instant::now().duration_since(self.last_server_ping) > Duration::from_secs(1) { let duration_since_last_pong = Instant::now().duration_since(self.last_server_pong); // Dispatch a notification to the HUD warning they will be kicked in {n} seconds - if duration_since_last_pong.as_secs_f64() > SERVER_TIMEOUT_GRACE_PERIOD.as_secs_f64() { - frontend_events.push(Event::DisconnectionNotification( - SERVER_TIMEOUT.as_secs() - duration_since_last_pong.as_secs(), - )); + if duration_since_last_pong.as_secs() >= SERVER_TIMEOUT_GRACE_PERIOD.as_secs() { + if let Some(seconds_until_kick) = + SERVER_TIMEOUT.checked_sub(duration_since_last_pong) + { + frontend_events.push(Event::DisconnectionNotification( + seconds_until_kick.as_secs(), + )); + } } } diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index b7a26a4b2c..30f6b2f202 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -22,7 +22,7 @@ fn main() { // Create server let mut server = Server::new(settings).expect("Failed to create server instance!"); - println!("Server is ready to accept connections"); + info!("Server is ready to accept connections"); loop { let events = server diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 9df3824b23..2dc73aad92 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -64,9 +64,14 @@ impl SessionState { } client::Event::Disconnect => {} // TODO client::Event::DisconnectionNotification(time) => { + let message = match time { + 0 => String::from("Goodbye!"), + _ => format!("Connection lost. Kicking in {} seconds", time), + }; + self.hud.new_message(Chat { chat_type: ChatType::Meta, - message: format!("Connection lost. Kicking in {} seconds", time), + message, }); } }