Add a better timeout handling to UDP and Media

This commit is contained in:
QuantumEntangledAndy authored and Andrew committed 2021-08-31 20:46:47 +07:00
1 parent c0e1a817d2
commit 8aee6e8e8d
3 files changed
+65 -26

No files matched your search

@@ -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;
}
}
}
+26 -9
View File
@@ -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());
}
}
}
}
}
+26 -9
View File
@@ -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());
}
}
}
}
}