From d18a4c1e0bca0b6ff86b18e224ad4c4e8eb1b8ad Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 24 Jan 2024 16:29:15 +0100 Subject: [PATCH] it works --- Cargo.toml | 2 +- src/client/mod.rs | 9 ++++++--- src/server/mod.rs | 6 +++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c9a1733..fae3473 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ workspace = { members = ["spammer"] } [package] name = "netpong" -version = "0.1.1" +version = "0.2.0" edition = "2021" publish = true authors = ["Christoph J. Scherr "] diff --git a/src/client/mod.rs b/src/client/mod.rs index 4a4db17..10fc9c4 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -62,12 +62,15 @@ impl Client { let mut stream = self.connector.connect(self.domain, self.stream).await?; let mut buf = [0; BUF_SIZE]; stream.write_all(b"ping").await?; - info!("> {} ping", self.cfg.hostname); + info!("> ({}) ping", self.cfg.hostname); while stream.read(&mut buf).await? > 0 { 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?; - info!("> {} ping", self.cfg.hostname); + info!("> ({}) ping", self.cfg.hostname); // we should wait, so that we don't spam the client std::thread::sleep(self.cfg.delay); } diff --git a/src/server/mod.rs b/src/server/mod.rs index c747388..842ab5a 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -162,18 +162,22 @@ impl Server { debug!("new peer: {:?}", addr); while stream.read(&mut buf).await? > 0 { let request = decode(&buf)?; - debug!(pings, "< {:?}\n\"{:X?}\"", addr, request); + debug!(pings, "< ({})\n\"{}\"", addr, request); 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); stream.flush().await?; stream.shutdown().await?; break; } 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?"); stream.flush().await?; } // we should wait, so that we don't spam the client