From 4a568e90d2eba112b7adae3526ef6be91cbc78b6 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 13 Apr 2018 00:25:02 -0700 Subject: [PATCH] API review changes (#1992) - Changed GetMessageBytes to return ReadOnlyMemory - Make HandshakeProtocol.SuccessHandshakeData a readonly field --- .../DefaultHubDispatcherBenchmark.cs | 2 +- .../HubProtocolBenchmark.cs | 2 +- .../RedisHubLifetimeManagerBenchmark.cs | 3 ++- .../RedisProtocolBenchmark.cs | 2 +- .../Internal/Protocol/HandshakeProtocol.cs | 2 +- .../Internal/Protocol/IHubProtocol.cs | 3 ++- .../HubConnectionContext.cs | 8 ++------ .../Internal/SerializedHubMessage.cs | 4 ++-- .../Internal/SerializedMessage.cs | 9 +++++++-- .../Internal/Protocol/JsonHubProtocol.cs | 2 +- .../Internal/Protocol/MessagePackHubProtocol.cs | 2 +- .../HubConnectionTests.cs | 2 +- .../RedisProtocolTests.cs | 4 ++-- 13 files changed, 24 insertions(+), 21 deletions(-) diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/DefaultHubDispatcherBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/DefaultHubDispatcherBenchmark.cs index d68c885b24..8c92785f80 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/DefaultHubDispatcherBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/DefaultHubDispatcherBenchmark.cs @@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks { } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { return HubProtocolExtensions.GetMessageBytes(this, message); } diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubProtocolBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubProtocolBenchmark.cs index 7c3cc5fc66..cbd9bd3d17 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubProtocolBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/HubProtocolBenchmark.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks public class HubProtocolBenchmark { private IHubProtocol _hubProtocol; - private byte[] _binaryInput; + private ReadOnlyMemory _binaryInput; private TestBinder _binder; private HubMessage _hubMessage; diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs index 7835c9f39a..55e6312158 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Buffers; using System.Collections.Generic; using System.Linq; @@ -195,7 +196,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks _innerProtocol.WriteMessage(message, output); } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { return HubProtocolExtensions.GetMessageBytes(this, message); } diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs index af14d3de5d..c8cef67e6d 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs @@ -144,7 +144,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks output.Write(_fixedOutput); } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { return HubProtocolExtensions.GetMessageBytes(this, message); } diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HandshakeProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HandshakeProtocol.cs index 6ec77d39b5..7f47cd8c69 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HandshakeProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HandshakeProtocol.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol private const string ErrorPropertyName = "error"; private const string TypePropertyName = "type"; - public static ReadOnlyMemory SuccessHandshakeData { get; } + public static ReadOnlyMemory SuccessHandshakeData; static HandshakeProtocol() { diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/IHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/IHubProtocol.cs index a2f341deb5..f9a03f5840 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/IHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/IHubProtocol.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Buffers; using Microsoft.AspNetCore.Connections; @@ -18,7 +19,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol void WriteMessage(HubMessage message, IBufferWriter output); - byte[] GetMessageBytes(HubMessage message); + ReadOnlyMemory GetMessageBytes(HubMessage message); bool IsVersionSupported(int version); } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs index 15ffc74fa5..95f2b210d5 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.SignalR private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1); private long _lastSendTimestamp = Stopwatch.GetTimestamp(); - private byte[] _cachedPingMessage; + private ReadOnlyMemory _cachedPingMessage; public HubConnectionContext(ConnectionContext connectionContext, TimeSpan keepAliveInterval, ILoggerFactory loggerFactory) { @@ -223,11 +223,7 @@ namespace Microsoft.AspNetCore.SignalR { try { - Debug.Assert(_cachedPingMessage != null); - - _connectionContext.Transport.Output.Write(_cachedPingMessage); - - await _connectionContext.Transport.Output.FlushAsync(); + await _connectionContext.Transport.Output.WriteAsync(_cachedPingMessage); Log.SentPing(_logger); } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedHubMessage.cs b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedHubMessage.cs index e13f8b68c3..649116a059 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedHubMessage.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedHubMessage.cs @@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal return serialized; } - private void SetCache(string protocolName, byte[] serialized) + private void SetCache(string protocolName, ReadOnlyMemory serialized) { if (_cachedItem1.ProtocolName == null) { @@ -80,7 +80,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal } } - private bool TryGetCached(string protocolName, out byte[] result) + private bool TryGetCached(string protocolName, out ReadOnlyMemory result) { if (string.Equals(_cachedItem1.ProtocolName, protocolName, StringComparison.Ordinal)) { diff --git a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedMessage.cs b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedMessage.cs index 4d2d80fda0..e591944b06 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedMessage.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SerializedMessage.cs @@ -1,11 +1,16 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + namespace Microsoft.AspNetCore.SignalR.Internal { public readonly struct SerializedMessage { public string ProtocolName { get; } - public byte[] Serialized { get; } + public ReadOnlyMemory Serialized { get; } - public SerializedMessage(string protocolName, byte[] serialized) + public SerializedMessage(string protocolName, ReadOnlyMemory serialized) { ProtocolName = protocolName; Serialized = serialized; diff --git a/src/Microsoft.AspNetCore.SignalR.Protocols.Json/Internal/Protocol/JsonHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Protocols.Json/Internal/Protocol/JsonHubProtocol.cs index f15ad11cbe..5e26cbcca9 100644 --- a/src/Microsoft.AspNetCore.SignalR.Protocols.Json/Internal/Protocol/JsonHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Protocols.Json/Internal/Protocol/JsonHubProtocol.cs @@ -82,7 +82,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol TextMessageFormatter.WriteRecordSeparator(output); } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { return HubProtocolExtensions.GetMessageBytes(this, message); } diff --git a/src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/Internal/Protocol/MessagePackHubProtocol.cs b/src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/Internal/Protocol/MessagePackHubProtocol.cs index 9e63892f45..9968874553 100644 --- a/src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/Internal/Protocol/MessagePackHubProtocol.cs +++ b/src/Microsoft.AspNetCore.SignalR.Protocols.MsgPack/Internal/Protocol/MessagePackHubProtocol.cs @@ -303,7 +303,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol } } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { var writer = MemoryBufferWriter.Get(); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs index 3d909c6f71..f803fc453e 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs @@ -168,7 +168,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests } } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { return HubProtocolExtensions.GetMessageBytes(this, message); } diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisProtocolTests.cs index 3f6459239e..e270448a10 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisProtocolTests.cs @@ -202,10 +202,10 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests public void WriteMessage(HubMessage message, IBufferWriter output) { - output.Write(GetMessageBytes(message)); + output.Write(GetMessageBytes(message).Span); } - public byte[] GetMessageBytes(HubMessage message) + public ReadOnlyMemory GetMessageBytes(HubMessage message) { SerializationCount += 1;