Still broken

This commit is contained in:
Claus Vium 2019-02-26 15:13:06 +01:00
parent 38f52a139e
commit d6c6f3c10c
5 changed files with 49 additions and 33 deletions

View File

@ -740,7 +740,7 @@ namespace Emby.Server.Implementations
var request = context.Request; var request = context.Request;
var response = context.Response; var response = context.Response;
var localPath = context.Request.Path.ToString().TrimStart('/'); var localPath = context.Request.Path.ToString();
var req = new WebSocketSharpRequest(request, response, request.Path, Logger); var req = new WebSocketSharpRequest(request, response, request.Path, Logger);
await ((HttpListenerHost)HttpServer).RequestHandler(req, request.GetDisplayUrl(), request.Host.ToString(), localPath, CancellationToken.None).ConfigureAwait(false); await ((HttpListenerHost)HttpServer).RequestHandler(req, request.GetDisplayUrl(), request.Host.ToString(), localPath, CancellationToken.None).ConfigureAwait(false);

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Net.WebSockets;
using MediaBrowser.Model.Services; using MediaBrowser.Model.Services;
namespace Emby.Server.Implementations.Net namespace Emby.Server.Implementations.Net

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Emby.Server.Implementations.Net; using Emby.Server.Implementations.Net;
@ -20,25 +21,18 @@ namespace Emby.Server.Implementations.SocketSharp
/// Gets or sets the web socket. /// Gets or sets the web socket.
/// </summary> /// </summary>
/// <value>The web socket.</value> /// <value>The web socket.</value>
private SocketHttpListener.WebSocket WebSocket { get; set; } private WebSocket WebSocket { get; set; }
private TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>(); private TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private bool _disposed = false; private bool _disposed = false;
public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger) public SharpWebSocket(WebSocket socket, ILogger logger)
{ {
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
WebSocket = socket ?? throw new ArgumentNullException(nameof(socket)); WebSocket = socket ?? throw new ArgumentNullException(nameof(socket));
socket.OnMessage += OnSocketMessage;
socket.OnClose += OnSocketClose;
socket.OnError += OnSocketError;
} }
public Task ConnectAsServerAsync()
=> WebSocket.ConnectAsServer();
public Task StartReceive() public Task StartReceive()
{ {
return _taskCompletionSource.Task; return _taskCompletionSource.Task;
@ -58,7 +52,7 @@ namespace Emby.Server.Implementations.SocketSharp
Closed?.Invoke(this, EventArgs.Empty); Closed?.Invoke(this, EventArgs.Empty);
} }
private void OnSocketMessage(object sender, SocketHttpListener.MessageEventArgs e) private void OnSocketMessage(SocketHttpListener.MessageEventArgs e)
{ {
if (OnReceiveBytes != null) if (OnReceiveBytes != null)
{ {
@ -66,11 +60,15 @@ namespace Emby.Server.Implementations.SocketSharp
} }
} }
public Task ConnectAsServerAsync()
{
return Task.CompletedTask;
}
/// <summary> /// <summary>
/// Gets or sets the state. /// Gets or sets the state.
/// </summary> /// </summary>
/// <value>The state.</value> /// <value>The state.</value>
public WebSocketState State => WebSocket.ReadyState; public WebSocketState State => WebSocket.State;
/// <summary> /// <summary>
/// Sends the async. /// Sends the async.
@ -81,7 +79,7 @@ namespace Emby.Server.Implementations.SocketSharp
/// <returns>Task.</returns> /// <returns>Task.</returns>
public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken) public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
{ {
return WebSocket.SendAsync(bytes); return WebSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Binary, endOfMessage, cancellationToken);
} }
/// <summary> /// <summary>
@ -93,7 +91,7 @@ namespace Emby.Server.Implementations.SocketSharp
/// <returns>Task.</returns> /// <returns>Task.</returns>
public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken) public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken)
{ {
return WebSocket.SendAsync(text); return WebSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
} }
/// <summary> /// <summary>
@ -118,13 +116,10 @@ namespace Emby.Server.Implementations.SocketSharp
if (dispose) if (dispose)
{ {
WebSocket.OnMessage -= OnSocketMessage;
WebSocket.OnClose -= OnSocketClose;
WebSocket.OnError -= OnSocketError;
_cancellationTokenSource.Cancel(); _cancellationTokenSource.Cancel();
WebSocket.CloseAsync().GetAwaiter().GetResult(); // TODO
WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "bye", CancellationToken.None).GetAwaiter().GetResult();
} }
_disposed = true; _disposed = true;

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.WebSockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Emby.Server.Implementations.HttpServer; using Emby.Server.Implementations.HttpServer;
@ -144,23 +145,42 @@ using Microsoft.Extensions.Logging;
_logger.LogDebug("Web socket connection allowed"); _logger.LogDebug("Web socket connection allowed");
var webSocketContext = await ctx.WebSockets.AcceptWebSocketAsync(null).ConfigureAwait(false); var webSocketContext = await ctx.WebSockets.AcceptWebSocketAsync(null).ConfigureAwait(false);
var socket = new SharpWebSocket(webSocketContext, _logger);
await socket.ConnectAsServerAsync().ConfigureAwait(false);
if (WebSocketConnected != null) WebSocketConnected(new WebSocketConnectEventArgs
{ {
//SharpWebSocket socket = new SharpWebSocket(webSocketContext, _logger); Url = url,
//await socket.ConnectAsServerAsync().ConfigureAwait(false); QueryString = queryString,
WebSocket = socket,
Endpoint = endpoint
});
//WebSocketConnected(new WebSocketConnectEventArgs //await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false);
var buffer = WebSocket.CreateClientBuffer(1024 * 4, 1024 * 4);
WebSocketReceiveResult result = await webSocketContext.ReceiveAsync(buffer, CancellationToken.None);
socket.OnReceiveBytes(buffer.Array);
//while (!result.CloseStatus.HasValue)
//{
// await webSocketContext.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
// result = await webSocketContext.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
//}
//WebSocketConnected?.Invoke(new WebSocketConnectEventArgs
//{ //{
// Url = url, // Url = url,
// QueryString = queryString, // QueryString = queryString,
// WebSocket = socket, // WebSocket = webSocketContext,
// Endpoint = endpoint // Endpoint = endpoint
//}); //});
await webSocketContext.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
//SharpWebSocket socket = new SharpWebSocket(webSocketContext, _logger);
//await socket.ConnectAsServerAsync().ConfigureAwait(false);
//await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false); //await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false);
} }
}
else else
{ {
_logger.LogWarning("Web socket connection not allowed"); _logger.LogWarning("Web socket connection not allowed");

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
@ -152,7 +152,7 @@ using Microsoft.Extensions.Logging;
{ {
Url = url, Url = url,
QueryString = queryString, QueryString = queryString,
WebSocket = socket, WebSocket = null, // socket,
Endpoint = endpoint Endpoint = endpoint
}); });