diff --git a/Cargo.toml b/Cargo.toml index fae3473..cc13494 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ workspace = { members = ["spammer"] } [package] name = "netpong" -version = "0.2.0" +version = "0.2.1" edition = "2021" publish = true authors = ["Christoph J. Scherr "] diff --git a/src/common/args.rs b/src/common/args.rs index 3d161f8..64c8d2b 100644 --- a/src/common/args.rs +++ b/src/common/args.rs @@ -54,6 +54,10 @@ pub(crate) struct Cli { pub key: Option, #[arg(short, long)] pub certs: Option, + + #[cfg(feature = "server")] + #[arg(short, long)] + pub indefinitely: bool, } impl Cli { diff --git a/src/common/conf.rs b/src/common/conf.rs index f80bd52..d39d49a 100644 --- a/src/common/conf.rs +++ b/src/common/conf.rs @@ -24,6 +24,8 @@ pub struct Config { #[cfg(feature = "server")] pub key: Option, pub certs: Option, + #[cfg(feature = "server")] + pub indefinitely: bool, } impl Config { @@ -56,6 +58,7 @@ impl Config { #[cfg(feature = "server")] key: cli.key.clone(), certs: cli.certs.clone(), + indefinitely: cli.indefinitely, }) } } diff --git a/src/server/mod.rs b/src/server/mod.rs index 842ab5a..007a95a 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -4,6 +4,7 @@ use std::{ net::SocketAddr, sync::{atomic::AtomicUsize, Arc}, time::Duration, + usize, }; use libpt::log::{debug, error, info, trace, warn}; @@ -158,14 +159,16 @@ impl Server { addr: SocketAddr, ) -> Result<()> { let mut buf = [0; BUF_SIZE]; - let mut pings = 0; + let mut pings: usize = 0; debug!("new peer: {:?}", addr); while stream.read(&mut buf).await? > 0 { let request = decode(&buf)?; debug!(pings, "< ({})\n\"{}\"", addr, request); - if request == format!("ping") { - pings += 1; - if pings > self.cfg.win_after { + if request == "ping" { + pings = pings.saturating_add(1); + if (pings > self.cfg.win_after && !self.cfg.indefinitely) + || (pings > usize::MAX - 1) + { stream.write_all(b"You win!").await?; debug!(pings, "> ({})\n\"{}\"", addr, "You win!"); info!("{} won!", addr); @@ -173,8 +176,9 @@ impl Server { stream.shutdown().await?; break; } - stream.write_all(b"pong").await?; - debug!(pings, "> ({})\n\"{}\"", addr, "pong"); + let response = format!("pong ({:x})", pings); + stream.write_all(response.as_bytes()).await?; + debug!(pings, "> ({})\n\"{}\"", addr, response); } else { stream.write_all(b"what is the magic word?").await?; debug!(pings, "> ({})\n\"{}\"", addr, "what is the magic word?");