Skip to content

Commit

Permalink
feat: binary serialization - initial support (see Stl.Serialization.I…
Browse files Browse the repository at this point in the history
…ByteXxx)
  • Loading branch information
alexyakunin committed Sep 27, 2021
1 parent bb2366a commit 320a22b
Show file tree
Hide file tree
Showing 36 changed files with 478 additions and 483 deletions.
1 change: 1 addition & 0 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkCoreVersion)" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Sqlite" Version="$(EntityFrameworkCoreVersion)" />
<PackageReference Update="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EntityFrameworkCoreVersion)" />
<PackageReference Update="Microsoft.Toolkit.HighPerformance" Version="7.0.*" />
<PackageReference Update="Npgsql" Version="$(EntityFrameworkCoreVersion)" />
<PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(EntityFrameworkCoreNpgsqlVersion)" />
<PackageReference Update="Microsoft.Identity.Web" Version="1.16.*" />
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Fusion.Client/WebSocketChannelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public async Task<Channel<BridgeMessage>> CreateChannel(
if (IsMessageLoggingEnabled)
stringChannel = stringChannel.WithLogger(clientId, Log, MessageLogLevel, MessageMaxLength);
var serializers = SerializerFactory.Invoke(Services);
var resultChannel = stringChannel.WithSerializer(serializers);
var resultChannel = stringChannel.WithUtf16Serializer(serializers);
_ = wsChannel.WhenCompleted(CancellationToken.None).ContinueWith(async _ => {
await Task.Delay(1000, default).ConfigureAwait(false);
await wsChannel.DisposeAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace Stl.Fusion.EntityFramework.Authentication
[Index(nameof(IPAddress), nameof(IsSignOutForced))]
public class DbSessionInfo<TDbUserId> : IHasId<string>, IHasVersion<long>
{
private readonly NewtonsoftJsonSerialized<ImmutableOptionSet?> _options = new(ImmutableOptionSet.Empty);
private readonly NewtonsoftJsonSerialized<ImmutableOptionSet> _options =
NewtonsoftJsonSerialized.New(ImmutableOptionSet.Empty);
private DateTime _createdAt;
private DateTime _lastSeenAt;

Expand Down Expand Up @@ -50,7 +51,7 @@ public string OptionsJson {

[NotMapped, JsonIgnore]
public ImmutableOptionSet Options {
get => _options.Value ?? ImmutableOptionSet.Empty;
get => _options.Value;
set => _options.Value = value;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Stl.Fusion.EntityFramework/Authentication/DbUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace Stl.Fusion.EntityFramework.Authentication
public class DbUser<TDbUserId> : IHasId<TDbUserId>, IHasVersion<long>
where TDbUserId : notnull
{
private readonly NewtonsoftJsonSerialized<ImmutableDictionary<string, string>?> _claims =
new(ImmutableDictionary<string, string>.Empty);
private readonly NewtonsoftJsonSerialized<ImmutableDictionary<string, string>> _claims =
NewtonsoftJsonSerialized.New(ImmutableDictionary<string, string>.Empty);

[Key] public TDbUserId Id { get; set; } = default!;
[ConcurrencyCheck] public long Version { get; set; }
Expand All @@ -29,7 +29,7 @@ public string ClaimsJson {

[NotMapped, JsonIgnore]
public ImmutableDictionary<string, string> Claims {
get => _claims.Value ?? ImmutableDictionary<string, string>.Empty;
get => _claims.Value;
set => _claims.Value = value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Stl.Fusion.EntityFramework/Operations/DbOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Stl.Fusion.EntityFramework.Operations
[Index(nameof(CommitTime), Name = "IX_CommitTime")]
public class DbOperation : IOperation
{
private readonly NewtonsoftJsonSerialized<object?> _command = new(default(object?));
private readonly NewtonsoftJsonSerialized<OptionSet> _items = new(new OptionSet());
private readonly NewtonsoftJsonSerialized<object?> _command = NewtonsoftJsonSerialized.New(default(object?));
private readonly NewtonsoftJsonSerialized<OptionSet> _items = NewtonsoftJsonSerialized.New(new OptionSet());
private DateTime _startTime;
private DateTime _commitTime;

Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Fusion.Server.NetFx/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private async Task HandleWebSocket(WebSocketContext wsContext, string clientId)
var webSocket = wsContext.WebSocket;
await using var wsChannel = new WebSocketChannel(webSocket);
var channel = wsChannel
.WithSerializer(serializers)
.WithUtf16Serializer(serializers)
.WithId(clientId);
Publisher.ChannelHub.Attach(channel);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/Stl.Fusion.Server/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task HandleRequest(HttpContext context)
var webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);
await using var wsChannel = new WebSocketChannel(webSocket);
var channel = wsChannel
.WithSerializer(serializers)
.WithUtf16Serializer(serializers)
.WithId(clientId);
Publisher.ChannelHub.Attach(channel);
try {
Expand Down
27 changes: 19 additions & 8 deletions src/Stl.Testing/SerializationTestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static T AssertPassesThroughAllSerializers<T>(this T value, ITestOutputHe
v.Should().Be(value);
v = v.PassThroughNewtonsoftJsonSerialized(output);
v.Should().Be(value);
v = v.PassThroughMessagePackSerializer(output);
v = v.PassThroughMessagePackByteSerializer(output);
v.Should().Be(value);
v = v.PassThroughMessagePackSerialized(output);
v.Should().Be(value);
return v;
}
Expand All @@ -47,7 +49,8 @@ public static T PassThroughAllSerializers<T>(this T value, ITestOutputHelper? ou
v = v.PassThroughTypeWritingSerializer(output);
v = v.PassThroughSystemJsonSerialized(output);
v = v.PassThroughNewtonsoftJsonSerialized(output);
v = v.PassThroughMessagePackSerializer(output);
v = v.PassThroughMessagePackByteSerializer(output);
v = v.PassThroughMessagePackSerialized(output);
return v;
}

Expand Down Expand Up @@ -100,14 +103,22 @@ public static T PassThroughNewtonsoftJsonSerialized<T>(this T value, ITestOutput

// MessagePack serializer

public static T PassThroughMessagePackSerializer<T>(this T value, ITestOutputHelper? output = null)
public static T PassThroughMessagePackByteSerializer<T>(this T value, ITestOutputHelper? output = null)
{
var options = MessagePackSerializer.DefaultOptions;
using var ms = new MemoryStream();
MessagePackSerializer.Serialize(ms, value, options);
ms.Position = 0;
var v1 = MessagePackSerializer.Deserialize<T>(ms, options);
var s = new MessagePackByteSerializer().ToTyped<T>();
using var bufferWriter = s.Writer.Write(value);
var data = bufferWriter.WrittenMemory.ToArray();
output?.WriteLine($"MessagePackByteSerializer: {SystemJsonSerializer.Default.Write(data)}");
var v1 = s.Reader.Read(data);
return v1;
}

public static T PassThroughMessagePackSerialized<T>(this T value, ITestOutputHelper? output = null)
{
var v1 = MessagePackSerialized.New(value);
output?.WriteLine($"MessagePackSerialized: {v1.Data}");
var v2 = MessagePackSerialized.New<T>(v1.Data);
return v2.Value;
}
}
}
32 changes: 31 additions & 1 deletion src/Stl/Channels/ChannelExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static async Task ConsumeSilent<T>(
}
}

public static Channel<T> WithSerializer<T>(
public static Channel<T> WithUtf16Serializer<T>(
this Channel<string> downstreamChannel,
IUtf16Serializer<T> serializer,
BoundedChannelOptions? channelOptions = null,
Expand All @@ -134,6 +134,36 @@ public static Channel<T> WithSerializer<T>(
return pair.Channel2;
}


public static Channel<T> WithByteSerializer<T>(
this Channel<ReadOnlyMemory<byte>> downstreamChannel,
ByteSerializer<T> serializer,
BoundedChannelOptions? channelOptions = null,
CancellationToken cancellationToken = default)
{
channelOptions ??= new BoundedChannelOptions(16) {
FullMode = BoundedChannelFullMode.Wait,
SingleReader = true,
SingleWriter = true,
AllowSynchronousContinuations = true,
};
var pair = ChannelPair.CreateTwisted(
Channel.CreateBounded<T>(channelOptions),
Channel.CreateBounded<T>(channelOptions));

downstreamChannel.Connect(pair.Channel1,
serializer.Reader.Read,
Write,
ChannelCompletionMode.CompleteAndPropagateError,
cancellationToken);
return pair.Channel2;

ReadOnlyMemory<byte> Write(T value) {
using var bufferWriter = serializer.Writer.Write(value);
return bufferWriter.WrittenMemory.ToArray();
}
}

public static Channel<T> WithLogger<T>(
this Channel<T> channel,
string channelName,
Expand Down
4 changes: 3 additions & 1 deletion src/Stl/Collections/ImmutableOptionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public ImmutableOptionSet(ImmutableDictionary<Symbol, object>? items)

[JsonConstructor]
public ImmutableOptionSet(Dictionary<string, NewtonsoftJsonSerialized<object>>? jsonCompatibleItems)
: this(jsonCompatibleItems?.ToImmutableDictionary(p => (Symbol) p.Key, p => p.Value.Value))
: this(jsonCompatibleItems?.ToImmutableDictionary(
p => (Symbol) p.Key,
p => p.Value.Value))
{ }

public object? GetService(Type serviceType)
Expand Down
Loading

0 comments on commit 320a22b

Please sign in to comment.