fedimint_core/net/peers.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use std::sync::Arc;
use async_trait::async_trait;
use fedimint_core::PeerId;
#[cfg(not(target_family = "wasm"))]
pub mod fake;
pub type DynP2PConnections<M> = Arc<dyn IP2PConnections<M>>;
/// Connection manager that tries to keep connections open to all peers
#[async_trait]
pub trait IP2PConnections<M>: Send + Sync + 'static {
/// Send message to recipient; block if channel is full.
async fn send(&self, recipient: Recipient, msg: M);
/// Try to send message to recipient; drop message if channel is full.
fn try_send(&self, recipient: Recipient, msg: M);
/// Await the next message; return None if we are shutting down.
async fn receive(&self) -> Option<(PeerId, M)>;
/// Await the next message from peer; return None if we are shutting down.
async fn receive_from_peer(&self, peer: PeerId) -> Option<M>;
/// Convert the struct to trait object.
fn into_dyn(self) -> DynP2PConnections<M>
where
Self: Sized,
{
Arc::new(self)
}
}
/// This enum defines the intended recipient of a p2p message.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Recipient {
Everyone,
Peer(PeerId),
}