controller input timeout
This commit is contained in:
parent
951dafc7d5
commit
6a285fe7cd
1 changed files with 7 additions and 3 deletions
|
@ -1,10 +1,12 @@
|
||||||
#![feature(async_closure)]
|
#![feature(async_closure)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use common::{Command, Response, BAUDRATE};
|
use common::{Command, Response, BAUDRATE};
|
||||||
use framed_codec::FramedCodec;
|
use framed_codec::FramedCodec;
|
||||||
use futures::{SinkExt, StreamExt};
|
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_serial::SerialPortBuilderExt;
|
||||||
use tokio_util::codec::Framed;
|
use tokio_util::codec::Framed;
|
||||||
|
|
||||||
|
@ -30,6 +32,7 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
|
let _ = send.send(Command::Stop);
|
||||||
if let Err(e) = control(send.clone()).await {
|
if let Err(e) = control(send.clone()).await {
|
||||||
println!("controller exited: {e}");
|
println!("controller exited: {e}");
|
||||||
}
|
}
|
||||||
|
@ -75,9 +78,10 @@ async fn control(sender: Sender<Command>) -> Result<()> {
|
||||||
println!("connected to {addr:?}");
|
println!("connected to {addr:?}");
|
||||||
|
|
||||||
loop {
|
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];
|
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)?;
|
let cmd: Command = postcard::from_bytes(&buf)?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue