aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-11-08 13:44:23 -0500
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-11-08 13:44:23 -0500
commita8b340cbb29dbcf7fd5d101e640d66470c6d32bf (patch)
treea626c151e9ccb8809dd6d667fb9146fe4bb3ffea /Emby.Common.Implementations
parent05a5ce58a9293f6669960c735911e9455c5d8188 (diff)
update portable projects
Diffstat (limited to 'Emby.Common.Implementations')
-rw-r--r--Emby.Common.Implementations/BaseApplicationHost.cs10
-rw-r--r--Emby.Common.Implementations/Cryptography/CryptographyProvider.cs17
-rw-r--r--Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs4
-rw-r--r--Emby.Common.Implementations/Net/NetSocket.cs85
-rw-r--r--Emby.Common.Implementations/Net/SocketAcceptor.cs111
-rw-r--r--Emby.Common.Implementations/Net/SocketFactory.cs37
-rw-r--r--Emby.Common.Implementations/Net/UdpSocket.cs12
-rw-r--r--Emby.Common.Implementations/Networking/BaseNetworkManager.cs91
-rw-r--r--Emby.Common.Implementations/TextEncoding/TextEncoding.cs13
9 files changed, 316 insertions, 64 deletions
diff --git a/Emby.Common.Implementations/BaseApplicationHost.cs b/Emby.Common.Implementations/BaseApplicationHost.cs
index 9585abb2a..0cf11e825 100644
--- a/Emby.Common.Implementations/BaseApplicationHost.cs
+++ b/Emby.Common.Implementations/BaseApplicationHost.cs
@@ -170,7 +170,7 @@ namespace Emby.Common.Implementations
/// <value><c>true</c> if this instance is running as service; otherwise, <c>false</c>.</value>
public abstract bool IsRunningAsService { get; }
- protected ICryptographyProvider CryptographyProvider = new CryptographyProvider();
+ protected ICryptoProvider CryptographyProvider = new CryptographyProvider();
protected IEnvironmentInfo EnvironmentInfo = new Emby.Common.Implementations.EnvironmentInfo.EnvironmentInfo();
@@ -183,7 +183,7 @@ namespace Emby.Common.Implementations
{
_deviceId = new DeviceId(ApplicationPaths, LogManager.GetLogger("SystemId"), FileSystemManager);
}
-
+
return _deviceId.Value;
}
}
@@ -193,7 +193,7 @@ namespace Emby.Common.Implementations
get { return EnvironmentInfo.OperatingSystemName; }
}
- public IMemoryStreamProvider MemoryStreamProvider { get; set; }
+ public IMemoryStreamFactory MemoryStreamProvider { get; set; }
/// <summary>
/// The container
@@ -209,7 +209,7 @@ namespace Emby.Common.Implementations
{
// hack alert, until common can target .net core
BaseExtensions.CryptographyProvider = CryptographyProvider;
-
+
XmlSerializer = new MyXmlSerializer(fileSystem, logManager.GetLogger("XmlSerializer"));
FailedAssemblies = new List<string>();
@@ -267,7 +267,7 @@ namespace Emby.Common.Implementations
progress.Report(100);
}
- protected abstract IMemoryStreamProvider CreateMemoryStreamProvider();
+ protected abstract IMemoryStreamFactory CreateMemoryStreamProvider();
protected abstract ISystemEvents CreateSystemEvents();
protected virtual void OnLoggerLoaded(bool isFirstLoad)
diff --git a/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs b/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs
index 7b8d95b96..01a31bcc0 100644
--- a/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs
+++ b/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs
@@ -6,21 +6,14 @@ using MediaBrowser.Model.Cryptography;
namespace Emby.Common.Implementations.Cryptography
{
- public class CryptographyProvider : ICryptographyProvider
+ public class CryptographyProvider : ICryptoProvider
{
public Guid GetMD5(string str)
{
- return new Guid(GetMD5Bytes(str));
- }
- public byte[] GetMD5Bytes(string str)
- {
- using (var provider = MD5.Create())
- {
- return provider.ComputeHash(Encoding.Unicode.GetBytes(str));
- }
+ return new Guid(ComputeMD5(Encoding.Unicode.GetBytes(str)));
}
- public byte[] GetSHA1Bytes(byte[] bytes)
+ public byte[] ComputeSHA1(byte[] bytes)
{
using (var provider = SHA1.Create())
{
@@ -28,7 +21,7 @@ namespace Emby.Common.Implementations.Cryptography
}
}
- public byte[] GetMD5Bytes(Stream str)
+ public byte[] ComputeMD5(Stream str)
{
using (var provider = MD5.Create())
{
@@ -36,7 +29,7 @@ namespace Emby.Common.Implementations.Cryptography
}
}
- public byte[] GetMD5Bytes(byte[] bytes)
+ public byte[] ComputeMD5(byte[] bytes)
{
using (var provider = MD5.Create())
{
diff --git a/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs
index 85fcb556f..06af5af53 100644
--- a/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs
+++ b/Emby.Common.Implementations/HttpClientManager/HttpClientManager.cs
@@ -42,7 +42,7 @@ namespace Emby.Common.Implementations.HttpClientManager
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
- private readonly IMemoryStreamProvider _memoryStreamProvider;
+ private readonly IMemoryStreamFactory _memoryStreamProvider;
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientManager" /> class.
@@ -53,7 +53,7 @@ namespace Emby.Common.Implementations.HttpClientManager
/// <exception cref="System.ArgumentNullException">appPaths
/// or
/// logger</exception>
- public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem, IMemoryStreamProvider memoryStreamProvider)
+ public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem, IMemoryStreamFactory memoryStreamProvider)
{
if (appPaths == null)
{
diff --git a/Emby.Common.Implementations/Net/NetSocket.cs b/Emby.Common.Implementations/Net/NetSocket.cs
new file mode 100644
index 000000000..72faa41a9
--- /dev/null
+++ b/Emby.Common.Implementations/Net/NetSocket.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+using System.Threading;
+using Emby.Common.Implementations.Networking;
+using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Logging;
+
+namespace Emby.Common.Implementations.Net
+{
+ public class NetSocket : ISocket
+ {
+ public Socket Socket { get; private set; }
+ private readonly ILogger _logger;
+
+ public NetSocket(Socket socket, ILogger logger)
+ {
+ Socket = socket;
+ _logger = logger;
+ }
+
+ public IpEndPointInfo LocalEndPoint
+ {
+ get
+ {
+ return BaseNetworkManager.ToIpEndPointInfo((IPEndPoint)Socket.LocalEndPoint);
+ }
+ }
+
+ public IpEndPointInfo RemoteEndPoint
+ {
+ get
+ {
+ return BaseNetworkManager.ToIpEndPointInfo((IPEndPoint)Socket.RemoteEndPoint);
+ }
+ }
+
+ public void Close()
+ {
+#if NET46
+ Socket.Close();
+#else
+ Socket.Dispose();
+#endif
+ }
+
+ public void Shutdown(bool both)
+ {
+ if (both)
+ {
+ Socket.Shutdown(SocketShutdown.Both);
+ }
+ else
+ {
+ // Change interface if ever needed
+ throw new NotImplementedException();
+ }
+ }
+
+ public void Listen(int backlog)
+ {
+ Socket.Listen(backlog);
+ }
+
+ public void Bind(IpEndPointInfo endpoint)
+ {
+ var nativeEndpoint = BaseNetworkManager.ToIPEndPoint(endpoint);
+
+ Socket.Bind(nativeEndpoint);
+ }
+
+ private SocketAcceptor _acceptor;
+ public void StartAccept(Action<ISocket> onAccept, Func<bool> isClosed)
+ {
+ _acceptor = new SocketAcceptor(_logger, Socket, onAccept, isClosed);
+
+ _acceptor.StartAccept();
+ }
+
+ public void Dispose()
+ {
+ Socket.Dispose();
+ }
+ }
+}
diff --git a/Emby.Common.Implementations/Net/SocketAcceptor.cs b/Emby.Common.Implementations/Net/SocketAcceptor.cs
new file mode 100644
index 000000000..fd65e9fbc
--- /dev/null
+++ b/Emby.Common.Implementations/Net/SocketAcceptor.cs
@@ -0,0 +1,111 @@
+using System;
+using System.Net.Sockets;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Net;
+
+namespace Emby.Common.Implementations.Net
+{
+ public class SocketAcceptor
+ {
+ private readonly ILogger _logger;
+ private readonly Socket _originalSocket;
+ private readonly Func<bool> _isClosed;
+ private readonly Action<ISocket> _onAccept;
+
+ public SocketAcceptor(ILogger logger, Socket originalSocket, Action<ISocket> onAccept, Func<bool> isClosed)
+ {
+ _logger = logger;
+ _originalSocket = originalSocket;
+ _isClosed = isClosed;
+ _onAccept = onAccept;
+ }
+
+ public void StartAccept()
+ {
+ Socket dummy = null;
+ StartAccept(null, ref dummy);
+ }
+
+ public void StartAccept(SocketAsyncEventArgs acceptEventArg, ref Socket accepted)
+ {
+ if (acceptEventArg == null)
+ {
+ acceptEventArg = new SocketAsyncEventArgs();
+ acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
+ }
+ else
+ {
+ // socket must be cleared since the context object is being reused
+ acceptEventArg.AcceptSocket = null;
+ }
+
+ try
+ {
+ bool willRaiseEvent = _originalSocket.AcceptAsync(acceptEventArg);
+
+ if (!willRaiseEvent)
+ {
+ ProcessAccept(acceptEventArg);
+ }
+ }
+ catch (Exception ex)
+ {
+ if (accepted != null)
+ {
+ try
+ {
+#if NET46
+ accepted.Close();
+#else
+ accepted.Dispose();
+#endif
+ }
+ catch
+ {
+ }
+ accepted = null;
+ }
+ }
+ }
+
+ // This method is the callback method associated with Socket.AcceptAsync
+ // operations and is invoked when an accept operation is complete
+ //
+ void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
+ {
+ ProcessAccept(e);
+ }
+
+ private void ProcessAccept(SocketAsyncEventArgs e)
+ {
+ if (_isClosed())
+ {
+ return;
+ }
+
+ // http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.acceptasync%28v=vs.110%29.aspx
+ // Under certain conditions ConnectionReset can occur
+ // Need to attept to re-accept
+ if (e.SocketError == SocketError.ConnectionReset)
+ {
+ _logger.Error("SocketError.ConnectionReset reported. Attempting to re-accept.");
+ Socket dummy = null;
+ StartAccept(e, ref dummy);
+ return;
+ }
+
+ var acceptSocket = e.AcceptSocket;
+ if (acceptSocket != null)
+ {
+ //ProcessAccept(acceptSocket);
+ _onAccept(new NetSocket(acceptSocket, _logger));
+ }
+
+ if (_originalSocket != null)
+ {
+ // Accept the next connection request
+ StartAccept(e, ref acceptSocket);
+ }
+ }
+ }
+}
diff --git a/Emby.Common.Implementations/Net/SocketFactory.cs b/Emby.Common.Implementations/Net/SocketFactory.cs
index bb38c72da..922b0f3cc 100644
--- a/Emby.Common.Implementations/Net/SocketFactory.cs
+++ b/Emby.Common.Implementations/Net/SocketFactory.cs
@@ -4,6 +4,7 @@ using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
+using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
namespace Emby.Common.Implementations.Net
@@ -22,16 +23,28 @@ namespace Emby.Common.Implementations.Net
/// </summary>
private IPAddress _LocalIP;
- /// <summary>
- /// Default constructor.
- /// </summary>
- /// <param name="localIP">A string containing the IP address of the local network adapter to bind sockets to. Null or empty string will use <see cref="IPAddress.Any"/>.</param>
- public SocketFactory(string localIP)
+ private ILogger _logger;
+
+ public SocketFactory(ILogger logger)
+ {
+ _logger = logger;
+ _LocalIP = IPAddress.Any;
+ }
+
+ public ISocket CreateSocket(IpAddressFamily family, MediaBrowser.Model.Net.SocketType socketType, MediaBrowser.Model.Net.ProtocolType protocolType, bool dualMode)
{
- if (String.IsNullOrEmpty(localIP))
- _LocalIP = IPAddress.Any;
- else
- _LocalIP = IPAddress.Parse(localIP);
+ var addressFamily = family == IpAddressFamily.InterNetwork
+ ? AddressFamily.InterNetwork
+ : AddressFamily.InterNetworkV6;
+
+ var socket = new Socket(addressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
+
+ if (dualMode)
+ {
+ socket.DualMode = true;
+ }
+
+ return new NetSocket(socket, _logger);
}
#region ISocketFactory Members
@@ -44,7 +57,7 @@ namespace Emby.Common.Implementations.Net
{
if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
- var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+ var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
try
{
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
@@ -68,7 +81,7 @@ namespace Emby.Common.Implementations.Net
{
if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
- var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+ var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
try
{
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
@@ -99,7 +112,7 @@ namespace Emby.Common.Implementations.Net
if (multicastTimeToLive <= 0) throw new ArgumentException("multicastTimeToLive cannot be zero or less.", "multicastTimeToLive");
if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort");
- var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+ var retVal = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
try
{
diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs
index d999d3fe8..244b37bb4 100644
--- a/Emby.Common.Implementations/Net/UdpSocket.cs
+++ b/Emby.Common.Implementations/Net/UdpSocket.cs
@@ -5,6 +5,7 @@ using System.Net;
using System.Net.Sockets;
using System.Security;
using System.Threading.Tasks;
+using Emby.Common.Implementations.Networking;
using MediaBrowser.Model.Net;
namespace Emby.Common.Implementations.Net
@@ -174,16 +175,7 @@ namespace Emby.Common.Implementations.Net
return null;
}
- return new IpEndPointInfo
- {
- IpAddress = new IpAddressInfo
- {
- Address = endpoint.Address.ToString(),
- IsIpv6 = endpoint.AddressFamily == AddressFamily.InterNetworkV6
- },
-
- Port = endpoint.Port
- };
+ return BaseNetworkManager.ToIpEndPointInfo(endpoint);
}
private void ProcessResponse(IAsyncResult asyncResult)
diff --git a/Emby.Common.Implementations/Networking/BaseNetworkManager.cs b/Emby.Common.Implementations/Networking/BaseNetworkManager.cs
index 10d0db968..f1ac8413b 100644
--- a/Emby.Common.Implementations/Networking/BaseNetworkManager.cs
+++ b/Emby.Common.Implementations/Networking/BaseNetworkManager.cs
@@ -22,14 +22,10 @@ namespace Emby.Common.Implementations.Networking
Logger = logger;
}
- private List<IPAddress> _localIpAddresses;
+ private List<IpAddressInfo> _localIpAddresses;
private readonly object _localIpAddressSyncLock = new object();
- /// <summary>
- /// Gets the machine's local ip address
- /// </summary>
- /// <returns>IPAddress.</returns>
- public IEnumerable<IPAddress> GetLocalIpAddresses()
+ public IEnumerable<IpAddressInfo> GetLocalIpAddresses()
{
const int cacheMinutes = 5;
@@ -39,7 +35,7 @@ namespace Emby.Common.Implementations.Networking
if (_localIpAddresses == null || forceRefresh)
{
- var addresses = GetLocalIpAddressesInternal().ToList();
+ var addresses = GetLocalIpAddressesInternal().Select(ToIpAddressInfo).ToList();
_localIpAddresses = addresses;
_lastRefresh = DateTime.UtcNow;
@@ -405,18 +401,85 @@ namespace Emby.Common.Implementations.Networking
IPAddress address;
if (IPAddress.TryParse(ipAddress, out address))
{
-
- ipAddressInfo = new IpAddressInfo
- {
- Address = address.ToString(),
- IsIpv6 = address.AddressFamily == AddressFamily.InterNetworkV6
- };
-
+ ipAddressInfo = ToIpAddressInfo(address);
return true;
}
ipAddressInfo = null;
return false;
}
+
+ public static IpEndPointInfo ToIpEndPointInfo(IPEndPoint endpoint)
+ {
+ if (endpoint == null)
+ {
+ return null;
+ }
+
+ return new IpEndPointInfo(ToIpAddressInfo(endpoint.Address), endpoint.Port);
+ }
+
+ public static IPEndPoint ToIPEndPoint(IpEndPointInfo endpoint)
+ {
+ if (endpoint == null)
+ {
+ return null;
+ }
+
+ return new IPEndPoint(ToIPAddress(endpoint.IpAddress), endpoint.Port);
+ }
+
+ public static IPAddress ToIPAddress(IpAddressInfo address)
+ {
+ if (address.Equals(IpAddressInfo.Any))
+ {
+ return IPAddress.Any;
+ }
+ if (address.Equals(IpAddressInfo.IPv6Any))
+ {
+ return IPAddress.IPv6Any;
+ }
+ if (address.Equals(IpAddressInfo.Loopback))
+ {
+ return IPAddress.Loopback;
+ }
+ if (address.Equals(IpAddressInfo.IPv6Loopback))
+ {
+ return IPAddress.IPv6Loopback;
+ }
+
+ return IPAddress.Parse(address.Address);
+ }
+
+ public static IpAddressInfo ToIpAddressInfo(IPAddress address)
+ {
+ if (address.Equals(IPAddress.Any))
+ {
+ return IpAddressInfo.Any;
+ }
+ if (address.Equals(IPAddress.IPv6Any))
+ {
+ return IpAddressInfo.IPv6Any;
+ }
+ if (address.Equals(IPAddress.Loopback))
+ {
+ return IpAddressInfo.Loopback;
+ }
+ if (address.Equals(IPAddress.IPv6Loopback))
+ {
+ return IpAddressInfo.IPv6Loopback;
+ }
+ return new IpAddressInfo
+ {
+ Address = address.ToString(),
+ AddressFamily = address.AddressFamily == AddressFamily.InterNetworkV6 ? IpAddressFamily.InterNetworkV6 : IpAddressFamily.InterNetwork
+ };
+ }
+
+ public async Task<IpAddressInfo[]> GetHostAddressesAsync(string host)
+ {
+ var addresses = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
+ return addresses.Select(ToIpAddressInfo).ToArray();
+ }
}
}
diff --git a/Emby.Common.Implementations/TextEncoding/TextEncoding.cs b/Emby.Common.Implementations/TextEncoding/TextEncoding.cs
index 35b869e43..254d35222 100644
--- a/Emby.Common.Implementations/TextEncoding/TextEncoding.cs
+++ b/Emby.Common.Implementations/TextEncoding/TextEncoding.cs
@@ -1,10 +1,10 @@
using System.Text;
using MediaBrowser.Model.IO;
-using MediaBrowser.Model.TextEncoding;
+using MediaBrowser.Model.Text;
namespace Emby.Common.Implementations.TextEncoding
{
- public class TextEncoding : IEncoding
+ public class TextEncoding : ITextEncoding
{
private readonly IFileSystem _fileSystem;
@@ -13,14 +13,9 @@ namespace Emby.Common.Implementations.TextEncoding
_fileSystem = fileSystem;
}
- public byte[] GetASCIIBytes(string text)
+ public Encoding GetASCIIEncoding()
{
- return Encoding.ASCII.GetBytes(text);
- }
-
- public string GetASCIIString(byte[] bytes, int startIndex, int length)
- {
- return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
+ return Encoding.ASCII;
}
public Encoding GetFileEncoding(string srcFile)