Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NerdbankMessagePackFormatter #1100

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="$(VisualStudioThreadingVersion)" />
<PackageVersion Include="Microsoft.VisualStudio.Threading" Version="$(VisualStudioThreadingVersion)" />
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<PackageVersion Include="Nerdbank.MessagePack" Version="0.3.54-beta" />
<PackageVersion Include="Nerdbank.Streams" Version="2.11.74" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="System.Collections.Immutable" Version="6.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
<PackageVersion Include="System.IO.Pipelines" Version="9.0.0" />
<PackageVersion Include="System.IO.Pipes" Version="4.3.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
Expand Down
15 changes: 15 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@
</config>
<packageSources>
<clear />
<add key="PublicCI" value="https://pkgs.dev.azure.com/andrewarnott/OSS/_packaging/PublicCI/nuget/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="msft_consumption" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/msft_consumption/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources>
<!-- Defend against user or machine level disabling of sources that we list in this file. -->
<clear />
</disabledPackageSources>
<packageSourceMapping>
<!-- key value for <packageSource> should match key values from <packageSources> element -->
<packageSource key="msft_consumption">
<package pattern="*" />
</packageSource>
<packageSource key="nuget.org">
<package pattern="System.*" />
<package pattern="Microsoft.Bcl.*" />
</packageSource>
<packageSource key="PublicCI">
<package pattern="Nerdbank.MessagePack" />
</packageSource>
</packageSourceMapping>
</configuration>
1 change: 1 addition & 0 deletions src/StreamJsonRpc/FormatterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO.Pipelines;
using System.Reflection;
using System.Runtime.Serialization;
using Nerdbank.MessagePack;
using Nerdbank.Streams;
using StreamJsonRpc.Protocol;
using StreamJsonRpc.Reflection;
Expand Down
2 changes: 1 addition & 1 deletion src/StreamJsonRpc/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ protected virtual async ValueTask<JsonRpcMessage> DispatchRequestAsync(JsonRpcRe
}

/// <summary>
/// Sends the JSON-RPC message to <see cref="IJsonRpcMessageHandler"/> intance to be transmitted.
/// Sends the JSON-RPC message to <see cref="IJsonRpcMessageHandler"/> instance to be transmitted.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="cancellationToken">A token to cancel the send request.</param>
Expand Down
104 changes: 104 additions & 0 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.CommonString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using NBMP = Nerdbank.MessagePack;

namespace StreamJsonRpc;

