aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs
diff options
context:
space:
mode:
authorstefan <stefan@hegedues.at>2018-09-12 19:26:21 +0200
committerstefan <stefan@hegedues.at>2018-09-12 19:26:21 +0200
commit48facb797ed912e4ea6b04b17d1ff190ac2daac4 (patch)
tree8dae77a31670a888d733484cb17dd4077d5444e8 /MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs
parentc32d8656382a0eacb301692e0084377fc433ae9b (diff)
Update to 3.5.2 and .net core 2.1
Diffstat (limited to 'MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs')
-rw-r--r--MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs159
1 files changed, 159 insertions, 0 deletions
diff --git a/MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs b/MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs
new file mode 100644
index 000000000..6c2dd0f76
--- /dev/null
+++ b/MediaBrowser.Server.Mono/SocketSharp/SharpWebSocket.cs
@@ -0,0 +1,159 @@
+using MediaBrowser.Common.Events;
+using MediaBrowser.Model.Logging;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Net.WebSockets;
+using Emby.Server.Implementations.Net;
+
+namespace EmbyServer.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 SocketHttpListener.WebSocket WebSocket { get; set; }
+
+ private TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
+ private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
+
+ public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger)
+ {
+ if (socket == null)
+ {
+ throw new ArgumentNullException("socket");
+ }
+
+ if (logger == null)
+ {
+ throw new ArgumentNullException("logger");
+ }
+
+ _logger = logger;
+ WebSocket = socket;
+
+ socket.OnMessage += socket_OnMessage;
+ socket.OnClose += socket_OnClose;
+ socket.OnError += socket_OnError;
+
+ WebSocket.ConnectAsServer();
+ }
+
+ public Task StartReceive()
+ {
+ return _taskCompletionSource.Task;
+ }
+
+ void socket_OnError(object sender, SocketHttpListener.ErrorEventArgs e)
+ {
+ _logger.Error("Error in SharpWebSocket: {0}", e.Message ?? string.Empty);
+ //EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger);
+ }
+
+ void socket_OnClose(object sender, SocketHttpListener.CloseEventArgs e)
+ {
+ _taskCompletionSource.TrySetResult(true);
+
+ EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger);
+ }
+
+ void socket_OnMessage(object sender, SocketHttpListener.MessageEventArgs e)
+ {
+ //if (!string.IsNullOrEmpty(e.Data))
+ //{
+ // if (OnReceive != null)
+ // {
+ // OnReceive(e.Data);
+ // }
+ // return;
+ //}
+ if (OnReceiveBytes != null)
+ {
+ OnReceiveBytes(e.RawData);
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the state.
+ /// </summary>
+ /// <value>The state.</value>
+ public WebSocketState State
+ {
+ get
+ {
+ return WebSocket.ReadyState;
+ }
+ }
+
+ /// <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(bytes);
+ }
+
+ /// <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(text);
+ }
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ /// <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 (dispose)
+ {
+ WebSocket.OnMessage -= socket_OnMessage;
+ WebSocket.OnClose -= socket_OnClose;
+ WebSocket.OnError -= socket_OnError;
+
+ _cancellationTokenSource.Cancel();
+
+ WebSocket.Close();
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the receive action.
+ /// </summary>
+ /// <value>The receive action.</value>
+ public Action<byte[]> OnReceiveBytes { get; set; }
+
+ /// <summary>
+ /// Gets or sets the on receive.
+ /// </summary>
+ /// <value>The on receive.</value>
+ public Action<string> OnReceive { get; set; }
+ }
+}