From 72aaecb27930e04aa356febf35db2372bc417166 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 4 Nov 2016 14:56:47 -0400 Subject: move classes to new server project --- Emby.Common.Implementations/Net/SocketFactory.cs | 30 +++++++++++++++++-- Emby.Common.Implementations/Net/UdpSocket.cs | 38 +++++++++++++----------- 2 files changed, 47 insertions(+), 21 deletions(-) (limited to 'Emby.Common.Implementations/Net') diff --git a/Emby.Common.Implementations/Net/SocketFactory.cs b/Emby.Common.Implementations/Net/SocketFactory.cs index 3a2cea12a7..6f0ff29961 100644 --- a/Emby.Common.Implementations/Net/SocketFactory.cs +++ b/Emby.Common.Implementations/Net/SocketFactory.cs @@ -36,13 +36,37 @@ namespace Emby.Common.Implementations.Net #region ISocketFactory Members + /// + /// Creates a new UDP socket and binds it to the specified local port. + /// + /// An integer specifying the local port to bind the socket to. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] + public IUdpSocket CreateUdpSocket(int localPort) + { + if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort"); + + var retVal = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); + try + { + retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + return new UdpSocket(retVal, localPort, _LocalIP); + } + catch + { + if (retVal != null) + retVal.Dispose(); + + throw; + } + } + /// /// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port. /// /// An integer specifying the local port to bind the socket to. /// An implementation of the interface used by RSSDP components to perform socket operations. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] - public IUdpSocket CreateUdpSocket(int localPort) + public IUdpSocket CreateSsdpUdpSocket(int localPort) { if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort"); @@ -52,7 +76,7 @@ namespace Emby.Common.Implementations.Net retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4); retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), _LocalIP)); - return new UdpSocket(retVal, localPort, _LocalIP.ToString()); + return new UdpSocket(retVal, localPort, _LocalIP); } catch { @@ -97,7 +121,7 @@ namespace Emby.Common.Implementations.Net retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(ipAddress), _LocalIP)); retVal.MulticastLoopback = true; - return new UdpSocket(retVal, localPort, _LocalIP.ToString()); + return new UdpSocket(retVal, localPort, _LocalIP); } catch { diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs index 86ce9c83be..997d3f25fa 100644 --- a/Emby.Common.Implementations/Net/UdpSocket.cs +++ b/Emby.Common.Implementations/Net/UdpSocket.cs @@ -24,19 +24,13 @@ namespace Emby.Common.Implementations.Net #region Constructors - public UdpSocket(System.Net.Sockets.Socket socket, int localPort, string ipAddress) + public UdpSocket(System.Net.Sockets.Socket socket, int localPort, IPAddress ip) { if (socket == null) throw new ArgumentNullException("socket"); _Socket = socket; _LocalPort = localPort; - IPAddress ip = null; - if (String.IsNullOrEmpty(ipAddress)) - ip = IPAddress.Any; - else - ip = IPAddress.Parse(ipAddress); - _Socket.Bind(new IPEndPoint(ip, _LocalPort)); if (_LocalPort == 0) _LocalPort = (_Socket.LocalEndPoint as IPEndPoint).Port; @@ -46,11 +40,11 @@ namespace Emby.Common.Implementations.Net #region IUdpSocket Members - public Task ReceiveAsync() + public Task ReceiveAsync() { ThrowIfDisposed(); - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(); System.Net.EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0); var state = new AsyncReceiveState(_Socket, receivedFromEndPoint); @@ -74,22 +68,30 @@ namespace Emby.Common.Implementations.Net return tcs.Task; } - public Task SendTo(byte[] messageData, IpEndPointInfo endPoint) + public Task SendAsync(byte[] buffer, int size, IpEndPointInfo endPoint) { ThrowIfDisposed(); - if (messageData == null) throw new ArgumentNullException("messageData"); + if (buffer == null) throw new ArgumentNullException("messageData"); if (endPoint == null) throw new ArgumentNullException("endPoint"); #if NETSTANDARD1_6 - _Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port)); + + if (size != buffer.Length) + { + byte[] copy = new byte[size]; + Buffer.BlockCopy(buffer, 0, copy, 0, size); + buffer = copy; + } + + _Socket.SendTo(buffer, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port)); return Task.FromResult(true); #else var taskSource = new TaskCompletionSource(); try { - _Socket.BeginSendTo(messageData, 0, messageData.Length, SocketFlags.None, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port), result => + _Socket.BeginSendTo(buffer, 0, size, SocketFlags.None, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port), result => { try { @@ -160,11 +162,11 @@ namespace Emby.Common.Implementations.Net var ipEndPoint = state.EndPoint as IPEndPoint; state.TaskCompletionSource.SetResult( - new ReceivedUdpData() + new SocketReceiveResult() { Buffer = state.Buffer, ReceivedBytes = bytesRead, - ReceivedFrom = ToIpEndPointInfo(ipEndPoint) + RemoteEndPoint = ToIpEndPointInfo(ipEndPoint) } ); } @@ -215,11 +217,11 @@ namespace Emby.Common.Implementations.Net var ipEndPoint = state.EndPoint as IPEndPoint; state.TaskCompletionSource.SetResult( - new ReceivedUdpData() + new SocketReceiveResult { Buffer = state.Buffer, ReceivedBytes = bytesRead, - ReceivedFrom = ToIpEndPointInfo(ipEndPoint) + RemoteEndPoint = ToIpEndPointInfo(ipEndPoint) } ); } @@ -258,7 +260,7 @@ namespace Emby.Common.Implementations.Net public System.Net.Sockets.Socket Socket { get; private set; } - public TaskCompletionSource TaskCompletionSource { get; set; } + public TaskCompletionSource TaskCompletionSource { get; set; } } -- cgit v1.2.3 From 67ad1db6b77b2c2cb6d81c22808d99564a5f3ebc Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 4 Nov 2016 15:51:59 -0400 Subject: add environment info --- Emby.Common.Implementations/BaseApplicationHost.cs | 17 ++---- .../EnvironmentInfo/EnvironmentInfo.cs | 70 ++++++++++++++++++++++ Emby.Common.Implementations/Net/SocketFactory.cs | 7 +-- Emby.Common.Implementations/Net/UdpSocket.cs | 18 +++--- .../Serialization/XmlSerializer.cs | 17 +++--- Emby.Common.Implementations/project.json | 9 +-- Emby.Dlna/Main/DlnaEntryPoint.cs | 8 ++- Emby.Server.Implementations/IO/FileRefresher.cs | 15 +++-- MediaBrowser.Model/MediaBrowser.Model.csproj | 1 + MediaBrowser.Model/System/IEnvironmentInfo.cs | 22 +++++++ .../IO/LibraryMonitor.cs | 6 +- .../ApplicationHost.cs | 2 +- 12 files changed, 142 insertions(+), 50 deletions(-) create mode 100644 Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs create mode 100644 MediaBrowser.Model/System/IEnvironmentInfo.cs (limited to 'Emby.Common.Implementations/Net') diff --git a/Emby.Common.Implementations/BaseApplicationHost.cs b/Emby.Common.Implementations/BaseApplicationHost.cs index bdebe894ef..9585abb2a2 100644 --- a/Emby.Common.Implementations/BaseApplicationHost.cs +++ b/Emby.Common.Implementations/BaseApplicationHost.cs @@ -29,6 +29,7 @@ using MediaBrowser.Common.Extensions; using Emby.Common.Implementations.Cryptography; using Emby.Common.Implementations.Diagnostics; using Emby.Common.Implementations.Net; +using Emby.Common.Implementations.EnvironmentInfo; using Emby.Common.Implementations.Threading; using MediaBrowser.Common; using MediaBrowser.Common.IO; @@ -171,6 +172,8 @@ namespace Emby.Common.Implementations protected ICryptographyProvider CryptographyProvider = new CryptographyProvider(); + protected IEnvironmentInfo EnvironmentInfo = new Emby.Common.Implementations.EnvironmentInfo.EnvironmentInfo(); + private DeviceId _deviceId; public string SystemId { @@ -187,16 +190,7 @@ namespace Emby.Common.Implementations public virtual string OperatingSystemDisplayName { - get - { -#if NET46 - return Environment.OSVersion.VersionString; -#endif -#if NETSTANDARD1_6 - return System.Runtime.InteropServices.RuntimeInformation.OSDescription; -#endif - return "Operating System"; - } + get { return EnvironmentInfo.OperatingSystemName; } } public IMemoryStreamProvider MemoryStreamProvider { get; set; } @@ -216,7 +210,7 @@ namespace Emby.Common.Implementations // hack alert, until common can target .net core BaseExtensions.CryptographyProvider = CryptographyProvider; - XmlSerializer = new XmlSerializer(fileSystem, logManager.GetLogger("XmlSerializer")); + XmlSerializer = new MyXmlSerializer(fileSystem, logManager.GetLogger("XmlSerializer")); FailedAssemblies = new List(); ApplicationPaths = applicationPaths; @@ -534,6 +528,7 @@ return null; RegisterSingleInstance(Logger); RegisterSingleInstance(TaskManager); + RegisterSingleInstance(EnvironmentInfo); RegisterSingleInstance(FileSystemManager); diff --git a/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs b/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs new file mode 100644 index 0000000000..8cea617eae --- /dev/null +++ b/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using MediaBrowser.Model.System; + +namespace Emby.Common.Implementations.EnvironmentInfo +{ + public class EnvironmentInfo : IEnvironmentInfo + { + public MediaBrowser.Model.System.OperatingSystem OperatingSystem + { + get + { +#if NET46 + switch (Environment.OSVersion.Platform) + { + case PlatformID.MacOSX: + return MediaBrowser.Model.System.OperatingSystem.OSX; + case PlatformID.Win32NT: + return MediaBrowser.Model.System.OperatingSystem.Windows; + case PlatformID.Unix: + return MediaBrowser.Model.System.OperatingSystem.Linux; + } +#elif NETSTANDARD1_6 + if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return OperatingSystem.OSX; + } + if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return OperatingSystem.Windows; + } + if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return OperatingSystem.Linux; + } +#endif + return MediaBrowser.Model.System.OperatingSystem.Windows; + } + } + + public string OperatingSystemName + { + get + { +#if NET46 + return Environment.OSVersion.Platform.ToString(); +#elif NETSTANDARD1_6 + return System.Runtime.InteropServices.RuntimeInformation.OSDescription; +#endif + return "Operating System"; + } + } + + public string OperatingSystemVersion + { + get + { +#if NET46 + return Environment.OSVersion.Version.ToString() + " " + Environment.OSVersion.ServicePack.ToString(); +#elif NETSTANDARD1_6 + return System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription; +#endif + return "1.0"; + } + } + } +} diff --git a/Emby.Common.Implementations/Net/SocketFactory.cs b/Emby.Common.Implementations/Net/SocketFactory.cs index 6f0ff29961..bb38c72da5 100644 --- a/Emby.Common.Implementations/Net/SocketFactory.cs +++ b/Emby.Common.Implementations/Net/SocketFactory.cs @@ -40,12 +40,11 @@ namespace Emby.Common.Implementations.Net /// Creates a new UDP socket and binds it to the specified local port. /// /// An integer specifying the local port to bind the socket to. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] public IUdpSocket CreateUdpSocket(int localPort) { if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort"); - var retVal = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); + var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); @@ -65,12 +64,11 @@ namespace Emby.Common.Implementations.Net /// /// An integer specifying the local port to bind the socket to. /// An implementation of the interface used by RSSDP components to perform socket operations. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] public IUdpSocket CreateSsdpUdpSocket(int localPort) { if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort"); - var retVal = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); + var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); @@ -94,7 +92,6 @@ namespace Emby.Common.Implementations.Net /// The multicast time to live value for the socket. /// The number of the local port to bind to. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "ip"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] public IUdpSocket CreateUdpMulticastSocket(string ipAddress, int multicastTimeToLive, int localPort) { if (ipAddress == null) throw new ArgumentNullException("ipAddress"); diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs index 997d3f25fa..6afb071ae3 100644 --- a/Emby.Common.Implementations/Net/UdpSocket.cs +++ b/Emby.Common.Implementations/Net/UdpSocket.cs @@ -17,14 +17,14 @@ namespace Emby.Common.Implementations.Net #region Fields - private System.Net.Sockets.Socket _Socket; + private Socket _Socket; private int _LocalPort; #endregion #region Constructors - public UdpSocket(System.Net.Sockets.Socket socket, int localPort, IPAddress ip) + public UdpSocket(Socket socket, int localPort, IPAddress ip) { if (socket == null) throw new ArgumentNullException("socket"); @@ -46,12 +46,12 @@ namespace Emby.Common.Implementations.Net var tcs = new TaskCompletionSource(); - System.Net.EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0); + EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0); var state = new AsyncReceiveState(_Socket, receivedFromEndPoint); state.TaskCompletionSource = tcs; #if NETSTANDARD1_6 - _Socket.ReceiveFromAsync(new System.ArraySegment(state.Buffer), System.Net.Sockets.SocketFlags.None, state.EndPoint) + _Socket.ReceiveFromAsync(new ArraySegment(state.Buffer),SocketFlags.None, state.EndPoint) .ContinueWith((task, asyncState) => { if (task.Status != TaskStatus.Faulted) @@ -62,7 +62,7 @@ namespace Emby.Common.Implementations.Net } }, state); #else - _Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, System.Net.Sockets.SocketFlags.None, ref state.EndPoint, new AsyncCallback(this.ProcessResponse), state); + _Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.EndPoint, new AsyncCallback(this.ProcessResponse), state); #endif return tcs.Task; @@ -84,7 +84,7 @@ namespace Emby.Common.Implementations.Net buffer = copy; } - _Socket.SendTo(buffer, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port)); + _Socket.SendTo(buffer, new IPEndPoint(IPAddress.Parse(endPoint.IpAddress.ToString()), endPoint.Port)); return Task.FromResult(true); #else var taskSource = new TaskCompletionSource(); @@ -153,7 +153,6 @@ namespace Emby.Common.Implementations.Net #region Private Methods - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions via task methods should be reported by task completion source, so this should be ok.")] private static void ProcessResponse(AsyncReceiveState state, Func receiveData) { try @@ -206,7 +205,6 @@ namespace Emby.Common.Implementations.Net }; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions via task methods should be reported by task completion source, so this should be ok.")] private void ProcessResponse(IAsyncResult asyncResult) { #if NET46 @@ -249,7 +247,7 @@ namespace Emby.Common.Implementations.Net private class AsyncReceiveState { - public AsyncReceiveState(System.Net.Sockets.Socket socket, EndPoint endPoint) + public AsyncReceiveState(Socket socket, EndPoint endPoint) { this.Socket = socket; this.EndPoint = endPoint; @@ -258,7 +256,7 @@ namespace Emby.Common.Implementations.Net public EndPoint EndPoint; public byte[] Buffer = new byte[8192]; - public System.Net.Sockets.Socket Socket { get; private set; } + public Socket Socket { get; private set; } public TaskCompletionSource TaskCompletionSource { get; set; } diff --git a/Emby.Common.Implementations/Serialization/XmlSerializer.cs b/Emby.Common.Implementations/Serialization/XmlSerializer.cs index aea63a57e2..3583f998e5 100644 --- a/Emby.Common.Implementations/Serialization/XmlSerializer.cs +++ b/Emby.Common.Implementations/Serialization/XmlSerializer.cs @@ -4,6 +4,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Xml; +using System.Xml.Serialization; using MediaBrowser.Common.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; @@ -13,12 +14,12 @@ namespace Emby.Common.Implementations.Serialization /// /// Provides a wrapper around third party xml serialization. /// - public class XmlSerializer : IXmlSerializer + public class MyXmlSerializer : IXmlSerializer { private readonly IFileSystem _fileSystem; private readonly ILogger _logger; - public XmlSerializer(IFileSystem fileSystem, ILogger logger) + public MyXmlSerializer(IFileSystem fileSystem, ILogger logger) { _fileSystem = fileSystem; _logger = logger; @@ -26,18 +27,18 @@ namespace Emby.Common.Implementations.Serialization // Need to cache these // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html - private readonly Dictionary _serializers = - new Dictionary(); + private readonly Dictionary _serializers = + new Dictionary(); - private System.Xml.Serialization.XmlSerializer GetSerializer(Type type) + private XmlSerializer GetSerializer(Type type) { var key = type.FullName; lock (_serializers) { - System.Xml.Serialization.XmlSerializer serializer; + XmlSerializer serializer; if (!_serializers.TryGetValue(key, out serializer)) { - serializer = new System.Xml.Serialization.XmlSerializer(type); + serializer = new XmlSerializer(type); _serializers[key] = serializer; } return serializer; @@ -80,7 +81,7 @@ namespace Emby.Common.Implementations.Serialization #if NET46 using (var writer = new XmlTextWriter(stream, null)) { - writer.Formatting = System.Xml.Formatting.Indented; + writer.Formatting = Formatting.Indented; SerializeToWriter(obj, writer); } #else diff --git a/Emby.Common.Implementations/project.json b/Emby.Common.Implementations/project.json index dc96f5726c..2b2357e383 100644 --- a/Emby.Common.Implementations/project.json +++ b/Emby.Common.Implementations/project.json @@ -12,15 +12,14 @@ "System.IO": "4.0.0.0", "System.Net": "4.0.0.0", "System.Net.Http": "4.0.0.0", - "System.Net.Http.WebRequest": "4.0.0.0", "System.Net.Primitives": "4.0.0.0", + "System.Net.Http.WebRequest": "4.0.0.0", "System.Runtime": "4.0.0.0", "System.Runtime.Extensions": "4.0.0.0", "System.Text.Encoding": "4.0.0.0", "System.Threading": "4.0.0.0", "System.Threading.Tasks": "4.0.0.0", - "System.Xml": "4.0.0.0", - "System.Xml.Serialization": "4.0.0.0" + "System.Xml.ReaderWriter": "4.0.0" }, "dependencies": { "SimpleInjector": "3.2.4", @@ -30,7 +29,8 @@ }, "MediaBrowser.Common": { "target": "project" - } } + } + } }, "netstandard1.6": { "imports": "dnxcore50", @@ -40,6 +40,7 @@ "System.Diagnostics.Process": "4.1.0", "System.Threading.Timer": "4.0.1", "System.Net.Requests": "4.0.11", + "System.Xml.ReaderWriter": "4.0.11", "System.Xml.XmlSerializer": "4.0.11", "System.Net.Http": "4.1.0", "System.Net.Primitives": "4.0.11", diff --git a/Emby.Dlna/Main/DlnaEntryPoint.cs b/Emby.Dlna/Main/DlnaEntryPoint.cs index 14fbbcfa20..bff87bcacc 100644 --- a/Emby.Dlna/Main/DlnaEntryPoint.cs +++ b/Emby.Dlna/Main/DlnaEntryPoint.cs @@ -20,6 +20,7 @@ using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Net; +using MediaBrowser.Model.System; using MediaBrowser.Model.Threading; using Rssdp; using Rssdp.Infrastructure; @@ -52,7 +53,7 @@ namespace Emby.Dlna.Main private readonly ITimerFactory _timerFactory; private readonly ISocketFactory _socketFactory; - + private readonly IEnvironmentInfo _environmentInfo; public DlnaEntryPoint(IServerConfigurationManager config, ILogManager logManager, @@ -66,7 +67,7 @@ namespace Emby.Dlna.Main IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager, - IDeviceDiscovery deviceDiscovery, IMediaEncoder mediaEncoder, ISocketFactory socketFactory, ITimerFactory timerFactory) + IDeviceDiscovery deviceDiscovery, IMediaEncoder mediaEncoder, ISocketFactory socketFactory, ITimerFactory timerFactory, IEnvironmentInfo environmentInfo) { _config = config; _appHost = appHost; @@ -83,6 +84,7 @@ namespace Emby.Dlna.Main _mediaEncoder = mediaEncoder; _socketFactory = socketFactory; _timerFactory = timerFactory; + _environmentInfo = environmentInfo; _logger = logManager.GetLogger("Dlna"); } @@ -169,7 +171,7 @@ namespace Emby.Dlna.Main private void StartPublishing() { SsdpDevicePublisherBase.LogFunction = LogMessage; - _Publisher = new SsdpDevicePublisher(_socketFactory, _timerFactory, "Windows", "10"); + _Publisher = new SsdpDevicePublisher(_socketFactory, _timerFactory, _environmentInfo.OperatingSystemName, _environmentInfo.OperatingSystemVersion); } private void StartDeviceDiscovery() diff --git a/Emby.Server.Implementations/IO/FileRefresher.cs b/Emby.Server.Implementations/IO/FileRefresher.cs index 295ecc4651..39033249fd 100644 --- a/Emby.Server.Implementations/IO/FileRefresher.cs +++ b/Emby.Server.Implementations/IO/FileRefresher.cs @@ -13,6 +13,7 @@ using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.System; using MediaBrowser.Model.Tasks; using MediaBrowser.Model.Threading; @@ -32,8 +33,9 @@ namespace Emby.Server.Implementations.IO public string Path { get; private set; } public event EventHandler Completed; + private readonly IEnvironmentInfo _environmentInfo; - public FileRefresher(string path, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ITaskManager taskManager, ILogger logger, ITimerFactory timerFactory) + public FileRefresher(string path, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ITaskManager taskManager, ILogger logger, ITimerFactory timerFactory, IEnvironmentInfo environmentInfo) { logger.Debug("New file refresher created for {0}", path); Path = path; @@ -44,6 +46,7 @@ namespace Emby.Server.Implementations.IO TaskManager = taskManager; Logger = logger; _timerFactory = timerFactory; + _environmentInfo = environmentInfo; AddPath(path); } @@ -226,11 +229,11 @@ namespace Emby.Server.Implementations.IO private bool IsFileLocked(string path) { - //if (Environment.OSVersion.Platform != PlatformID.Win32NT) - //{ - // // Causing lockups on linux - // return false; - //} + if (_environmentInfo.OperatingSystem != OperatingSystem.Windows) + { + // Causing lockups on linux + return false; + } try { diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index c85b215f2c..52e477b1a0 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -141,6 +141,7 @@ + diff --git a/MediaBrowser.Model/System/IEnvironmentInfo.cs b/MediaBrowser.Model/System/IEnvironmentInfo.cs new file mode 100644 index 0000000000..3fcacb30d4 --- /dev/null +++ b/MediaBrowser.Model/System/IEnvironmentInfo.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.Model.System +{ + public interface IEnvironmentInfo + { + MediaBrowser.Model.System.OperatingSystem OperatingSystem { get; } + string OperatingSystemName { get; } + string OperatingSystemVersion { get; } + } + + public enum OperatingSystem + { + Windows, + Linux, + OSX + } +} diff --git a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs index 34fc85e7b1..a8363558d5 100644 --- a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs +++ b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs @@ -139,11 +139,12 @@ namespace MediaBrowser.Server.Implementations.IO private readonly IFileSystem _fileSystem; private readonly ITimerFactory _timerFactory; + private readonly IEnvironmentInfo _environmentInfo; /// /// Initializes a new instance of the class. /// - public LibraryMonitor(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ITimerFactory timerFactory, ISystemEvents systemEvents) + public LibraryMonitor(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ITimerFactory timerFactory, ISystemEvents systemEvents, IEnvironmentInfo environmentInfo) { if (taskManager == null) { @@ -156,6 +157,7 @@ namespace MediaBrowser.Server.Implementations.IO ConfigurationManager = configurationManager; _fileSystem = fileSystem; _timerFactory = timerFactory; + _environmentInfo = environmentInfo; systemEvents.Resume += _systemEvents_Resume; } @@ -525,7 +527,7 @@ namespace MediaBrowser.Server.Implementations.IO } } - var newRefresher = new FileRefresher(path, _fileSystem, ConfigurationManager, LibraryManager, TaskManager, Logger, _timerFactory); + var newRefresher = new FileRefresher(path, _fileSystem, ConfigurationManager, LibraryManager, TaskManager, Logger, _timerFactory, _environmentInfo); newRefresher.Completed += NewRefresher_Completed; _activeRefreshers.Add(newRefresher); } diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index 79f7b5f051..ba04338bb9 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -592,7 +592,7 @@ namespace MediaBrowser.Server.Startup.Common var musicManager = new MusicManager(LibraryManager); RegisterSingleInstance(new MusicManager(LibraryManager)); - LibraryMonitor = new LibraryMonitor(LogManager, TaskManager, LibraryManager, ServerConfigurationManager, FileSystemManager, TimerFactory, SystemEvents); + LibraryMonitor = new LibraryMonitor(LogManager, TaskManager, LibraryManager, ServerConfigurationManager, FileSystemManager, TimerFactory, SystemEvents, EnvironmentInfo); RegisterSingleInstance(LibraryMonitor); ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, LibraryMonitor, LogManager, FileSystemManager, ApplicationPaths, () => LibraryManager, JsonSerializer, MemoryStreamProvider); -- cgit v1.2.3 From 25312d7d03af665818cfd2cee2edb549e0e940f2 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 4 Nov 2016 19:57:21 -0400 Subject: add udp error handling --- Emby.Common.Implementations/Net/UdpSocket.cs | 23 +----- .../Networking/BaseNetworkManager.cs | 85 ++++++++++++---------- Emby.Dlna/Main/DlnaEntryPoint.cs | 11 ++- Emby.Dlna/Service/BaseControlHandler.cs | 2 +- .../Security/MBLicenseFile.cs | 12 ++- RSSDP/SsdpCommunicationsServer.cs | 51 ++++++------- 6 files changed, 87 insertions(+), 97 deletions(-) (limited to 'Emby.Common.Implementations/Net') diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs index 6afb071ae3..d999d3fe87 100644 --- a/Emby.Common.Implementations/Net/UdpSocket.cs +++ b/Emby.Common.Implementations/Net/UdpSocket.cs @@ -102,29 +102,10 @@ namespace Emby.Common.Implementations.Net { taskSource.TrySetException(ex); } - catch (ObjectDisposedException ex) - { - taskSource.TrySetException(ex); - } - catch (InvalidOperationException ex) - { - taskSource.TrySetException(ex); - } - catch (SecurityException ex) - { - taskSource.TrySetException(ex); - } + }, null); } - catch (SocketException ex) - { - taskSource.TrySetException(ex); - } - catch (ObjectDisposedException ex) - { - taskSource.TrySetException(ex); - } - catch (SecurityException ex) + catch (Exception ex) { taskSource.TrySetException(ex); } diff --git a/Emby.Common.Implementations/Networking/BaseNetworkManager.cs b/Emby.Common.Implementations/Networking/BaseNetworkManager.cs index d1c299dc96..10d0db9684 100644 --- a/Emby.Common.Implementations/Networking/BaseNetworkManager.cs +++ b/Emby.Common.Implementations/Networking/BaseNetworkManager.cs @@ -22,7 +22,7 @@ namespace Emby.Common.Implementations.Networking Logger = logger; } - private List _localIpAddresses; + private List _localIpAddresses; private readonly object _localIpAddressSyncLock = new object(); /// @@ -51,24 +51,24 @@ namespace Emby.Common.Implementations.Networking return _localIpAddresses; } - private IEnumerable GetLocalIpAddressesInternal() + private IEnumerable GetLocalIpAddressesInternal() { var list = GetIPsDefault() .ToList(); if (list.Count == 0) { - list.AddRange(GetLocalIpAddressesFallback().Result); + list.AddRange(GetLocalIpAddressesFallback().Result); } - return list.Where(FilterIpAddress).DistinctBy(i => i.ToString()); + return list.Where(FilterIpAddress).DistinctBy(i => i.ToString()); } - private bool FilterIpAddress(IPAddress address) + private bool FilterIpAddress(IPAddress address) { - var addressString = address.ToString (); + var addressString = address.ToString(); - if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase)) + if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase)) { return false; } @@ -156,12 +156,12 @@ namespace Emby.Common.Implementations.Networking { var prefix = addressString.Substring(0, lengthMatch); - if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) + if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) { return true; } } - } + } else if (resolveHost) { Uri uri; @@ -200,45 +200,50 @@ namespace Emby.Common.Implementations.Networking return Dns.GetHostAddressesAsync(hostName); } - private List GetIPsDefault() - { - NetworkInterface[] interfaces; + private List GetIPsDefault() + { + NetworkInterface[] interfaces; - try - { - interfaces = NetworkInterface.GetAllNetworkInterfaces(); - } - catch (Exception ex) - { - Logger.ErrorException("Error in GetAllNetworkInterfaces", ex); - return new List(); - } + try + { + var validStatuses = new[] { OperationalStatus.Up, OperationalStatus.Unknown }; - return interfaces.SelectMany(network => { + interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(i => validStatuses.Contains(i.OperationalStatus)) + .ToArray(); + } + catch (Exception ex) + { + Logger.ErrorException("Error in GetAllNetworkInterfaces", ex); + return new List(); + } + + return interfaces.SelectMany(network => + { - try - { + try + { Logger.Debug("Querying interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus); - var properties = network.GetIPProperties(); + var properties = network.GetIPProperties(); - return properties.UnicastAddresses + return properties.UnicastAddresses .Where(i => i.IsDnsEligible) .Select(i => i.Address) .Where(i => i.AddressFamily == AddressFamily.InterNetwork) - .ToList(); - } - catch (Exception ex) - { - Logger.ErrorException("Error querying network interface", ex); - return new List(); - } - - }).DistinctBy(i => i.ToString()) - .ToList(); - } - - private async Task> GetLocalIpAddressesFallback() + .ToList(); + } + catch (Exception ex) + { + Logger.ErrorException("Error querying network interface", ex); + return new List(); + } + + }).DistinctBy(i => i.ToString()) + .ToList(); + } + + private async Task> GetLocalIpAddressesFallback() { var host = await Dns.GetHostEntryAsync(Dns.GetHostName()).ConfigureAwait(false); @@ -310,7 +315,7 @@ namespace Emby.Common.Implementations.Networking string[] values = endpointstring.Split(new char[] { ':' }); IPAddress ipaddy; int port = -1; - + //check if we have an IPv6 or ports if (values.Length <= 2) // ipv4 or hostname { diff --git a/Emby.Dlna/Main/DlnaEntryPoint.cs b/Emby.Dlna/Main/DlnaEntryPoint.cs index bff87bcacc..142b9f96e7 100644 --- a/Emby.Dlna/Main/DlnaEntryPoint.cs +++ b/Emby.Dlna/Main/DlnaEntryPoint.cs @@ -240,6 +240,8 @@ namespace Emby.Dlna.Main var addresses = (await _appHost.GetLocalIpAddresses().ConfigureAwait(false)).ToList(); + var udn = CreateUuid(_appHost.SystemId); + foreach (var address in addresses) { //if (IPAddress.IsLoopback(address)) @@ -250,8 +252,6 @@ namespace Emby.Dlna.Main var addressString = address.ToString(); - var udn = CreateUuid(addressString); - var fullService = "urn:schemas-upnp-org:device:MediaServer:1"; _logger.Info("Registering publisher for {0} on {1}", fullService, addressString); @@ -299,7 +299,12 @@ namespace Emby.Dlna.Main private string CreateUuid(string text) { - return text.GetMD5().ToString("N"); + Guid guid; + if (!Guid.TryParse(text, out guid)) + { + guid = text.GetMD5(); + } + return guid.ToString("N"); } private void SetProperies(SsdpDevice device, string fullDeviceType) diff --git a/Emby.Dlna/Service/BaseControlHandler.cs b/Emby.Dlna/Service/BaseControlHandler.cs index df4d29e8b4..afc66b1d7f 100644 --- a/Emby.Dlna/Service/BaseControlHandler.cs +++ b/Emby.Dlna/Service/BaseControlHandler.cs @@ -228,7 +228,7 @@ namespace Emby.Dlna.Service var headers = string.Join(", ", request.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value)).ToArray()); builder.AppendFormat("Headers: {0}", headers); builder.AppendLine(); - builder.Append(request.InputXml); + //builder.Append(request.InputXml); Logger.LogMultiline("Control request", LogSeverity.Debug, builder); } diff --git a/Emby.Server.Implementations/Security/MBLicenseFile.cs b/Emby.Server.Implementations/Security/MBLicenseFile.cs index 454ee6026b..7cb6165a52 100644 --- a/Emby.Server.Implementations/Security/MBLicenseFile.cs +++ b/Emby.Server.Implementations/Security/MBLicenseFile.cs @@ -124,9 +124,17 @@ namespace Emby.Server.Implementations.Security //the rest of the lines should be pairs of features and timestamps for (var i = 2; i < contents.Length; i = i + 2) { - var feat = Guid.Parse(contents[i]); + var line = contents[i]; + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } - SetUpdateRecord(feat, new DateTime(Convert.ToInt64(contents[i + 1]))); + Guid feat; + if (Guid.TryParse(line, out feat)) + { + SetUpdateRecord(feat, new DateTime(Convert.ToInt64(contents[i + 1]))); + } } } } diff --git a/RSSDP/SsdpCommunicationsServer.cs b/RSSDP/SsdpCommunicationsServer.cs index dadb1bff4b..c0b9c6542d 100644 --- a/RSSDP/SsdpCommunicationsServer.cs +++ b/RSSDP/SsdpCommunicationsServer.cs @@ -78,17 +78,6 @@ namespace Rssdp.Infrastructure { } - /// - /// Partial constructor. - /// - /// An implementation of the interface that can be used to make new unicast and multicast sockets. Cannot be null. - /// The specific local port to use for all sockets created by this instance. Specify zero to indicate the system should choose a free port itself. - /// The argument is null. - public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort) - : this(socketFactory, localPort, SsdpConstants.SsdpDefaultMulticastTimeToLive) - { - } - /// /// Full constructor. /// @@ -170,7 +159,12 @@ namespace Rssdp.Infrastructure EnsureSendSocketCreated(); // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. - await Repeat(SsdpConstants.UdpResendCount, TimeSpan.FromMilliseconds(100), () => SendMessageIfSocketNotDisposed(messageData, destination)).ConfigureAwait(false); + for (var i = 0; i < SsdpConstants.UdpResendCount; i++) + { + await SendMessageIfSocketNotDisposed(messageData, destination).ConfigureAwait(false); + + await Task.Delay(100).ConfigureAwait(false); + } } /// @@ -188,8 +182,17 @@ namespace Rssdp.Infrastructure EnsureSendSocketCreated(); // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. - await Repeat(SsdpConstants.UdpResendCount, TimeSpan.FromMilliseconds(100), - () => SendMessageIfSocketNotDisposed(messageData, new IpEndPointInfo() { IpAddress = new IpAddressInfo { Address = SsdpConstants.MulticastLocalAdminAddress }, Port = SsdpConstants.MulticastPort })).ConfigureAwait(false); + for (var i = 0; i < SsdpConstants.UdpResendCount; i++) + { + await SendMessageIfSocketNotDisposed(messageData, new IpEndPointInfo + { + IpAddress = new IpAddressInfo { Address = SsdpConstants.MulticastLocalAdminAddress }, + Port = SsdpConstants.MulticastPort + + }).ConfigureAwait(false); + + await Task.Delay(100).ConfigureAwait(false); + } } /// @@ -255,28 +258,16 @@ namespace Rssdp.Infrastructure #region Private Methods - private async Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination) + private Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination) { var socket = _SendSocket; if (socket != null) { - await _SendSocket.SendAsync(messageData, messageData.Length, destination).ConfigureAwait(false); - } - else - { - ThrowIfDisposed(); + return _SendSocket.SendAsync(messageData, messageData.Length, destination); } - } - - private static async Task Repeat(int repetitions, TimeSpan delay, Func work) - { - for (int cnt = 0; cnt < repetitions; cnt++) - { - await work().ConfigureAwait(false); - if (delay != TimeSpan.Zero) - await Task.Delay(delay).ConfigureAwait(false); - } + ThrowIfDisposed(); + return Task.FromResult(true); } private IUdpSocket ListenForBroadcastsAsync() -- cgit v1.2.3