From e2d5eb061f3ed8764ae6798d65116ef9f70077a0 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 10 May 2023 13:25:51 +0700 Subject: [PATCH] Add time out to map discovery --- .../core/src/bc_protocol/connection/discovery.rs | 15 ++++++++++----- .../core/src/bc_protocol/connection/udpsource.rs | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index e44e022..9daabe4 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -189,11 +189,16 @@ impl Discoverer { F: Fn(UdpDiscovery, SocketAddr) -> Option, { let mut reply = ReceiverStream::new(self.subscribe(0).await?); - loop { - let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??; - if let Some(result) = map(reply, addr) { - return Ok(result); - } + tokio::select! { + v = async { + loop { + let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??; + if let Some(result) = map(reply, addr) { + return Ok(result); + } + } + } => v, + _ = tokio::time::sleep(*MAXIMUM_WAIT) => Err::(Error::DiscoveryTimeout), } } diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index b57ae0f..cb35b4b 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -198,6 +198,7 @@ impl Sink<(BcUdp, SocketAddr)> for BcUdpSource { } } +#[allow(dead_code)] enum State { Normal, // Normal recieve Flushing, // Used to send ack packets and things in the buffer @@ -298,15 +299,15 @@ impl Stream for UdpPayloadSource { let mut this = self.get_mut(); match this.state { State::Normal => { - this.state = State::YieldNow; + // this.state = State::YieldNow; + // Handle resend events + this.maintanence(cx); // Data ready to go if let Some(payload) = this.recieved.remove(&this.packets_want) { this.packets_want += 1; // error!("packets_want: {}", this.packets_want); return Poll::Ready(Some(Ok(payload))); } - // Handle resend events - this.maintanence(cx); // Normal behaviors match this.inner.poll_next_unpin(cx) { Poll::Ready(Some(Ok((