2019-01-16 19:50:40 +00:00
|
|
|
|
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>
|
|
|
|
|
/// <param name="socket">The socket.</param>
|
|
|
|
|
/// <param name="remoteEndPoint">The remote end point.</param>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2019-01-13 20:37:13 +00:00
|
|
|
|
/// <exception cref="ArgumentNullException">socket</exception>
|
2019-12-17 22:15:02 +00:00
|
|
|
|
public WebSocketConnection(ILogger<WebSocketConnection> logger, WebSocket socket, IPAddress remoteEndPoint)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (socket == null)
|
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
|
throw new ArgumentNullException(nameof(socket));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
|
if (remoteEndPoint != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
|
throw new ArgumentNullException(nameof(remoteEndPoint));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
2013-02-21 21:06:23 +00:00
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
|
throw new ArgumentNullException(nameof(logger));
|
2013-02-21 21:06:23 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
_socket = socket;
|
|
|
|
|
RemoteEndPoint = remoteEndPoint;
|
2013-02-21 21:06:23 +00:00
|
|
|
|
_logger = logger;
|
2014-05-17 18:37:40 +00:00
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
|
_jsonOptions = JsonDefaults.GetOptions();
|
2014-05-17 18:37:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public event EventHandler<EventArgs> Closed;
|
|
|
|
|
|
|
|
|
|
/// <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-17 22:15:02 +00:00
|
|
|
|
public IPAddress RemoteEndPoint { get; private set; }
|
2019-11-01 17:38:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the receive action.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The receive action.</value>
|
|
|
|
|
public Func<WebSocketMessageInfo, Task> OnReceive { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the last activity date.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The last activity date.</value>
|
|
|
|
|
public DateTime LastActivityDate { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the URL.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The URL.</value>
|
|
|
|
|
public string Url { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the query string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The query string.</value>
|
|
|
|
|
public IQueryCollection QueryString { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
/// <exception cref="ArgumentNullException">message</exception>
|
|
|
|
|
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
|
|
|
|
if (message == null)
|
2013-02-26 03:43:04 +00:00
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
|
throw new ArgumentNullException(nameof(message));
|
2013-02-26 03:43:04 +00:00
|
|
|
|
}
|
2015-03-09 00:36:02 +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)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (receiveresult.EndOfMessage)
|
|
|
|
|
{
|
|
|
|
|
await ProcessInternal(pipe.Reader).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
} while (_socket.State == WebSocketState.Open && receiveresult.MessageType != WebSocketMessageType.Close);
|
|
|
|
|
|
|
|
|
|
if (_socket.State == WebSocketState.Open)
|
2015-03-09 00:36:02 +00:00
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
|
await _socket.CloseAsync(
|
|
|
|
|
WebSocketCloseStatus.NormalClosure,
|
|
|
|
|
string.Empty, // REVIEW: human readable explanation as to why the connection is closed.
|
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2015-03-09 00:36:02 +00:00
|
|
|
|
}
|
2019-12-17 22:15:02 +00:00
|
|
|
|
|
|
|
|
|
Closed?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
|
|
_socket.Dispose();
|
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
|
|
|
|
{
|
|
|
|
|
LastActivityDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
if (OnReceive == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-09-07 15:59:29 +00:00
|
|
|
|
|
2013-09-05 21:34:46 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
|
var result = await reader.ReadAsync().ConfigureAwait(false);
|
|
|
|
|
if (!result.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebSocketMessage<object> stub;
|
|
|
|
|
var buffer = result.Buffer;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
var info = new WebSocketMessageInfo
|
|
|
|
|
{
|
|
|
|
|
MessageType = stub.MessageType,
|
2019-12-17 22:15:02 +00:00
|
|
|
|
Data = stub.Data.ToString(),
|
2014-04-06 17:53:23 +00:00
|
|
|
|
Connection = this
|
2013-09-05 21:34:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
|
await OnReceive(info).ConfigureAwait(false);
|
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
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
|
_logger.LogError(ex, "Error processing web socket message");
|
2013-09-05 21:34:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-07 15:08:13 +00:00
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
|
/// <inheritdoc />
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2019-11-01 17:38:54 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
2013-02-21 01:33:05 +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-12-17 22:15:02 +00:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
_socket.Dispose();
|
|
|
|
|
}
|
2019-12-17 22:15:02 +00:00
|
|
|
|
|
|
|
|
|
_disposed = true;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|