From 6a285fe7cd39629d9043b3d5a1eb0e831840765f Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Tue, 4 Feb 2025 19:27:26 -0500 Subject: [PATCH] controller input timeout --- northbridge/src/main.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/northbridge/src/main.rs b/northbridge/src/main.rs index 2c6ec78..321903d 100644 --- a/northbridge/src/main.rs +++ b/northbridge/src/main.rs @@ -1,10 +1,12 @@ #![feature(async_closure)] +use std::time::Duration; + use anyhow::{Context, Result}; use common::{Command, Response, BAUDRATE}; use framed_codec::FramedCodec; use futures::{SinkExt, StreamExt}; -use tokio::{io::AsyncReadExt, net::{TcpListener, TcpSocket}, sync::{broadcast, watch::{self, Sender}}, task::JoinHandle}; +use tokio::{io::AsyncReadExt, net::{TcpListener, TcpSocket}, sync::{broadcast, watch::{self, Sender}}, task::JoinHandle, time::timeout}; use tokio_serial::SerialPortBuilderExt; use tokio_util::codec::Framed; @@ -30,6 +32,7 @@ async fn main() -> Result<()> { tokio::spawn(async move { loop { + let _ = send.send(Command::Stop); if let Err(e) = control(send.clone()).await { println!("controller exited: {e}"); } @@ -75,9 +78,10 @@ async fn control(sender: Sender) -> Result<()> { println!("connected to {addr:?}"); loop { - let len = stream.read_u32().await?; + + let len = timeout(Duration::from_millis(30), stream.read_u32()).await??; let mut buf = vec![0; len as usize]; - stream.read_exact(&mut buf).await?; + timeout(Duration::from_millis(30), stream.read_exact(&mut buf)).await??; let cmd: Command = postcard::from_bytes(&buf)?;