Skip to content

Commit

Permalink
Add MessagePack Hub Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Sindo committed Dec 24, 2024
1 parent caf37b7 commit 56fd73a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 614 deletions.
9 changes: 4 additions & 5 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@
<PackageReference Update="CloudNative.CloudEvents.SystemTextJson" Version="2.0.0" />
<PackageReference Update="Google.Protobuf" Version="3.24.3" />
<PackageReference Update="Grpc.Tools" Version="2.51.0" PrivateAssets="all" />
<PackageReference Update="MessagePack" Version="1.9.11" />
<PackageReference Update="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.1.5" />
<PackageReference Update="Microsoft.Azure.SignalR" Version="1.25.2" />
<PackageReference Update="Microsoft.Azure.SignalR.Management" Version="1.25.2" />
<PackageReference Update="Microsoft.Azure.SignalR.Protocols" Version="1.25.2" />
<PackageReference Update="MessagePack" Version="2.5.192" />
<PackageReference Update="Microsoft.Azure.SignalR" Version="1.29.0" />
<PackageReference Update="Microsoft.Azure.SignalR.Management" Version="1.29.0" />
<PackageReference Update="Microsoft.Azure.SignalR.Protocols" Version="1.29.0" />
<PackageReference Update="Microsoft.Azure.SignalR.Serverless.Protocols" Version="1.10.0" />
<PackageReference Update="Microsoft.Azure.WebJobs" Version="3.0.37" />
<PackageReference Update="Microsoft.Azure.WebJobs.Sources" Version="3.0.37" PrivateAssets="All"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks);net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<PackageId>Microsoft.Azure.WebJobs.Extensions.SignalRService</PackageId>
<Version>1.16.0-beta.1</Version>
<ApiCompatVersion>1.15.0</ApiCompatVersion>
<Version>2.0.0-beta.1</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<SignAssembly>true</SignAssembly>
<IsExtensionClientLibrary>true</IsExtensionClientLibrary>
Expand All @@ -16,7 +15,6 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Microsoft.Extensions.Azure" />
<PackageReference Include="Microsoft.AspNetCore.Http.Connections" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" />
<PackageReference Include="Microsoft.Azure.SignalR.Management" />
<PackageReference Include="Microsoft.Azure.SignalR" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Buffers;

namespace Microsoft.AspNetCore.Internal;

/// <summary>
/// Copied from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/
/// </summary>
internal static class BinaryMessageFormatter
{
public static void WriteLengthPrefix(long length, IBufferWriter<byte> output)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.IO;
Expand All @@ -8,6 +8,11 @@

namespace Microsoft.AspNetCore.SignalR.Protocol;

#nullable enable

/// <summary>
/// Copied from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/
/// </summary>
internal sealed class DefaultMessagePackHubProtocolWorker : MessagePackHubProtocolWorker
{
private readonly MessagePackSerializerOptions _messagePackSerializerOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Internal;

/// <summary>
/// Copied from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/
/// </summary>
internal sealed class MemoryBufferWriter : Stream, IBufferWriter<byte>
{
[ThreadStatic]
private static MemoryBufferWriter? _cachedInstance;
private static MemoryBufferWriter _cachedInstance;

#if DEBUG
private bool _inUse;
Expand All @@ -26,8 +26,8 @@ internal sealed class MemoryBufferWriter : Stream, IBufferWriter<byte>
private readonly int _minimumSegmentSize;
private int _bytesWritten;

private List<CompletedBuffer>? _completedSegments;
private byte[]? _currentSegment;
private List<CompletedBuffer> _completedSegments;
private byte[] _currentSegment;
private int _position;

public MemoryBufferWriter(int minimumSegmentSize = 4096)
Expand Down Expand Up @@ -146,7 +146,6 @@ public override Task CopyToAsync(Stream destination, int bufferSize, Cancellatio
return CopyToSlowAsync(destination, cancellationToken);
}

[MemberNotNull(nameof(_currentSegment))]
private void EnsureCapacity(int sizeHint)
{
// This does the Right Thing. It only subtracts _position from the current segment length if it's non-null.
Expand All @@ -166,7 +165,6 @@ private void EnsureCapacity(int sizeHint)
AddSegment(sizeHint);
}

[MemberNotNull(nameof(_currentSegment))]
private void AddSegment(int sizeHint = 0)
{
if (_currentSegment != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Buffers;
Expand All @@ -11,70 +11,54 @@
using MessagePack.Resolvers;

using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.SignalR.Protocol;

#nullable enable

/// <summary>
/// Implements the SignalR Hub Protocol using MessagePack.
/// Implements the SignalR Hub Protocol using MessagePack. Copied from https://github.com/dotnet/aspnetcore/blob/0825def633c99d9fdd74e47e69bcde3935a5fe74/
/// </summary>
public class MessagePackHubProtocol : IHubProtocol
internal class MessagePackHubProtocol : IHubProtocol
{
private const string ProtocolName = "messagepack";
private const int ProtocolVersion = 2;
private readonly DefaultMessagePackHubProtocolWorker _worker;
private readonly MessagePackSerializerOptions _messagePackSerializerOptions =
MessagePackSerializerOptions
.Standard
.WithResolver(SignalRResolver.Instance)
.WithSecurity(MessagePackSecurity.UntrustedData);

/// <inheritdoc />
public string Name => ProtocolName;

/// <inheritdoc />
public int Version => ProtocolVersion;

/// <inheritdoc />
public TransferFormat TransferFormat => TransferFormat.Binary;

/// <summary>
/// Initializes a new instance of the <see cref="MessagePackHubProtocol"/> class.
/// </summary>
public MessagePackHubProtocol()
: this(Options.Create(new MessagePackHubProtocolOptions()))
{ }

/// <summary>
/// Initializes a new instance of the <see cref="MessagePackHubProtocol"/> class.
/// </summary>
/// <param name="options">The options used to initialize the protocol.</param>
public MessagePackHubProtocol(IOptions<MessagePackHubProtocolOptions> options)
{
ArgumentNullThrowHelper.ThrowIfNull(options);

_worker = new DefaultMessagePackHubProtocolWorker(options.Value.SerializerOptions);
_worker = new DefaultMessagePackHubProtocolWorker(_messagePackSerializerOptions);
}

/// <inheritdoc />
public bool IsVersionSupported(int version)
{
return version <= Version;
}

/// <inheritdoc />
#if NETSTANDARD2_0
public bool TryParseMessage(ref ReadOnlySequence<byte> input, IInvocationBinder binder, out HubMessage? message)
#else
public bool TryParseMessage(ref ReadOnlySequence<byte> input, IInvocationBinder binder, [NotNullWhen(true)] out HubMessage? message)
=> _worker.TryParseMessage(ref input, binder, out message);
#endif
=> throw new NotImplementedException();

/// <inheritdoc />
public void WriteMessage(HubMessage message, IBufferWriter<byte> output)
=> _worker.WriteMessage(message, output);

/// <inheritdoc />
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
=> _worker.GetMessageBytes(message);

internal static MessagePackSerializerOptions CreateDefaultMessagePackSerializerOptions() =>
MessagePackSerializerOptions
.Standard
.WithResolver(SignalRResolver.Instance)
.WithSecurity(MessagePackSecurity.UntrustedData);

internal sealed class SignalRResolver : IFormatterResolver
{
public static readonly IFormatterResolver Instance = new SignalRResolver();
Expand Down
Loading

0 comments on commit 56fd73a

Please sign in to comment.