move classes to new server project

This commit is contained in:
Luke Pulverenti
2016-11-04 14:56:47 -04:00
parent a7b11c8ee9
commit 72aaecb279
25 changed files with 186 additions and 308 deletions

View File

@@ -37,22 +37,46 @@ namespace Emby.Common.Implementations.Net
#region ISocketFactory Members
/// <summary>
/// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
/// Creates a new UDP socket and binds it to the specified local port.
/// </summary>
/// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
/// <returns>An implementation of the <see cref="IUdpSocket"/> interface used by RSSDP components to perform socket operations.</returns>
[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;
}
}
/// <summary>
/// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port.
/// </summary>
/// <param name="localPort">An integer specifying the local port to bind the socket to.</param>
/// <returns>An implementation of the <see cref="IUdpSocket"/> interface used by RSSDP components to perform socket operations.</returns>
[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);
try
{
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
{

View File

@@ -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<ReceivedUdpData> ReceiveAsync()
public Task<SocketReceiveResult> ReceiveAsync()
{
ThrowIfDisposed();
var tcs = new TaskCompletionSource<ReceivedUdpData>();
var tcs = new TaskCompletionSource<SocketReceiveResult>();
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<bool>();
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<ReceivedUdpData> TaskCompletionSource { get; set; }
public TaskCompletionSource<SocketReceiveResult> TaskCompletionSource { get; set; }
}