@@ -40,7 +40,7 @@ mod smb_types;
4040mod smb_util;
4141
4242use crate :: ignore_poison:: IgnorePoison ;
43- use log:: { debug, warn } ;
43+ use log:: debug;
4444use serde:: { Deserialize , Serialize } ;
4545use std:: collections:: HashMap ;
4646use std:: sync:: { Mutex , OnceLock } ;
@@ -161,32 +161,43 @@ pub(crate) fn on_discovery_state_changed(new_state: DiscoveryState, app_handle:
161161/// Called by the mDNS discovery module when a host's address is resolved.
162162pub ( crate ) fn on_host_resolved (
163163 host_id : & str ,
164+ name : & str ,
164165 hostname : Option < String > ,
165166 ip_address : Option < String > ,
166167 port : u16 ,
167168 app_handle : & AppHandle ,
168169) {
169170 let mut state = get_discovery_state ( ) . lock_ignore_poison ( ) ;
170171
171- // Update the host with resolved info
172- if let Some ( host) = state. hosts . get_mut ( host_id) {
173- host. hostname = hostname. clone ( ) . or ( host. hostname . clone ( ) ) ;
174- host. ip_address = ip_address. clone ( ) . or ( host. ip_address . clone ( ) ) ;
175- host. port = port;
176-
172+ // If host wasn't seen via ServiceFound (race or library quirk), create it now
173+ if !state. hosts . contains_key ( host_id) {
177174 debug ! (
178- "Host RESOLVED: id={}, hostname={:?}, ip={:?}, port={}" ,
179- host_id, host. hostname, host. ip_address, port
180- ) ;
181-
182- // Emit event to frontend with updated host info
183- let _ = app_handle. emit ( "network-host-resolved" , host. clone ( ) ) ;
184- } else {
185- warn ! (
186- "Host RESOLVED but not found in state: id={}, hostname={:?}, ip={:?}" ,
187- host_id, hostname, ip_address
175+ "Host RESOLVED before FOUND, creating entry: id={}, name={}, hostname={:?}, ip={:?}" ,
176+ host_id, name, hostname, ip_address
188177 ) ;
178+ let host = NetworkHost {
179+ id : host_id. to_string ( ) ,
180+ name : name. to_string ( ) ,
181+ hostname : None ,
182+ ip_address : None ,
183+ port,
184+ } ;
185+ state. hosts . insert ( host_id. to_string ( ) , host. clone ( ) ) ;
186+ let _ = app_handle. emit ( "network-host-found" , & host) ;
189187 }
188+
189+ let host = state. hosts . get_mut ( host_id) . expect ( "just inserted or already present" ) ;
190+ host. hostname = hostname. clone ( ) . or ( host. hostname . clone ( ) ) ;
191+ host. ip_address = ip_address. clone ( ) . or ( host. ip_address . clone ( ) ) ;
192+ host. port = port;
193+
194+ debug ! (
195+ "Host RESOLVED: id={}, hostname={:?}, ip={:?}, port={}" ,
196+ host_id, host. hostname, host. ip_address, port
197+ ) ;
198+
199+ // Emit event to frontend with updated host info
200+ let _ = app_handle. emit ( "network-host-resolved" , host. clone ( ) ) ;
190201}
191202
192203/// Generates a stable ID from a service name.
0 commit comments