Browse Source

Still broken

Claus Vium 6 years ago
parent
commit
d6c6f3c10c

+ 1 - 1
Emby.Server.Implementations/ApplicationHost.cs

@@ -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);

+ 1 - 0
Emby.Server.Implementations/Net/WebSocketConnectEventArgs.cs

@@ -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

+ 13 - 18
Emby.Server.Implementations/SocketSharp/SharpWebSocket.cs

@@ -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;

+ 34 - 14
Emby.Server.Implementations/SocketSharp/WebSocketSharpListener.cs

@@ -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,22 +145,41 @@ 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);
-                        //await socket.ConnectAsServerAsync().ConfigureAwait(false);
-
-                        //WebSocketConnected(new WebSocketConnectEventArgs
-                        //{
-                        //    Url = url,
-                        //    QueryString = queryString,
-                        //    WebSocket = socket,
-                        //    Endpoint = endpoint
-                        //});
-
-                        //await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false);
-                    }
+                        Url = url,
+                        QueryString = queryString,
+                        WebSocket = socket,
+                        Endpoint = endpoint
+                    });
+
+                    //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,
+                    //    QueryString = queryString,
+                    //    WebSocket = webSocketContext,
+                    //    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);
                 }
                 }
                 else
                 else
                 {
                 {

+ 2 - 2
Jellyfin.Server/SocketSharp/WebSocketSharpListener.cs

@@ -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
                         });
                         });