Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mdns): emit ToSwarm::NewExternalAddrOfPeer #5623

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ use libp2p_swarm::{
};
use smallvec::SmallVec;
use std::collections::hash_map::{Entry, HashMap};
use std::collections::VecDeque;
use std::future::Future;
use std::sync::{Arc, RwLock};
use std::{cmp, fmt, io, net::IpAddr, pin::Pin, task::Context, task::Poll, time::Instant};
use void::Void;

/// An abstraction to allow for compatibility with various async runtimes.
pub trait Provider: 'static {
Expand Down Expand Up @@ -174,6 +176,9 @@ where
listen_addresses: Arc<RwLock<ListenAddresses>>,

local_peer_id: PeerId,

/// Queued events to return when the behaviour is being polled.
pending_events: VecDeque<ToSwarm<Event, Void>>,
}

impl<P> Behaviour<P>
Expand All @@ -193,6 +198,7 @@ where
discovered_nodes: Default::default(),
closest_expiration: Default::default(),
listen_addresses: Default::default(),
pending_events: VecDeque::new(),
local_peer_id,
})
}
Expand Down Expand Up @@ -345,6 +351,11 @@ where
} else {
tracing::info!(%peer, address=%addr, "discovered peer on address");
self.discovered_nodes.push((peer, addr.clone(), expiration));
self.pending_events
.push_back(ToSwarm::NewExternalAddrOfPeer {
peer_id: peer,
address: addr.clone(),
});
discovered.push((peer, addr));
}
}
Expand All @@ -353,6 +364,12 @@ where
let event = Event::Discovered(discovered);
return Poll::Ready(ToSwarm::GenerateEvent(event));
}

// If there are pending addresses to be emitted we emit them.
if let Some(event) = self.pending_events.pop_front() {
return Poll::Ready(event);
}

// Emit expired event.
let now = Instant::now();
let mut closest_expiration = None;
Expand Down
Loading