From 4eecde23b76ac248055057e5c53514b5d01e0349 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 4 Nov 2025 16:39:56 +0100 Subject: [PATCH] set read timeout --- src/transport/tcp.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/transport/tcp.rs b/src/transport/tcp.rs index 1c65b00..2740430 100644 --- a/src/transport/tcp.rs +++ b/src/transport/tcp.rs @@ -1,5 +1,5 @@ //! # Packet transport via TCP with COBS encoding. -use std::io::{Read as _, Write as _}; +use std::{io::{Read as _, Write as _}, time::Duration}; use crate::transport::PacketTransport; @@ -20,6 +20,7 @@ impl PacketTransportTcpWithCobs { /// The `tcp_stream` parameter is the underlying TCP stream which should already be connected. pub fn new(tcp_stream: std::net::TcpStream, decoder: cobs::CobsDecoderOwned) -> Self { tcp_stream.set_nonblocking(true).unwrap(); + tcp_stream.set_read_timeout(Some(Duration::from_millis(100))).unwrap(); Self { tcp_stream, decoder, @@ -42,6 +43,9 @@ impl PacketTransportTcpWithCobs { /// This function pulls bytes from the TCP stream and feeds them into the COBS decoder. /// For each received packet, the closure will be called with the decoded packet as an argument. /// The function will return the number of received packets. + /// + /// Please note that this function may block on the TCP stream read call, but it will not + /// block indifinitely due to the read timeout set on the TCP stream. pub fn receive(&mut self, mut f: impl FnMut(&[u8])) -> Result { let mut decoded_packets = 0; loop {