Fix p2p message handling

This commit is contained in:
IQuant 2024-10-13 14:50:05 +03:00
parent bf5a2079dd
commit ff52f046e4
2 changed files with 32 additions and 9 deletions

View file

@ -18,7 +18,7 @@ pub enum Reliability {
Reliable,
}
#[derive(Debug, Encode, Decode, Clone, Copy)]
#[derive(Debug, Encode, Decode, Clone, Copy, PartialEq, Eq)]
pub enum Destination {
One(PeerId),
Broadcast,
@ -86,4 +86,11 @@ impl Destination {
pub(crate) fn is_broadcast(self) -> bool {
matches!(self, Destination::Broadcast)
}
pub(crate) fn to_one(self) -> Option<PeerId> {
if let Self::One(peer_id) = self {
Some(peer_id)
} else {
None
}
}
}

View file

@ -267,17 +267,33 @@ impl ConnectionManager {
async fn handle_incoming_message(&mut self, msg: InternalMessage) {
match msg {
InternalMessage::Normal(msg) => {
let intended_for_me = self
.shared
.my_id
.load()
.map(|my_id| msg.dst == Destination::One(my_id))
.unwrap_or(false);
if self.is_server && !intended_for_me && !msg.dst.is_broadcast() {
self.server_send_internal_message(
msg.dst.to_one().unwrap(),
&InternalMessage::Normal(msg),
)
.await;
return;
}
if self.is_server && msg.dst.is_broadcast() {
self.server_send_to_peers(msg.clone()).await;
}
self.shared
.inbound_channel
.0
.send(NetworkEvent::Message(crate::Message {
src: msg.src,
data: msg.data,
}))
.expect("channel to be open");
if msg.dst.is_broadcast() || intended_for_me {
self.shared
.inbound_channel
.0
.send(NetworkEvent::Message(crate::Message {
src: msg.src,
data: msg.data,
}))
.expect("channel to be open");
}
}
InternalMessage::RemoteConnected(peer_id) => {
debug!("Got notified of peer {peer_id}");