2018-12-28 14:30:53 +00:00
|
|
|
using System;
|
2019-01-13 19:24:27 +00:00
|
|
|
using System.Net.WebSockets;
|
2014-07-19 01:28:40 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2018-09-12 17:26:21 +00:00
|
|
|
using Emby.Server.Implementations.Net;
|
2019-01-01 15:27:11 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-07-19 01:28:40 +00:00
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace Jellyfin.Server.SocketSharp
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
|
|
|
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>
|
2015-01-03 19:38:22 +00:00
|
|
|
private SocketHttpListener.WebSocket WebSocket { get; set; }
|
2014-07-19 01:28:40 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
|
2014-07-19 01:28:40 +00:00
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
2019-02-13 17:37:44 +00:00
|
|
|
private bool _disposed = false;
|
2014-07-19 01:28:40 +00:00
|
|
|
|
2015-01-03 19:38:22 +00:00
|
|
|
public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger)
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
|
|
|
if (socket == null)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(socket));
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (logger == null)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(logger));
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
WebSocket = socket;
|
|
|
|
|
2019-02-13 17:37:44 +00:00
|
|
|
socket.OnMessage += OnSocketMessage;
|
|
|
|
socket.OnClose += OnSocketClose;
|
|
|
|
socket.OnError += OnSocketError;
|
2014-07-19 01:28:40 +00:00
|
|
|
|
|
|
|
WebSocket.ConnectAsServer();
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task StartReceive()
|
|
|
|
{
|
|
|
|
return _taskCompletionSource.Task;
|
|
|
|
}
|
|
|
|
|
2019-02-13 17:37:44 +00:00
|
|
|
private void OnSocketError(object sender, SocketHttpListener.ErrorEventArgs e)
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
2018-12-28 14:21:02 +00:00
|
|
|
_logger.LogError("Error in SharpWebSocket: {Message}", e.Message ?? string.Empty);
|
2019-02-13 16:19:55 +00:00
|
|
|
|
2019-02-13 15:52:53 +00:00
|
|
|
// Closed?.Invoke(this, EventArgs.Empty);
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 17:37:44 +00:00
|
|
|
private void OnSocketClose(object sender, SocketHttpListener.CloseEventArgs e)
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
_taskCompletionSource.TrySetResult(true);
|
|
|
|
|
2018-12-28 14:21:02 +00:00
|
|
|
Closed?.Invoke(this, EventArgs.Empty);
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 17:37:44 +00:00
|
|
|
private void OnSocketMessage(object sender, SocketHttpListener.MessageEventArgs e)
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
2015-03-08 18:49:51 +00:00
|
|
|
if (OnReceiveBytes != null)
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
|
|
|
OnReceiveBytes(e.RawData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the state.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The state.</value>
|
2019-01-13 20:31:14 +00:00
|
|
|
public WebSocketState State => WebSocket.ReadyState;
|
2014-07-19 01:28:40 +00:00
|
|
|
|
|
|
|
/// <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>
|
2014-07-20 04:46:29 +00:00
|
|
|
public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
|
2014-07-19 01:28:40 +00:00
|
|
|
{
|
2016-12-13 23:38:26 +00:00
|
|
|
return WebSocket.SendAsync(bytes);
|
2014-07-20 04:46:29 +00:00
|
|
|
}
|
2014-07-19 01:28:40 +00:00
|
|
|
|
2014-07-20 04:46:29 +00:00
|
|
|
/// <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)
|
|
|
|
{
|
2016-12-13 23:38:26 +00:00
|
|
|
return WebSocket.SendAsync(text);
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2019-02-13 17:37:44 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
2019-02-13 17:37:44 +00:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:28:40 +00:00
|
|
|
if (dispose)
|
|
|
|
{
|
2019-02-13 17:37:44 +00:00
|
|
|
WebSocket.OnMessage -= OnSocketMessage;
|
|
|
|
WebSocket.OnClose -= OnSocketClose;
|
|
|
|
WebSocket.OnError -= OnSocketError;
|
2015-03-09 00:36:02 +00:00
|
|
|
|
2014-07-19 01:28:40 +00:00
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
|
|
|
WebSocket.Close();
|
|
|
|
}
|
2019-02-13 17:37:44 +00:00
|
|
|
|
|
|
|
_disposed = true;
|
2014-07-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the receive action.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The receive action.</value>
|
|
|
|
public Action<byte[]> OnReceiveBytes { get; set; }
|
|
|
|
}
|
|
|
|
}
|