Compare commits

..

No commits in common. "52ac674e50368098c4dbe47de40e5e1221efe629" and "26521989bedb65a9c14ccf4a25f4c5800a0cfab1" have entirely different histories.

4 changed files with 9 additions and 20 deletions

View file

@ -1,7 +1,7 @@
workspace = { members = ["spammer"] } workspace = { members = ["spammer"] }
[package] [package]
name = "netpong" name = "netpong"
version = "0.2.1" version = "0.2.0"
edition = "2021" edition = "2021"
publish = true publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"] authors = ["Christoph J. Scherr <software@cscherr.de>"]

View file

@ -54,10 +54,6 @@ pub(crate) struct Cli {
pub key: Option<PathBuf>, pub key: Option<PathBuf>,
#[arg(short, long)] #[arg(short, long)]
pub certs: Option<PathBuf>, pub certs: Option<PathBuf>,
#[cfg(feature = "server")]
#[arg(short, long)]
pub indefinitely: bool,
} }
impl Cli { impl Cli {

View file

@ -10,8 +10,8 @@ use anyhow::{anyhow, Result};
use libpt::log::{error, trace}; use libpt::log::{error, trace};
const DEFAULT_TIMEOUT_LEN: u64 = 5000; // ms const DEFAULT_TIMEOUT_LEN: u64 = 5000; // ms
const DEFAULT_DELAY_LEN: u64 = 200; // ms const DEFAULT_DELAY_LEN: u64 = 500; // ms
const DEFAULT_WIN_AFTER: usize = 0xff; const DEFAULT_WIN_AFTER: usize = 20;
#[derive(Clone)] #[derive(Clone)]
pub struct Config { pub struct Config {
@ -24,8 +24,6 @@ pub struct Config {
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub key: Option<PathBuf>, pub key: Option<PathBuf>,
pub certs: Option<PathBuf>, pub certs: Option<PathBuf>,
#[cfg(feature = "server")]
pub indefinitely: bool,
} }
impl Config { impl Config {
@ -58,7 +56,6 @@ impl Config {
#[cfg(feature = "server")] #[cfg(feature = "server")]
key: cli.key.clone(), key: cli.key.clone(),
certs: cli.certs.clone(), certs: cli.certs.clone(),
indefinitely: cli.indefinitely,
}) })
} }
} }

View file

@ -4,7 +4,6 @@ use std::{
net::SocketAddr, net::SocketAddr,
sync::{atomic::AtomicUsize, Arc}, sync::{atomic::AtomicUsize, Arc},
time::Duration, time::Duration,
usize,
}; };
use libpt::log::{debug, error, info, trace, warn}; use libpt::log::{debug, error, info, trace, warn};
@ -159,16 +158,14 @@ impl Server {
addr: SocketAddr, addr: SocketAddr,
) -> Result<()> { ) -> Result<()> {
let mut buf = [0; BUF_SIZE]; let mut buf = [0; BUF_SIZE];
let mut pings: usize = 0; let mut pings = 0;
debug!("new peer: {:?}", addr); debug!("new peer: {:?}", addr);
while stream.read(&mut buf).await? > 0 { while stream.read(&mut buf).await? > 0 {
let request = decode(&buf)?; let request = decode(&buf)?;
debug!(pings, "< ({})\n\"{}\"", addr, request); debug!(pings, "< ({})\n\"{}\"", addr, request);
if request == "ping" { if request == format!("ping") {
pings = pings.saturating_add(1); pings += 1;
if (pings > self.cfg.win_after && !self.cfg.indefinitely) if pings > self.cfg.win_after {
|| (pings > usize::MAX - 1)
{
stream.write_all(b"You win!").await?; stream.write_all(b"You win!").await?;
debug!(pings, "> ({})\n\"{}\"", addr, "You win!"); debug!(pings, "> ({})\n\"{}\"", addr, "You win!");
info!("{} won!", addr); info!("{} won!", addr);
@ -176,9 +173,8 @@ impl Server {
stream.shutdown().await?; stream.shutdown().await?;
break; break;
} }
let response = format!("pong ({:x})", pings); stream.write_all(b"pong").await?;
stream.write_all(response.as_bytes()).await?; debug!(pings, "> ({})\n\"{}\"", addr, "pong");
debug!(pings, "> ({})\n\"{}\"", addr, response);
} else { } else {
stream.write_all(b"what is the magic word?").await?; stream.write_all(b"what is the magic word?").await?;
debug!(pings, "> ({})\n\"{}\"", addr, "what is the magic word?"); debug!(pings, "> ({})\n\"{}\"", addr, "what is the magic word?");