aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs
diff options
context:
space:
mode:
authorBond-009 <bond.009@outlook.com>2019-03-07 21:08:57 +0100
committerGitHub <noreply@github.com>2019-03-07 21:08:57 +0100
commit10a0d6bdba821449abfb1d48e9708ba6f3fc6a62 (patch)
tree602be322daedca127ba66de07837ac8e792730a7 /Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs
parentae0ecc1b10982d9240ecdcc82cb7299fc708aafb (diff)
parent0abe57e930e44eab9566991f33b089d1e61cfb83 (diff)
Merge pull request #1010 from cvium/kestrel_poc
Remove System.Net and port to Kestrel
Diffstat (limited to 'Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs')
-rw-r--r--Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs105
1 files changed, 105 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs b/Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs
new file mode 100644
index 000000000..62b16ed8c
--- /dev/null
+++ b/Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs
@@ -0,0 +1,105 @@
+using System;
+using System.Net.WebSockets;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Emby.Server.Implementations.Net;
+using Microsoft.Extensions.Logging;
+
+namespace Emby.Server.Implementations.SocketSharp
+{
+ public class SharpWebSocket : IWebSocket
+ {
+ /// <summary>
+ /// The logger
+ /// </summary>
+ private readonly ILogger _logger;
+
+ public event EventHandler<EventArgs> Closed;
+
+ /// <summary>
+ /// Gets or sets the web socket.
+ /// </summary>
+ /// <value>The web socket.</value>
+ private readonly WebSocket _webSocket;
+
+ private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
+ private bool _disposed;
+
+ public SharpWebSocket(WebSocket socket, ILogger logger)
+ {
+ _logger = logger ?? throw new ArgumentNullException(nameof(logger));
+ _webSocket = socket ?? throw new ArgumentNullException(nameof(socket));
+ }
+
+ /// <summary>
+ /// Gets or sets the state.
+ /// </summary>
+ /// <value>The state.</value>
+ public WebSocketState State => _webSocket.State;
+
+ /// <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>
+ public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
+ {
+ return _webSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Binary, endOfMessage, 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>
+ public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken)
+ {
+ return _webSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
+ }
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ /// <summary>
+ /// Releases unmanaged and - optionally - managed resources.
+ /// </summary>
+ /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
+ protected virtual void Dispose(bool dispose)
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ if (dispose)
+ {
+ _cancellationTokenSource.Cancel();
+ if (_webSocket.State == WebSocketState.Open)
+ {
+ _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed by client",
+ CancellationToken.None);
+ }
+ Closed?.Invoke(this, EventArgs.Empty);
+ }
+
+ _disposed = true;
+ }
+
+ /// <summary>
+ /// Gets or sets the receive action.
+ /// </summary>
+ /// <value>The receive action.</value>
+ public Action<byte[]> OnReceiveBytes { get; set; }
+ }
+}