2019-01-16 14:50:40 -05:00
|
|
|
|
using System;
|
2019-12-17 17:15:02 -05:00
|
|
|
|
using System.Buffers;
|
|
|
|
|
using System.IO.Pipelines;
|
|
|
|
|
using System.Net;
|
2019-01-13 14:20:41 -05:00
|
|
|
|
using System.Net.WebSockets;
|
2019-12-17 17:15:02 -05:00
|
|
|
|
using System.Text.Json;
|
2019-01-13 14:20:41 -05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-12-17 17:15:02 -05:00
|
|
|
|
using MediaBrowser.Common.Json;
|
2014-12-27 13:06:32 -05:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-03-25 23:01:47 -04:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2019-02-27 08:23:39 -05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-01-13 14:20:41 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-11-01 13:38:54 -04:00
|
|
|
|
/// Class WebSocketConnection.
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// </summary>
|
2013-02-26 11:10:55 -05:00
|
|
|
|
public class WebSocketConnection : IWebSocketConnection
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-11-01 13:38:54 -04:00
|
|
|
|
/// The logger.
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// </summary>
|
2013-02-21 16:06:23 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-02-24 16:53:54 -05:00
|
|
|
|
/// <summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
|
/// The json serializer options.
|
2013-02-24 16:53:54 -05:00
|
|
|
|
/// </summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
2013-02-25 22:43:04 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-10 10:22:03 -05:00
|
|
|
|
/// The socket.
|
2013-10-02 21:22:50 -04:00
|
|
|
|
/// </summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
|
private readonly WebSocket _socket;
|
|
|
|
|
|
|
|
|
|
private bool _disposed = false;
|
2016-10-06 14:55:01 -04:00
|
|
|
|
|
2013-02-20 20:33:05 -05: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 16:53:54 -05:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2019-01-13 15:37:13 -05:00
|
|
|
|
/// <exception cref="ArgumentNullException">socket</exception>
|
2019-12-17 17:15:02 -05:00
|
|
|
|
public WebSocketConnection(ILogger<WebSocketConnection> logger, WebSocket socket, IPAddress remoteEndPoint)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
if (socket == null)
|
|
|
|
|
{
|
2019-01-06 15:50:43 -05:00
|
|
|
|
throw new ArgumentNullException(nameof(socket));
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
2019-11-01 13:38:54 -04:00
|
|
|
|
|
2019-12-17 17:15:02 -05:00
|
|
|
|
if (remoteEndPoint != null)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2019-01-06 15:50:43 -05:00
|
|
|
|
throw new ArgumentNullException(nameof(remoteEndPoint));
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
2019-11-01 13:38:54 -04:00
|
|
|
|
|
2013-02-21 16:06:23 -05:00
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
2019-01-06 15:50:43 -05:00
|
|
|
|
throw new ArgumentNullException(nameof(logger));
|
2013-02-21 16:06:23 -05:00
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
_socket = socket;
|
|
|
|
|
RemoteEndPoint = remoteEndPoint;
|
2013-02-21 16:06:23 -05:00
|
|
|
|
_logger = logger;
|
2014-05-17 14:37:40 -04:00
|
|
|
|
|
2019-12-17 17:15:02 -05:00
|
|
|
|
_jsonOptions = JsonDefaults.GetOptions();
|
2014-05-17 14:37:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 13:38:54 -04:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public event EventHandler<EventArgs> Closed;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-10 10:22:03 -05:00
|
|
|
|
/// Gets or sets the remote end point.
|
2019-11-01 13:38:54 -04:00
|
|
|
|
/// </summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
|
public IPAddress RemoteEndPoint { get; private set; }
|
2019-11-01 13:38:54 -04: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-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
|
/// Sends a message asynchronously.
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// </summary>
|
2019-12-17 17:15:02 -05: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-20 20:33:05 -05:00
|
|
|
|
{
|
2019-12-17 17:15:02 -05:00
|
|
|
|
if (message == null)
|
2013-02-25 22:43:04 -05:00
|
|
|
|
{
|
2019-12-17 17:15:02 -05:00
|
|
|
|
throw new ArgumentNullException(nameof(message));
|
2013-02-25 22:43:04 -05:00
|
|
|
|
}
|
2015-03-08 20:36:02 -04:00
|
|
|
|
|
2019-12-17 17:15:02 -05: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-08 20:36:02 -04:00
|
|
|
|
{
|
2019-12-17 17:15:02 -05: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-08 20:36:02 -04:00
|
|
|
|
{
|
2019-12-17 17:15:02 -05:00
|
|
|
|
await _socket.CloseAsync(
|
|
|
|
|
WebSocketCloseStatus.NormalClosure,
|
|
|
|
|
string.Empty, // REVIEW: human readable explanation as to why the connection is closed.
|
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2015-03-08 20:36:02 -04:00
|
|
|
|
}
|
2019-12-17 17:15:02 -05:00
|
|
|
|
|
|
|
|
|
Closed?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
|
|
_socket.Dispose();
|
2015-03-08 20:36:02 -04:00
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2019-12-17 17:15:02 -05:00
|
|
|
|
private async Task ProcessInternal(PipeReader reader)
|
2013-09-05 17:34:46 -04:00
|
|
|
|
{
|
|
|
|
|
LastActivityDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
if (OnReceive == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-09-07 11:59:29 -04:00
|
|
|
|
|
2013-09-05 17:34:46 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-17 17:15:02 -05: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 17:34:46 -04:00
|
|
|
|
|
|
|
|
|
var info = new WebSocketMessageInfo
|
|
|
|
|
{
|
|
|
|
|
MessageType = stub.MessageType,
|
2019-12-17 17:15:02 -05:00
|
|
|
|
Data = stub.Data.ToString(),
|
2014-04-06 13:53:23 -04:00
|
|
|
|
Connection = this
|
2013-09-05 17:34:46 -04:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-17 17:15:02 -05:00
|
|
|
|
await OnReceive(info).ConfigureAwait(false);
|
2013-09-05 17:34:46 -04:00
|
|
|
|
}
|
2019-12-17 17:15:02 -05:00
|
|
|
|
catch (JsonException ex)
|
2013-09-05 17:34:46 -04:00
|
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
|
_logger.LogError(ex, "Error processing web socket message");
|
2013-09-05 17:34:46 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-07 11:08:13 -04:00
|
|
|
|
|
2019-11-01 13:38:54 -04:00
|
|
|
|
/// <inheritdoc />
|
2013-02-20 20:33:05 -05:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2019-11-01 13:38:54 -04:00
|
|
|
|
GC.SuppressFinalize(this);
|
2013-02-20 20:33:05 -05: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 17:15:02 -05:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
_socket.Dispose();
|
|
|
|
|
}
|
2019-12-17 17:15:02 -05:00
|
|
|
|
|
|
|
|
|
_disposed = true;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|