public partial class NerdbankMessagePackFormatter
{
[DebuggerDisplay("{" + nameof(Value) + "}")]
private struct CommonString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully we can replace this with the API that fulfills AArnott/Nerdbank.MessagePack#183

{
internal CommonString(string value)
{
Requires.Argument(value.Length > 0 && value.Length <= 16, nameof(value), "Length must be >0 and <=16.");
this.Value = value;
ReadOnlyMemory<byte> encodedBytes = MessagePack.Internal.CodeGenHelpers.GetEncodedStringBytes(value);
this.EncodedBytes = encodedBytes;

ReadOnlySpan<byte> span = this.EncodedBytes.Span.Slice(1);
this.Key = MessagePack.Internal.AutomataKeyGen.GetKey(ref span); // header is 1 byte because string length <= 16
this.Key2 = span.Length > 0 ? (ulong?)MessagePack.Internal.AutomataKeyGen.GetKey(ref span) : null;
}

/// <summary>
/// Gets the original string.
/// </summary>
internal string Value { get; }

/// <summary>
/// Gets the 64-bit integer that represents the string without decoding it.
/// </summary>
private ulong Key { get; }

/// <summary>
/// Gets the next 64-bit integer that represents the string without decoding it.
/// </summary>
private ulong? Key2 { get; }

/// <summary>
/// Gets the messagepack header and UTF-8 bytes for this string.
/// </summary>
private ReadOnlyMemory<byte> EncodedBytes { get; }

/// <summary>
/// Writes out the messagepack binary for this common string, if it matches the given value.
/// </summary>
/// <param name="writer">The writer to use.</param>
/// <param name="value">The value to be written, if it matches this <see cref="CommonString"/>.</param>
/// <returns><see langword="true"/> if <paramref name="value"/> matches this <see cref="Value"/> and it was written; <see langword="false"/> otherwise.</returns>
internal bool TryWrite(ref NBMP::MessagePackWriter writer, string value)
{
if (value == this.Value)
{
this.Write(ref writer);
return true;
}

return false;
}

internal readonly void Write(ref NBMP::MessagePackWriter writer) => writer.WriteRaw(this.EncodedBytes.Span);

/// <summary>
/// Checks whether a span of UTF-8 bytes equal this common string.
/// </summary>
/// <param name="utf8String">The UTF-8 string.</param>
/// <returns><see langword="true"/> if the UTF-8 bytes are the encoding of this common string; <see langword="false"/> otherwise.</returns>
internal readonly bool TryRead(ReadOnlySpan<byte> utf8String)
{
if (utf8String.Length != this.EncodedBytes.Length - 1)
{
return false;
}

ulong key1 = MessagePack.Internal.AutomataKeyGen.GetKey(ref utf8String);
if (key1 != this.Key)
{
return false;
}

if (utf8String.Length > 0)
{
if (!this.Key2.HasValue)
{
return false;
}

ulong key2 = MessagePack.Internal.AutomataKeyGen.GetKey(ref utf8String);
if (key2 != this.Key2.Value)
{
return false;
}
}
else if (this.Key2.HasValue)
{
return false;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.IO.Pipelines;
using Nerdbank.MessagePack;
using PolyType;
using PolyType.Abstractions;
using StreamJsonRpc.Reflection;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
/// <remarks>
/// The MessagePack implementation used here comes from https://github.com/AArnott/Nerdbank.MessagePack.
/// </remarks>
public sealed partial class NerdbankMessagePackFormatter
{
/// <summary>
/// Provides methods to build a serialization context for the <see cref="NerdbankMessagePackFormatter"/>.
/// </summary>
public class FormatterContextBuilder
{
private readonly NerdbankMessagePackFormatter formatter;
private readonly FormatterContext baseContext;

private ImmutableArray<ITypeShapeProvider>.Builder? typeShapeProvidersBuilder = null;

/// <summary>
/// Initializes a new instance of the <see cref="FormatterContextBuilder"/> class.
/// </summary>
/// <param name="formatter">The formatter to use.</param>
/// <param name="baseContext">The base context to build upon.</param>
internal FormatterContextBuilder(NerdbankMessagePackFormatter formatter, FormatterContext baseContext)
{
this.formatter = formatter;
this.baseContext = baseContext;
}

/// <summary>
/// Adds a type shape provider to the context.
/// </summary>
/// <param name="provider">The type shape provider to add.</param>
public void AddTypeShapeProvider(ITypeShapeProvider provider)
{
this.typeShapeProvidersBuilder ??= ImmutableArray.CreateBuilder<ITypeShapeProvider>();
this.typeShapeProvidersBuilder.Add(provider);
}

/// <summary>
/// Registers an async enumerable type with the context.
/// </summary>
/// <typeparam name="TEnumerable">The type of the async enumerable.</typeparam>
/// <typeparam name="TElement">The type of the elements in the async enumerable.</typeparam>
public void RegisterAsyncEnumerableType<TEnumerable, TElement>()
where TEnumerable : IAsyncEnumerable<TElement>
{
MessagePackConverter<TEnumerable> converter = this.formatter.asyncEnumerableConverterResolver.GetConverter<TEnumerable>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers a converter with the context.
/// </summary>
/// <typeparam name="T">The type the converter handles.</typeparam>
/// <param name="converter">The converter to register.</param>
public void RegisterConverter<T>(MessagePackConverter<T> converter)
{
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers known subtypes for a base type with the context.
/// </summary>
/// <typeparam name="TBase">The base type.</typeparam>
/// <param name="mapping">The mapping of known subtypes.</param>
public void RegisterKnownSubTypes<TBase>(KnownSubTypeMapping<TBase> mapping)
{
this.baseContext.Serializer.RegisterKnownSubTypes(mapping);
}

/// <summary>
/// Registers a progress type with the context.
/// </summary>
/// <typeparam name="TProgress">The type of the progress.</typeparam>
/// <typeparam name="TReport">The type of the report.</typeparam>
public void RegisterProgressType<TProgress, TReport>()
where TProgress : IProgress<TReport>
{
MessagePackConverter<TProgress> converter = this.formatter.progressConverterResolver.GetConverter<TProgress>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers a duplex pipe type with the context.
/// </summary>
/// <typeparam name="TPipe">The type of the duplex pipe.</typeparam>
public void RegisterDuplexPipeType<TPipe>()
where TPipe : IDuplexPipe
{
MessagePackConverter<TPipe> converter = this.formatter.pipeConverterResolver.GetConverter<TPipe>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers a pipe reader type with the context.
/// </summary>
/// <typeparam name="TReader">The type of the pipe reader.</typeparam>
public void RegisterPipeReaderType<TReader>()
where TReader : PipeReader
{
MessagePackConverter<TReader> converter = this.formatter.pipeConverterResolver.GetConverter<TReader>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers a pipe writer type with the context.
/// </summary>
/// <typeparam name="TWriter">The type of the pipe writer.</typeparam>
public void RegisterPipeWriterType<TWriter>()
where TWriter : PipeWriter
{
MessagePackConverter<TWriter> converter = this.formatter.pipeConverterResolver.GetConverter<TWriter>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers a stream type with the context.
/// </summary>
/// <typeparam name="TStream">The type of the stream.</typeparam>
public void RegisterStreamType<TStream>()
where TStream : Stream
{
MessagePackConverter<TStream> converter = this.formatter.pipeConverterResolver.GetConverter<TStream>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers an exception type with the context.
/// </summary>
/// <typeparam name="TException">The type of the exception.</typeparam>
public void RegisterExceptionType<TException>()
where TException : Exception
{
MessagePackConverter<TException> converter = this.formatter.exceptionResolver.GetConverter<TException>();
this.baseContext.Serializer.RegisterConverter(converter);
}

/// <summary>
/// Registers an RPC marshalable type with the context.
/// </summary>
/// <typeparam name="T">The type to register.</typeparam>
public void RegisterRpcMarshalableType<T>()
where T : class
{
if (MessageFormatterRpcMarshaledContextTracker.TryGetMarshalOptionsForType(
typeof(T),
out JsonRpcProxyOptions? proxyOptions,
out JsonRpcTargetOptions? targetOptions,
out RpcMarshalableAttribute? attribute))
{
var converter = (RpcMarshalableConverter<T>)Activator.CreateInstance(
typeof(RpcMarshalableConverter<>).MakeGenericType(typeof(T)),
this.formatter,
proxyOptions,
targetOptions,
attribute)!;

this.baseContext.Serializer.RegisterConverter(converter);
}

// TODO: Throw?
}

/// <summary>
/// Builds the formatter context.
/// </summary>
/// <returns>The built formatter context.</returns>
internal FormatterContext Build()
{
if (this.typeShapeProvidersBuilder is null || this.typeShapeProvidersBuilder.Count < 1)
{
return this.baseContext;
}

ITypeShapeProvider provider = this.typeShapeProvidersBuilder.Count == 1
? this.typeShapeProvidersBuilder[0]
: new CompositeTypeShapeProvider(this.typeShapeProvidersBuilder.ToImmutable());

return new FormatterContext(this.baseContext.Serializer, provider);
}
}

private class CompositeTypeShapeProvider : ITypeShapeProvider
{
private readonly ImmutableArray<ITypeShapeProvider> providers;

internal CompositeTypeShapeProvider(ImmutableArray<ITypeShapeProvider> providers)
{
this.providers = providers;
}

public ITypeShape? GetShape(Type type)
{
foreach (ITypeShapeProvider provider in this.providers)
{
ITypeShape? shape = provider.GetShape(type);
if (shape is not null)
{
return shape;
}
}

return null;
}
}
}
Loading