From 1bb287ccff73901b48fc089bca68f510ede9e5be Mon Sep 17 00:00:00 2001 From: nfnty Date: Thu, 21 Apr 2016 08:02:23 +0200 Subject: [PATCH] Dlna: Stop SSDP if disabled, Fixes #1578 --- MediaBrowser.Dlna/Main/DlnaEntryPoint.cs | 49 ++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs b/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs index 37584f006..5e8be1c4c 100644 --- a/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs +++ b/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs @@ -39,6 +39,7 @@ namespace MediaBrowser.Dlna.Main private readonly IDeviceDiscovery _deviceDiscovery; private readonly List _registeredServerIds = new List(); + private bool _ssdpHandlerStarted; private bool _dlnaServerStarted; public DlnaEntryPoint(IServerConfigurationManager config, @@ -75,12 +76,22 @@ namespace MediaBrowser.Dlna.Main public void Run() { - StartSsdpHandler(); ReloadComponents(); + _config.ConfigurationUpdated += _config_ConfigurationUpdated; _config.NamedConfigurationUpdated += _config_NamedConfigurationUpdated; } + private bool _lastEnableUPnP; + void _config_ConfigurationUpdated(object sender, EventArgs e) + { + if (_lastEnableUPnP != _config.Configuration.EnableUPnP) + { + ReloadComponents(); + } + _lastEnableUPnP = _config.Configuration.EnableUPnP; + } + void _config_NamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e) { if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase)) @@ -91,10 +102,24 @@ namespace MediaBrowser.Dlna.Main private void ReloadComponents() { - var isServerStarted = _dlnaServerStarted; - var options = _config.GetDlnaConfiguration(); + if (!options.EnableServer && !options.EnablePlayTo && !_config.Configuration.EnableUPnP) + { + if (_ssdpHandlerStarted) + { + StopSsdpHandler(); + } + return; + } + + if (!_ssdpHandlerStarted) + { + StartSsdpHandler(); + } + + var isServerStarted = _dlnaServerStarted; + if (options.EnableServer && !isServerStarted) { StartDlnaServer(); @@ -123,6 +148,8 @@ namespace MediaBrowser.Dlna.Main _ssdpHandler.Start(); ((DeviceDiscovery)_deviceDiscovery).Start(_ssdpHandler); + + _ssdpHandlerStarted = true; } catch (Exception ex) { @@ -130,6 +157,22 @@ namespace MediaBrowser.Dlna.Main } } + private void StopSsdpHandler() + { + try + { + ((DeviceDiscovery)_deviceDiscovery).Dispose(); + + _ssdpHandler.Dispose(); + + _ssdpHandlerStarted = false; + } + catch (Exception ex) + { + _logger.ErrorException("Error stopping ssdp handlers", ex); + } + } + public void StartDlnaServer() { try