jellyfin/Emby.Server.Implementations/SocketSharp/WebSocketSharpListener.cs

136 lines
4.5 KiB
C#
Raw Normal View History

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