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!'
This commit is contained in:
Shane Handley 2019-10-22 12:46:12 +09:00 committed by timokoesters
parent b15c107f0b
commit 15c725bfde
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
4 changed files with 25 additions and 11 deletions

View File

@ -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();

View File

@ -502,17 +502,21 @@ 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() {
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(
SERVER_TIMEOUT.as_secs() - duration_since_last_pong.as_secs(),
seconds_until_kick.as_secs(),
));
}
}
}
let new_msgs = self.postbox.new_messages();

View File

@ -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

View File

@ -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,
});
}
}