mdns
This commit is contained in:
@@ -12,6 +12,7 @@ todo = "warn"
|
||||
unwrap_used = "warn"
|
||||
|
||||
[dependencies]
|
||||
mdns-sd = { workspace = true }
|
||||
eyre = { workspace = true }
|
||||
log = { workspace = true }
|
||||
mdns-sd = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
|
||||
@@ -47,10 +47,7 @@ impl Drop for MdnsAdvertiser {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn discover_service(
|
||||
service_type: &str,
|
||||
instance_name: Option<&str>,
|
||||
) -> eyre::Result<SocketAddr> {
|
||||
pub fn discover_service(service_type: &str) -> eyre::Result<SocketAddr> {
|
||||
let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon.");
|
||||
|
||||
let receiver = mdns.browse(service_type)?;
|
||||
@@ -58,28 +55,27 @@ pub fn discover_service(
|
||||
while let Ok(event) = receiver.recv() {
|
||||
match event {
|
||||
ServiceEvent::ServiceResolved(info) => {
|
||||
tracing::trace!(?info, "mdns ServiceResolved event");
|
||||
log::trace!("mdns ServiceResolved event: {info:?}");
|
||||
|
||||
if let Some(instance_name) = instance_name
|
||||
&& info.get_fullname() != format!("{instance_name}.{service_type}")
|
||||
{
|
||||
tracing::warn!(
|
||||
"Found service with wrong instance name: {} (expected: {})",
|
||||
info.get_fullname(),
|
||||
instance_name,
|
||||
// Check if this service matches our expected service type
|
||||
if info.ty_domain != service_type {
|
||||
log::trace!(
|
||||
"Got mDNS with uninteresting service type: {} (expected: {})",
|
||||
info.ty_domain,
|
||||
service_type,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(address) = info.get_addresses().iter().next() {
|
||||
tracing::info!("Found server at {}:{}", address, info.get_port());
|
||||
log::info!("Found server at {}:{}", address, info.get_port());
|
||||
return Ok((address.to_ip_addr(), info.get_port()).into());
|
||||
}
|
||||
|
||||
tracing::error!(?info, "No address found in mDNS response.");
|
||||
log::error!("No address found in mDNS response: {info:?}");
|
||||
}
|
||||
other_event => {
|
||||
tracing::trace!(?other_event, "mdns unrelated event");
|
||||
log::trace!("mdns unrelated event: {other_event:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,7 +370,7 @@ async fn find_server(app: AppHandle) {
|
||||
log::info!("Looking for server...");
|
||||
|
||||
loop {
|
||||
match discover_service(LANSPREAD_SERVICE_TYPE, None) {
|
||||
match discover_service(LANSPREAD_SERVICE_TYPE) {
|
||||
Ok(server_addr) => {
|
||||
log::info!("Found server at {server_addr}");
|
||||
let state: tauri::State<LanSpreadState> = app.state();
|
||||
|
||||
Reference in New Issue
Block a user