From 48facb797ed912e4ea6b04b17d1ff190ac2daac4 Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 12 Sep 2018 19:26:21 +0200 Subject: Update to 3.5.2 and .net core 2.1 --- .../Net/DisposableManagedObjectBase.cs | 11 +-- Emby.Server.Implementations/Net/IWebSocket.cs | 53 ++++++++++++ Emby.Server.Implementations/Net/NetAcceptSocket.cs | 98 ---------------------- Emby.Server.Implementations/Net/SocketFactory.cs | 35 -------- Emby.Server.Implementations/Net/UdpSocket.cs | 34 +++++++- .../Net/WebSocketConnectEventArgs.cs | 31 +++++++ 6 files changed, 117 insertions(+), 145 deletions(-) create mode 100644 Emby.Server.Implementations/Net/IWebSocket.cs delete mode 100644 Emby.Server.Implementations/Net/NetAcceptSocket.cs create mode 100644 Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs (limited to 'Emby.Server.Implementations/Net') diff --git a/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs b/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs index b18335da77..b721e8a262 100644 --- a/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs +++ b/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs @@ -54,16 +54,9 @@ namespace Emby.Server.Implementations.Net /// public void Dispose() { - try - { - IsDisposed = true; + IsDisposed = true; - Dispose(true); - } - finally - { - GC.SuppressFinalize(this); - } + Dispose(true); } #endregion diff --git a/Emby.Server.Implementations/Net/IWebSocket.cs b/Emby.Server.Implementations/Net/IWebSocket.cs new file mode 100644 index 0000000000..f79199a071 --- /dev/null +++ b/Emby.Server.Implementations/Net/IWebSocket.cs @@ -0,0 +1,53 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Net.WebSockets; + +namespace Emby.Server.Implementations.Net +{ + /// + /// Interface IWebSocket + /// + public interface IWebSocket : IDisposable + { + /// + /// Occurs when [closed]. + /// + event EventHandler Closed; + + /// + /// Gets or sets the state. + /// + /// The state. + WebSocketState State { get; } + + /// + /// Gets or sets the receive action. + /// + /// The receive action. + Action OnReceiveBytes { get; set; } + + /// + /// Sends the async. + /// + /// The bytes. + /// if set to true [end of message]. + /// The cancellation token. + /// Task. + Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken); + + /// + /// Sends the asynchronous. + /// + /// The text. + /// if set to true [end of message]. + /// The cancellation token. + /// Task. + Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken); + } + + public interface IMemoryWebSocket + { + Action, int> OnReceiveMemoryBytes { get; set; } + } +} diff --git a/Emby.Server.Implementations/Net/NetAcceptSocket.cs b/Emby.Server.Implementations/Net/NetAcceptSocket.cs deleted file mode 100644 index d80341a077..0000000000 --- a/Emby.Server.Implementations/Net/NetAcceptSocket.cs +++ /dev/null @@ -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); - } - } -} diff --git a/Emby.Server.Implementations/Net/SocketFactory.cs b/Emby.Server.Implementations/Net/SocketFactory.cs index bdae1728f9..9726ef0974 100644 --- a/Emby.Server.Implementations/Net/SocketFactory.cs +++ b/Emby.Server.Implementations/Net/SocketFactory.cs @@ -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"); diff --git a/Emby.Server.Implementations/Net/UdpSocket.cs b/Emby.Server.Implementations/Net/UdpSocket.cs index 58e4d6f89b..523ca3752a 100644 --- a/Emby.Server.Implementations/Net/UdpSocket.cs +++ b/Emby.Server.Implementations/Net/UdpSocket.cs @@ -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 ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { + ThrowIfDisposed(); + var taskCompletion = new TaskCompletionSource(); + bool isResultSet = false; Action 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 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(); + bool isResultSet = false; Action 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); } diff --git a/Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs b/Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs new file mode 100644 index 0000000000..7b7f12d505 --- /dev/null +++ b/Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs @@ -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 + { + /// + /// Gets or sets the URL. + /// + /// The URL. + public string Url { get; set; } + /// + /// Gets or sets the query string. + /// + /// The query string. + public QueryParamCollection QueryString { get; set; } + /// + /// Gets or sets the web socket. + /// + /// The web socket. + public IWebSocket WebSocket { get; set; } + /// + /// Gets or sets the endpoint. + /// + /// The endpoint. + public string Endpoint { get; set; } + } +} -- cgit v1.2.3