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

@@ -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; }
}