The Free Software Media System - Server Backend & API
Fix websocket handling
Bond-0094d311870d2f4
diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.csindex 88974f9aba..913a51217f 100644--- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs+++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs@@ -99,7 +99,6 @@ namespace Emby.Server.Implementations.HttpServer/// <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){var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);@@ -117,7 +116,6 @@ namespace Emby.Server.Implementations.HttpServer{// Allocate at least 512 bytes from the PipeWriterMemory<byte> memory = writer.GetMemory(512);-receiveresult = await _socket.ReceiveAsync(memory, cancellationToken);int bytesRead = receiveresult.Count;if (bytesRead == 0)@@ -144,33 +142,30 @@ namespace Emby.Server.Implementations.HttpServer}} while (_socket.State == WebSocketState.Open && receiveresult.MessageType != WebSocketMessageType.Close);- if (_socket.State == WebSocketState.Open)- {- _logger.LogWarning("Stopped reading from websocket before it was closed");- }-Closed?.Invoke(this, EventArgs.Empty);- _socket.Dispose();+ await _socket.CloseAsync(+ WebSocketCloseStatus.NormalClosure,+ string.Empty,+ cancellationToken).ConfigureAwait(false);}private async Task ProcessInternal(PipeReader reader){+ ReadResult result = await reader.ReadAsync().ConfigureAwait(false);+ ReadOnlySequence<byte> buffer = result.Buffer;+if (OnReceive == null){+ // Tell the PipeReader how much of the buffer we have consumed+ reader.AdvanceTo(buffer.End);return;}+ WebSocketMessage<object> stub;try{- 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);@@ -188,46 +183,36 @@ namespace Emby.Server.Implementations.HttpServerArrayPool<byte>.Shared.Return(buf);}}-- var info = new WebSocketMessageInfo- {- MessageType = stub.MessageType,- Data = stub.Data.ToString(),- Connection = this- };-- await OnReceive(info).ConfigureAwait(false);}catch (JsonException ex){+ // Tell the PipeReader how much of the buffer we have consumed+ reader.AdvanceTo(buffer.End);_logger.LogError(ex, "Error processing web socket message");+ return;}- }- /// <inheritdoc />- public void Dispose()- {- Dispose(true);- GC.SuppressFinalize(this);- }+ // Tell the PipeReader how much of the buffer we have consumed+ reader.AdvanceTo(buffer.End);- /// <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)- {- if (_disposed)+ _logger.LogDebug("WS received message: {@Message}", stub);++ var info = new WebSocketMessageInfo{- return;- }+ MessageType = stub.MessageType,+ Data = stub.Data?.ToString(), // Data can be null+ Connection = this+ };++ _logger.LogDebug("WS message info: {@MessageInfo}", info);- if (dispose)+ await OnReceive(info).ConfigureAwait(false);++ // Stop reading if there's no more data coming+ if (result.IsCompleted){- _socket.Dispose();+ return;}-- _disposed = true;}}}diff --git a/Emby.Server.Implementations/Session/WebSocketController.cs b/Emby.Server.Implementations/Session/WebSocketController.csindex 536013c7ad..c3c4b716fc 100644--- a/Emby.Server.Implementations/Session/WebSocketController.cs+++ b/Emby.Server.Implementations/Session/WebSocketController.cs@@ -57,7 +57,6 @@ namespace Emby.Server.Implementations.Session_logger.LogDebug("Removing websocket from session {Session}", _session.Id);_sockets.Remove(connection);connection.Closed -= OnConnectionClosed;- connection.Dispose();_sessionManager.CloseIfNeeded(_session);}@@ -96,7 +95,6 @@ namespace Emby.Server.Implementations.Sessionforeach (var socket in _sockets){socket.Closed -= OnConnectionClosed;- socket.Dispose();}_disposed = true;diff --git a/MediaBrowser.Controller/Net/IWebSocketConnection.cs b/MediaBrowser.Controller/Net/IWebSocketConnection.csindex d5555884df..09e43c683f 100644--- a/MediaBrowser.Controller/Net/IWebSocketConnection.cs+++ b/MediaBrowser.Controller/Net/IWebSocketConnection.cs@@ -10,7 +10,7 @@ using Microsoft.AspNetCore.Http;namespace MediaBrowser.Controller.Net{- public interface IWebSocketConnection : IDisposable+ public interface IWebSocketConnection{/// <summary>/// Occurs when [closed].