From 51899f1880ebdd7ae05fdfe501476cdf68f713f2 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 28 Jan 2016 21:59:00 -0500 Subject: [PATCH] fixes #1218 - Dlna server doesn't work after resuming to sleep. --- MediaBrowser.Dlna/Ssdp/SsdpHandler.cs | 130 +++----------------------- 1 file changed, 15 insertions(+), 115 deletions(-) diff --git a/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs b/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs index 32c369a8ff..f800a12c1f 100644 --- a/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs +++ b/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs @@ -33,12 +33,8 @@ namespace MediaBrowser.Dlna.Ssdp private readonly IPAddress _ssdpIp = IPAddress.Parse(SSDPAddr); private readonly IPEndPoint _ssdpEndp = new IPEndPoint(IPAddress.Parse(SSDPAddr), SSDPPort); - private Timer _queueTimer; private Timer _notificationTimer; - private readonly AutoResetEvent _datagramPosted = new AutoResetEvent(false); - private readonly ConcurrentQueue _messageQueue = new ConcurrentQueue(); - private bool _isDisposed; private readonly ConcurrentDictionary> _devices = new ConcurrentDictionary>(); @@ -121,9 +117,13 @@ namespace MediaBrowser.Dlna.Ssdp public void Start() { - RestartSocketListener(); + DisposeSocket(); + StopAliveNotifier(); + RestartSocketListener(); ReloadAliveNotifier(); + + SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; } @@ -131,7 +131,7 @@ namespace MediaBrowser.Dlna.Ssdp { if (e.Mode == PowerModes.Resume) { - NotifyAll(); + Start(); } } @@ -154,7 +154,7 @@ namespace MediaBrowser.Dlna.Ssdp SendDatagram("M-SEARCH * HTTP/1.1", values, _ssdpEndp, localIp, true, 2); } - public void SendDatagram(string header, + public async void SendDatagram(string header, Dictionary values, EndPoint endpoint, EndPoint localAddress, @@ -162,28 +162,18 @@ namespace MediaBrowser.Dlna.Ssdp int sendCount) { var msg = new SsdpMessageBuilder().BuildMessage(header, values); - var queued = false; var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLogging; for (var i = 0; i < sendCount; i++) { - var dgram = new Datagram(endpoint, localAddress, _logger, msg, isBroadcast, enableDebugLogging); - - if (_messageQueue.Count == 0) + if (i > 0) { - dgram.Send(); + await Task.Delay(500).ConfigureAwait(false); } - else - { - _messageQueue.Enqueue(dgram); - queued = true; - } - } - if (queued) - { - StartQueueTimer(); + var dgram = new Datagram(endpoint, localAddress, _logger, msg, isBroadcast, enableDebugLogging); + dgram.Send(); } } @@ -254,47 +244,10 @@ namespace MediaBrowser.Dlna.Ssdp } } - private readonly object _queueTimerSyncLock = new object(); - private void StartQueueTimer() - { - lock (_queueTimerSyncLock) - { - if (_queueTimer == null) - { - _queueTimer = new Timer(QueueTimerCallback, null, 500, Timeout.Infinite); - } - else - { - _queueTimer.Change(500, Timeout.Infinite); - } - } - } - - private void QueueTimerCallback(object state) - { - Datagram msg; - while (_messageQueue.TryDequeue(out msg)) - { - msg.Send(); - } - - _datagramPosted.Set(); - - if (_messageQueue.Count > 0) - { - StartQueueTimer(); - } - else - { - DisposeQueueTimer(); - } - } - private void RestartSocketListener() { if (_isDisposed) { - StopSocketRetryTimer(); return; } @@ -304,8 +257,6 @@ namespace MediaBrowser.Dlna.Ssdp _logger.Info("MultiCast socket created"); - StopSocketRetryTimer(); - Receive(); } catch (Exception ex) @@ -315,31 +266,6 @@ namespace MediaBrowser.Dlna.Ssdp } } - private Timer _socketRetryTimer; - private readonly object _socketRetryLock = new object(); - private void StartSocketRetryTimer() - { - lock (_socketRetryLock) - { - if (_socketRetryTimer == null) - { - _socketRetryTimer = new Timer(s => RestartSocketListener(), null, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30)); - } - } - } - - private void StopSocketRetryTimer() - { - lock (_socketRetryLock) - { - if (_socketRetryTimer != null) - { - _socketRetryTimer.Dispose(); - _socketRetryTimer = null; - } - } - } - private void Receive() { try @@ -448,16 +374,9 @@ namespace MediaBrowser.Dlna.Ssdp SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; _isDisposed = true; - while (_messageQueue.Count != 0) - { - _datagramPosted.WaitOne(); - } DisposeSocket(); - DisposeQueueTimer(); - DisposeNotificationTimer(); - - _datagramPosted.Dispose(); + StopAliveNotifier(); } private void DisposeSocket() @@ -470,18 +389,6 @@ namespace MediaBrowser.Dlna.Ssdp } } - private void DisposeQueueTimer() - { - lock (_queueTimerSyncLock) - { - if (_queueTimer != null) - { - _queueTimer.Dispose(); - _queueTimer = null; - } - } - } - private Socket CreateMulticastSocket() { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); @@ -534,14 +441,7 @@ namespace MediaBrowser.Dlna.Ssdp public void RegisterNotification(Guid uuid, Uri descriptionUri, IPAddress address, IEnumerable services) { - List list; - lock (_devices) - { - if (!_devices.TryGetValue(uuid, out list)) - { - _devices.TryAdd(uuid, list = new List()); - } - } + var list = _devices.GetOrAdd(uuid, new List()); list.AddRange(services.Select(i => new UpnpDevice(uuid, i, descriptionUri, address))); @@ -572,7 +472,7 @@ namespace MediaBrowser.Dlna.Ssdp if (!config.BlastAliveMessages) { - DisposeNotificationTimer(); + StopAliveNotifier(); return; } @@ -599,7 +499,7 @@ namespace MediaBrowser.Dlna.Ssdp } } - private void DisposeNotificationTimer() + private void StopAliveNotifier() { lock (_notificationTimerSyncLock) {