2014-03-24 12:47:39 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using System.Threading;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.Server
|
|
|
|
|
{
|
|
|
|
|
public class SsdpHandler : IDisposable
|
|
|
|
|
{
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private readonly AutoResetEvent _datagramPosted = new AutoResetEvent(false);
|
|
|
|
|
private readonly ConcurrentQueue<Datagram> _messageQueue = new ConcurrentQueue<Datagram>();
|
|
|
|
|
|
2014-03-24 12:47:39 +00:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly string _serverSignature;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private bool _isDisposed;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
const string SSDPAddr = "239.255.255.250";
|
|
|
|
|
const int SSDPPort = 1900;
|
|
|
|
|
|
|
|
|
|
private readonly IPEndPoint _ssdpEndp = new IPEndPoint(IPAddress.Parse(SSDPAddr), SSDPPort);
|
|
|
|
|
private readonly IPAddress _ssdpIp = IPAddress.Parse(SSDPAddr);
|
|
|
|
|
|
|
|
|
|
private UdpClient _udpClient;
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<Guid, List<UpnpDevice>> _devices = new Dictionary<Guid, List<UpnpDevice>>();
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private Timer _queueTimer;
|
|
|
|
|
private Timer _notificationTimer;
|
|
|
|
|
|
2014-03-24 12:47:39 +00:00
|
|
|
|
public SsdpHandler(ILogger logger, IServerConfigurationManager config, string serverSignature)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
|
|
|
|
_serverSignature = serverSignature;
|
|
|
|
|
|
|
|
|
|
Start();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-18 05:03:01 +00:00
|
|
|
|
public IEnumerable<UpnpDevice> Devices
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
UpnpDevice[] devs;
|
|
|
|
|
lock (_devices)
|
|
|
|
|
{
|
|
|
|
|
devs = _devices.Values.SelectMany(i => i).ToArray();
|
|
|
|
|
}
|
|
|
|
|
return devs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
_udpClient = new UdpClient();
|
|
|
|
|
_udpClient.Client.UseOnlyOverlappedIO = true;
|
|
|
|
|
_udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
|
_udpClient.ExclusiveAddressUse = false;
|
|
|
|
|
_udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, SSDPPort));
|
|
|
|
|
_udpClient.JoinMulticastGroup(_ssdpIp, 2);
|
|
|
|
|
_logger.Info("SSDP service started");
|
|
|
|
|
Receive();
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
|
|
|
|
StartNotificationTimer();
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Receive()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_udpClient.BeginReceive(ReceiveCallback, null);
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReceiveCallback(IAsyncResult result)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var endpoint = new IPEndPoint(IPAddress.None, SSDPPort);
|
|
|
|
|
var received = _udpClient.EndReceive(result, ref endpoint);
|
|
|
|
|
|
|
|
|
|
if (_config.Configuration.DlnaOptions.EnableDebugLogging)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} - SSDP Received a datagram", endpoint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var reader = new StreamReader(new MemoryStream(received), Encoding.ASCII))
|
|
|
|
|
{
|
|
|
|
|
var proto = (reader.ReadLine() ?? string.Empty).Trim();
|
|
|
|
|
var method = proto.Split(new[] { ' ' }, 2)[0];
|
|
|
|
|
var headers = new Headers();
|
|
|
|
|
for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
|
|
|
|
|
{
|
|
|
|
|
line = line.Trim();
|
|
|
|
|
if (string.IsNullOrEmpty(line))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-03-25 05:25:03 +00:00
|
|
|
|
var parts = line.Split(new[] { ':' }, 2);
|
2014-04-20 05:21:08 +00:00
|
|
|
|
|
|
|
|
|
if (parts.Length >= 2)
|
|
|
|
|
{
|
|
|
|
|
headers[parts[0]] = parts[1].Trim();
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_config.Configuration.DlnaOptions.EnableDebugLogging)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} - Datagram method: {1}", endpoint, method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.Equals(method, "M-SEARCH", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
RespondToSearch(endpoint, headers["st"]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Failed to read SSDP message", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_isDisposed)
|
|
|
|
|
{
|
|
|
|
|
Receive();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RespondToSearch(IPEndPoint endpoint, string req)
|
|
|
|
|
{
|
2014-04-19 17:43:12 +00:00
|
|
|
|
if (string.Equals(req, "ssdp:all", StringComparison.OrdinalIgnoreCase))
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
req = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_config.Configuration.DlnaOptions.EnableDebugLogging)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("RespondToSearch");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var d in Devices)
|
|
|
|
|
{
|
2014-04-15 03:54:52 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(req) && !string.Equals(req, d.Type, StringComparison.OrdinalIgnoreCase))
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SendSearchResponse(endpoint, d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendSearchResponse(IPEndPoint endpoint, UpnpDevice dev)
|
|
|
|
|
{
|
2014-04-20 05:21:08 +00:00
|
|
|
|
var builder = new StringBuilder();
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-20 05:21:08 +00:00
|
|
|
|
const string argFormat = "{0}: {1}\r\n";
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-20 05:21:08 +00:00
|
|
|
|
builder.Append("HTTP/1.1 200 OK\r\n");
|
|
|
|
|
builder.AppendFormat(argFormat, "CACHE-CONTROL", "max-age = 600");
|
|
|
|
|
builder.AppendFormat(argFormat, "DATE", DateTime.Now.ToString("R"));
|
|
|
|
|
builder.AppendFormat(argFormat, "EXT", "");
|
|
|
|
|
builder.AppendFormat(argFormat, "LOCATION", dev.Descriptor);
|
|
|
|
|
builder.AppendFormat(argFormat, "SERVER", _serverSignature);
|
|
|
|
|
builder.AppendFormat(argFormat, "ST", dev.Type);
|
|
|
|
|
builder.AppendFormat(argFormat, "USN", dev.USN);
|
|
|
|
|
builder.Append("\r\n");
|
|
|
|
|
|
|
|
|
|
SendDatagram(endpoint, dev.Address, builder.ToString(), false);
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-19 17:43:12 +00:00
|
|
|
|
_logger.Info("{1} - Responded to a {0} request to {2}", dev.Type, endpoint, dev.Address.ToString());
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private void SendDatagram(IPEndPoint endpoint, IPAddress localAddress, string msg, bool sticky)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (_isDisposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
|
|
|
|
var dgram = new Datagram(endpoint, localAddress, _logger, msg, sticky);
|
|
|
|
|
if (_messageQueue.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
dgram.Send();
|
|
|
|
|
}
|
|
|
|
|
_messageQueue.Enqueue(dgram);
|
|
|
|
|
StartQueueTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void QueueTimerCallback(object state)
|
|
|
|
|
{
|
|
|
|
|
while (_messageQueue.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
Datagram msg;
|
|
|
|
|
if (!_messageQueue.TryPeek(out msg))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg != null && (!_isDisposed || msg.Sticky))
|
|
|
|
|
{
|
|
|
|
|
msg.Send();
|
|
|
|
|
if (msg.SendCount > 2)
|
|
|
|
|
{
|
|
|
|
|
_messageQueue.TryDequeue(out msg);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_messageQueue.TryDequeue(out msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_datagramPosted.Set();
|
|
|
|
|
|
|
|
|
|
if (_messageQueue.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
StartQueueTimer();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DisposeQueueTimer();
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NotifyAll()
|
|
|
|
|
{
|
2014-04-21 16:02:30 +00:00
|
|
|
|
if (_config.Configuration.DlnaOptions.EnableDebugLogging)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Sending alive notifications");
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
foreach (var d in Devices)
|
|
|
|
|
{
|
|
|
|
|
NotifyDevice(d, "alive", false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NotifyDevice(UpnpDevice dev, string type, bool sticky)
|
|
|
|
|
{
|
2014-04-20 05:21:08 +00:00
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
const string argFormat = "{0}: {1}\r\n";
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-20 05:21:08 +00:00
|
|
|
|
builder.Append("NOTIFY * HTTP/1.1\r\n{0}\r\n");
|
|
|
|
|
builder.AppendFormat(argFormat, "HOST", "239.255.255.250:1900");
|
|
|
|
|
builder.AppendFormat(argFormat, "CACHE-CONTROL", "max-age = 600");
|
|
|
|
|
builder.AppendFormat(argFormat, "LOCATION", dev.Descriptor);
|
|
|
|
|
builder.AppendFormat(argFormat, "SERVER", _serverSignature);
|
|
|
|
|
builder.AppendFormat(argFormat, "NTS", "ssdp:" + type);
|
|
|
|
|
builder.AppendFormat(argFormat, "NT", dev.Type);
|
|
|
|
|
builder.AppendFormat(argFormat, "USN", dev.USN);
|
|
|
|
|
builder.Append("\r\n");
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-21 16:02:30 +00:00
|
|
|
|
if (_config.Configuration.DlnaOptions.EnableDebugLogging)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} said {1}", dev.USN, type);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-20 05:21:08 +00:00
|
|
|
|
SendDatagram(_ssdpEndp, dev.Address, builder.ToString(), sticky);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
public void RegisterNotification(Guid uuid, Uri descriptor, IPAddress address)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
List<UpnpDevice> list;
|
|
|
|
|
lock (_devices)
|
|
|
|
|
{
|
2014-04-10 15:06:54 +00:00
|
|
|
|
if (!_devices.TryGetValue(uuid, out list))
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-10 15:06:54 +00:00
|
|
|
|
_devices.Add(uuid, list = new List<UpnpDevice>());
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 03:54:52 +00:00
|
|
|
|
foreach (var t in new[]
|
|
|
|
|
{
|
|
|
|
|
"upnp:rootdevice",
|
|
|
|
|
"urn:schemas-upnp-org:device:MediaServer:1",
|
|
|
|
|
"urn:schemas-upnp-org:service:ContentDirectory:1",
|
|
|
|
|
"uuid:" + uuid
|
|
|
|
|
})
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-10 15:06:54 +00:00
|
|
|
|
list.Add(new UpnpDevice(uuid, t, descriptor, address));
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NotifyAll();
|
2014-04-15 03:54:52 +00:00
|
|
|
|
_logger.Debug("Registered mount {0} at {1}", uuid, descriptor);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private void UnregisterNotification(Guid uuid)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
List<UpnpDevice> dl;
|
|
|
|
|
lock (_devices)
|
|
|
|
|
{
|
2014-04-10 15:06:54 +00:00
|
|
|
|
if (!_devices.TryGetValue(uuid, out dl))
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
_devices.Remove(uuid);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
foreach (var d in dl)
|
|
|
|
|
{
|
|
|
|
|
NotifyDevice(d, "byebye", true);
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
_logger.Debug("Unregistered mount {0}", uuid);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_isDisposed = true;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
while (_messageQueue.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
_datagramPosted.WaitOne();
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
_udpClient.DropMulticastGroup(_ssdpIp);
|
|
|
|
|
_udpClient.Close();
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
DisposeNotificationTimer();
|
|
|
|
|
DisposeQueueTimer();
|
|
|
|
|
_datagramPosted.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly object _queueTimerSyncLock = new object();
|
|
|
|
|
private void StartQueueTimer()
|
|
|
|
|
{
|
|
|
|
|
lock (_queueTimerSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (_queueTimer == null)
|
|
|
|
|
{
|
|
|
|
|
_queueTimer = new Timer(QueueTimerCallback, null, 1000, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_queueTimer.Change(1000, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeQueueTimer()
|
|
|
|
|
{
|
|
|
|
|
lock (_queueTimerSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (_queueTimer != null)
|
|
|
|
|
{
|
|
|
|
|
_queueTimer.Dispose();
|
|
|
|
|
_queueTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly object _notificationTimerSyncLock = new object();
|
|
|
|
|
private void StartNotificationTimer()
|
|
|
|
|
{
|
2014-04-20 05:21:08 +00:00
|
|
|
|
if (!_config.Configuration.DlnaOptions.BlastAliveMessages)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var intervalMs = _config.Configuration.DlnaOptions.BlastAliveMessageIntervalSeconds * 1000;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
|
|
|
|
lock (_notificationTimerSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (_notificationTimer == null)
|
|
|
|
|
{
|
|
|
|
|
_notificationTimer = new Timer(state => NotifyAll(), null, intervalMs, intervalMs);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_notificationTimer.Change(intervalMs, intervalMs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeNotificationTimer()
|
|
|
|
|
{
|
|
|
|
|
lock (_notificationTimerSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (_notificationTimer != null)
|
|
|
|
|
{
|
|
|
|
|
_notificationTimer.Dispose();
|
|
|
|
|
_notificationTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|