Skip to content
Open
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
9 changes: 2 additions & 7 deletions Common.Tests/Redis/PubSubSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MessagePack;
using MessagePack.Resolvers;
using OpenShock.Common.Models;
using OpenShock.Common.OpenShockDb;
using OpenShock.Common.Redis.PubSub;
Expand All @@ -12,13 +11,9 @@ namespace OpenShock.Common.Tests.Redis;
/// </summary>
public sealed class PubSubSerializationTests
{
// Same resolver chain as OpenShockApplication sets as the default
private static readonly MessagePackSerializerOptions Options = MessagePackSerializerOptions.Standard.WithResolver(
CompositeResolver.Create(NativeGuidResolver.Instance, StandardResolver.Instance));
private static T FromHex<T>(string hex) => MessagePackSerializer.Deserialize<T>(Convert.FromHexString(hex));

private static T FromHex<T>(string hex) => MessagePackSerializer.Deserialize<T>(Convert.FromHexString(hex), Options);

private static string ToHex<T>(T value) => Convert.ToHexString(MessagePackSerializer.Serialize(value, Options));
private static string ToHex<T>(T value) => Convert.ToHexString(MessagePackSerializer.Serialize(value));

private static readonly Guid FixedId = Guid.Parse("01234567-89ab-cdef-0123-456789abcdef");
private static readonly string FixedIdHex = Convert.ToHexString(FixedId.ToByteArray());
Expand Down
10 changes: 1 addition & 9 deletions Common/OpenShockApplication.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using MessagePack;
using MessagePack.Resolvers;
using OpenShock.Common.Extensions;
using OpenShock.Common.Extensions;
using Serilog;

namespace OpenShock.Common;
Expand All @@ -9,12 +7,6 @@ public static class OpenShockApplication
{
public static WebApplicationBuilder CreateDefaultBuilder<TProgram>(string[] args) where TProgram : class
{
MessagePackSerializer.DefaultOptions = MessagePackSerializerOptions.Standard.WithResolver(
CompositeResolver.Create(
NativeGuidResolver.Instance,
StandardResolver.Instance
));

var builder = WebApplication.CreateSlimBuilder(args);

builder.Configuration.Sources.Clear();
Expand Down
11 changes: 6 additions & 5 deletions Common/Redis/PubSub/DeviceMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ public sealed class DeviceMessage
};
}

[Union(0, typeof(DeviceTriggerPayload))]
[Union(1, typeof(DeviceTogglePayload))]
[Union(2, typeof(DeviceControlPayload))]
[Union(3, typeof(DeviceOtaInstallPayload))]
[MessagePackObject]
[UnionTag<DeviceTriggerPayload>(0)]
[UnionTag<DeviceTogglePayload>(1)]
[UnionTag<DeviceControlPayload>(2)]
[UnionTag<DeviceOtaInstallPayload>(3)]
public closed class DeviceMessagePayload;

public enum DeviceTriggerType : byte
Expand Down Expand Up @@ -70,7 +71,7 @@ public sealed class DeviceControlPayload : DeviceMessagePayload
[MessagePackObject]
public sealed class ShockerControlCommand
{
[Key(0)] public required Guid ShockerId { get; init; }
[Key(0), MessagePackFormatter(typeof(DotNetOptimizedFormatterFactory))] public required Guid ShockerId { get; init; }
[Key(1)] public required ushort RfId { get; init; }
[Key(2)] public required byte Intensity { get; init; }
[Key(3)] public required ushort Duration { get; init; }
Expand Down
5 changes: 3 additions & 2 deletions Common/Redis/PubSub/DeviceStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OpenShock.Common.Redis.PubSub;
[MessagePackObject]
public sealed class DeviceStatus
{
[Key(0)] public required Guid DeviceId { get; init; }
[Key(0), MessagePackFormatter(typeof(DotNetOptimizedFormatterFactory))] public required Guid DeviceId { get; init; }
[Key(1)] public required DeviceStatusPayload Payload { get; init; }

public static DeviceStatus Create(Guid deviceId, DeviceBoolStateType stateType, bool state) => new()
Expand All @@ -19,7 +19,8 @@ public sealed class DeviceStatus
};
}

[Union(0, typeof(DeviceBoolStatePayload))]
[MessagePackObject]
[UnionTag<DeviceBoolStatePayload>(0)]
public closed class DeviceStatusPayload;

public enum DeviceBoolStateType : byte
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageVersion Include="Hangfire.PostgreSql" Version="1.21.1" />
<PackageVersion Include="IDisposableAnalyzers" Version="4.0.8" />
<PackageVersion Include="MailKit" Version="5.0.0" />
<PackageVersion Include="MessagePack" Version="3.1.12" />
<PackageVersion Include="MessagePack" Version="4.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Google" Version="11.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="11.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="11.0.0" />
Expand Down
3 changes: 1 addition & 2 deletions LiveControlGateway/LifetimeManager/HubLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ private async Task ConsumeDeviceQueue(RedisValue value, CancellationToken cancel
try
{
ReadOnlyMemory<byte> memView = value;
message = MessagePackSerializer.Deserialize<DeviceMessage>(memView,
cancellationToken: cancellationToken);
message = MessagePackSerializer.Deserialize<DeviceMessage>(memView.Span);
}
catch (Exception e)
{
Expand Down
Loading