diff --git a/MediaBrowser.Controller/Dlna/ISsdpHandler.cs b/MediaBrowser.Controller/Dlna/ISsdpHandler.cs deleted file mode 100644 index ec3a00aad..000000000 --- a/MediaBrowser.Controller/Dlna/ISsdpHandler.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace MediaBrowser.Controller.Dlna -{ - public interface ISsdpHandler - { - } -} diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index a8062af19..36d59d3e4 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -111,7 +111,6 @@ - diff --git a/MediaBrowser.Dlna/MediaBrowser.Dlna.csproj b/MediaBrowser.Dlna/MediaBrowser.Dlna.csproj index ef60be227..ebffe6c57 100644 --- a/MediaBrowser.Dlna/MediaBrowser.Dlna.csproj +++ b/MediaBrowser.Dlna/MediaBrowser.Dlna.csproj @@ -108,7 +108,6 @@ - @@ -130,7 +129,6 @@ - @@ -160,8 +158,6 @@ - - diff --git a/MediaBrowser.Dlna/Ssdp/Datagram.cs b/MediaBrowser.Dlna/Ssdp/Datagram.cs deleted file mode 100644 index 590194534..000000000 --- a/MediaBrowser.Dlna/Ssdp/Datagram.cs +++ /dev/null @@ -1,126 +0,0 @@ -using MediaBrowser.Model.Logging; -using System; -using System.Net; -using System.Net.Sockets; -using System.Text; - -namespace MediaBrowser.Dlna.Ssdp -{ - public class Datagram - { - public EndPoint ToEndPoint { get; private set; } - public EndPoint FromEndPoint { get; private set; } - public string Message { get; private set; } - public bool IsBroadcast { get; private set; } - public bool EnableDebugLogging { get; private set; } - - private readonly ILogger _logger; - - public Datagram(EndPoint toEndPoint, EndPoint fromEndPoint, ILogger logger, string message, bool isBroadcast, bool enableDebugLogging) - { - Message = message; - _logger = logger; - EnableDebugLogging = enableDebugLogging; - IsBroadcast = isBroadcast; - FromEndPoint = fromEndPoint; - ToEndPoint = toEndPoint; - } - - public void Send() - { - var msg = Encoding.ASCII.GetBytes(Message); - - var socket = CreateSocket(); - - if (socket == null) - { - return; - } - - if (FromEndPoint != null) - { - try - { - socket.Bind(FromEndPoint); - } - catch (Exception ex) - { - if (EnableDebugLogging) - { - _logger.ErrorException("Error binding datagram socket", ex); - } - - if (IsBroadcast) - { - CloseSocket(socket, false); - return; - } - } - } - - try - { - socket.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, ToEndPoint, result => - { - try - { - socket.EndSend(result); - } - catch (Exception ex) - { - if (EnableDebugLogging) - { - _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString()); - } - } - finally - { - CloseSocket(socket, true); - } - }, null); - } - catch (Exception ex) - { - _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString()); - CloseSocket(socket, false); - } - } - - private void CloseSocket(Socket socket, bool logError) - { - try - { - socket.Close(); - } - catch (Exception ex) - { - if (logError && EnableDebugLogging) - { - _logger.ErrorException("Error closing datagram socket", ex); - } - } - } - - private Socket CreateSocket() - { - try - { - var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4); - - if (IsBroadcast) - { - socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); - } - - return socket; - } - catch (Exception ex) - { - _logger.ErrorException("Error creating socket", ex); - return null; - } - } - } -} diff --git a/MediaBrowser.Dlna/Ssdp/DeviceDiscoveryInfo.cs b/MediaBrowser.Dlna/Ssdp/DeviceDiscoveryInfo.cs deleted file mode 100644 index e9de45522..000000000 --- a/MediaBrowser.Dlna/Ssdp/DeviceDiscoveryInfo.cs +++ /dev/null @@ -1,21 +0,0 @@ -using MediaBrowser.Dlna.PlayTo; -using System; -using System.Net; - -namespace MediaBrowser.Dlna.Ssdp -{ - public class DeviceDiscoveryInfo - { - public Device Device { get; set; } - - /// - /// The server's ip address that the device responded to - /// - public IPAddress LocalIpAddress { get; set; } - - public Uri Uri { get; set; } - - public string Usn { get; set; } - public string Nt { get; set; } - } -} diff --git a/MediaBrowser.Dlna/Ssdp/Extensions.cs b/MediaBrowser.Dlna/Ssdp/Extensions.cs index 12589e80f..17ebcc7ea 100644 --- a/MediaBrowser.Dlna/Ssdp/Extensions.cs +++ b/MediaBrowser.Dlna/Ssdp/Extensions.cs @@ -9,30 +9,6 @@ namespace MediaBrowser.Dlna.Ssdp { public static class Extensions { - public static Task ReceiveAsync(this Socket socket, byte[] buffer, int offset, int size) - { - var tcs = new TaskCompletionSource(socket); - var remoteip = new IPEndPoint(IPAddress.Any, 0); - var endpoint = (EndPoint)remoteip; - - socket.BeginReceiveFrom(buffer, offset, size, SocketFlags.None, ref endpoint, iar => - { - var result = (TaskCompletionSource)iar.AsyncState; - var iarSocket = (Socket)result.Task.AsyncState; - - try - { - result.TrySetResult(iarSocket.EndReceive(iar)); - } - catch (Exception exc) - { - result.TrySetException(exc); - } - }, tcs); - - return tcs.Task; - } - public static string GetValue(this XElement container, XName name) { var node = container.Element(name); diff --git a/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs b/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs deleted file mode 100644 index 0d0ca98a2..000000000 --- a/MediaBrowser.Dlna/Ssdp/SsdpHandler.cs +++ /dev/null @@ -1,328 +0,0 @@ -using MediaBrowser.Common; -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Controller.Configuration; -using MediaBrowser.Controller.Dlna; -using MediaBrowser.Dlna.Server; -using MediaBrowser.Model.Logging; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Win32; - -namespace MediaBrowser.Dlna.Ssdp -{ - public class SsdpHandler : IDisposable, ISsdpHandler - { - private Socket _multicastSocket; - - private readonly ILogger _logger; - private readonly IServerConfigurationManager _config; - - const string SSDPAddr = "239.255.255.250"; - const int SSDPPort = 1900; - private readonly string _serverSignature; - - private readonly IPAddress _ssdpIp = IPAddress.Parse(SSDPAddr); - private readonly IPEndPoint _ssdpEndp = new IPEndPoint(IPAddress.Parse(SSDPAddr), SSDPPort); - - private Timer _notificationTimer; - - private bool _isDisposed; - private readonly Dictionary> _devices = new Dictionary>(); - - private readonly IApplicationHost _appHost; - - private readonly int _unicastPort = 1901; - private UdpClient _unicastClient; - - public SsdpHandler(ILogger logger, IServerConfigurationManager config, IApplicationHost appHost) - { - _logger = logger; - _config = config; - _appHost = appHost; - - _config.NamedConfigurationUpdated += _config_ConfigurationUpdated; - _serverSignature = GenerateServerSignature(); - } - - private string GenerateServerSignature() - { - var os = Environment.OSVersion; - var pstring = os.Platform.ToString(); - switch (os.Platform) - { - case PlatformID.Win32NT: - case PlatformID.Win32S: - case PlatformID.Win32Windows: - pstring = "WIN"; - break; - } - - return String.Format( - "{0}{1}/{2}.{3} UPnP/1.0 DLNADOC/1.5 Emby/{4}", - pstring, - IntPtr.Size * 8, - os.Version.Major, - os.Version.Minor, - _appHost.ApplicationVersion - ); - } - - void _config_ConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e) - { - if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase)) - { - ReloadAliveNotifier(); - } - } - - public IEnumerable RegisteredDevices - { - get - { - lock (_devices) - { - var devices = _devices.ToList(); - - return devices.SelectMany(i => i.Value).ToList(); - } - } - } - - public void Start() - { - DisposeSocket(); - StopAliveNotifier(); - - RestartSocketListener(); - ReloadAliveNotifier(); - - SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; - SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; - } - - void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) - { - if (e.Mode == PowerModes.Resume) - { - Start(); - } - } - - public async void SendDatagram(string msg, - EndPoint endpoint, - EndPoint localAddress, - bool isBroadcast, - int sendCount = 3) - { - var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLog; - - for (var i = 0; i < sendCount; i++) - { - if (i > 0) - { - await Task.Delay(500).ConfigureAwait(false); - } - - var dgram = new Datagram(endpoint, localAddress, _logger, msg, isBroadcast, enableDebugLogging); - dgram.Send(); - } - } - - private void RestartSocketListener() - { - if (_isDisposed) - { - return; - } - - try - { - _multicastSocket = CreateMulticastSocket(); - - _logger.Info("MultiCast socket created"); - } - catch (Exception ex) - { - _logger.ErrorException("Error creating MultiCast socket", ex); - //StartSocketRetryTimer(); - } - } - - public void Dispose() - { - _config.NamedConfigurationUpdated -= _config_ConfigurationUpdated; - SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; - - _isDisposed = true; - - DisposeSocket(); - StopAliveNotifier(); - } - - private void DisposeSocket() - { - if (_multicastSocket != null) - { - _multicastSocket.Close(); - _multicastSocket.Dispose(); - _multicastSocket = null; - } - } - - private Socket CreateMulticastSocket() - { - var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); - socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4); - socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(_ssdpIp, 0)); - - socket.Bind(new IPEndPoint(IPAddress.Any, SSDPPort)); - - return socket; - } - - private void NotifyAll() - { - var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLog; - - if (enableDebugLogging) - { - _logger.Debug("Sending alive notifications"); - } - foreach (var d in RegisteredDevices) - { - NotifyDevice(d, "alive", enableDebugLogging); - } - } - - private void NotifyDevice(UpnpDevice dev, string type, bool logMessage) - { - const string header = "NOTIFY * HTTP/1.1"; - - var values = new Dictionary(StringComparer.OrdinalIgnoreCase); - - // If needed later for non-server devices, these headers will need to be dynamic - values["HOST"] = "239.255.255.250:1900"; - values["CACHE-CONTROL"] = "max-age = 600"; - values["LOCATION"] = dev.Descriptor.ToString(); - values["SERVER"] = _serverSignature; - values["NTS"] = "ssdp:" + type; - values["NT"] = dev.Type; - values["USN"] = dev.USN; - - if (logMessage) - { - _logger.Debug("{0} said {1}", dev.USN, type); - } - - var msg = new SsdpMessageBuilder().BuildMessage(header, values); - - SendDatagram(msg, _ssdpEndp, new IPEndPoint(dev.Address, 0), true, 2); - //SendUnicastRequest(msg, 1); - } - - public void RegisterNotification(string uuid, Uri descriptionUri, IPAddress address, IEnumerable services) - { - lock (_devices) - { - List list; - List dl; - if (_devices.TryGetValue(uuid, out dl)) - { - list = dl; - } - else - { - list = new List(); - _devices[uuid] = list; - } - - list.AddRange(services.Select(i => new UpnpDevice(uuid, i, descriptionUri, address))); - - NotifyAll(); - _logger.Debug("Registered mount {0} at {1}", uuid, descriptionUri); - } - } - - public void UnregisterNotification(string uuid) - { - lock (_devices) - { - List dl; - if (_devices.TryGetValue(uuid, out dl)) - { - _devices.Remove(uuid); - foreach (var d in dl.ToList()) - { - NotifyDevice(d, "byebye", true); - } - - _logger.Debug("Unregistered mount {0}", uuid); - } - } - } - - private readonly object _notificationTimerSyncLock = new object(); - private int _aliveNotifierIntervalMs; - private void ReloadAliveNotifier() - { - var config = _config.GetDlnaConfiguration(); - - if (!config.BlastAliveMessages) - { - StopAliveNotifier(); - return; - } - - var intervalMs = config.BlastAliveMessageIntervalSeconds * 1000; - - if (_notificationTimer == null || _aliveNotifierIntervalMs != intervalMs) - { - lock (_notificationTimerSyncLock) - { - if (_notificationTimer == null) - { - _logger.Debug("Starting alive notifier"); - const int initialDelayMs = 3000; - _notificationTimer = new Timer(state => NotifyAll(), null, initialDelayMs, intervalMs); - } - else - { - _logger.Debug("Updating alive notifier"); - _notificationTimer.Change(intervalMs, intervalMs); - } - - _aliveNotifierIntervalMs = intervalMs; - } - } - } - - private void StopAliveNotifier() - { - lock (_notificationTimerSyncLock) - { - if (_notificationTimer != null) - { - _logger.Debug("Stopping alive notifier"); - _notificationTimer.Dispose(); - _notificationTimer = null; - } - } - } - - public class UdpState - { - public UdpClient UdpClient; - public IPEndPoint EndPoint; - } - } -} diff --git a/MediaBrowser.Dlna/Ssdp/SsdpMessageBuilder.cs b/MediaBrowser.Dlna/Ssdp/SsdpMessageBuilder.cs deleted file mode 100644 index e479ca19a..000000000 --- a/MediaBrowser.Dlna/Ssdp/SsdpMessageBuilder.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Collections.Generic; -using System.Text; - -namespace MediaBrowser.Dlna.Ssdp -{ - public class SsdpMessageBuilder - { - public string BuildMessage(string header, Dictionary values) - { - var builder = new StringBuilder(); - - const string argFormat = "{0}: {1}\r\n"; - - builder.AppendFormat("{0}\r\n", header); - - foreach (var pair in values) - { - builder.AppendFormat(argFormat, pair.Key, pair.Value); - } - - builder.Append("\r\n"); - - return builder.ToString(); - } - } -}