Update to 3.5.2 and .net core 2.1

This commit is contained in:
stefan
2018-09-12 19:26:21 +02:00
parent c32d865638
commit 48facb797e
1419 changed files with 27525 additions and 88927 deletions

View File

@@ -54,16 +54,9 @@ namespace Emby.Server.Implementations.Net
/// <seealso cref="IsDisposed"/>
public void Dispose()
{
try
{
IsDisposed = true;
IsDisposed = true;
Dispose(true);
}
finally
{
GC.SuppressFinalize(this);
}
Dispose(true);
}
#endregion

View File

@@ -0,0 +1,53 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net.WebSockets;
namespace Emby.Server.Implementations.Net
{
/// <summary>
/// Interface IWebSocket
/// </summary>
public interface IWebSocket : IDisposable
{
/// <summary>
/// Occurs when [closed].
/// </summary>
event EventHandler<EventArgs> Closed;
/// <summary>
/// Gets or sets the state.
/// </summary>
/// <value>The state.</value>
WebSocketState State { get; }
/// <summary>
/// Gets or sets the receive action.
/// </summary>
/// <value>The receive action.</value>
Action<byte[]> OnReceiveBytes { get; set; }
/// <summary>
/// Sends the async.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken);
/// <summary>
/// Sends the asynchronous.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken);
}
public interface IMemoryWebSocket
{
Action<Memory<byte>, int> OnReceiveMemoryBytes { get; set; }
}
}

View File

@@ -1,98 +0,0 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Emby.Server.Implementations.Networking;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
namespace Emby.Server.Implementations.Net
{
public class NetAcceptSocket : IAcceptSocket
{
public Socket Socket { get; private set; }
private readonly ILogger _logger;
public bool DualMode { get; private set; }
public NetAcceptSocket(Socket socket, ILogger logger, bool isDualMode)
{
if (socket == null)
{
throw new ArgumentNullException("socket");
}
if (logger == null)
{
throw new ArgumentNullException("logger");
}
Socket = socket;
_logger = logger;
DualMode = isDualMode;
}
public IpEndPointInfo LocalEndPoint
{
get
{
return NetworkManager.ToIpEndPointInfo((IPEndPoint)Socket.LocalEndPoint);
}
}
public IpEndPointInfo RemoteEndPoint
{
get
{
return NetworkManager.ToIpEndPointInfo((IPEndPoint)Socket.RemoteEndPoint);
}
}
public void Connect(IpEndPointInfo endPoint)
{
var nativeEndpoint = NetworkManager.ToIPEndPoint(endPoint);
Socket.Connect(nativeEndpoint);
}
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 = NetworkManager.ToIPEndPoint(endpoint);
Socket.Bind(nativeEndpoint);
}
public void Dispose()
{
Socket.Dispose();
GC.SuppressFinalize(this);
}
}
}

View File

