aboutsummaryrefslogtreecommitdiff
path: root/Emby.Dlna/Ssdp/DeviceDiscovery.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Dlna/Ssdp/DeviceDiscovery.cs')
-rw-r--r--Emby.Dlna/Ssdp/DeviceDiscovery.cs64
1 files changed, 51 insertions, 13 deletions
diff --git a/Emby.Dlna/Ssdp/DeviceDiscovery.cs b/Emby.Dlna/Ssdp/DeviceDiscovery.cs
index bd5ad31c24..a75e065c30 100644
--- a/Emby.Dlna/Ssdp/DeviceDiscovery.cs
+++ b/Emby.Dlna/Ssdp/DeviceDiscovery.cs
@@ -26,13 +26,38 @@ namespace Emby.Dlna.Ssdp
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
- public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscovered;
+ private event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscoveredInternal;
+
+ private int _listenerCount;
+ private object _syncLock = new object();
+ public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscovered
+ {
+ add
+ {
+ lock (_syncLock)
+ {
+ _listenerCount++;
+ DeviceDiscoveredInternal += value;
+ }
+ StartInternal();
+ }
+ remove
+ {
+ lock (_syncLock)
+ {
+ _listenerCount--;
+ DeviceDiscoveredInternal -= value;
+ }
+ }
+ }
+
public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceLeft;
private SsdpDeviceLocator _deviceLocator;
private readonly ITimerFactory _timerFactory;
private readonly ISocketFactory _socketFactory;
+ private ISsdpCommunicationsServer _commsServer;
public DeviceDiscovery(ILogger logger, IServerConfigurationManager config, ISocketFactory socketFactory, ITimerFactory timerFactory)
{
@@ -45,21 +70,34 @@ namespace Emby.Dlna.Ssdp
// Call this method from somewhere in your code to start the search.
public void Start(ISsdpCommunicationsServer communicationsServer)
{
- _deviceLocator = new SsdpDeviceLocator(communicationsServer, _timerFactory);
+ _commsServer = communicationsServer;
+
+ StartInternal();
+ }
+
+ private void StartInternal()
+ {
+ lock (_syncLock)
+ {
+ if (_listenerCount > 0 && _deviceLocator == null)
+ {
+ _deviceLocator = new SsdpDeviceLocator(_commsServer, _timerFactory);
- // (Optional) Set the filter so we only see notifications for devices we care about
- // (can be any search target value i.e device type, uuid value etc - any value that appears in the
- // DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method).
- //_DeviceLocator.NotificationFilter = "upnp:rootdevice";
+ // (Optional) Set the filter so we only see notifications for devices we care about
+ // (can be any search target value i.e device type, uuid value etc - any value that appears in the
+ // DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method).
+ //_DeviceLocator.NotificationFilter = "upnp:rootdevice";
- // Connect our event handler so we process devices as they are found
- _deviceLocator.DeviceAvailable += deviceLocator_DeviceAvailable;
- _deviceLocator.DeviceUnavailable += _DeviceLocator_DeviceUnavailable;
+ // Connect our event handler so we process devices as they are found
+ _deviceLocator.DeviceAvailable += deviceLocator_DeviceAvailable;
+ _deviceLocator.DeviceUnavailable += _DeviceLocator_DeviceUnavailable;
- var dueTime = TimeSpan.FromSeconds(5);
- var interval = TimeSpan.FromSeconds(_config.GetDlnaConfiguration().ClientDiscoveryIntervalSeconds);
+ var dueTime = TimeSpan.FromSeconds(5);
+ var interval = TimeSpan.FromSeconds(_config.GetDlnaConfiguration().ClientDiscoveryIntervalSeconds);
- _deviceLocator.RestartBroadcastTimer(dueTime, interval);
+ _deviceLocator.RestartBroadcastTimer(dueTime, interval);
+ }
+ }
}
// Process each found device in the event handler
@@ -81,7 +119,7 @@ namespace Emby.Dlna.Ssdp
}
};
- EventHelper.FireEventIfNotNull(DeviceDiscovered, this, args, _logger);
+ EventHelper.FireEventIfNotNull(DeviceDiscoveredInternal, this, args, _logger);
}
private void _DeviceLocator_DeviceUnavailable(object sender, DeviceUnavailableEventArgs e)