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

mdns/fix: Failed to register opened substream #301

Merged
merged 6 commits into from
Dec 12, 2024
Merged
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
31 changes: 15 additions & 16 deletions src/protocol/mdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

//! [Multicast DNS](https://en.wikipedia.org/wiki/Multicast_DNS) implementation.

use crate::{error::Error, transport::manager::TransportManagerHandle, DEFAULT_CHANNEL_SIZE};
use crate::{transport::manager::TransportManagerHandle, DEFAULT_CHANNEL_SIZE};

use futures::Stream;
use multiaddr::Multiaddr;
Expand Down Expand Up @@ -95,7 +95,7 @@ pub(crate) struct Mdns {
socket: UdpSocket,

/// Query interval.
query_interval: Duration,
query_interval: tokio::time::Interval,

/// TX channel for sending events to user.
event_tx: Sender<MdnsEvent>,
Expand Down Expand Up @@ -138,12 +138,15 @@ impl Mdns {
socket.join_multicast_v4(&IPV4_MULTICAST_ADDRESS, &Ipv4Addr::UNSPECIFIED)?;
socket.set_nonblocking(true)?;

let mut query_interval = tokio::time::interval(config.query_interval);
query_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

Ok(Self {
_transport_handle,
event_tx: config.tx,
next_query_id: 1337u16,
discovered: HashSet::new(),
query_interval: config.query_interval,
query_interval,
receive_buffer: vec![0u8; 4096],
username: rand::thread_rng()
.sample_iter(&Alphanumeric)
Expand Down Expand Up @@ -276,24 +279,19 @@ impl Mdns {
}

/// Event loop for [`Mdns`].
pub(crate) async fn start(mut self) -> crate::Result<()> {
pub(crate) async fn start(mut self) {
tracing::debug!(target: LOG_TARGET, "starting mdns event loop");

// before starting the loop, make an initial query to the network
//
// bail early if the socket is not working
self.on_outbound_request().await?;

loop {
tokio::select! {
_ = tokio::time::sleep(self.query_interval) => {
tracing::trace!(target: LOG_TARGET, "timeout expired");
_ = self.query_interval.tick() => {
tracing::trace!(target: LOG_TARGET, "query interval ticked");

if let Err(error) = self.on_outbound_request().await {
tracing::error!(target: LOG_TARGET, ?error, "failed to send mdns query");
return Err(error);
}
}
},

result = self.socket.recv_from(&mut self.receive_buffer) => match result {
Ok((nread, address)) => match Packet::parse(&self.receive_buffer[..nread]) {
Ok(packet) => match packet.has_flags(PacketFlag::RESPONSE) {
Expand All @@ -308,9 +306,11 @@ impl Mdns {
}
}
false => if let Some(response) = self.on_inbound_request(packet) {
self.socket
if let Err(error) = self.socket
.send_to(&response, (IPV4_MULTICAST_ADDRESS, IPV4_MULTICAST_PORT))
.await?;
.await {
tracing::error!(target: LOG_TARGET, ?error, "failed to send mdns response");
}
}
}
Err(error) => tracing::debug!(
Expand All @@ -323,7 +323,6 @@ impl Mdns {
}
Err(error) => {
tracing::error!(target: LOG_TARGET, ?error, "failed to read from socket");
return Err(Error::from(error));
}
},
}
Expand Down
Loading