4 Commits ef93041e92 ... 26d968c072

Author SHA1 Message Date
  frankvandenbos 26d968c072 Merge branch 'dominic' into frank 2 days ago
  frankvandenbos 7630daebad Merge commit '8c3eb8264f6c4c5648708d3dcfc336218358e66f' into dominic 4 days ago
  frankvandenbos 92c99388f1 Added package 4 days ago
  Kenric Nugteren 8c3eb8264f rpc: Switched to MessagePack 5 days ago

+ 1 - 0
InABox.Client.RPC/InABox.Client.RPC.csproj

@@ -13,6 +13,7 @@
     <ItemGroup>
       <PackageReference Include="H.Formatters" Version="2.0.59" />
       <PackageReference Include="H.Formatters.BinaryFormatter" Version="2.0.59" />
+      <PackageReference Include="H.Formatters.MessagePack" Version="15.0.0" />
       <PackageReference Include="H.Pipes" Version="2.0.59" />
       <PackageReference Include="WebSocket4Net" Version="0.15.2" />
     </ItemGroup>

+ 9 - 2
InABox.Client.RPC/Transports/Pipe/RPCClientPipeTransport.cs

@@ -14,7 +14,7 @@ namespace InABox.Rpc
         public RpcClientPipeTransport(string name)
         {
             _name = name;
-            _pipe = new PipeClient<RpcMessage>(_name, formatter:new BinaryFormatter());
+            _pipe = new PipeClient<RpcMessage>(_name, formatter: new MessagePackFormatter<RpcMessage>());
             _pipe.Connected += PipeConnected;
             _pipe.Disconnected += PipeDisconnected;
             _pipe.MessageReceived += PipeMessageReceived;
@@ -38,7 +38,14 @@ namespace InABox.Rpc
 
         public override void Send(RpcMessage message)
         {
-            _pipe.WriteAsync(message);
+            _pipe.WriteAsync(message).ContinueWith(task =>
+            {
+                if(task.Exception != null)
+                {
+                    PipeExceptionOccurred(this, new H.Pipes.Args.ExceptionEventArgs(task.Exception));
+                    Fail(message, task.Exception);
+                }
+            });
         }
 
         private void PipeConnected(object? sender, H.Pipes.Args.ConnectionEventArgs<RpcMessage> e)

+ 26 - 2
InABox.Client.RPC/Transports/RPCClientTransport.cs

@@ -15,6 +15,7 @@ namespace InABox.Rpc
         private RpcClientSession _session = new RpcClientSession();
         private ConcurrentDictionary<Guid, ManualResetEventSlim> _events = new ConcurrentDictionary<Guid, ManualResetEventSlim>();
         private ConcurrentDictionary<Guid, RpcMessage> _responses = new ConcurrentDictionary<Guid, RpcMessage>();
+        private ConcurrentDictionary<Guid, Exception> _exceptions = new ConcurrentDictionary<Guid, Exception>();
 
         private const int DefaultRequestTimeout = 5 * 60 * 1000; // 5 minutes
 
@@ -45,6 +46,16 @@ namespace InABox.Rpc
         
         public abstract void Send(RpcMessage message);
 
+        public void Fail(RpcMessage message, Exception e)
+        {
+            CoreUtils.LogException("", e, "Message Failed");
+            if(_events.TryGetValue(message.Id, out var ev))
+            {
+                _exceptions[message.Id] = e;
+                ev.Set();
+            }
+        }
+
         public void Accept(RpcMessage? message)
         {
             if (message == null)
@@ -89,8 +100,12 @@ namespace InABox.Rpc
             
             Send(request);
 
-            var response = GetResponse(request.Id, ev, DefaultRequestTimeout)
+            var response = GetResponse(request.Id, ev, DefaultRequestTimeout, out var e)
                 ?? throw new Exception($"{typeof(TCommand).Name}({request.Id}) returned NULL");
+            if(e != null)
+            {
+                throw e;
+            }
 
             switch (response.Error)
             {
@@ -126,11 +141,19 @@ namespace InABox.Rpc
             return ev;
         }
 
-        public RpcMessage? GetResponse(Guid id, ManualResetEventSlim ev, int timeout)
+        public RpcMessage? GetResponse(Guid id, ManualResetEventSlim ev, int timeout, out Exception? error)
         {
+            error = null;
             if (_responses.TryGetValue(id, out var response))
             {
                 _responses.Remove(id, out response);
+                _exceptions.Remove(id, out var _e);
+                _events.Remove(id, out ev);
+                return response;
+            }
+            if(_exceptions.TryGetValue(id, out error))
+            {
+                _exceptions.Remove(id, out error);
                 _events.Remove(id, out ev);
                 return response;
             }
@@ -149,6 +172,7 @@ namespace InABox.Rpc
             }
             
             _responses.Remove(id, out response);
+            _exceptions.Remove(id, out error);
             _events.Remove(id, out ev);
             return response ?? new RpcMessage() { Error = RpcError.UNKNOWN };
         }

+ 4 - 0
InABox.RPC.Shared/InABox.RPC.Shared.csproj

@@ -6,6 +6,10 @@
         <TargetFramework>netstandard2.1</TargetFramework>
     </PropertyGroup>
 
+    <ItemGroup>
+      <PackageReference Include="MessagePack.Annotations" Version="2.5.192" />
+    </ItemGroup>
+
     <ItemGroup>
       <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
     </ItemGroup>

+ 2 - 0
InABox.RPC.Shared/RPCMessage.cs

@@ -1,9 +1,11 @@
 using System;
 using InABox.Core;
+using MessagePack;
 
 namespace InABox.Rpc
 {
     [Serializable]
+    [MessagePackObject(keyAsPropertyName: true)]
     public class RpcMessage : ISerializeBinary
     {
         public Guid Id { get; set; }

+ 1 - 0
InABox.Server/InABox.Server.csproj

@@ -12,6 +12,7 @@
         <PackageReference Include="GenHTTP.Modules.Practices" Version="8.4.0" />
         <PackageReference Include="H.Formatters.BinaryFormatter" Version="2.0.59" />
         <PackageReference Include="H.Formatters.Ceras" Version="2.0.59" />
+        <PackageReference Include="H.Formatters.MessagePack" Version="2.0.59" />
         <PackageReference Include="H.Pipes" Version="2.0.59" />
         <PackageReference Include="H.Pipes.AccessControl" Version="2.0.59" />
     </ItemGroup>

+ 1 - 1
InABox.Server/RPC/Transports/Pipe/RPCServerPipeTransport.cs

@@ -31,7 +31,7 @@ namespace InABox.Rpc
 
         public RpcServerPipeTransport(string name)
         {
-            _transport = new PipeServer<RpcMessage?>(name, formatter:new BinaryFormatter());
+            _transport = new PipeServer<RpcMessage?>(name, formatter:new MessagePackFormatter());
 
             #if WINDOWS
             SetPipeSecurity();

+ 1 - 0
inabox.wpf/InABox.Wpf.csproj

@@ -142,6 +142,7 @@
         <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
         <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
         <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2592.51" />
+        <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
         <PackageReference Include="net.sf.mpxj-ikvm" Version="11.1.0" />
         <PackageReference Include="Nominatim.API" Version="2.1.0" />
         <PackageReference Include="RoslynPad.Editor.Windows" Version="4.8.0" />