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"] }
[package]
name = "netpong"
version = "0.2.1"
version = "0.2.0"
edition = "2021"
publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"]

View file

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

View file

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

View file

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