2016-11-04 04:31:05 -04:00
|
|
|
|
using System;
|
2016-10-29 18:22:20 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Security;
|
2017-02-05 15:44:08 -05:00
|
|
|
|
using System.Threading;
|
2016-10-29 18:22:20 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-08 13:44:23 -05:00
|
|
|
|
using Emby.Common.Implementations.Networking;
|
2016-11-04 04:31:05 -04:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
2016-11-04 04:31:05 -04:00
|
|
|
|
namespace Emby.Common.Implementations.Net
|
2016-10-29 18:22:20 -04:00
|
|
|
|
{
|
|
|
|
|
// THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS
|
|
|
|
|
// Be careful to check any changes compile and work for all platform projects it is shared in.
|
|
|
|
|
|
2017-03-02 15:50:09 -05:00
|
|
|
|
internal sealed class UdpSocket : DisposableManagedObjectBase, ISocket
|
2016-10-29 18:22:20 -04:00
|
|
|
|
{
|
2016-11-04 15:51:59 -04:00
|
|
|
|
private Socket _Socket;
|
2016-10-29 18:22:20 -04:00
|
|
|
|
private int _LocalPort;
|
2017-03-26 12:26:52 -04:00
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
private readonly SocketAsyncEventArgs _receiveSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
|
|
|
|
{
|
|
|
|
|
SocketFlags = SocketFlags.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly SocketAsyncEventArgs _sendSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
2017-03-26 12:26:52 -04:00
|
|
|
|
{
|
|
|
|
|
SocketFlags = SocketFlags.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private TaskCompletionSource<SocketReceiveResult> _currentReceiveTaskCompletionSource;
|
2017-03-26 15:00:35 -04:00
|
|
|
|
private TaskCompletionSource<int> _currentSendTaskCompletionSource;
|
|
|
|
|
|
|
|
|
|
private readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1);
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
2016-11-04 15:51:59 -04:00
|
|
|
|
public UdpSocket(Socket socket, int localPort, IPAddress ip)
|
2016-10-29 18:22:20 -04:00
|
|
|
|
{
|
|
|
|
|
if (socket == null) throw new ArgumentNullException("socket");
|
|
|
|
|
|
|
|
|
|
_Socket = socket;
|
|
|
|
|
_LocalPort = localPort;
|
2016-12-04 16:30:38 -05:00
|
|
|
|
LocalIPAddress = NetworkManager.ToIpAddressInfo(ip);
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
|
|
|
|
_Socket.Bind(new IPEndPoint(ip, _LocalPort));
|
2017-03-26 12:26:52 -04:00
|
|
|
|
|
|
|
|
|
InitReceiveSocketAsyncEventArgs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitReceiveSocketAsyncEventArgs()
|
|
|
|
|
{
|
2017-03-26 15:00:35 -04:00
|
|
|
|
var receiveBuffer = new byte[8192];
|
|
|
|
|
_receiveSocketAsyncEventArgs.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
|
2017-03-26 12:26:52 -04:00
|
|
|
|
_receiveSocketAsyncEventArgs.Completed += _receiveSocketAsyncEventArgs_Completed;
|
2017-03-26 15:00:35 -04:00
|
|
|
|
|
|
|
|
|
var sendBuffer = new byte[8192];
|
|
|
|
|
_sendSocketAsyncEventArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
|
|
|
|
|
_sendSocketAsyncEventArgs.Completed += _sendSocketAsyncEventArgs_Completed;
|
2017-03-26 12:26:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _receiveSocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var tcs = _currentReceiveTaskCompletionSource;
|
|
|
|
|
if (tcs != null)
|
|
|
|
|
{
|
|
|
|
|
_currentReceiveTaskCompletionSource = null;
|
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
if (e.SocketError == SocketError.Success)
|
2017-03-26 12:26:52 -04:00
|
|
|
|
{
|
2017-03-26 15:00:35 -04:00
|
|
|
|
tcs.TrySetResult(new SocketReceiveResult
|
|
|
|
|
{
|
|
|
|
|
Buffer = e.Buffer,
|
|
|
|
|
ReceivedBytes = e.BytesTransferred,
|
|
|
|
|
RemoteEndPoint = ToIpEndPointInfo(e.RemoteEndPoint as IPEndPoint),
|
|
|
|
|
LocalIPAddress = LocalIPAddress
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tcs.TrySetException(new Exception("SocketError: " + e.SocketError));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _sendSocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var tcs = _currentSendTaskCompletionSource;
|
|
|
|
|
if (tcs != null)
|
|
|
|
|
{
|
|
|
|
|
_currentSendTaskCompletionSource = null;
|
|
|
|
|
|
|
|
|
|
if (e.SocketError == SocketError.Success)
|
|
|
|
|
{
|
|
|
|
|
tcs.TrySetResult(e.BytesTransferred);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tcs.TrySetException(new Exception("SocketError: " + e.SocketError));
|
|
|
|
|
}
|
2017-03-26 12:26:52 -04:00
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-02 15:50:09 -05:00
|
|
|
|
public UdpSocket(Socket socket, IpEndPointInfo endPoint)
|
|
|
|
|
{
|
|
|
|
|
if (socket == null) throw new ArgumentNullException("socket");
|
|
|
|
|
|
|
|
|
|
_Socket = socket;
|
|
|
|
|
_Socket.Connect(NetworkManager.ToIPEndPoint(endPoint));
|
2017-03-26 12:26:52 -04:00
|
|
|
|
|
|
|
|
|
InitReceiveSocketAsyncEventArgs();
|
2017-03-02 15:50:09 -05:00
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
2016-12-04 16:30:38 -05:00
|
|
|
|
public IpAddressInfo LocalIPAddress
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-02 15:50:09 -05:00
|
|
|
|
public Task<SocketReceiveResult> ReceiveAsync(CancellationToken cancellationToken)
|
2016-10-29 18:22:20 -04:00
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
|
2016-11-04 14:56:47 -04:00
|
|
|
|
var tcs = new TaskCompletionSource<SocketReceiveResult>();
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
2016-11-04 15:51:59 -04:00
|
|
|
|
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
2017-03-13 00:49:10 -04:00
|
|
|
|
cancellationToken.Register(() => tcs.TrySetCanceled());
|
|
|
|
|
|
2017-03-26 12:26:52 -04:00
|
|
|
|
_receiveSocketAsyncEventArgs.RemoteEndPoint = receivedFromEndPoint;
|
|
|
|
|
_currentReceiveTaskCompletionSource = tcs;
|
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
var willRaiseEvent = _Socket.ReceiveFromAsync(_receiveSocketAsyncEventArgs);
|
|
|
|
|
|
|
|
|
|
if (!willRaiseEvent)
|
|
|
|
|
{
|
|
|
|
|
_receiveSocketAsyncEventArgs_Completed(this, _receiveSocketAsyncEventArgs);
|
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
|
|
|
|
return tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-05 15:44:08 -05:00
|
|
|
|
public Task SendAsync(byte[] buffer, int size, IpEndPointInfo endPoint, CancellationToken cancellationToken)
|
2016-10-29 18:22:20 -04:00
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
|
|
|
|
|
2016-11-04 14:56:47 -04:00
|
|
|
|
if (buffer == null) throw new ArgumentNullException("messageData");
|
2016-10-29 18:22:20 -04:00
|
|
|
|
if (endPoint == null) throw new ArgumentNullException("endPoint");
|
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var tcs = new TaskCompletionSource<int>();
|
|
|
|
|
|
|
|
|
|
cancellationToken.Register(() => tcs.TrySetCanceled());
|
|
|
|
|
|
|
|
|
|
_sendSocketAsyncEventArgs.SetBuffer(buffer, 0, size);
|
|
|
|
|
_sendSocketAsyncEventArgs.RemoteEndPoint = NetworkManager.ToIPEndPoint(endPoint);
|
|
|
|
|
_currentSendTaskCompletionSource = tcs;
|
2016-12-04 16:30:38 -05:00
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
var willRaiseEvent = _Socket.SendAsync(_sendSocketAsyncEventArgs);
|
2016-11-04 14:56:47 -04:00
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
if (!willRaiseEvent)
|
2016-11-04 14:56:47 -04:00
|
|
|
|
{
|
2017-03-26 15:00:35 -04:00
|
|
|
|
_sendSocketAsyncEventArgs_Completed(this, _sendSocketAsyncEventArgs);
|
2016-11-04 14:56:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
return tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SendWithLockAsync(byte[] buffer, int size, IpEndPointInfo endPoint, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfDisposed();
|
2017-02-05 15:44:08 -05:00
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
await _sendLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-26 15:00:35 -04:00
|
|
|
|
await SendAsync(buffer, size, endPoint, cancellationToken).ConfigureAwait(false);
|
2016-10-29 18:22:20 -04:00
|
|
|
|
}
|
2017-03-26 15:00:35 -04:00
|
|
|
|
finally
|
2016-10-29 18:22:20 -04:00
|
|
|
|
{
|
2017-03-26 15:00:35 -04:00
|
|
|
|
_sendLock.Release();
|
2016-10-29 18:22:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
var socket = _Socket;
|
|
|
|
|
if (socket != null)
|
|
|
|
|
socket.Dispose();
|
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
_sendLock.Dispose();
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
2017-03-26 15:00:35 -04:00
|
|
|
|
var tcs = _currentReceiveTaskCompletionSource;
|
|
|
|
|
if (tcs != null)
|
|
|
|
|
{
|
|
|
|
|
tcs.TrySetCanceled();
|
|
|
|
|
}
|
|
|
|
|
var sendTcs = _currentSendTaskCompletionSource;
|
|
|
|
|
if (sendTcs != null)
|
|
|
|
|
{
|
|
|
|
|
sendTcs.TrySetCanceled();
|
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 04:31:05 -04:00
|
|
|
|
private static IpEndPointInfo ToIpEndPointInfo(IPEndPoint endpoint)
|
|
|
|
|
{
|
|
|
|
|
if (endpoint == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 02:24:36 -05:00
|
|
|
|
return NetworkManager.ToIpEndPointInfo(endpoint);
|
2016-11-04 04:31:05 -04:00
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
}
|
2016-11-04 04:31:05 -04:00
|
|
|
|
}
|