Skip to content

Commit

Permalink
fix: check if destination is already connected when infer the next ho…
Browse files Browse the repository at this point in the history
…p of message
  • Loading branch information
Ma233 committed Jan 16, 2024
1 parent 9cdc502 commit 931b899
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/core/src/message/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ pub trait PayloadSender {
/// Get access to DHT.
fn dht(&self) -> Arc<PeerRing>;

/// Used to check if destination is already connected when `infer_next_hop`
fn is_connected(&self, did: Did) -> bool;

/// Send a message payload to a specified DID.
async fn do_send_payload(&self, did: Did, payload: MessagePayload) -> Result<()>;

Expand All @@ -249,6 +252,10 @@ pub trait PayloadSender {
return Ok(next_hop);
}

if self.is_connected(destination) {
return Ok(destination);
}

match self.dht().find_successor(destination)? {
PeerRingAction::Some(did) => Ok(did),
PeerRingAction::RemoteAction(did, _) => Ok(did),
Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/swarm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rings_derive::JudgeConnection;
use rings_transport::core::transport::BoxedTransport;
use rings_transport::core::transport::ConnectionInterface;
use rings_transport::core::transport::TransportMessage;
use rings_transport::core::transport::WebrtcConnectionState;
use rings_transport::error::Error as TransportError;
pub use types::MeasureImpl;
pub use types::WrappedDid;
Expand Down Expand Up @@ -322,6 +323,13 @@ impl PayloadSender for Swarm {
Swarm::dht(self)
}

fn is_connected(&self, did: Did) -> bool {
let Some(conn) = self.get_connection(did) else {
return false;
};
conn.webrtc_connection_state() == WebrtcConnectionState::Connected
}

async fn do_send_payload(&self, did: Did, payload: MessagePayload) -> Result<()> {
#[cfg(test)]
{
Expand Down

0 comments on commit 931b899

Please sign in to comment.