From 3fee4aaf1861d6e2d45d49d0d2c06fc759244545 Mon Sep 17 00:00:00 2001 From: Garen Tyler Date: Fri, 19 Mar 2021 23:03:29 -0600 Subject: [PATCH] Record the number of packets sent and received, log on shutdown --- src/lib.rs | 3 +++ src/main.rs | 7 +++---- src/server/mod.rs | 33 ++++++++++++++++++++++++++++++--- src/server/net/mod.rs | 6 ++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 049c68f..71329bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,10 +58,13 @@ lazy_static! { file.read_to_end(&mut data)?; Ok(data) }; + pub static ref START_TIME: std::time::Instant = std::time::Instant::now(); } /// Set up logging, read the config file, etc. pub fn init() -> Receiver<()> { + // Load the START_TIME static - lazy_static lazy loads the value when first needed. + let _ = START_TIME.elapsed(); // Set up fern logging. fern::Dispatch::new() .format(move |out, message, record| { diff --git a/src/main.rs b/src/main.rs index bbb31f3..ef9b371 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ use log::info; use std::sync::mpsc::TryRecvError; -use std::time::{Duration, Instant}; +use std::time::Duration; #[tokio::main] pub async fn main() { - let start_time = Instant::now(); let ctrlc_rx = composition::init(); info!("Starting server..."); let mut server = composition::start_server().await; - info!("Done! Start took {:?}", start_time.elapsed()); + info!("Done! Start took {:?}", composition::START_TIME.elapsed()); // The main server loop. loop { @@ -17,7 +16,7 @@ pub async fn main() { server.shutdown().await; break; // Exit the loop. } - Err(TryRecvError::Empty) => {} // Doesn't matter if there's nothing for us + Err(TryRecvError::Empty) => {} // Doesn't matter if there's nothing for us. Err(TryRecvError::Disconnected) => panic!("Ctrl-C sender disconnected"), } server.update().await.unwrap(); diff --git a/src/server/mod.rs b/src/server/mod.rs index 22ee6ab..17d3b23 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -12,6 +12,8 @@ pub struct Server { network_clients: Vec, network_receiver: Receiver, pub players: Vec, + pub packets_read: usize, + pub packets_sent: usize, } impl Server { pub fn new(addr: A) -> Server { @@ -36,6 +38,8 @@ impl Server { network_receiver: rx, network_clients: vec![], players: vec![], + packets_read: 0, + packets_sent: 0, } } @@ -45,9 +49,18 @@ impl Server { pub async fn shutdown(&mut self) { info!("Server shutting down."); for client in self.network_clients.iter_mut() { - let _ = client.disconnect(Some("The server is shutting down")).await; // We don't care if it doesn't succeed in sending the packet. + let _ = client.disconnect(Some("The server is shutting down")).await; + // Count the number of packets from the remaining clients. + self.packets_read += client.packets_read; + self.packets_sent += client.packets_sent; } + info!( + "{} packets read and {} packets sent in {:?}", + self.packets_read, + self.packets_sent, + crate::START_TIME.elapsed() + ); } /// Update the network server. @@ -80,8 +93,22 @@ impl Server { } } // Remove disconnected clients. - self.network_clients - .retain(|nc| nc.state != NetworkClientState::Disconnected); + let mut index = 0; + loop { + if index >= self.network_clients.len() { + break; + } + if self.network_clients[index].state == NetworkClientState::Disconnected { + // Count the number of packets before removing it. + self.packets_read += self.network_clients[index].packets_read; + self.packets_sent += self.network_clients[index].packets_sent; + self.network_clients.remove(index); + continue; + } + index += 1; + } + // self.network_clients + // .retain(|nc| nc.state != NetworkClientState::Disconnected); Ok(()) } diff --git a/src/server/net/mod.rs b/src/server/net/mod.rs index 9fe8a37..53d3bea 100644 --- a/src/server/net/mod.rs +++ b/src/server/net/mod.rs @@ -30,6 +30,8 @@ pub struct NetworkClient { pub uuid: Option, pub username: Option, pub last_keep_alive: Instant, + pub packets_read: usize, + pub packets_sent: usize, } impl NetworkClient { /// Create a new `NetworkClient` @@ -42,6 +44,8 @@ impl NetworkClient { uuid: None, username: None, last_keep_alive: Instant::now(), + packets_read: 0, + packets_sent: 0, } } @@ -204,12 +208,14 @@ impl NetworkClient { /// Send a generic packet to the client. pub async fn send_packet(&mut self, packet: P) -> tokio::io::Result<()> { + self.packets_sent += 1; debug!("Sent {:?} {:#04X?} {:?}", self.state, P::id(), packet); packet.write(&mut self.stream).await } /// Read a generic packet from the network. pub async fn get_packet(&mut self) -> tokio::io::Result { + self.packets_read += 1; let packet = T::read(&mut self.stream).await?; debug!("Got {:?} {:#04X?} {:?}", self.state, T::id(), packet); Ok(packet)