API review changes (#1992)

- Changed GetMessageBytes to return ReadOnlyMemory
- Make HandshakeProtocol.SuccessHandshakeData a readonly field
This commit is contained in:
David Fowler 2018-04-13 00:25:02 -07:00 committed by GitHub
parent 70c63fe9e8
commit 4a568e90d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 24 additions and 21 deletions

View File

@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
{
}
public byte[] GetMessageBytes(HubMessage message)
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
return HubProtocolExtensions.GetMessageBytes(this, message);
}

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
public class HubProtocolBenchmark
{
private IHubProtocol _hubProtocol;
private byte[] _binaryInput;
private ReadOnlyMemory<byte> _binaryInput;
private TestBinder _binder;
private HubMessage _hubMessage;

View File

@ -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<byte> GetMessageBytes(HubMessage message)
{
return HubProtocolExtensions.GetMessageBytes(this, message);
}

View File

@ -144,7 +144,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
output.Write(_fixedOutput);
}
public byte[] GetMessageBytes(HubMessage message)
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
return HubProtocolExtensions.GetMessageBytes(this, message);
}

View File

@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
private const string ErrorPropertyName = "error";
private const string TypePropertyName = "type";
public static ReadOnlyMemory<byte> SuccessHandshakeData { get; }
public static ReadOnlyMemory<byte> SuccessHandshakeData;
static HandshakeProtocol()
{

View File

@ -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<byte> output);
byte[] GetMessageBytes(HubMessage message);
ReadOnlyMemory<byte> GetMessageBytes(HubMessage message);
bool IsVersionSupported(int version);
}

View File

@ -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<byte> _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);
}

View File

@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal
return serialized;
}
private void SetCache(string protocolName, byte[] serialized)
private void SetCache(string protocolName, ReadOnlyMemory<byte> 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<byte> result)
{
if (string.Equals(_cachedItem1.ProtocolName, protocolName, StringComparison.Ordinal))
{

View File

@ -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<byte> Serialized { get; }
public SerializedMessage(string protocolName, byte[] serialized)
public SerializedMessage(string protocolName, ReadOnlyMemory<byte> serialized)
{
ProtocolName = protocolName;
Serialized = serialized;

View File

@ -82,7 +82,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
TextMessageFormatter.WriteRecordSeparator(output);
}
public byte[] GetMessageBytes(HubMessage message)
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
return HubProtocolExtensions.GetMessageBytes(this, message);
}

View File

@ -303,7 +303,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
}
}
public byte[] GetMessageBytes(HubMessage message)
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
var writer = MemoryBufferWriter.Get();

View File

@ -168,7 +168,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}
}
public byte[] GetMessageBytes(HubMessage message)
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
return HubProtocolExtensions.GetMessageBytes(this, message);
}

View File

@ -202,10 +202,10 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests
public void WriteMessage(HubMessage message, IBufferWriter<byte> output)
{
output.Write(GetMessageBytes(message));
output.Write(GetMessageBytes(message).Span);
}
public byte[] GetMessageBytes(HubMessage message)
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
SerializationCount += 1;