From 9405a2d10b0b41a2e188f89284f215351b005b97 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Thu, 14 Sep 2023 22:09:23 -0500 Subject: [PATCH] :construction: wip: little TCP server --- src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main.rs b/src/main.rs index 84dbd32..bd931c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ fn main() -> Result <(), Error> println! ("{} ms", (stop - start).as_millis ()); }, + Some ("network") => main_network (), Some (_) => eprintln! ("Unknown subcommand"), } @@ -199,6 +200,41 @@ fn main_egui () eframe::run_native("Five Five Five", native_options, Box::new(|cc| Box::new(App::new(cc, send_to_ctl, recv_at_gui, send_gui_handle)))).unwrap (); } +fn main_network () +{ + use tokio::io:: + { + AsyncReadExt, + AsyncWriteExt, + }; + + let rt = tokio::runtime::Runtime::new ().unwrap (); + + rt.block_on (async + { + let server = tokio::net::TcpListener::bind ("127.0.0.1:9001").await.unwrap (); + + tokio::spawn (async move + { + while let Ok ((mut stream, _remote_addr)) = server.accept ().await + { + tokio::spawn (async move + { + for i in 0.. + { + let mut buf = vec! [0u8]; + if let Err (_) = stream.read_exact (&mut buf).await + { + break; + } + stream.write_all (format! ("{i}\n").as_bytes ()).await.unwrap (); + } + }); + } + }).await.unwrap (); + }); +} + #[derive (Debug, thiserror::Error)] enum Error {