it works
cargo devel CI / cargo CI (push) Successful in 3m3s Details

This commit is contained in:
Christoph J. Scherr 2024-01-24 16:29:15 +01:00
parent 81b54c66f2
commit d18a4c1e0b
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
3 changed files with 12 additions and 5 deletions

View File

@ -1,7 +1,7 @@
workspace = { members = ["spammer"] } workspace = { members = ["spammer"] }
[package] [package]
name = "netpong" name = "netpong"
version = "0.1.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

@ -62,12 +62,15 @@ impl Client {
let mut stream = self.connector.connect(self.domain, self.stream).await?; let mut stream = self.connector.connect(self.domain, self.stream).await?;
let mut buf = [0; BUF_SIZE]; let mut buf = [0; BUF_SIZE];
stream.write_all(b"ping").await?; stream.write_all(b"ping").await?;
info!("> {} ping", self.cfg.hostname); info!("> ({}) ping", self.cfg.hostname);
while stream.read(&mut buf).await? > 0 { while stream.read(&mut buf).await? > 0 {
let response = decode(&buf)?; let response = decode(&buf)?;
info!("< {} \"{}\"", self.cfg.hostname, response); info!("< ({}) {}", self.cfg.hostname, response);
if response == "You win!" {
break;
}
stream.write_all(b"ping").await?; stream.write_all(b"ping").await?;
info!("> {} ping", self.cfg.hostname); info!("> ({}) ping", self.cfg.hostname);
// we should wait, so that we don't spam the client // we should wait, so that we don't spam the client
std::thread::sleep(self.cfg.delay); std::thread::sleep(self.cfg.delay);
} }

View File

@ -162,18 +162,22 @@ impl Server {
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\"{:X?}\"", addr, request); debug!(pings, "< ({})\n\"{}\"", addr, request);
if request == format!("ping") { if request == format!("ping") {
pings += 1; pings += 1;
if pings > self.cfg.win_after { if pings > self.cfg.win_after {
stream.write_all(b"You win!").await?; stream.write_all(b"You win!").await?;
debug!(pings, "> ({})\n\"{}\"", addr, "You win!");
info!("{} won!", addr);
stream.flush().await?; stream.flush().await?;
stream.shutdown().await?; stream.shutdown().await?;
break; break;
} }
stream.write_all(b"pong").await?; stream.write_all(b"pong").await?;
debug!(pings, "> ({})\n\"{}\"", addr, "pong");
} 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?");
stream.flush().await?; stream.flush().await?;
} }
// we should wait, so that we don't spam the client // we should wait, so that we don't spam the client