Skip to content

Commit

Permalink
Make discovery more robust by sending multiple packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Sep 24, 2024
1 parent 3b617ea commit 1cb799e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This file lists the changes that have occurred since January 2024 in the project
* Clock synchronization now uses the average of the lowest 1/3rd of samples
* Adjust for clock drift in tests
* Fix connecting to servers on non-standard port with peers
* Make discovery more robust by sending multiple packets

## 0.3 - 2024-09-16

Expand Down
59 changes: 46 additions & 13 deletions src/crusader-lib/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ fn interfaces() -> Vec<u32> {

#[cfg(feature = "client")]
pub async fn locate(peer_server: bool) -> Result<Server, anyhow::Error> {
use crate::common::fresh_socket_addr;
use std::{net::SocketAddrV6, time::Duration};
use tokio::time::timeout;
use crate::{common::fresh_socket_addr, serve::OnDrop};
use std::{
net::SocketAddrV6,
sync::atomic::{AtomicBool, Ordering},
time::Duration,
};
use tokio::time::{self, timeout};

fn handle_packet(
peer_server: bool,
Expand Down Expand Up @@ -142,6 +146,8 @@ pub async fn locate(peer_server: bool) -> Result<Server, anyhow::Error> {

socket.set_broadcast(true)?;

let socket = Arc::new(socket);

let data = Data {
hello: Hello::new(),
message: Message::Discover { peer: peer_server },
Expand All @@ -151,20 +157,47 @@ pub async fn locate(peer_server: bool) -> Result<Server, anyhow::Error> {

let ip = Ipv6Addr::from_str("ff02::1").unwrap();

let mut any = false;
for interface in interfaces() {
if socket
.send_to(&buf, SocketAddrV6::new(ip, DISCOVER_PORT, 0, interface))
.await
.is_ok()
{
any = true;
let socket_ = socket.clone();
let send_packet = move || {
let socket = socket_.clone();
let buf = buf.clone();
async move {
let mut any = false;
for interface in interfaces() {
if socket
.send_to(&buf, SocketAddrV6::new(ip, DISCOVER_PORT, 0, interface))
.await
.is_ok()
{
any = true;
}
}
any
}
}
if !any {
};

if !send_packet().await {
bail!("Failed to send any discovery multicast packets");
}

let done = Arc::new(AtomicBool::new(false));
let done_ = done.clone();
let _on_drop = OnDrop(|| {
done_.store(true, Ordering::Release);
});

tokio::spawn(async move {
loop {
time::sleep(Duration::from_millis(100)).await;

if done.load(Ordering::Acquire) {
break;
}

send_packet().await;
}
});

let find = async {
let mut buf = [0; 1500];
loop {
Expand Down

0 comments on commit 1cb799e

Please sign in to comment.