From eb3bd6363c5b29deb7f8048bc9c0f68e0b6f134e Mon Sep 17 00:00:00 2001 From: Tristan Debrunner Date: Sun, 15 Oct 2023 20:48:03 -0700 Subject: [PATCH] Handle disconnections from the binary and JSON ports --- refbox/src/app/update_sender.rs | 41 +++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/refbox/src/app/update_sender.rs b/refbox/src/app/update_sender.rs index 0da5da39..50dce12a 100644 --- a/refbox/src/app/update_sender.rs +++ b/refbox/src/app/update_sender.rs @@ -267,16 +267,24 @@ impl WorkerHandle { json: &[u8], snapshot: &GameSnapshotNoHeap, white_on_right: bool, - ) -> Result<(), Box> { + ) -> Result<(), TrySendError> { match self.tx { - WorkerTx::Binary(ref tx) => tx.try_send(Vec::from(binary))?, - WorkerTx::Json(ref tx) => tx.try_send(Vec::from(json))?, - WorkerTx::Serial(ref tx) => tx.try_send(SerialWorkerMessage::NewSnapshot( - snapshot.clone(), - white_on_right, - ))?, - }; - Ok(()) + WorkerTx::Binary(ref tx) => tx.try_send(Vec::from(binary)).map_err(error_formatter), + WorkerTx::Json(ref tx) => tx.try_send(Vec::from(json)).map_err(error_formatter), + WorkerTx::Serial(ref tx) => tx + .try_send(SerialWorkerMessage::NewSnapshot( + snapshot.clone(), + white_on_right, + )) + .map_err(error_formatter), + } + } +} + +fn error_formatter(old: TrySendError) -> TrySendError { + match old { + TrySendError::Closed(o) => TrySendError::Closed(format!("{o:?}")), + TrySendError::Full(o) => TrySendError::Closed(format!("{o:?}")), } } @@ -432,16 +440,25 @@ impl Server { } }; - for (_, handle) in self.senders.iter().filter(filter) { + let mut to_drop = vec![]; + for (id, handle) in self.senders.iter().filter(filter) { if let Err(e) = handle.send( &self.binary, &self.json, &self.snapshot, self.white_on_right, ) { - error!("Error sending to worker: {e:?}"); + if matches!(e, TrySendError::Closed(_)) { + info!("Worker channel closed"); + to_drop.push(*id); + } else { + error!("Error sending to worker: {e:?}"); + } } } + for id in to_drop { + self.senders.remove(&id); + } } pub async fn run_loop(mut self) { @@ -477,7 +494,7 @@ impl Server { for (_, handle) in self.senders.iter().filter(|(_, handle)| handle.is_serial()) { if let WorkerTx::Serial(tx) = &handle.tx { if let Err(e) = tx.try_send(SerialWorkerMessage::TriggerFlash) { - error!("Error sending to worker: {e:?}"); + error!("Error sending to serial worker: {e:?}"); } } }