@@ -120,6 +120,8 @@ public actor OcaConnectionBroker {
120120 case deviceAdded
121121 /// A device has been removed from the network or is no longer available
122122 case deviceRemoved
123+ /// An existing device has been updated (e.g. renamed via DNS-SD)
124+ case deviceUpdated
123125 /// The connection state of a device has changed
124126 case connectionStateChanged( Ocp1ConnectionState )
125127 }
@@ -333,10 +335,12 @@ public actor OcaConnectionBroker {
333335 return
334336 }
335337
336- let event = Event ( eventType: . deviceAdded, deviceIdentifier: deviceIdentifier)
337338 let device = DeviceInfo ( deviceIdentifier: deviceIdentifier, serviceInfo: serviceInfo)
339+ let isExistingDevice =
340+ _devices [ deviceIdentifier] != nil || _connections [ deviceIdentifier] != nil
338341
339342 if let existingDevice = _devices [ deviceIdentifier] {
343+ // Device still in _devices (add arrived before remove, or address change)
340344 try await withDeviceConnection ( deviceIdentifier) { existingConnection in
341345 if ( try ? existingDevice. addresses) != ( try ? device. addresses) ,
342346 let mutableConnection = existingConnection as? Ocp1MutableConnection
@@ -351,9 +355,22 @@ public actor OcaConnectionBroker {
351355 try await existingConnection. set ( options: _connectionOptions)
352356 }
353357 }
358+ } else if _connections [ deviceIdentifier] != nil {
359+ // Device removed from _devices (e.g. DNS-SD name change) but connection persists;
360+ // re-enable automatic reconnect
361+ try ? await withDeviceConnection ( deviceIdentifier) { existingConnection in
362+ if _connectionOptions. flags. contains ( . automaticReconnect) ,
363+ await !existingConnection. options. flags. contains ( . automaticReconnect)
364+ {
365+ try await existingConnection. set ( options: _connectionOptions)
366+ }
367+ }
354368 }
355369
356370 _devices [ deviceIdentifier] = device
371+
372+ let eventType : EventType = isExistingDevice ? . deviceUpdated : . deviceAdded
373+ let event = Event ( eventType: eventType, deviceIdentifier: deviceIdentifier)
357374 _eventsContinuation. yield ( event)
358375 }
359376
0 commit comments