Fix sending websocket messages (#9948)

This commit is contained in:
Cody Robibero 2023-07-02 16:14:44 -06:00 committed by GitHub
parent ba8f4757fd
commit 52252fcd55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 38 deletions

View File

@ -10,6 +10,7 @@ using System.Threading.Tasks;
using Jellyfin.Extensions.Json;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Net.WebSocketMessages;
using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
@ -85,26 +86,15 @@ namespace Emby.Server.Implementations.HttpServer
/// <value>The state.</value>
public WebSocketState State => _socket.State;
/// <summary>
/// Sends a message asynchronously.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public Task SendAsync(WebSocketMessage message, CancellationToken cancellationToken)
/// <inheritdoc />
public Task SendAsync(OutboundWebSocketMessage message, CancellationToken cancellationToken)
{
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
}
/// <summary>
/// Sends a message asynchronously.
/// </summary>
/// <typeparam name="T">The type of the message.</typeparam>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
/// <inheritdoc />
public Task SendAsync<T>(OutboundWebSocketMessage<T> message, CancellationToken cancellationToken)
{
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
@ -183,7 +173,7 @@ namespace Emby.Server.Implementations.HttpServer
return;
}
WebSocketMessage<object>? stub;
InboundWebSocketMessage<object>? stub;
long bytesConsumed;
try
{
@ -224,10 +214,10 @@ namespace Emby.Server.Implementations.HttpServer
}
}
internal WebSocketMessage<object>? DeserializeWebSocketMessage(ReadOnlySequence<byte> bytes, out long bytesConsumed)
internal InboundWebSocketMessage<object>? DeserializeWebSocketMessage(ReadOnlySequence<byte> bytes, out long bytesConsumed)
{
var jsonReader = new Utf8JsonReader(bytes);
var ret = JsonSerializer.Deserialize<WebSocketMessage<object>>(ref jsonReader, _jsonOptions);
var ret = JsonSerializer.Deserialize<InboundWebSocketMessage<object>>(ref jsonReader, _jsonOptions);
bytesConsumed = jsonReader.BytesConsumed;
return ret;
}
@ -236,11 +226,7 @@ namespace Emby.Server.Implementations.HttpServer
{
LastKeepAliveDate = DateTime.UtcNow;
return SendAsync(
new OutboundWebSocketMessage
{
MessageId = Guid.NewGuid(),
MessageType = SessionMessageType.KeepAlive
},
new OutboundKeepAliveMessage(),
CancellationToken.None);
}

View File

@ -6,9 +6,8 @@ using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Api.Extensions;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Session;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
@ -308,11 +307,7 @@ namespace Emby.Server.Implementations.Session
private Task SendForceKeepAlive(IWebSocketConnection webSocket)
{
return webSocket.SendAsync(
new WebSocketMessage<int>
{
MessageType = SessionMessageType.ForceKeepAlive,
Data = WebSocketLostTimeout
},
new ForceKeepAliveMessage(WebSocketLostTimeout),
CancellationToken.None);
}

View File

@ -171,7 +171,6 @@ namespace MediaBrowser.Controller.Net
await connection.SendAsync(
new OutboundWebSocketMessage<TReturnDataType>
{
MessageId = Guid.NewGuid(),
MessageType = Type,
Data = data
},

View File

@ -5,6 +5,7 @@ using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Net.WebSocketMessages;
namespace MediaBrowser.Controller.Net
{
@ -45,6 +46,15 @@ namespace MediaBrowser.Controller.Net
/// <value>The remote end point.</value>
IPAddress? RemoteEndPoint { get; }
/// <summary>
/// Sends a message asynchronously.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="ArgumentNullException">The message is null.</exception>
Task SendAsync(OutboundWebSocketMessage message, CancellationToken cancellationToken);
/// <summary>
/// Sends a message asynchronously.
/// </summary>
@ -53,7 +63,7 @@ namespace MediaBrowser.Controller.Net
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="ArgumentNullException">The message is null.</exception>
Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken);
Task SendAsync<T>(OutboundWebSocketMessage<T> message, CancellationToken cancellationToken);
Task ProcessAsync(CancellationToken cancellationToken = default);
}

View File

@ -1,11 +1,13 @@
#nullable disable
using MediaBrowser.Controller.Net.WebSocketMessages;
namespace MediaBrowser.Controller.Net
{
/// <summary>
/// Class WebSocketMessageInfo.
/// </summary>
public class WebSocketMessageInfo : WebSocketMessage<string>
public class WebSocketMessageInfo : InboundWebSocketMessage<string>
{
/// <summary>
/// Gets or sets the connection.

View File

@ -6,13 +6,12 @@ namespace MediaBrowser.Controller.Net;
/// Class WebSocketMessage.
/// </summary>
/// <typeparam name="T">The type of the data.</typeparam>
// TODO make this abstract, remove empty ctor.
public class WebSocketMessage<T> : WebSocketMessage
public abstract class WebSocketMessage<T> : WebSocketMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketMessage{T}"/> class.
/// </summary>
public WebSocketMessage()
protected WebSocketMessage()
{
}

View File

@ -10,5 +10,5 @@ public class OutboundWebSocketMessage : WebSocketMessage, IOutboundWebSocketMess
/// <summary>
/// Gets or sets the message id.
/// </summary>
public Guid MessageId { get; set; }
public Guid MessageId { get; set; } = Guid.NewGuid();
}

View File

@ -29,5 +29,5 @@ public class OutboundWebSocketMessage<T> : WebSocketMessage<T>, IOutboundWebSock
/// <summary>
/// Gets or sets the message id.
/// </summary>
public Guid MessageId { get; set; }
public Guid MessageId { get; set; } = Guid.NewGuid();
}