From 0a37691e051edc2819dc9cc94ca9812e5a409087 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 24 Jan 2024 16:12:31 +0100 Subject: [PATCH] i played a round of netpong with tls --- src/client/mod.rs | 6 +++--- src/common/mod.rs | 8 ++++---- src/server/mod.rs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 8ac16df..4a4db17 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -62,12 +62,12 @@ 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!("< {:?}\n{}", self.cfg.hostname, response); + info!("< {} \"{}\"", self.cfg.hostname, response); 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/common/mod.rs b/src/common/mod.rs index 9ed27e4..f243fa8 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -5,8 +5,8 @@ pub mod conf; #[inline] pub fn decode(buf: &[u8]) -> Result { - match std::str::from_utf8(buf) { - Ok(s) => Ok(s.to_string()), - Err(err) => Err(err.into()), - } + Ok(match std::str::from_utf8(buf) { + Ok(s) => s.to_string(), + Err(err) => return Err(err.into()), + }.trim_matches(char::from(0)).to_string()) } diff --git a/src/server/mod.rs b/src/server/mod.rs index f7e07b3..c747388 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -162,8 +162,8 @@ impl Server { debug!("new peer: {:?}", addr); while stream.read(&mut buf).await? > 0 { let request = decode(&buf)?; - debug!("< {:?}\n{}", addr, request); - if request == "ping" { + debug!(pings, "< {:?}\n\"{:X?}\"", addr, request); + if request == format!("ping") { pings += 1; if pings > self.cfg.win_after { stream.write_all(b"You win!").await?;