use std::time::{Duration, Instant};
#[derive(Debug, Default)]
pub struct NetStat {
session_download_length: u64,
session_upload_length: u64,
download_speed: u64,
upload_speed: u64,
download_start_time: Option<Instant>,
download_stop_time: Option<Instant>,
}
impl NetStat {
pub fn download_start(&mut self) {
self.download_start_time = Some(Instant::now());
}
pub fn download_stop(&mut self) {
self.download_stop_time = Some(Instant::now());
}
pub fn update_download(&mut self, bytes: u64) {
self.session_download_length += bytes;
}
pub fn update_upload_length(&mut self, bytes: u64) {
self.session_upload_length += bytes;
}
pub fn update_upload_speed(&mut self, bytes: u64) {
self.upload_speed = bytes;
}
pub fn session_download_length(&self) -> u64 {
self.session_download_length
}
pub fn session_upload_length(&self) -> u64 {
self.session_upload_length
}
pub fn download_speed(&self) -> u64 {
self.download_speed
}
pub fn set_download_speed(&mut self, speed: u64) {
self.download_speed = speed;
}
pub fn upload_speed(&self) -> u64 {
self.upload_speed
}
pub fn download_start_time(&self) -> Option<Instant> {
self.download_start_time
}
pub fn download_stop_time(&self) -> Option<Instant> {
self.download_stop_time
}
pub fn calculate_session_time(&self) -> Duration {
match (self.download_start_time, self.download_stop_time) {
(Some(start), Some(stop)) => stop.duration_since(start),
_ => Duration::ZERO,
}
}
}