commit
a937ecfe82
|
@ -19,12 +19,20 @@ namespace Emby.Common.Implementations.Net
|
||||||
private Socket _Socket;
|
private Socket _Socket;
|
||||||
private int _LocalPort;
|
private int _LocalPort;
|
||||||
|
|
||||||
private SocketAsyncEventArgs _receiveSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
private readonly SocketAsyncEventArgs _receiveSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
||||||
|
{
|
||||||
|
SocketFlags = SocketFlags.None
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly SocketAsyncEventArgs _sendSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
||||||
{
|
{
|
||||||
SocketFlags = SocketFlags.None
|
SocketFlags = SocketFlags.None
|
||||||
};
|
};
|
||||||
|
|
||||||
private TaskCompletionSource<SocketReceiveResult> _currentReceiveTaskCompletionSource;
|
private TaskCompletionSource<SocketReceiveResult> _currentReceiveTaskCompletionSource;
|
||||||
|
private TaskCompletionSource<int> _currentSendTaskCompletionSource;
|
||||||
|
|
||||||
|
private readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1);
|
||||||
|
|
||||||
public UdpSocket(Socket socket, int localPort, IPAddress ip)
|
public UdpSocket(Socket socket, int localPort, IPAddress ip)
|
||||||
{
|
{
|
||||||
|
@ -41,9 +49,13 @@ namespace Emby.Common.Implementations.Net
|
||||||
|
|
||||||
private void InitReceiveSocketAsyncEventArgs()
|
private void InitReceiveSocketAsyncEventArgs()
|
||||||
{
|
{
|
||||||
var buffer = new byte[8192];
|
var receiveBuffer = new byte[8192];
|
||||||
_receiveSocketAsyncEventArgs.SetBuffer(buffer, 0, buffer.Length);
|
_receiveSocketAsyncEventArgs.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
|
||||||
_receiveSocketAsyncEventArgs.Completed += _receiveSocketAsyncEventArgs_Completed;
|
_receiveSocketAsyncEventArgs.Completed += _receiveSocketAsyncEventArgs_Completed;
|
||||||
|
|
||||||
|
var sendBuffer = new byte[8192];
|
||||||
|
_sendSocketAsyncEventArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
|
||||||
|
_sendSocketAsyncEventArgs.Completed += _sendSocketAsyncEventArgs_Completed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _receiveSocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
|
private void _receiveSocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
|
||||||
|
@ -53,6 +65,8 @@ namespace Emby.Common.Implementations.Net
|
||||||
{
|
{
|
||||||
_currentReceiveTaskCompletionSource = null;
|
_currentReceiveTaskCompletionSource = null;
|
||||||
|
|
||||||
|
if (e.SocketError == SocketError.Success)
|
||||||
|
{
|
||||||
tcs.TrySetResult(new SocketReceiveResult
|
tcs.TrySetResult(new SocketReceiveResult
|
||||||
{
|
{
|
||||||
Buffer = e.Buffer,
|
Buffer = e.Buffer,
|
||||||
|
@ -61,6 +75,29 @@ namespace Emby.Common.Implementations.Net
|
||||||
LocalIPAddress = LocalIPAddress
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UdpSocket(Socket socket, IpEndPointInfo endPoint)
|
public UdpSocket(Socket socket, IpEndPointInfo endPoint)
|
||||||
|
@ -79,8 +116,6 @@ namespace Emby.Common.Implementations.Net
|
||||||
private set;
|
private set;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region ISocket Members
|
|
||||||
|
|
||||||
public Task<SocketReceiveResult> ReceiveAsync(CancellationToken cancellationToken)
|
public Task<SocketReceiveResult> ReceiveAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
|
@ -90,31 +125,15 @@ namespace Emby.Common.Implementations.Net
|
||||||
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||||
cancellationToken.Register(() => tcs.TrySetCanceled());
|
cancellationToken.Register(() => tcs.TrySetCanceled());
|
||||||
|
|
||||||
#if NETSTANDARD1_6
|
|
||||||
var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
|
|
||||||
state.TaskCompletionSource = tcs;
|
|
||||||
|
|
||||||
_Socket.ReceiveFromAsync(new ArraySegment<Byte>(state.Buffer), SocketFlags.None, state.RemoteEndPoint)
|
|
||||||
.ContinueWith((task, asyncState) =>
|
|
||||||
{
|
|
||||||
if (task.Status != TaskStatus.Faulted)
|
|
||||||
{
|
|
||||||
var receiveState = asyncState as AsyncReceiveState;
|
|
||||||
receiveState.RemoteEndPoint = task.Result.RemoteEndPoint;
|
|
||||||
ProcessResponse(receiveState, () => task.Result.ReceivedBytes, LocalIPAddress);
|
|
||||||
}
|
|
||||||
}, state);
|
|
||||||
#else
|
|
||||||
//var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
|
|
||||||
//state.TaskCompletionSource = tcs;
|
|
||||||
|
|
||||||
//_Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.RemoteEndPoint, ProcessResponse, state);
|
|
||||||
|
|
||||||
_receiveSocketAsyncEventArgs.RemoteEndPoint = receivedFromEndPoint;
|
_receiveSocketAsyncEventArgs.RemoteEndPoint = receivedFromEndPoint;
|
||||||
_currentReceiveTaskCompletionSource = tcs;
|
_currentReceiveTaskCompletionSource = tcs;
|
||||||
|
|
||||||
var isPending = _Socket.ReceiveFromAsync(_receiveSocketAsyncEventArgs);
|
var willRaiseEvent = _Socket.ReceiveFromAsync(_receiveSocketAsyncEventArgs);
|
||||||
#endif
|
|
||||||
|
if (!willRaiseEvent)
|
||||||
|
{
|
||||||
|
_receiveSocketAsyncEventArgs_Completed(this, _receiveSocketAsyncEventArgs);
|
||||||
|
}
|
||||||
|
|
||||||
return tcs.Task;
|
return tcs.Task;
|
||||||
}
|
}
|
||||||
|
@ -126,59 +145,41 @@ namespace Emby.Common.Implementations.Net
|
||||||
if (buffer == null) throw new ArgumentNullException("messageData");
|
if (buffer == null) throw new ArgumentNullException("messageData");
|
||||||
if (endPoint == null) throw new ArgumentNullException("endPoint");
|
if (endPoint == null) throw new ArgumentNullException("endPoint");
|
||||||
|
|
||||||
var ipEndPoint = NetworkManager.ToIPEndPoint(endPoint);
|
|
||||||
|
|
||||||
#if NETSTANDARD1_6
|
|
||||||
|
|
||||||
if (size != buffer.Length)
|
|
||||||
{
|
|
||||||
byte[] copy = new byte[size];
|
|
||||||
Buffer.BlockCopy(buffer, 0, copy, 0, size);
|
|
||||||
buffer = copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
_Socket.SendTo(buffer, ipEndPoint);
|
var tcs = new TaskCompletionSource<int>();
|
||||||
return Task.FromResult(true);
|
|
||||||
#else
|
cancellationToken.Register(() => tcs.TrySetCanceled());
|
||||||
var taskSource = new TaskCompletionSource<bool>();
|
|
||||||
|
_sendSocketAsyncEventArgs.SetBuffer(buffer, 0, size);
|
||||||
|
_sendSocketAsyncEventArgs.RemoteEndPoint = NetworkManager.ToIPEndPoint(endPoint);
|
||||||
|
_currentSendTaskCompletionSource = tcs;
|
||||||
|
|
||||||
|
var willRaiseEvent = _Socket.SendAsync(_sendSocketAsyncEventArgs);
|
||||||
|
|
||||||
|
if (!willRaiseEvent)
|
||||||
|
{
|
||||||
|
_sendSocketAsyncEventArgs_Completed(this, _sendSocketAsyncEventArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tcs.Task;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendWithLockAsync(byte[] buffer, int size, IpEndPointInfo endPoint, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ThrowIfDisposed();
|
||||||
|
|
||||||
|
await _sendLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Socket.BeginSendTo(buffer, 0, size, SocketFlags.None, ipEndPoint, result =>
|
await SendAsync(buffer, size, endPoint, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
finally
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested)
|
_sendLock.Release();
|
||||||
{
|
|
||||||
taskSource.TrySetCanceled();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
try
|
|
||||||
{
|
|
||||||
_Socket.EndSend(result);
|
|
||||||
taskSource.TrySetResult(true);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
taskSource.TrySetException(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
}, null);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
taskSource.TrySetException(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
//_Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(RemoteEndPoint.IPAddress), RemoteEndPoint.Port));
|
|
||||||
|
|
||||||
return taskSource.Task;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Overrides
|
|
||||||
|
|
||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
|
@ -187,44 +188,19 @@ namespace Emby.Common.Implementations.Net
|
||||||
var socket = _Socket;
|
var socket = _Socket;
|
||||||
if (socket != null)
|
if (socket != null)
|
||||||
socket.Dispose();
|
socket.Dispose();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
_sendLock.Dispose();
|
||||||
|
|
||||||
#region Private Methods
|
var tcs = _currentReceiveTaskCompletionSource;
|
||||||
|
if (tcs != null)
|
||||||
private static void ProcessResponse(AsyncReceiveState state, Func<int> receiveData, IpAddressInfo localIpAddress)
|
|
||||||
{
|
{
|
||||||
try
|
tcs.TrySetCanceled();
|
||||||
{
|
|
||||||
var bytesRead = receiveData();
|
|
||||||
|
|
||||||
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
|
|
||||||
state.TaskCompletionSource.TrySetResult(
|
|
||||||
new SocketReceiveResult
|
|
||||||
{
|
|
||||||
Buffer = state.Buffer,
|
|
||||||
ReceivedBytes = bytesRead,
|
|
||||||
RemoteEndPoint = ToIpEndPointInfo(ipEndPoint),
|
|
||||||
LocalIPAddress = localIpAddress
|
|
||||||
}
|
}
|
||||||
);
|
var sendTcs = _currentSendTaskCompletionSource;
|
||||||
}
|
if (sendTcs != null)
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
{
|
||||||
state.TaskCompletionSource.TrySetCanceled();
|
sendTcs.TrySetCanceled();
|
||||||
}
|
}
|
||||||
catch (SocketException se)
|
|
||||||
{
|
|
||||||
if (se.SocketErrorCode != SocketError.Interrupted && se.SocketErrorCode != SocketError.OperationAborted && se.SocketErrorCode != SocketError.Shutdown)
|
|
||||||
state.TaskCompletionSource.TrySetException(se);
|
|
||||||
else
|
|
||||||
state.TaskCompletionSource.TrySetCanceled();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
state.TaskCompletionSource.TrySetException(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,59 +213,5 @@ namespace Emby.Common.Implementations.Net
|
||||||
|
|
||||||
return NetworkManager.ToIpEndPointInfo(endpoint);
|
return NetworkManager.ToIpEndPointInfo(endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessResponse(IAsyncResult asyncResult)
|
|
||||||
{
|
|
||||||
#if NET46
|
|
||||||
var state = asyncResult.AsyncState as AsyncReceiveState;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var bytesRead = state.Socket.EndReceiveFrom(asyncResult, ref state.RemoteEndPoint);
|
|
||||||
|
|
||||||
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
|
|
||||||
state.TaskCompletionSource.TrySetResult(
|
|
||||||
new SocketReceiveResult
|
|
||||||
{
|
|
||||||
Buffer = state.Buffer,
|
|
||||||
ReceivedBytes = bytesRead,
|
|
||||||
RemoteEndPoint = ToIpEndPointInfo(ipEndPoint),
|
|
||||||
LocalIPAddress = LocalIPAddress
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (ObjectDisposedException)
|
|
||||||
{
|
|
||||||
state.TaskCompletionSource.TrySetCanceled();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
state.TaskCompletionSource.TrySetException(ex);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Classes
|
|
||||||
|
|
||||||
private class AsyncReceiveState
|
|
||||||
{
|
|
||||||
public AsyncReceiveState(Socket socket, EndPoint remoteEndPoint)
|
|
||||||
{
|
|
||||||
this.Socket = socket;
|
|
||||||
this.RemoteEndPoint = remoteEndPoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EndPoint RemoteEndPoint;
|
|
||||||
public byte[] Buffer = new byte[8192];
|
|
||||||
|
|
||||||
public Socket Socket { get; private set; }
|
|
||||||
|
|
||||||
public TaskCompletionSource<SocketReceiveResult> TaskCompletionSource { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -421,17 +421,6 @@ namespace Emby.Server.Core.IO
|
||||||
|
|
||||||
var path = e.FullPath;
|
var path = e.FullPath;
|
||||||
|
|
||||||
// For deletes, use the parent path
|
|
||||||
if (e.ChangeType == WatcherChangeTypes.Deleted)
|
|
||||||
{
|
|
||||||
var parentPath = Path.GetDirectoryName(path);
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(parentPath))
|
|
||||||
{
|
|
||||||
path = parentPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ReportFileSystemChanged(path);
|
ReportFileSystemChanged(path);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
@ -203,19 +203,6 @@ namespace Emby.Server.Implementations.Udp
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops this instance.
|
|
||||||
/// </summary>
|
|
||||||
public void Stop()
|
|
||||||
{
|
|
||||||
_isDisposed = true;
|
|
||||||
|
|
||||||
if (_udpClient != null)
|
|
||||||
{
|
|
||||||
_udpClient.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Releases unmanaged and - optionally - managed resources.
|
/// Releases unmanaged and - optionally - managed resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -224,7 +211,12 @@ namespace Emby.Server.Implementations.Udp
|
||||||
{
|
{
|
||||||
if (dispose)
|
if (dispose)
|
||||||
{
|
{
|
||||||
Stop();
|
_isDisposed = true;
|
||||||
|
|
||||||
|
if (_udpClient != null)
|
||||||
|
{
|
||||||
|
_udpClient.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,9 +239,13 @@ namespace Emby.Server.Implementations.Udp
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _udpClient.SendAsync(bytes, bytes.Length, remoteEndPoint, CancellationToken.None).ConfigureAwait(false);
|
await _udpClient.SendWithLockAsync(bytes, bytes.Length, remoteEndPoint, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
|
||||||
_logger.Info("Udp message sent to {0}", remoteEndPoint);
|
_logger.Info("Udp message sent to {0}", remoteEndPoint);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,5 +24,6 @@ namespace MediaBrowser.Model.Net
|
||||||
/// Sends a UDP message to a particular end point (uni or multicast).
|
/// Sends a UDP message to a particular end point (uni or multicast).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task SendAsync(byte[] buffer, int bytes, IpEndPointInfo endPoint, CancellationToken cancellationToken);
|
Task SendAsync(byte[] buffer, int bytes, IpEndPointInfo endPoint, CancellationToken cancellationToken);
|
||||||
|
Task SendWithLockAsync(byte[] buffer, int bytes, IpEndPointInfo endPoint, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -177,11 +177,15 @@ namespace Rssdp.Infrastructure
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await socket.SendAsync(messageData, messageData.Length, destination, cancellationToken).ConfigureAwait(false);
|
await socket.SendWithLockAsync(messageData, messageData.Length, destination, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException)
|
catch (ObjectDisposedException)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -341,11 +345,9 @@ namespace Rssdp.Infrastructure
|
||||||
|
|
||||||
foreach (var socket in sockets)
|
foreach (var socket in sockets)
|
||||||
{
|
{
|
||||||
await socket.SendAsync(messageData, messageData.Length, destination, cancellationToken).ConfigureAwait(false);
|
await SendFromSocket(socket, messageData, destination, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ThrowIfDisposed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ISocket ListenForBroadcastsAsync()
|
private ISocket ListenForBroadcastsAsync()
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
[assembly: AssemblyVersion("3.2.8.13")]
|
[assembly: AssemblyVersion("3.2.8.14")]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user