From 6856542aa9639b69388faf6aa74ada4ed1dc1520 Mon Sep 17 00:00:00 2001 From: Chenyang Liu Date: Sat, 7 Oct 2023 15:08:26 +0800 Subject: [PATCH 1/3] Parsing message returns IList --- .../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 ++- 6 files changed, 15 insertions(+), 12 deletions(-) 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..6e0797422d80e 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 IList 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..09ebc898aa2ea 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 IList 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..8b6d626674958 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 IList 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..8f7c2011acf74 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 IList 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 11342ccbc60b4..7efb2e7cdc7e9 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/WebPubSubClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub.Client/src/WebPubSubClient.cs @@ -518,8 +518,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))] From d1f26a2bcf313a020edc535b89af0d58bbca8ab6 Mon Sep 17 00:00:00 2001 From: Chenyang Liu Date: Sat, 7 Oct 2023 15:25:39 +0800 Subject: [PATCH 2/3] update api --- .../api/Azure.Messaging.WebPubSub.Client.netstandard2.0.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 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..78ac27957519e 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.IList 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.IList 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.IList ParseMessage(System.Buffers.ReadOnlySequence input); public abstract void WriteMessage(Azure.Messaging.WebPubSub.Clients.WebPubSubMessage message, System.Buffers.IBufferWriter output); } public enum WebPubSubProtocolMessageType From 67ba5dc5d69ce5a665767372eca7da0c568aefb2 Mon Sep 17 00:00:00 2001 From: Chenyang Liu Date: Mon, 9 Oct 2023 11:18:29 +0800 Subject: [PATCH 3/3] Use IReadonlyList --- .../api/Azure.Messaging.WebPubSub.Client.netstandard2.0.cs | 6 +++--- .../src/Protocols/WebPubSubJsonProtocol.cs | 2 +- .../src/Protocols/WebPubSubJsonProtocolBase.cs | 2 +- .../src/Protocols/WebPubSubJsonReliableProtocol.cs | 2 +- .../src/Protocols/WebPubSubProtocol.cs | 2 +- 5 files changed, 7 insertions(+), 7 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 78ac27957519e..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 System.Collections.Generic.IList 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 System.Collections.Generic.IList 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 System.Collections.Generic.IList 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 6e0797422d80e..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 IList 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 09ebc898aa2ea..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 IList ParseMessage(ReadOnlySequence input) + public virtual IReadOnlyList ParseMessage(ReadOnlySequence input) { try { 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 8b6d626674958..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 IList 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 8f7c2011acf74..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 IList ParseMessage(ReadOnlySequence input); + public abstract IReadOnlyList ParseMessage(ReadOnlySequence input); /// /// Writes the specified to a writer.