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 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e934175
Update package versions and target frameworks
Dec 18, 2024
6819e6b
Continue implementing NerdbankMessagePackFormatter.
Dec 20, 2024
052592e
Refactor NerdbankMessagePackFormatter for converters
Dec 20, 2024
259558a
Add type converter registration methods to interface
Dec 21, 2024
e87da44
Enhance serialization context and type shape providers
Dec 21, 2024
ca439ea
Enhance NerdbankMessagePackFormatter with new methods
Dec 21, 2024
b12c357
Replaced the `IFormatterContextBuilder` interface with a concrete `Fo…
Dec 21, 2024
345a39c
Downgrade package versions for compatibility
Dec 21, 2024
ad436f7
User RawMessagePack from Nerdbank.MessagePack
Dec 21, 2024
1a795c7
Remove namespace aliases
Dec 21, 2024
0e6b4aa
Add unit tests
Dec 21, 2024
bca2af4
Some tests are now passing.
Dec 22, 2024
d693a1c
Cleaning up formatters. Rename `FormatterContext` to `FormatterProfile`.
Dec 22, 2024
0c06ae5
Adopt MessagePackString and configure test formatters.
Dec 24, 2024
166ae49
Adopt simpler MessagePackString write API
Dec 26, 2024
3d3cb90
Fix bug when searching property names from peek reader
Dec 26, 2024
d8430f4
Begin process of making converters stateless
Dec 27, 2024
6444f55
Remove state from converters.
Dec 27, 2024
bb22ee8
Refactor and add comments for Profile and Profile.Builder.
Dec 27, 2024
e171cdb
Add additional profile configuration to test suite.
Dec 28, 2024
d36d607
Update Benchmarks
Dec 28, 2024
949b3ab
Upgrade NB.MB version; Update tests.
Dec 29, 2024
f94bf1d
Refactor serialization to use ReadOnlySequence<byte>
Jan 1, 2025
eecd92c
Enhance NerdbankMessagePackFormatter with new types
Jan 1, 2025
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.71-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>
6 changes: 5 additions & 1 deletion src/StreamJsonRpc/FormatterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.IO.Pipelines;
using System.Reflection;
using System.Runtime.Serialization;
using Nerdbank.MessagePack;
using Nerdbank.Streams;
using PolyType;
using StreamJsonRpc.Protocol;
using StreamJsonRpc.Reflection;

Expand Down Expand Up @@ -320,7 +322,7 @@ public void Dispose()
/// <summary>
/// A base class for top-level property bags that should be declared in the derived formatter class.
/// </summary>
protected abstract class TopLevelPropertyBagBase
protected internal abstract class TopLevelPropertyBagBase
{
private readonly bool isOutbound;
private Dictionary<string, (Type, object?)>? outboundProperties;
Expand Down Expand Up @@ -436,6 +438,7 @@ protected abstract class JsonRpcErrorBase : JsonRpcError, IJsonRpcMessageBufferM
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[IgnoreDataMember]
[PropertyShape(Ignore = true)]
public TopLevelPropertyBagBase? TopLevelPropertyBag { get; set; }

void IJsonRpcMessageBufferManager.DeserializationComplete(JsonRpcMessage message)
Expand Down Expand Up @@ -480,6 +483,7 @@ protected abstract class JsonRpcResultBase : JsonRpcResult, IJsonRpcMessageBuffe
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[IgnoreDataMember]
[PropertyShape(Ignore = true)]
public TopLevelPropertyBagBase? TopLevelPropertyBag { get; set; }

void IJsonRpcMessageBufferManager.DeserializationComplete(JsonRpcMessage message)
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
60 changes: 60 additions & 0 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 Nerdbank.MessagePack;
using StreamJsonRpc.Protocol;
using NBMP = Nerdbank.MessagePack;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
public partial class NerdbankMessagePackFormatter
{
/// <summary>
/// The constant "jsonrpc", in its various forms.
/// </summary>
private static readonly MessagePackString VersionPropertyName = new(Constants.jsonrpc);

/// <summary>
/// The constant "id", in its various forms.
/// </summary>
private static readonly MessagePackString IdPropertyName = new(Constants.id);

/// <summary>
/// The constant "method", in its various forms.
/// </summary>
private static readonly MessagePackString MethodPropertyName = new(Constants.Request.method);

/// <summary>
/// The constant "result", in its various forms.
/// </summary>
private static readonly MessagePackString ResultPropertyName = new(Constants.Result.result);

/// <summary>
/// The constant "error", in its various forms.
/// </summary>
private static readonly MessagePackString ErrorPropertyName = new(Constants.Error.error);

/// <summary>
/// The constant "params", in its various forms.
/// </summary>
private static readonly MessagePackString ParamsPropertyName = new(Constants.Request.@params);

/// <summary>
/// The constant "traceparent", in its various forms.
/// </summary>
private static readonly MessagePackString TraceParentPropertyName = new(Constants.Request.traceparent);

/// <summary>
/// The constant "tracestate", in its various forms.
/// </summary>
private static readonly MessagePackString TraceStatePropertyName = new(Constants.Request.tracestate);

/// <summary>
/// The constant "2.0", in its various forms.
/// </summary>
private static readonly MessagePackString Version2 = new("2.0");
}
31 changes: 31 additions & 0 deletions src/StreamJsonRpc/NerdbankMessagePackFormatter.FormatterProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Nerdbank.MessagePack;
using PolyType;

namespace StreamJsonRpc;

/// <summary>
/// Serializes JSON-RPC messages using MessagePack (a fast, compact binary format).
/// </summary>
public sealed partial class NerdbankMessagePackFormatter
{
/// <summary>
/// Initializes a new instance of the <see cref="FormatterProfile"/> class.
/// </summary>
/// <param name="serializer">The MessagePack serializer to use.</param>
/// <param name="shapeProvider">The type shape provider to use.</param>
public class FormatterProfile(MessagePackSerializer serializer, ITypeShapeProvider shapeProvider)
{
/// <summary>
/// Gets the MessagePack serializer.
/// </summary>
internal MessagePackSerializer Serializer => serializer;

/// <summary>
/// Gets the type shape provider.
/// </summary>
internal ITypeShapeProvider ShapeProvider => shapeProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// 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>
public sealed partial class NerdbankMessagePackFormatter
{
/// <summary>
/// Provides methods to build a serialization profile for the <see cref="NerdbankMessagePackFormatter"/>.
/// </summary>
public class FormatterProfileBuilder
{
private readonly NerdbankMessagePackFormatter formatter;
private readonly FormatterProfile baseProfile;

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

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

/// <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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.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.baseProfile.Serializer.RegisterConverter(converter);
return;
}

// TODO: Throw?
throw new NotSupportedException();
}

/// <summary>
/// Builds the formatter profile.
/// </summary>
/// <returns>The built formatter profile.</returns>
public FormatterProfile Build()
{
if (this.typeShapeProvidersBuilder is null || this.typeShapeProvidersBuilder.Count < 1)
{
return this.baseProfile;
}

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

return new FormatterProfile(this.baseProfile.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