generated from PlexSheep/rs-base
Merge branch 'devel'
cargo devel CI / cargo CI (push) Successful in 2m7s
Details
cargo devel CI / cargo CI (push) Successful in 2m7s
Details
This commit is contained in:
commit
52ac674e50
|
@ -1,7 +1,7 @@
|
||||||
workspace = { members = ["spammer"] }
|
workspace = { members = ["spammer"] }
|
||||||
[package]
|
[package]
|
||||||
name = "netpong"
|
name = "netpong"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = true
|
publish = true
|
||||||
authors = ["Christoph J. Scherr <software@cscherr.de>"]
|
authors = ["Christoph J. Scherr <software@cscherr.de>"]
|
||||||
|
|
|
@ -54,6 +54,10 @@ 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 {
|
||||||
|
|
|
@ -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 = 500; // ms
|
const DEFAULT_DELAY_LEN: u64 = 200; // ms
|
||||||
const DEFAULT_WIN_AFTER: usize = 20;
|
const DEFAULT_WIN_AFTER: usize = 0xff;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -24,6 +24,8 @@ 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 {
|
||||||
|
@ -56,6 +58,7 @@ 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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ 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};
|
||||||
|
@ -158,14 +159,16 @@ impl Server {
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut buf = [0; BUF_SIZE];
|
let mut buf = [0; BUF_SIZE];
|
||||||
let mut pings = 0;
|
let mut pings: usize = 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 == format!("ping") {
|
if request == "ping" {
|
||||||
pings += 1;
|
pings = pings.saturating_add(1);
|
||||||
if pings > self.cfg.win_after {
|
if (pings > self.cfg.win_after && !self.cfg.indefinitely)
|
||||||
|
|| (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);
|
||||||
|
@ -173,8 +176,9 @@ impl Server {
|
||||||
stream.shutdown().await?;
|
stream.shutdown().await?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
stream.write_all(b"pong").await?;
|
let response = format!("pong ({:x})", pings);
|
||||||
debug!(pings, "> ({})\n\"{}\"", addr, "pong");
|
stream.write_all(response.as_bytes()).await?;
|
||||||
|
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?");
|
||||||
|
|
Loading…
Reference in New Issue