2019-12-26 19:57:46 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-12-17 22:15:02 +00:00
|
|
|
|
using System.Buffers;
|
|
|
|
|
using System.IO.Pipelines;
|
|
|
|
|
using System.Net;
|
2019-01-13 19:20:41 +00:00
|
|
|
|
using System.Net.WebSockets;
|
2019-12-17 22:15:02 +00:00
|
|
|
|
using System.Text.Json;
|
2019-01-13 19:20:41 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-12-17 22:15:02 +00:00
|
|
|
|
using MediaBrowser.Common.Json;
|
2014-12-27 18:06:32 +00:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-03-26 03:01:47 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2019-02-27 13:23:39 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-01-13 19:20:41 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-11-01 17:38:54 +00:00
|
|
|
|
/// Class WebSocketConnection.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
public class WebSocketConnection : IWebSocketConnection
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-11-01 17:38:54 +00:00
|
|
|
|
/// The logger.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-21 21:06:23 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <summary>
|
2019-12-17 22:15:02 +00:00
|
|
|
|
/// The json serializer options.
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// </summary>
|
2019-12-17 22:15:02 +00:00
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
2013-02-26 03:43:04 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-10 15:22:03 +00:00
|
|
|
|
/// The socket.
|
2013-10-03 01:22:50 +00:00
|
|
|
|
/// </summary>
|
2019-12-17 22:15:02 +00:00
|
|
|
|
private readonly WebSocket _socket;
|
|
|
|
|
|
|
|
|
|
private bool _disposed = false;
|
2016-10-06 18:55:01 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="WebSocketConnection" /> class.
|
|
|
|
|
/// </summary>
|
2019-12-26 19:57:46 +00:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="socket">The socket.</param>
|
|
|
|
|
/// <param name="remoteEndPoint">The remote end point.</param>
|
2019-12-26 19:57:46 +00:00
|
|
|
|
/// <param name="query">The query.</param>
|
|
|
|
|
public WebSocketConnection(
|
|
|
|
|
ILogger<WebSocketConnection> logger,
|
|
|
|
|
WebSocket socket,
|
|
|
|
|
IPAddress? remoteEndPoint,
|
|
|
|
|
IQueryCollection query)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2019-12-26 19:57:46 +00:00
|
|
|
|
_logger = logger;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
_socket = socket;
|
|
|
|
|
RemoteEndPoint = remoteEndPoint;
|
2019-12-26 19:57:46 +00:00
|
|
|
|
QueryString = query;
|
2014-05-17 18:37:40 +00:00
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
|
_jsonOptions = JsonDefaults.GetOptions();
|
2019-12-26 19:57:46 +00:00
|
|
|
|
LastActivityDate = DateTime.Now;
|
2014-05-17 18:37:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
|
/// <inheritdoc />
|
2019-12-26 19:57:46 +00:00
|
|
|
|
public event EventHandler<EventArgs>? Closed;
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-10 15:22:03 +00:00
|
|
|
|
/// Gets or sets the remote end point.
|
2019-11-01 17:38:54 +00:00
|
|
|
|
/// </summary>
|
2019-12-26 19:57:46 +00:00
|
|
|
|
public IPAddress? RemoteEndPoint { get; }
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the receive action.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The receive action.</value>
|
2019-12-26 19:57:46 +00:00
|
|
|
|
public Func<WebSocketMessageInfo, Task>? OnReceive { get; set; }
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the last activity date.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The last activity date.</value>
|
|
|
|
|
public DateTime LastActivityDate { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the query string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The query string.</value>
|
2019-12-26 19:57:46 +00:00
|
|
|
|
public IQueryCollection QueryString { get; }
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The state.</value>
|
|
|
|
|
public WebSocketState State => _socket.State;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2019-12-17 22:15:02 +00:00
|
|
|
|
/// Sends a message asynchronously.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2019-12-17 22:15:02 +00:00
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
|
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
|
|
|
|
|
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task ProcessAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var pipe = new Pipe();
|
|
|
|
|
var writer = pipe.Writer;
|
|
|
|
|
|
|
|
|
|
ValueWebSocketReceiveResult receiveresult;
|
|
|
|
|
do
|
2015-03-09 00:36:02 +00:00
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
|
// Allocate at least 512 bytes from the PipeWriter
|
|
|
|
|
Memory<byte> memory = writer.GetMemory(512);
|
|
|
|
|
receiveresult = await _socket.ReceiveAsync(memory, cancellationToken);
|
|
|
|
|
int bytesRead = receiveresult.Count;
|
|
|
|
|
if (bytesRead == 0)
|
|
|
|
|
{
|
2019-12-26 19:57:46 +00:00
|
|
|
|
break;
|
2019-12-17 22:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tell the PipeWriter how much was read from the Socket
|
|
|
|
|
writer.Advance(bytesRead);
|
|
|
|
|
|
|
|
|
|
// Make the data available to the PipeReader
|
|
|
|
|
FlushResult flushResult = await writer.FlushAsync();
|
|
|
|
|
if (flushResult.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
// The PipeReader stopped reading
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 19:57:46 +00:00
|
|
|
|
LastActivityDate = DateTime.UtcNow;
|
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
|
if (receiveresult.EndOfMessage)
|
|
|
|
|
{
|
|
|
|
|
await ProcessInternal(pipe.Reader).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
} while (_socket.State == WebSocketState.Open && receiveresult.MessageType != WebSocketMessageType.Close);
|
|
|
|
|
|
|
|
|
|
Closed?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
2019-12-27 13:42:53 +00:00
|
|
|
|
await _socket.CloseAsync(
|
|
|
|
|
WebSocketCloseStatus.NormalClosure,
|
|
|
|
|
string.Empty,
|
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2015-03-09 00:36:02 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
|
private async Task ProcessInternal(PipeReader reader)
|
2013-09-05 21:34:46 +00:00
|
|
|
|
{
|
2019-12-27 13:42:53 +00:00
|
|
|
|
ReadResult result = await reader.ReadAsync().ConfigureAwait(false);
|
|
|
|
|
ReadOnlySequence<byte> buffer = result.Buffer;
|
|
|
|
|
|
2013-09-05 21:34:46 +00:00
|
|
|
|
if (OnReceive == null)
|
|
|
|
|
{
|
2019-12-27 13:42:53 +00:00
|
|
|
|
// Tell the PipeReader how much of the buffer we have consumed
|
|
|
|
|
reader.AdvanceTo(buffer.End);
|
2013-09-05 21:34:46 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2013-09-07 15:59:29 +00:00
|
|
|
|
|
2019-12-27 13:42:53 +00:00
|
|
|
|
WebSocketMessage<object> stub;
|
2013-09-05 21:34:46 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
|
|
|
|
|
|
if (buffer.IsSingleSegment)
|
|
|
|
|
{
|
|
|
|
|
stub = JsonSerializer.Deserialize<WebSocketMessage<object>>(buffer.FirstSpan, _jsonOptions);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var buf = ArrayPool<byte>.Shared.Rent(Convert.ToInt32(buffer.Length));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
buffer.CopyTo(buf);
|
|
|
|
|
stub = JsonSerializer.Deserialize<WebSocketMessage<object>>(buf, _jsonOptions);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ArrayPool<byte>.Shared.Return(buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-05 21:34:46 +00:00
|
|
|
|
}
|
2019-12-17 22:15:02 +00:00
|
|
|
|
catch (JsonException ex)
|
2013-09-05 21:34:46 +00:00
|
|
|
|
{
|
2019-12-27 13:42:53 +00:00
|
|
|
|
// Tell the PipeReader how much of the buffer we have consumed
|
|
|
|
|
reader.AdvanceTo(buffer.End);
|
2018-12-20 12:11:26 +00:00
|
|
|
|
_logger.LogError(ex, "Error processing web socket message");
|
2019-12-27 13:42:53 +00:00
|
|
|
|
return;
|
2013-09-05 21:34:46 +00:00
|
|
|
|
}
|
2016-10-07 15:08:13 +00:00
|
|
|
|
|
2019-12-27 13:42:53 +00:00
|
|
|
|
// Tell the PipeReader how much of the buffer we have consumed
|
|
|
|
|
reader.AdvanceTo(buffer.End);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2019-12-27 14:20:27 +00:00
|
|
|
|
_logger.LogDebug("WS {IP} received message: {@Message}", RemoteEndPoint, stub);
|
2019-12-27 13:42:53 +00:00
|
|
|
|
|
|
|
|
|
var info = new WebSocketMessageInfo
|
2019-12-17 22:15:02 +00:00
|
|
|
|
{
|
2019-12-27 13:42:53 +00:00
|
|
|
|
MessageType = stub.MessageType,
|
|
|
|
|
Data = stub.Data?.ToString(), // Data can be null
|
|
|
|
|
Connection = this
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-27 14:20:27 +00:00
|
|
|
|
_logger.LogDebug("WS {IP} message info: {@MessageInfo}", RemoteEndPoint, info);
|
2019-12-17 22:15:02 +00:00
|
|
|
|
|
2019-12-27 13:42:53 +00:00
|
|
|
|
await OnReceive(info).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Stop reading if there's no more data coming
|
|
|
|
|
if (result.IsCompleted)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2019-12-27 13:42:53 +00:00
|
|
|
|
return;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|