diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 5a14030..14c7fb2 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -15,7 +15,7 @@ type MsgHandler = Box Option + Send + Sync>; #[derive(Default)] struct Subscriber { /// Subscribers based on their Num - num: RwLock>>, + num: RwLock>>>, /// Subscribers based on their ID id: RwLock>, } @@ -48,11 +48,7 @@ impl BcConnection { loop { let packet = source.next().await; let bc = match packet { - Some(Ok(bc)) => bc, - Some(Err(e)) => { - error!("Deserialization error: {:?}", e); - break; - } + Some(res) => res, None => continue, }; @@ -119,39 +115,55 @@ impl BcConnection { Ok(()) } - async fn poll(response: Bc, subscribers: &Subscriber, sink: &Mutex) -> Result<()> { - // Don't hold the lock during deserialization so we don't poison the subscribers mutex if - // something goes wrong - let msg_num = response.meta.msg_num; - let msg_id = response.meta.msg_id; + async fn poll( + response: Result, + subscribers: &Subscriber, + sink: &Mutex, + ) -> Result<()> { + match response { + Ok(response) => { + // Don't hold the lock during deserialization so we don't poison the subscribers mutex if + // something goes wrong + let msg_num = response.meta.msg_num; + let msg_id = response.meta.msg_id; - let mut remove_it_num = false; - match ( - subscribers.id.read().await.get(&msg_id), - subscribers.num.read().await.get(&msg_num), - ) { - (Some(occ), _) => { - if let Some(reply) = occ(&response) { - assert!(reply.meta.msg_num == response.meta.msg_num); - sink.lock().await.send(reply).await?; + let mut remove_it_num = false; + match ( + subscribers.id.read().await.get(&msg_id), + subscribers.num.read().await.get(&msg_num), + ) { + (Some(occ), _) => { + if let Some(reply) = occ(&response) { + assert!(reply.meta.msg_num == response.meta.msg_num); + sink.lock().await.send(reply).await?; + } + } + (None, Some(occ)) => { + trace!("occ.full: {}/{}", occ.capacity(), occ.max_capacity()); + if occ.send(Ok(response)).await.is_err() { + remove_it_num = true; + } + } + (None, None) => { + debug!( + "Ignoring uninteresting message id {} (number: {})", + msg_id, msg_num + ); + trace!("Contents: {:?}", response); + } + } + if remove_it_num { + subscribers.num.write().await.remove(&msg_num); } } - (None, Some(occ)) => { - trace!("occ.full: {}/{}", occ.capacity(), occ.max_capacity()); - if occ.send(response).await.is_err() { - remove_it_num = true; + Err(e) => { + for sub in subscribers.num.read().await.values() { + let _ = sub.send(Err(e.clone())).await; } + subscribers.num.write().await.clear(); + subscribers.id.write().await.clear(); + return Err(e); } - (None, None) => { - debug!( - "Ignoring uninteresting message id {} (number: {})", - msg_id, msg_num - ); - trace!("Contents: {:?}", response); - } - } - if remove_it_num { - subscribers.num.write().await.remove(&msg_num); } Ok(()) diff --git a/crates/core/src/bc_protocol/connection/bcsub.rs b/crates/core/src/bc_protocol/connection/bcsub.rs index cd6696f..ae07514 100644 --- a/crates/core/src/bc_protocol/connection/bcsub.rs +++ b/crates/core/src/bc_protocol/connection/bcsub.rs @@ -2,7 +2,7 @@ use super::BcConnection; use crate::bcmedia::codex::BcMediaCodex; use crate::{bc::model::*, Error, Result}; use futures::stream::{IntoAsyncRead, Stream, StreamExt, TryStreamExt}; -use std::io::Result as IoResult; +use std::io::{Error as IoError, ErrorKind, Result as IoResult}; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::sync::mpsc::Receiver; @@ -11,21 +11,21 @@ use tokio_util::codec::FramedRead; use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt}; pub struct BcSubscription<'a> { - rx: ReceiverStream, + rx: ReceiverStream>, msg_num: u32, conn: &'a BcConnection, } pub struct BcStream<'a> { - rx: &'a mut ReceiverStream, + rx: &'a mut ReceiverStream>, } impl<'a> Unpin for BcStream<'a> {} impl<'a> Stream for BcStream<'a> { - type Item = Bc; + type Item = Result; - fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>> { let mut this = self.as_mut(); match Pin::new(&mut this.rx).poll_next(cx) { Poll::Ready(Some(bc)) => Poll::Ready(Some(bc)), @@ -36,7 +36,7 @@ impl<'a> Stream for BcStream<'a> { } pub struct BcPayloadStream<'a> { - rx: &'a mut ReceiverStream, + rx: &'a mut ReceiverStream>, } impl<'a> Unpin for BcPayloadStream<'a> {} @@ -49,7 +49,7 @@ impl<'a> Stream for BcPayloadStream<'a> { cx: &mut Context<'_>, ) -> Poll>>> { match Pin::new(&mut self.rx).poll_next(cx) { - Poll::Ready(Some(bc)) => { + Poll::Ready(Some(Ok(bc))) => { match bc { Bc { body: @@ -65,6 +65,7 @@ impl<'a> Stream for BcPayloadStream<'a> { } } } + Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e)))), Poll::Ready(None) => Poll::Ready(None), Poll::Pending => Poll::Pending, } @@ -74,7 +75,11 @@ impl<'a> Stream for BcPayloadStream<'a> { pub type BcMediaStream<'b> = FramedRead>>, BcMediaCodex>; impl<'a> BcSubscription<'a> { - pub fn new(rx: Receiver, msg_num: u32, conn: &'a BcConnection) -> BcSubscription<'a> { + pub fn new( + rx: Receiver>, + msg_num: u32, + conn: &'a BcConnection, + ) -> BcSubscription<'a> { BcSubscription { rx: ReceiverStream::new(rx), msg_num, @@ -90,8 +95,10 @@ impl<'a> BcSubscription<'a> { pub async fn recv(&mut self) -> Result { let bc = self.rx.next().await.ok_or(Error::DroppedSubscriber)?; - assert!(bc.meta.msg_num as u32 == self.msg_num); - Ok(bc) + if let Ok(bc) = &bc { + assert!(bc.meta.msg_num as u32 == self.msg_num); + } + bc } #[allow(unused)]