🚧 wip: little TCP server

main
_ 2023-09-14 22:09:23 -05:00
parent cfae81c780
commit 9405a2d10b
1 changed files with 36 additions and 0 deletions

View File

@ -48,6 +48,7 @@ fn main() -> Result <(), Error>
println! ("{} ms", (stop - start).as_millis ()); println! ("{} ms", (stop - start).as_millis ());
}, },
Some ("network") => main_network (),
Some (_) => eprintln! ("Unknown subcommand"), 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 (); 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)] #[derive (Debug, thiserror::Error)]
enum Error enum Error
{ {