Explorar o código

net/p2p: Implement broadcast_to() when we want to broadcast to specific peers.

parazyd %!s(int64=2) %!d(string=hai) anos
pai
achega
b96a54bb4f
Modificáronse 1 ficheiros con 26 adicións e 0 borrados
  1. 26 0
      src/net/p2p.rs

+ 26 - 0
src/net/p2p.rs

@@ -173,6 +173,32 @@ impl P2p {
         self.broadcast_with_exclude(message, &[]).await
     }
 
+    /// Broadcast a message concurrently to all given peers.
+    pub async fn broadcast_to<M: Message>(&self, message: &M, peer_list: &[ChannelPtr]) {
+        let mut futures = FuturesUnordered::new();
+
+        for channel in peer_list {
+            futures.push(channel.send(message).map_err(|e| {
+                (
+                    format!("[P2P] Broadcasting message to {} failed: {}", channel.address(), e),
+                    channel.clone(),
+                )
+            }));
+        }
+
+        if futures.is_empty() {
+            warn!(target: "net::p2p::broadcast()", "[P2P] No connected channels found for broadcast");
+            return
+        }
+
+        while let Some(entry) = futures.next().await {
+            if let Err((e, chan)) = entry {
+                error!(target: "net::p2p::broadcast()", "{}", e);
+                self.remove(chan).await;
+            }
+        }
+    }
+
     /// Broadcasts a message concurrently across active channels, excluding
     /// the ones provided in `exclude_list`.
     pub async fn broadcast_with_exclude<M: Message>(&self, message: &M, exclude_list: &[Url]) {