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)?;
|
file.read_to_end(&mut data)?;
|
||||||
Ok(data)
|
Ok(data)
|
||||||
};
|
};
|
||||||
|
pub static ref START_TIME: std::time::Instant = std::time::Instant::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set up logging, read the config file, etc.
|
/// Set up logging, read the config file, etc.
|
||||||
pub fn init() -> Receiver<()> {
|
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.
|
// Set up fern logging.
|
||||||
fern::Dispatch::new()
|
fern::Dispatch::new()
|
||||||
.format(move |out, message, record| {
|
.format(move |out, message, record| {
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
use log::info;
|
use log::info;
|
||||||
use std::sync::mpsc::TryRecvError;
|
use std::sync::mpsc::TryRecvError;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::Duration;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() {
|
pub async fn main() {
|
||||||
let start_time = Instant::now();
|
|
||||||
let ctrlc_rx = composition::init();
|
let ctrlc_rx = composition::init();
|
||||||
info!("Starting server...");
|
info!("Starting server...");
|
||||||
let mut server = composition::start_server().await;
|
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.
|
// The main server loop.
|
||||||
loop {
|
loop {
|
||||||
@ -17,7 +16,7 @@ pub async fn main() {
|
|||||||
server.shutdown().await;
|
server.shutdown().await;
|
||||||
break; // Exit the loop.
|
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"),
|
Err(TryRecvError::Disconnected) => panic!("Ctrl-C sender disconnected"),
|
||||||
}
|
}
|
||||||
server.update().await.unwrap();
|
server.update().await.unwrap();
|
||||||
|
@ -12,6 +12,8 @@ pub struct Server {
|
|||||||
network_clients: Vec<NetworkClient>,
|
network_clients: Vec<NetworkClient>,
|
||||||
network_receiver: Receiver<NetworkClient>,
|
network_receiver: Receiver<NetworkClient>,
|
||||||
pub players: Vec<Player>,
|
pub players: Vec<Player>,
|
||||||
|
pub packets_read: usize,
|
||||||
|
pub packets_sent: usize,
|
||||||
}
|
}
|
||||||
impl Server {
|
impl Server {
|
||||||
pub fn new<A: 'static + ToSocketAddrs + Send>(addr: A) -> Server {
|
pub fn new<A: 'static + ToSocketAddrs + Send>(addr: A) -> Server {
|
||||||
@ -36,6 +38,8 @@ impl Server {
|
|||||||
network_receiver: rx,
|
network_receiver: rx,
|
||||||
network_clients: vec![],
|
network_clients: vec![],
|
||||||
players: vec![],
|
players: vec![],
|
||||||
|
packets_read: 0,
|
||||||
|
packets_sent: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +49,18 @@ impl Server {
|
|||||||
pub async fn shutdown(&mut self) {
|
pub async fn shutdown(&mut self) {
|
||||||
info!("Server shutting down.");
|
info!("Server shutting down.");
|
||||||
for client in self.network_clients.iter_mut() {
|
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.
|
// 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.
|
/// Update the network server.
|
||||||
@ -80,8 +93,22 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Remove disconnected clients.
|
// Remove disconnected clients.
|
||||||
self.network_clients
|
let mut index = 0;
|
||||||
.retain(|nc| nc.state != NetworkClientState::Disconnected);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,8 @@ pub struct NetworkClient {
|
|||||||
pub uuid: Option<String>,
|
pub uuid: Option<String>,
|
||||||
pub username: Option<String>,
|
pub username: Option<String>,
|
||||||
pub last_keep_alive: Instant,
|
pub last_keep_alive: Instant,
|
||||||
|
pub packets_read: usize,
|
||||||
|
pub packets_sent: usize,
|
||||||
}
|
}
|
||||||
impl NetworkClient {
|
impl NetworkClient {
|
||||||
/// Create a new `NetworkClient`
|
/// Create a new `NetworkClient`
|
||||||
@ -42,6 +44,8 @@ impl NetworkClient {
|
|||||||
uuid: None,
|
uuid: None,
|
||||||
username: None,
|
username: None,
|
||||||
last_keep_alive: Instant::now(),
|
last_keep_alive: Instant::now(),
|
||||||
|
packets_read: 0,
|
||||||
|
packets_sent: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,12 +208,14 @@ impl NetworkClient {
|
|||||||
|
|
||||||
/// Send a generic packet to the client.
|
/// Send a generic packet to the client.
|
||||||
pub async fn send_packet<P: PacketCommon>(&mut self, packet: P) -> tokio::io::Result<()> {
|
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);
|
debug!("Sent {:?} {:#04X?} {:?}", self.state, P::id(), packet);
|
||||||
packet.write(&mut self.stream).await
|
packet.write(&mut self.stream).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read a generic packet from the network.
|
/// Read a generic packet from the network.
|
||||||
pub async fn get_packet<T: PacketCommon>(&mut self) -> tokio::io::Result<T> {
|
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?;
|
let packet = T::read(&mut self.stream).await?;
|
||||||
debug!("Got {:?} {:#04X?} {:?}", self.state, T::id(), packet);
|
debug!("Got {:?} {:#04X?} {:?}", self.state, T::id(), packet);
|
||||||
Ok(packet)
|
Ok(packet)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user