Record the number of packets sent and received, log on shutdown
This commit is contained in:
parent
6841e785d9
commit
3fee4aaf18
@ -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| {
|
||||
|
@ -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();
|
||||
|
@ -12,6 +12,8 @@ pub struct Server {
|
||||
network_clients: Vec<NetworkClient>,
|
||||
network_receiver: Receiver<NetworkClient>,
|
||||
pub players: Vec<Player>,
|
||||
pub packets_read: usize,
|
||||
pub packets_sent: usize,
|
||||
}
|
||||
impl Server {
|
||||
pub fn new<A: 'static + ToSocketAddrs + Send>(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(())
|
||||
}
|
||||
|
@ -30,6 +30,8 @@ pub struct NetworkClient {
|
||||
pub uuid: Option<String>,
|
||||
pub username: Option<String>,
|
||||
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<P: PacketCommon>(&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<T: PacketCommon>(&mut self) -> tokio::io::Result<T> {
|
||||
self.packets_read += 1;
|
||||
let packet = T::read(&mut self.stream).await?;
|
||||
debug!("Got {:?} {:#04X?} {:?}", self.state, T::id(), packet);
|
||||
Ok(packet)
|
||||
|
Loading…
x
Reference in New Issue
Block a user