1
Fork 0

controller input timeout

This commit is contained in:
Andy Killorin 2025-02-04 19:27:26 -05:00
parent 951dafc7d5
commit 6a285fe7cd
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -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<Command>) -> 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)?;