From de2c99731f0bbda10106cee3b83cd20f107e8ff3 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Mon, 16 Jun 2025 18:31:11 -0500 Subject: [PATCH] server updates --- client/src/input/keyboard.rs | 2 +- server/src/main.rs | 73 +++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/client/src/input/keyboard.rs b/client/src/input/keyboard.rs index eac0f38..a2dd640 100644 --- a/client/src/input/keyboard.rs +++ b/client/src/input/keyboard.rs @@ -92,7 +92,7 @@ impl KeyboardAggregator { self.dev_log(|| debug!(?report, "Keyboard HID report")); self.send_report(report); if self.is_magic_chord() { - self.dev_log(|| warn!("Magic chord pressed => πŸ§™β€β™‚οΈπŸͺ„πŸ’₯ AVADA KEDAVRA!!! πŸ’€")); + self.dev_log(|| warn!("Magic chord pressed => πŸ§™β€β™‚οΈπŸͺ„ AVADA KEDAVRA!!! πŸ’₯πŸ’€")); std::process::exit(0); } } diff --git a/server/src/main.rs b/server/src/main.rs index 4f347bb..001dcaa 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -36,50 +36,55 @@ impl Relay for Handler { let (tx, rx) = tokio::sync::mpsc::channel(32); tokio::spawn(async move { - while let Some(msg) = in_stream.next().await.transpose()? { - match msg.kind { - /* ───── KEYBOARD ───── */ - Some(hid_report::Kind::KeyboardReport(ref v)) if v.len() == 8 => { - match kb.lock().await.write_all(v).await { - Ok(_) => info!("⌨️ β†’ /dev/hidg0 (8 B)"), + loop { + match in_stream.next().await { + /* ──────────────── message received ──────────────── */ + Some(Ok(msg)) => { + // 1. write to the right gadget --------------------------------- + let io_res = match &msg.kind { + Some(hid_report::Kind::KeyboardReport(v)) if v.len() == 8 => { + kb.lock().await.write_all(v).await.map(|_| "⌨️ β†’ /dev/hidg0 (8β€―B)") + } + Some(hid_report::Kind::MouseReport(v)) if v.len() == 4 => { + ms.lock().await.write_all(v).await.map(|_| "πŸ–±οΈ β†’ /dev/hidg1 (4β€―B)") + } + _ => { + error!(?msg.kind, "⚠️ malformed packet"); + continue; // skip echo + } + }; + + // 2. I/O result ------------------------------------------------- + match io_res { + Ok(msg_txt) => info!("{msg_txt}"), Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { - trace!("⌨️ /dev/hidg0 busy, dropped packet"); - continue; + trace!("πŸ› gadget busy, dropped packet"); + continue; // skip echo } Err(e) => { - error!("⌨️ write error: {e}"); - continue; + error!("write error: {e}"); + continue; // skip echo } } + + // 3. echo back (best‑effort) ----------------------------------- + let _ = tx.try_send(Ok(msg)); } - - /* ───── MOUSE ───── */ - Some(hid_report::Kind::MouseReport(ref v)) if v.len() == 4 => { - match ms.lock().await.write_all(v).await { - Ok(_) => info!("πŸ–±οΈ β†’ /dev/hidg1 (4 B)"), - Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { - trace!("πŸ–±οΈ /dev/hidg1 busy, dropped packet"); - continue; - } - Err(e) => { - error!("πŸ–±οΈ write error: {e}"); - continue; - } - } - } - - /* ───── bad / unknown ───── */ - _ => { - error!(?msg.kind, "⚠️ malformed packet"); - continue; + + /* ──────────────── benign back‑pressure error ──────────────── */ + Some(Err(status)) => { + trace!("grpc recv error (ignored): {status}"); + continue; // keep the stream alive } + + /* ──────────────── client closed the stream ──────────────── */ + None => break, } - - // echo back so the client knows we’re alive - let _ = tx.send(Ok(msg)).await; } + info!("πŸ”š client stream closed"); - Ok::<_, Status>(()) + // dropping `tx` here terminates the serverβ†’client stream gracefully + Ok::<(), Status>(()) }); Ok(Response::new(Box::pin(ReceiverStream::new(rx))))