From 3a4401038a5842fccd1027d3cb91d75a59c4598d Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Fri, 10 Feb 2023 12:31:08 +0100 Subject: [PATCH] Messagepack (#43) * add messagepack to client * add resolvers * fixes on client --------- Co-authored-by: Stanley Dimant Co-authored-by: rootdarkarchon --- MareAPI | 2 +- MareSynchronos/MareSynchronos.csproj | 3 ++- MareSynchronos/WebAPI/ApiController.cs | 32 ++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/MareAPI b/MareAPI index f604e6d..24faa35 160000 --- a/MareAPI +++ b/MareAPI @@ -1 +1 @@ -Subproject commit f604e6d27ebf760dd44feb53a11b8c04e91edc67 +Subproject commit 24faa352e7a7dee6df8fc1413075a9129b3357d5 diff --git a/MareSynchronos/MareSynchronos.csproj b/MareSynchronos/MareSynchronos.csproj index 9e8dd22..c35577b 100644 --- a/MareSynchronos/MareSynchronos.csproj +++ b/MareSynchronos/MareSynchronos.csproj @@ -3,7 +3,7 @@ - 0.7.22 + 0.7.23 https://github.com/Penumbra-Sync/client @@ -33,6 +33,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/MareSynchronos/WebAPI/ApiController.cs b/MareSynchronos/WebAPI/ApiController.cs index 602cee0..6cf50ca 100644 --- a/MareSynchronos/WebAPI/ApiController.cs +++ b/MareSynchronos/WebAPI/ApiController.cs @@ -13,6 +13,9 @@ using MareSynchronos.Managers; using Dalamud.Utility; using MareSynchronos.MareConfiguration; using MareSynchronos.Mediator; +using MessagePack; +using Microsoft.Extensions.DependencyInjection; +using MessagePack.Resolvers; namespace MareSynchronos.WebAPI; public partial class ApiController : MediatorSubscriberBase, IDisposable, IMareHubClient @@ -183,12 +186,12 @@ public partial class ApiController : MediatorSubscriberBase, IDisposable, IMareH await _mareHub.StartAsync(token).ConfigureAwait(false); - await InitializeData().ConfigureAwait(false); - _connectionDto = await GetConnectionDto().ConfigureAwait(false); ServerState = ServerState.Connected; + await InitializeData().ConfigureAwait(false); + if (_connectionDto.ServerVersion != IMareHub.ApiVersion) { await StopConnection(token, ServerState.VersionMisMatch).ConfigureAwait(false); @@ -315,6 +318,25 @@ public partial class ApiController : MediatorSubscriberBase, IDisposable, IMareH options.Headers.Add("Authorization", "Bearer " + _serverManager.GetToken()); options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling; }) + .AddMessagePackProtocol(opt => + { + var resolver = CompositeResolver.Create(StandardResolverAllowPrivate.Instance, + BuiltinResolver.Instance, + AttributeFormatterResolver.Instance, + // replace enum resolver + DynamicEnumAsStringResolver.Instance, + DynamicGenericResolver.Instance, + DynamicUnionResolver.Instance, + DynamicObjectResolver.Instance, + PrimitiveObjectResolver.Instance, + // final fallback(last priority) + StandardResolver.Instance); + + opt.SerializerOptions = + MessagePackSerializerOptions.Standard + .WithCompression(MessagePackCompression.Lz4Block) + .WithResolver(resolver); + }) .WithAutomaticReconnect(new ForeverRetryPolicy(Mediator)) .ConfigureLogging(a => { @@ -352,6 +374,12 @@ public partial class ApiController : MediatorSubscriberBase, IDisposable, IMareH ServerState = ServerState.Connecting; await InitializeData().ConfigureAwait(false); _connectionDto = await GetConnectionDto().ConfigureAwait(false); + if (_connectionDto.ServerVersion != IMareHub.ApiVersion) + { + CancellationTokenSource cts = new(); + await StopConnection(cts.Token, ServerState.VersionMisMatch).ConfigureAwait(false); + return; + } ServerState = ServerState.Connected; }