From d9948ec7486522c984de37228e1d6e33209c01b6 Mon Sep 17 00:00:00 2001 From: Chenyang Liu Date: Mon, 9 Oct 2023 21:11:12 +0800 Subject: [PATCH] [WebPubSubClient] Parsing message returns IList (#39137) * Parsing message returns IList * update api * Use IReadonlyList --- ...zure.Messaging.WebPubSub.Client.netstandard2.0.cs | 6 +++--- .../src/Protocols/WebPubSubJsonProtocol.cs | 2 +- .../src/Protocols/WebPubSubJsonProtocolBase.cs | 12 ++++++------ .../src/Protocols/WebPubSubJsonReliableProtocol.cs | 2 +- .../src/Protocols/WebPubSubProtocol.cs | 2 +- .../src/WebPubSubClient.cs | 6 ++++-- .../tests/Protocols/JsonProtocolTests.cs | 3 ++- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/api/Azure.Messaging.WebPubSub.Client.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/api/Azure.Messaging.WebPubSub.Client.netstandard2.0.cs index 0110be1c4dc3d..fd548ce588fc0 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/api/Azure.Messaging.WebPubSub.Client.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/api/Azure.Messaging.WebPubSub.Client.netstandard2.0.cs @@ -152,7 +152,7 @@ public WebPubSubJsonProtocol() { } public override string Name { get { throw null; } } public override Azure.Messaging.WebPubSub.Clients.WebPubSubProtocolMessageType WebSocketMessageType { get { throw null; } } public override System.ReadOnlyMemory GetMessageBytes(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message) { throw null; } - public override Azure.Messaging.WebPubSub.Clients.WebPubSubMessage ParseMessage(System.Buffers.ReadOnlySequence input) { throw null; } + public override System.Collections.Generic.IReadOnlyList ParseMessage(System.Buffers.ReadOnlySequence input) { throw null; } public override void WriteMessage(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message, System.Buffers.IBufferWriter output) { } } public partial class WebPubSubJsonReliableProtocol : Azure.Messaging.WebPubSub.Clients.WebPubSubProtocol @@ -162,7 +162,7 @@ public WebPubSubJsonReliableProtocol() { } public override string Name { get { throw null; } } public override Azure.Messaging.WebPubSub.Clients.WebPubSubProtocolMessageType WebSocketMessageType { get { throw null; } } public override System.ReadOnlyMemory GetMessageBytes(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message) { throw null; } - public override Azure.Messaging.WebPubSub.Clients.WebPubSubMessage ParseMessage(System.Buffers.ReadOnlySequence input) { throw null; } + public override System.Collections.Generic.IReadOnlyList ParseMessage(System.Buffers.ReadOnlySequence input) { throw null; } public override void WriteMessage(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message, System.Buffers.IBufferWriter output) { } } public abstract partial class WebPubSubMessage @@ -176,7 +176,7 @@ protected WebPubSubProtocol() { } public abstract string Name { get; } public abstract Azure.Messaging.WebPubSub.Clients.WebPubSubProtocolMessageType WebSocketMessageType { get; } public abstract System.ReadOnlyMemory GetMessageBytes(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message); - public abstract Azure.Messaging.WebPubSub.Clients.WebPubSubMessage ParseMessage(System.Buffers.ReadOnlySequence input); + public abstract System.Collections.Generic.IReadOnlyList ParseMessage(System.Buffers.ReadOnlySequence input); public abstract void WriteMessage(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message, System.Buffers.IBufferWriter output); } public enum WebPubSubProtocolMessageType diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocol.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocol.cs index e8dcab7c3dbf3..fc54a90b7d0a3 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocol.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocol.cs @@ -46,7 +46,7 @@ public override ReadOnlyMemory GetMessageBytes(WebPubSubMessage message) /// /// The serialized representation of the message. /// A - public override WebPubSubMessage ParseMessage(ReadOnlySequence input) + public override IReadOnlyList ParseMessage(ReadOnlySequence input) { return _processor.ParseMessage(input); } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocolBase.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocolBase.cs index e3109c2172daa..49cc0152a92d2 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocolBase.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonProtocolBase.cs @@ -67,7 +67,7 @@ public ReadOnlyMemory GetMessageBytes(WebPubSubMessage message) return new Memory(writer.ToArray()); } - public virtual WebPubSubMessage ParseMessage(ReadOnlySequence input) + public virtual IReadOnlyList ParseMessage(ReadOnlySequence input) { try { @@ -264,7 +264,7 @@ public virtual WebPubSubMessage ParseMessage(ReadOnlySequence input) case DownstreamEventType.Ack: AssertNotNull(ackId, AckIdPropertyName); AssertNotNull(success, SuccessPropertyName); - return new AckMessage(ackId.Value, success.Value, errorDetail); + return new List { new AckMessage(ackId.Value, success.Value, errorDetail) }; case DownstreamEventType.Message: AssertNotNull(from, FromPropertyName); @@ -273,10 +273,10 @@ public virtual WebPubSubMessage ParseMessage(ReadOnlySequence input) switch (fromType) { case FromType.Server: - return new ServerDataMessage(dataType, data, sequenceId); + return new List { new ServerDataMessage(dataType, data, sequenceId) }; case FromType.Group: AssertNotNull(group, GroupPropertyName); - return new GroupDataMessage(group, dataType, data, sequenceId, fromUserId); + return new List { new GroupDataMessage(group, dataType, data, sequenceId, fromUserId) }; // Forward compatible default: return null; @@ -288,9 +288,9 @@ public virtual WebPubSubMessage ParseMessage(ReadOnlySequence input) switch (systemEventType) { case SystemEventType.Connected: - return new ConnectedMessage(userId, connectionId, reconnectionToken); + return new List { new ConnectedMessage(userId, connectionId, reconnectionToken) }; case SystemEventType.Disconnected: - return new DisconnectedMessage(message); + return new List { new DisconnectedMessage(message) }; // Forward compatible default: return null; diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonReliableProtocol.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonReliableProtocol.cs index 019d6e6a8fcc2..c9db9f8e2afb2 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonReliableProtocol.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubJsonReliableProtocol.cs @@ -46,7 +46,7 @@ public override ReadOnlyMemory GetMessageBytes(WebPubSubMessage message) /// /// The serialized representation of the message. /// A - public override WebPubSubMessage ParseMessage(ReadOnlySequence input) + public override IReadOnlyList ParseMessage(ReadOnlySequence input) { return _processor.ParseMessage(input); } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubProtocol.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubProtocol.cs index ee27741d6701b..b4ffd0d9fd519 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubProtocol.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/Protocols/WebPubSubProtocol.cs @@ -29,7 +29,7 @@ public abstract class WebPubSubProtocol /// /// The serialized representation of the message. /// A - public abstract WebPubSubMessage ParseMessage(ReadOnlySequence input); + public abstract IReadOnlyList ParseMessage(ReadOnlySequence input); /// /// Writes the specified to a writer. diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/WebPubSubClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/WebPubSubClient.cs index a6ba69596bab4..e85c9621ed824 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/WebPubSubClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/WebPubSubClient.cs @@ -514,8 +514,10 @@ private async Task ListenLoop(IWebSocketClient client, CancellationToken token) { try { - var message = _protocol.ParseMessage(result.Payload); - await HandleMessageAsync(message, token).ConfigureAwait(false); + foreach (var message in _protocol.ParseMessage(result.Payload)) + { + await HandleMessageAsync(message, token).ConfigureAwait(false); + } } catch (Exception ex) { diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/tests/Protocols/JsonProtocolTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/tests/Protocols/JsonProtocolTests.cs index c2ca7e1214170..1d6dc80882683 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/tests/Protocols/JsonProtocolTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/tests/Protocols/JsonProtocolTests.cs @@ -4,6 +4,7 @@ using System; using System.Buffers; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -152,7 +153,7 @@ public void ParseMessageTest(byte[] payload, Action messageAss { var protocol = new WebPubSubJsonProtocol(); var resolvedMessage = protocol.ParseMessage(new ReadOnlySequence(payload)); - messageAssert(resolvedMessage); + messageAssert(resolvedMessage[0]); } [TestCaseSource(nameof(GetSerializingTestData))]