diff --git a/crates/core/src/bc_protocol/connection/udpconn.rs b/crates/core/src/bc_protocol/connection/udpconn.rs index 95cf7b9..c97eb2d 100644 --- a/crates/core/src/bc_protocol/connection/udpconn.rs +++ b/crates/core/src/bc_protocol/connection/udpconn.rs @@ -3,6 +3,7 @@ use crate::bc; use crate::bc::model::*; use crate::bcudp; use crate::bcudp::{model::*, xml::*}; +use crate::RX_TIMEOUT; use log::*; use rand::{seq::SliceRandom, thread_rng, Rng}; use socket2::{Domain, Socket, Type}; @@ -456,14 +457,18 @@ impl BufRead for UdpSource { } if self.read_buffer.buffer.len() <= self.read_buffer.consumed { // Get next packet of the read queue - if let Some(msg) = self - .incoming - .lock() - .unwrap() - .remove(&self.next_to_consume.load(Ordering::Relaxed)) - { - self.next_to_consume.fetch_add(1, Ordering::Relaxed); - self.read_buffer.buffer.extend(msg.buf); + let start_time = OffsetDateTime::now_utc(); + while (start_time - OffsetDateTime::now_utc()) < RX_TIMEOUT { + if let Some(msg) = self + .incoming + .lock() + .unwrap() + .remove(&self.next_to_consume.load(Ordering::Relaxed)) + { + self.next_to_consume.fetch_add(1, Ordering::Relaxed); + self.read_buffer.buffer.extend(msg.buf); + break; + } } } diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs index 1fd7e1b..04aacb8 100644 --- a/crates/core/src/bcmedia/de.rs +++ b/crates/core/src/bcmedia/de.rs @@ -1,9 +1,11 @@ use super::model::BcMediaIframe; use super::model::*; +use crate::RX_TIMEOUT; use err_derive::Error; use nom::IResult; use nom::{combinator::*, named, number::streaming::*, take, take_str}; use std::io::Read; +use time::OffsetDateTime; // PAD_SIZE: Media packets use 8 byte padding const PAD_SIZE: u32 = 8; @@ -50,15 +52,30 @@ where Err(e) => return Err(e.into()), }; - if 0 == (&mut rdr) - .take(to_read.get() as u64) - .read_to_end(&mut input)? - { - return Err(std::io::Error::new( - std::io::ErrorKind::UnexpectedEof, - "Read returned 0 bytes", - ) - .into()); + let start_time = OffsetDateTime::now_utc(); + loop { + match (&mut rdr) + .take(to_read.get() as u64) + .read_to_end(&mut input) + { + Ok(0) => { + if (OffsetDateTime::now_utc() - start_time) > RX_TIMEOUT { + return Err(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + "Read returned 0 bytes", + ) + .into()); + } + } + Ok(_) => break, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + // This is a temporaily unavaliable resource + // We should just wait and try again + } + Err(e) => { + return Err(e.into()); + } + } } } } diff --git a/crates/core/src/bcudp/de.rs b/crates/core/src/bcudp/de.rs index a8cc9ff..1115905 100644 --- a/crates/core/src/bcudp/de.rs +++ b/crates/core/src/bcudp/de.rs @@ -1,4 +1,5 @@ use super::{crc::calc_crc, model::*, xml::*, xml_crypto::decrypt}; +use crate::RX_TIMEOUT; use err_derive::Error; use nom::IResult; use nom::{ @@ -8,6 +9,7 @@ use nom::{ take, Err, }; use std::io::Read; +use time::OffsetDateTime; /// The error types used during deserialisation #[derive(Debug, Error)] @@ -51,15 +53,30 @@ where Err(e) => return Err(e.into()), }; - if 0 == (&mut rdr) - .take(to_read.get() as u64) - .read_to_end(&mut input)? - { - return Err(std::io::Error::new( - std::io::ErrorKind::UnexpectedEof, - "Read returned 0 bytes", - ) - .into()); + let start_time = OffsetDateTime::now_utc(); + loop { + match (&mut rdr) + .take(to_read.get() as u64) + .read_to_end(&mut input) + { + Ok(0) => { + if (OffsetDateTime::now_utc() - start_time) > RX_TIMEOUT { + return Err(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + "Read returned 0 bytes", + ) + .into()); + } + } + Ok(_) => break, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + // This is a temporaily unavaliable resource + // We should just wait and try again + } + Err(e) => { + return Err(e.into()); + } + } } } }