Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix ssdp hanging if it can't find any packets in time
  • Loading branch information
Sticks committed Mar 6, 2024
1 parent 0fe7688 commit 3d3b996
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
45 changes: 31 additions & 14 deletions src-tauri/src/handlers/bambu/mod.rs
Expand Up @@ -617,21 +617,35 @@ impl BambuClient {
for listener in ssdp_listeners {
println!("[BambuClient::get_device_ips] Running SSDP Discovery ...");

let messages = listener.listen(Duration::from_secs(5)).await.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"[BambuClient::get_device_ips] Failed to listen for SSDP messages: {}",
// Wrap in a timeout to ensure we don't hang forever
let messages = tokio::time::timeout(
Duration::from_secs(5),
listener.listen(Duration::from_secs(5)),
);

match messages.await {
Ok(Ok(messages)) => {
println!(
"[BambuClient::get_device_ips] Successfully discovered {} SSDP messages.",
messages.len()
);
ssdp_messages.extend(messages);
}
Ok(Err(e)) => {
println!(
"[BambuClient::get_device_ips] Failed to discover SSDP messages: {}",
e
),
)
})?;
);
}
Err(_) => {
println!(
"[BambuClient::get_device_ips] Timed out while discovering SSDP messages."
);

ssdp_messages.extend(messages);
println!(
"[BambuClient::get_device_ips] Found {} SSDP messages so far ...",
ssdp_messages.len(),
);
// Continue to the next listener
continue;
}
}
}

// de-dupe the messages by location
Expand All @@ -647,7 +661,10 @@ impl BambuClient {

if unique_messages.len() == 0 {
println!("[BambuClient::get_device_ips] No unique messages found. Exiting ...");
return Ok(vec![]);
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"No unique messages found during SSDP discovery. Please ensure your devices are connected to the network and try again.",
));
}

println!(
Expand Down
23 changes: 17 additions & 6 deletions src-tauri/src/handlers/ssdp/mod.rs
Expand Up @@ -93,6 +93,10 @@ impl SsdpListener {
) -> Result<Vec<SsdpMessage>, Box<dyn std::error::Error>> {
// Create a UDP socket bound to the specified port
let socket = UdpSocket::bind(format!("0.0.0.0:{}", self.port))?;
socket.set_read_timeout(Some(duration)).map_err(|e| {
eprintln!("Error setting read timeout: {}", e);
e
})?;

// Join the SSDP multicast group
socket.join_multicast_v4(
Expand All @@ -113,15 +117,22 @@ impl SsdpListener {

// Receive messages until the specified duration elapses
while Instant::now() - start_time < duration {
match socket.recv_from(&mut buf[..]) {
match socket.recv_from(&mut buf) {
Ok((size, _)) => {
// Parse and handle the SSDP NOTIFY message
let message = std::str::from_utf8(&buf[..size])?.to_string();
messages.push(SsdpMessage::from_message(&message)?);
println!("Received message of size {}", size);
let message = std::str::from_utf8(&buf[..size])?;
let ssdp_message = SsdpMessage::from_message(message)?;
messages.push(ssdp_message);
}
Err(e) => {
eprintln!("Error receiving SSDP message: {}", e);
break; // Exit loop on error
if e.kind() == std::io::ErrorKind::WouldBlock
|| e.kind() == std::io::ErrorKind::TimedOut
{
println!("No more messages received within the specified duration");
break;
} else {
eprintln!("Error receiving message: {}", e);
}
}
}
}
Expand Down

0 comments on commit 3d3b996

Please sign in to comment.