From ca9a2ba34d509df0df2b3141342c8196b409826b Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 30 Apr 2023 22:48:25 +0800 Subject: [PATCH] feat: skip send colors for disconnected board. --- src-tauri/src/main.rs | 1 - src-tauri/src/rpc/board_info.rs | 4 ++- src-tauri/src/rpc/udp.rs | 47 ++++++++++++++++++--------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index aad2db6..8b17901 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -425,7 +425,6 @@ async fn main() { error!("boards change receiver changed error: {}", err); return; } - log::info!("boards changed"); let boards = receiver.borrow().clone(); diff --git a/src-tauri/src/rpc/board_info.rs b/src-tauri/src/rpc/board_info.rs index 73d7833..bb3a290 100644 --- a/src-tauri/src/rpc/board_info.rs +++ b/src-tauri/src/rpc/board_info.rs @@ -14,6 +14,7 @@ pub enum BoardConnectStatus { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct BoardInfo { + pub fullname: String, pub host: String, pub address: Ipv4Addr, pub port: u16, @@ -23,8 +24,9 @@ pub struct BoardInfo { } impl BoardInfo { - pub fn new(host: String, address: Ipv4Addr, port: u16) -> Self { + pub fn new(fullname: String, host: String, address: Ipv4Addr, port: u16) -> Self { Self { + fullname, host, address, port, diff --git a/src-tauri/src/rpc/udp.rs b/src-tauri/src/rpc/udp.rs index 7f83257..ad6dfc6 100644 --- a/src-tauri/src/rpc/udp.rs +++ b/src-tauri/src/rpc/udp.rs @@ -1,9 +1,4 @@ -use std::{ - collections::{HashMap, HashSet}, - net::Ipv4Addr, - sync::Arc, - time::Duration, -}; +use std::{collections::HashMap, net::Ipv4Addr, sync::Arc, time::Duration}; use futures::future::join_all; use mdns_sd::{ServiceDaemon, ServiceEvent}; @@ -13,12 +8,12 @@ use tokio::{ sync::{watch, OnceCell, RwLock}, }; -use super::{BoardConnectStatus, BoardInfo}; +use super::{BoardInfo, BoardConnectStatus}; #[derive(Debug, Clone)] pub struct UdpRpc { socket: Arc, - boards: Arc>>, + boards: Arc>>, boards_change_sender: Arc>>, } @@ -71,18 +66,6 @@ impl UdpRpc { tokio::spawn(async move { shared_self_for_check.check_boards().await; }); - - // let shared_self_for_watch = shared_self.clone(); - // tokio::spawn(async move { - // let mut rx = shared_self_for_watch.clone_boards_change_receiver().await; - - // // let mut rx = sub_tx.subscribe(); - // // drop(sub_tx); - // while rx.changed().await.is_ok() { - // let boards = rx.borrow().clone(); - // info!("boards changed: {:?}", boards); - // } - // }); } async fn search_boards(&self) -> anyhow::Result<()> { @@ -109,12 +92,13 @@ impl UdpRpc { let mut boards = self.boards.write().await; let board = BoardInfo::new( + info.get_fullname().to_string(), info.get_fullname().to_string(), info.get_addresses().iter().next().unwrap().clone(), info.get_port(), ); - if boards.insert(board.address, board.clone()).is_some() { + if boards.insert(board.fullname.clone(), board.clone()).is_some() { info!("added board {:?}", board); } @@ -124,8 +108,21 @@ impl UdpRpc { sender.send(tx_boards)?; tokio::task::yield_now().await; } + ServiceEvent::ServiceRemoved(_, fullname) => { + info!("removed board {:?}", fullname); + let mut boards = self.boards.write().await; + if boards.remove(&fullname).is_some() { + info!("removed board {:?} successful", fullname); + } + + let tx_boards = boards.values().cloned().collect(); + drop(boards); + + sender.send(tx_boards)?; + tokio::task::yield_now().await; + } other_event => { - warn!("{:?}", &other_event); + // log::info!("{:?}", &other_event); } } } @@ -147,6 +144,12 @@ impl UdpRpc { let socket = self.socket.clone(); let handlers = boards.into_iter().map(|board| { + if board.connect_status == BoardConnectStatus::Disconnected { + return tokio::spawn(async move { + log::debug!("board {} is disconnected, skip.", board.host); + }); + } + let socket = socket.clone(); let buff = buff.clone(); tokio::spawn(async move {