2019-02-26 00:09:42 -07:00
|
|
|
using System;
|
2019-02-25 15:34:32 -07:00
|
|
|
using System.Collections.Generic;
|
2019-02-26 12:13:48 -07:00
|
|
|
using System.Linq;
|
2019-02-26 07:13:06 -07:00
|
|
|
using System.Net.WebSockets;
|
2019-02-25 15:34:32 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Emby.Server.Implementations.HttpServer;
|
|
|
|
using Emby.Server.Implementations.Net;
|
|
|
|
using MediaBrowser.Model.Services;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-02-26 00:09:42 -07:00
|
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
2019-02-25 15:34:32 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-03-04 11:31:26 -07:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2019-02-25 15:34:32 -07:00
|
|
|
|
2019-03-04 11:31:26 -07:00
|
|
|
namespace Emby.Server.Implementations.SocketSharp
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
|
|
|
public class WebSocketSharpListener : IHttpListener
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
private CancellationTokenSource _disposeCancellationTokenSource = new CancellationTokenSource();
|
|
|
|
private CancellationToken _disposeCancellationToken;
|
|
|
|
|
|
|
|
public WebSocketSharpListener(
|
|
|
|
ILogger logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
_disposeCancellationToken = _disposeCancellationTokenSource.Token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Func<Exception, IRequest, bool, bool, Task> ErrorHandler { get; set; }
|
|
|
|
public Func<IHttpRequest, string, string, string, CancellationToken, Task> RequestHandler { get; set; }
|
|
|
|
|
|
|
|
public Action<WebSocketConnectEventArgs> WebSocketConnected { get; set; }
|
|
|
|
|
2019-02-26 12:13:48 -07:00
|
|
|
private static void LogRequest(ILogger logger, HttpRequest request)
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
2019-02-26 12:13:48 -07:00
|
|
|
var url = request.GetDisplayUrl();
|
2019-02-25 15:34:32 -07:00
|
|
|
|
2019-03-04 11:31:26 -07:00
|
|
|
logger.LogInformation("WS {Url}. UserAgent: {UserAgent}", url, request.Headers[HeaderNames.UserAgent].ToString());
|
2019-02-25 15:34:32 -07:00
|
|
|
}
|
|
|
|
|
2019-02-26 00:09:42 -07:00
|
|
|
public async Task ProcessWebSocketRequest(HttpContext ctx)
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-02-26 12:13:48 -07:00
|
|
|
LogRequest(_logger, ctx.Request);
|
2019-02-26 00:09:42 -07:00
|
|
|
var endpoint = ctx.Connection.RemoteIpAddress.ToString();
|
|
|
|
var url = ctx.Request.GetDisplayUrl();
|
2019-02-25 15:34:32 -07:00
|
|
|
|
2019-02-27 15:22:55 -07:00
|
|
|
var webSocketContext = await ctx.WebSockets.AcceptWebSocketAsync(null).ConfigureAwait(false);
|
|
|
|
var socket = new SharpWebSocket(webSocketContext, _logger);
|
|
|
|
|
|
|
|
WebSocketConnected(new WebSocketConnectEventArgs
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
|
|
|
Url = url,
|
2019-02-27 06:23:39 -07:00
|
|
|
QueryString = ctx.Request.Query,
|
2019-02-27 15:22:55 -07:00
|
|
|
WebSocket = socket,
|
2019-02-25 15:34:32 -07:00
|
|
|
Endpoint = endpoint
|
2019-02-27 15:22:55 -07:00
|
|
|
});
|
2019-02-25 15:34:32 -07:00
|
|
|
|
2019-02-27 15:22:55 -07:00
|
|
|
WebSocketReceiveResult result;
|
|
|
|
var message = new List<byte>();
|
2019-02-25 15:34:32 -07:00
|
|
|
|
2019-02-27 15:22:55 -07:00
|
|
|
do
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
2019-02-27 15:22:55 -07:00
|
|
|
var buffer = WebSocket.CreateServerBuffer(4096);
|
|
|
|
result = await webSocketContext.ReceiveAsync(buffer, _disposeCancellationToken);
|
|
|
|
message.AddRange(buffer.Array.Take(result.Count));
|
2019-02-25 15:34:32 -07:00
|
|
|
|
2019-02-27 15:22:55 -07:00
|
|
|
if (result.EndOfMessage)
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
2019-02-27 15:22:55 -07:00
|
|
|
socket.OnReceiveBytes(message.ToArray());
|
|
|
|
message.Clear();
|
2019-02-27 13:48:28 -07:00
|
|
|
}
|
2019-02-27 15:22:55 -07:00
|
|
|
} while (socket.State == WebSocketState.Open && result.MessageType != WebSocketMessageType.Close);
|
2019-02-27 13:48:28 -07:00
|
|
|
|
2019-02-27 15:22:55 -07:00
|
|
|
|
|
|
|
if (webSocketContext.State == WebSocketState.Open)
|
2019-02-25 15:34:32 -07:00
|
|
|
{
|
2019-10-25 03:47:20 -07:00
|
|
|
await webSocketContext.CloseAsync(
|
|
|
|
result.CloseStatus ?? WebSocketCloseStatus.NormalClosure,
|
|
|
|
result.CloseStatusDescription,
|
|
|
|
_disposeCancellationToken).ConfigureAwait(false);
|
2019-02-25 15:34:32 -07:00
|
|
|
}
|
2019-02-27 15:22:55 -07:00
|
|
|
|
|
|
|
socket.Dispose();
|
2019-02-25 15:34:32 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "AcceptWebSocketAsync error");
|
2019-03-07 14:26:23 -07:00
|
|
|
if (!ctx.Response.HasStarted)
|
|
|
|
{
|
|
|
|
ctx.Response.StatusCode = 500;
|
|
|
|
}
|
2019-02-25 15:34:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task Stop()
|
|
|
|
{
|
|
|
|
_disposeCancellationTokenSource.Cancel();
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases the unmanaged resources and disposes of the managed resources used.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases the unmanaged resources and disposes of the managed resources used.
|
|
|
|
/// </summary>
|
2019-08-16 12:03:45 -07:00
|
|
|
/// <param name="disposing">Whether or not the managed resources should be disposed.</param>
|
2019-02-25 15:34:32 -07:00
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
Stop().GetAwaiter().GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
_disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|