jellyfin-server/Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs

106 lines
3.6 KiB
C#
Raw Normal View History

2019-02-25 22:34:32 +00:00
using System;
using System.Net.WebSockets;
2019-02-26 14:13:06 +00:00
using System.Text;
2019-02-25 22:34:32 +00:00
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>
2019-02-26 20:51:02 +00:00
private readonly WebSocket _webSocket;
2019-02-25 22:34:32 +00:00
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
2019-02-27 06:32:36 +00:00
private bool _disposed;
2019-02-25 22:34:32 +00:00
2019-02-26 14:13:06 +00:00
public SharpWebSocket(WebSocket socket, ILogger logger)
2019-02-25 22:34:32 +00:00
{
2019-02-26 09:23:58 +00:00
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2019-02-26 20:51:02 +00:00
_webSocket = socket ?? throw new ArgumentNullException(nameof(socket));
2019-02-25 22:34:32 +00:00
}
/// <summary>
2019-10-25 10:47:20 +00:00
/// Gets the state.
2019-02-25 22:34:32 +00:00
/// </summary>
/// <value>The state.</value>
2019-02-26 20:51:02 +00:00
public WebSocketState State => _webSocket.State;
2019-02-25 22:34:32 +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>
public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
{
2019-02-26 20:51:02 +00:00
return _webSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Binary, endOfMessage, cancellationToken);
2019-02-25 22:34:32 +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)
{
2019-02-26 20:51:02 +00:00
return _webSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
2019-02-25 22:34:32 +00:00
}
/// <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();
2019-02-27 20:40:47 +00:00
if (_webSocket.State == WebSocketState.Open)
{
_webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed by client",
CancellationToken.None);
}
Closed?.Invoke(this, EventArgs.Empty);
2019-02-25 22:34:32 +00:00
}
_disposed = true;
}
/// <summary>
/// Gets or sets the receive action.
/// </summary>
/// <value>The receive action.</value>
public Action<byte[]> OnReceiveBytes { get; set; }
}
}