@@ -29,41 +29,6 @@ namespace Emby.Server.Implementations.Net
_logger = logger;
}
public IAcceptSocket CreateSocket(IpAddressFamily family, MediaBrowser.Model.Net.SocketType socketType, MediaBrowser.Model.Net.ProtocolType protocolType, bool dualMode)
{
try
{
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 NetAcceptSocket(socket, _logger, dualMode);
}
catch (SocketException ex)
{
throw new SocketCreateException(ex.SocketErrorCode.ToString(), ex);
}
catch (ArgumentException ex)
{
if (dualMode)
{
// Mono for BSD incorrectly throws ArgumentException instead of SocketException
throw new SocketCreateException("AddressFamilyNotSupported", ex);
}
else
{
throw;
}
}
}
public ISocket CreateTcpSocket(IpAddressInfo remoteAddress, int remotePort)
{
if (remotePort < 0) throw new ArgumentException("remotePort cannot be less than zero.", "remotePort");

View File

@@ -118,6 +118,8 @@ namespace Emby.Server.Implementations.Net
public IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback)
{
ThrowIfDisposed();
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
return _Socket.BeginReceiveFrom(buffer, offset, count, SocketFlags.None, ref receivedFromEndPoint, callback, buffer);
@@ -125,17 +127,21 @@ namespace Emby.Server.Implementations.Net
public int Receive(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
return _Socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
}
public SocketReceiveResult EndReceive(IAsyncResult result)
{
ThrowIfDisposed();
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteEndPoint = (EndPoint)sender;
var receivedBytes = _Socket.EndReceiveFrom(result, ref remoteEndPoint);
var buffer = (byte[]) result.AsyncState;
var buffer = (byte[])result.AsyncState;
return new SocketReceiveResult
{
@@ -148,13 +154,20 @@ namespace Emby.Server.Implementations.Net
public Task<SocketReceiveResult> ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ThrowIfDisposed();
var taskCompletion = new TaskCompletionSource<SocketReceiveResult>();
bool isResultSet = false;
Action<IAsyncResult> callback = callbackResult =>
{
try
{
taskCompletion.TrySetResult(EndReceive(callbackResult));
if (!isResultSet)
{
isResultSet = true;
taskCompletion.TrySetResult(EndReceive(callbackResult));
}
}
catch (Exception ex)
{
@@ -167,6 +180,7 @@ namespace Emby.Server.Implementations.Net
if (result.CompletedSynchronously)
{
callback(result);
return taskCompletion.Task;
}
cancellationToken.Register(() => taskCompletion.TrySetCanceled());
@@ -176,6 +190,8 @@ namespace Emby.Server.Implementations.Net
public Task<SocketReceiveResult> ReceiveAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
var buffer = new byte[8192];
return ReceiveAsync(buffer, 0, buffer.Length, cancellationToken);
@@ -183,13 +199,20 @@ namespace Emby.Server.Implementations.Net
public Task SendToAsync(byte[] buffer, int offset, int size, IpEndPointInfo endPoint, CancellationToken cancellationToken)
{
ThrowIfDisposed();
var taskCompletion = new TaskCompletionSource<int>();
bool isResultSet = false;
Action<IAsyncResult> callback = callbackResult =>
{
try
{
taskCompletion.TrySetResult(EndSendTo(callbackResult));
if (!isResultSet)
{
isResultSet = true;
taskCompletion.TrySetResult(EndSendTo(callbackResult));
}
}
catch (Exception ex)
{
@@ -202,6 +225,7 @@ namespace Emby.Server.Implementations.Net
if (result.CompletedSynchronously)
{
callback(result);
return taskCompletion.Task;
}
cancellationToken.Register(() => taskCompletion.TrySetCanceled());
@@ -211,6 +235,8 @@ namespace Emby.Server.Implementations.Net
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, IpEndPointInfo endPoint, AsyncCallback callback, object state)
{
ThrowIfDisposed();
var ipEndPoint = NetworkManager.ToIPEndPoint(endPoint);
return _Socket.BeginSendTo(buffer, offset, size, SocketFlags.None, ipEndPoint, callback, state);
@@ -218,6 +244,8 @@ namespace Emby.Server.Implementations.Net
public int EndSendTo(IAsyncResult result)
{
ThrowIfDisposed();
return _Socket.EndSendTo(result);
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using MediaBrowser.Model.Services;
namespace Emby.Server.Implementations.Net
{
public class WebSocketConnectEventArgs : EventArgs
{
/// <summary>
/// Gets or sets the URL.
/// </summary>
/// <value>The URL.</value>
public string Url { get; set; }
/// <summary>
/// Gets or sets the query string.
/// </summary>
/// <value>The query string.</value>
public QueryParamCollection QueryString { get; set; }
/// <summary>
/// Gets or sets the web socket.
/// </summary>
/// <value>The web socket.</value>
public IWebSocket WebSocket { get; set; }
/// <summary>
/// Gets or sets the endpoint.
/// </summary>
/// <value>The endpoint.</value>
public string Endpoint { get; set; }
}
}