diff options
| author | stefan <stefan@hegedues.at> | 2018-09-12 19:26:21 +0200 |
|---|---|---|
| committer | stefan <stefan@hegedues.at> | 2018-09-12 19:26:21 +0200 |
| commit | 48facb797ed912e4ea6b04b17d1ff190ac2daac4 (patch) | |
| tree | 8dae77a31670a888d733484cb17dd4077d5444e8 /Emby.Server.Implementations/Net | |
| parent | c32d8656382a0eacb301692e0084377fc433ae9b (diff) | |
Update to 3.5.2 and .net core 2.1
Diffstat (limited to 'Emby.Server.Implementations/Net')
6 files changed, 117 insertions, 145 deletions
diff --git a/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs b/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs index b18335da7..b721e8a26 100644 --- a/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs +++ b/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs @@ -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 diff --git a/Emby.Server.Implementations/Net/IWebSocket.cs b/Emby.Server.Implementations/Net/IWebSocket.cs new file mode 100644 index 000000000..f79199a07 --- /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 +{ + /// <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; } + } +} diff --git a/Emby.Server.Implementations/Net/NetAcceptSocket.cs b/Emby.Server.Implementations/Net/NetAcceptSocket.cs deleted file mode 100644 index d80341a07..000000000 --- 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 bdae1728f..9726ef097 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 58e4d6f89..523ca3752 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<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); } diff --git a/Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs b/Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs new file mode 100644 index 000000000..7b7f12d50 --- /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 + { + /// <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; } + } +} |
