Procházet zdrojové kódy

Implemented MemoryPack for Rpc and IPC transports

frogsoftware před 1 dnem
rodič
revize
99ff5132cd

+ 2 - 2
InABox.Client.RPC/InABox.Client.RPC.csproj

@@ -3,16 +3,16 @@
     <PropertyGroup>
         <OutputType>Library</OutputType>
         <Nullable>enable</Nullable>
-        <TargetFramework>netstandard2.1</TargetFramework>
+        <TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
     </PropertyGroup>
 
     <ItemGroup>
+      <ProjectReference Include="..\InABox.Formatters.MemoryPack\InABox.Formatters.MemoryPack.csproj" />
       <ProjectReference Include="..\InABox.RPC.Shared\InABox.RPC.Shared.csproj" />
     </ItemGroup>
 
     <ItemGroup>
       <PackageReference Include="H.Formatters" Version="15.0.0" />
-      <PackageReference Include="H.Formatters.BinaryFormatter" Version="15.0.0" />
       <PackageReference Include="H.Formatters.MessagePack" Version="15.0.0" />
       <PackageReference Include="H.Pipes" Version="15.0.0" />
       <PackageReference Include="WebSocket4Net" Version="0.15.2" />

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

@@ -3,6 +3,7 @@ using System.Threading;
 using H.Formatters;
 using H.Pipes;
 using InABox.Core;
+using InABox.Formatters;
 
 namespace InABox.Rpc
 {
@@ -14,7 +15,7 @@ namespace InABox.Rpc
         public RpcClientPipeTransport(string name)
         {
             _name = name;
-            _pipe = new PipeClient<RpcMessage>(_name, formatter: new MessagePackFormatter<RpcMessage>());
+            _pipe = new PipeClient<RpcMessage>(_name, new MemoryPackFormatter<RpcMessage>());
             _pipe.Connected += PipeConnected;
             _pipe.Disconnected += PipeDisconnected;
             _pipe.MessageReceived += PipeMessageReceived;

+ 13 - 0
InABox.Formatters.MemoryPack/InABox.Formatters.MemoryPack.csproj

@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <Nullable>enable</Nullable>
+        <TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
+    </PropertyGroup>
+
+    <ItemGroup>
+      <PackageReference Include="H.Formatters" Version="15.0.0" />
+      <PackageReference Include="MemoryPack" Version="1.21.4" />
+    </ItemGroup>
+
+</Project>

+ 19 - 0
InABox.Formatters.MemoryPack/MemoryPackFormatter.cs

@@ -0,0 +1,19 @@
+using H.Formatters;
+using MemoryPack;
+
+namespace InABox.Formatters
+{
+
+    public class MemoryPackFormatter<T1> : FormatterBase
+    {
+        protected override byte[] SerializeInternal(object obj)
+        {
+            return MemoryPackSerializer.Serialize<T1>((T1)obj);
+        }
+
+        protected override T DeserializeInternal<T>(byte[] bytes)
+        {
+            return MemoryPackSerializer.Deserialize<T>(bytes);
+        }
+    }
+}

+ 0 - 1
InABox.Integration.Logikal/InABox.Integration.Logikal.csproj

@@ -8,7 +8,6 @@
     </PropertyGroup>
 
     <ItemGroup>
-      <PackageReference Include="H.Formatters.Newtonsoft.Json" Version="15.0.0" />
       <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
     </ItemGroup>
 

+ 1 - 1
InABox.Logging/InABox.Logging.csproj

@@ -21,12 +21,12 @@
     </ItemGroup>
 
     <ItemGroup>
-        <PackageReference Include="H.Formatters.Ceras" Version="15.0.0" />
         <PackageReference Include="H.Formatters.MessagePack" Version="15.0.0" />
         <PackageReference Include="H.Pipes" Version="15.0.0" />
     </ItemGroup>
 
     <ItemGroup>
+        <ProjectReference Include="..\InABox.Formatters.MemoryPack\InABox.Formatters.MemoryPack.csproj" />
         <ProjectReference Include="..\inabox.logging.shared\InABox.Logging.Shared.csproj" />
     </ItemGroup>
 

+ 2 - 2
InABox.Logging/NamedPipeLogger.cs

@@ -4,6 +4,7 @@ using System.Text;
 using H.Formatters;
 using H.Pipes;
 using InABox.Core;
+using InABox.Formatters;
 
 namespace InABox.Logging
 {
@@ -165,8 +166,7 @@ namespace InABox.Logging
         public NamedPipeLogger(string name = "")
         {
             _name = string.IsNullOrWhiteSpace(name) ? Process.GetCurrentProcess().ProcessName : name;
-
-            _pipe = new PipeServer<string>(_name, new MessagePackFormatter());
+            _pipe = new PipeServer<string>(_name, new MemoryPackFormatter<string>());
             _pipe.ClientConnected += _pipe_ClientConnected;
             _pipe.StartAsync();
         }

+ 2 - 1
InABox.RPC.Shared/InABox.RPC.Shared.csproj

@@ -3,10 +3,11 @@
     <PropertyGroup>
         <OutputType>Library</OutputType>
         <Nullable>enable</Nullable>
-        <TargetFramework>netstandard2.1</TargetFramework>
+        <TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
     </PropertyGroup>
 
     <ItemGroup>
+      <PackageReference Include="MemoryPack" Version="1.21.4" />
       <PackageReference Include="MessagePack.Annotations" Version="2.5.192" />
     </ItemGroup>
 

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

@@ -1,12 +1,14 @@
 using System;
 using InABox.Core;
 using MessagePack;
+using MemoryPack;
 
 namespace InABox.Rpc
 {
     [Serializable]
     [MessagePackObject(keyAsPropertyName: true)]
-    public class RpcMessage : ISerializeBinary
+    [MemoryPackable]
+    public partial class RpcMessage : ISerializeBinary
     {
         public Guid Id { get; set; }
         public String Command { get; set; }
@@ -15,6 +17,7 @@ namespace InABox.Rpc
 
         public override string ToString() => $"{Command} [{Error}]";
 
+        [MemoryPackConstructor]
         public RpcMessage()
         {
             Id = Guid.NewGuid();
@@ -22,8 +25,10 @@ namespace InABox.Rpc
             Payload = Array.Empty<byte>();
             Error = RpcError.NONE;
         }
+        
+        
 
-        public RpcMessage(Guid id, string command, byte[] payload)
+        public RpcMessage(Guid id, string command, byte[] payload) : this()
         {
             Id = id;
             Command = command;

+ 3 - 1
InABox.Server/IPC/IPCServer.cs

@@ -9,6 +9,7 @@ using System.IO.Pipes;
 using System.Reflection;
 using System.Security.Principal;
 using H.Formatters;
+using InABox.Formatters;
 
 namespace InABox.IPC
 {
@@ -20,8 +21,9 @@ namespace InABox.IPC
 
         public IPCServer(string name)
         {
-            Server = new PipeServer<IPCMessage>(name, new MessagePackFormatter<IPCMessage>());
 
+            Server = new PipeServer<IPCMessage>(name, new MemoryPackFormatter<IPCMessage>());
+            
             #if WINDOWS
             SetPipeSecurity();
             #endif

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

@@ -10,8 +10,6 @@
         <PackageReference Include="Fleck" Version="1.2.0" />
         <PackageReference Include="GenHTTP.Core" Version="8.4.1" />
         <PackageReference Include="GenHTTP.Modules.Practices" Version="8.4.0" />
-        <PackageReference Include="H.Formatters.BinaryFormatter" Version="15.0.0" />
-        <PackageReference Include="H.Formatters.Ceras" Version="15.0.0" />
         <PackageReference Include="H.Formatters.MessagePack" Version="15.0.0" />
         <PackageReference Include="H.Pipes" Version="15.0.0" />
         <PackageReference Include="H.Pipes.AccessControl" Version="15.0.0" />
@@ -19,6 +17,7 @@
 
     <ItemGroup>
         <ProjectReference Include="..\InABox.Database\InABox.Database.csproj" />
+        <ProjectReference Include="..\InABox.Formatters.MemoryPack\InABox.Formatters.MemoryPack.csproj" />
         <ProjectReference Include="..\InABox.IPC.Shared\InABox.IPC.Shared.csproj" />
         <ProjectReference Include="..\InABox.Logging.Shared\InABox.Logging.Shared.csproj" />
         <ProjectReference Include="..\InABox.Mailer.Exchange\InABox.Mailer.Exchange.csproj" />

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

@@ -3,6 +3,7 @@ using System.Security.Principal;
 using H.Formatters;
 using H.Pipes;
 using H.Pipes.AccessControl;
+using InABox.Formatters;
 
 namespace InABox.Rpc
 {
@@ -31,7 +32,9 @@ namespace InABox.Rpc
 
         public RpcServerPipeTransport(string name)
         {
-            _transport = new PipeServer<RpcMessage?>(name, new MessagePackFormatter<RpcMessage>());
+
+            _transport = new PipeServer<RpcMessage?>(name, new MemoryPackFormatter<RpcMessage>());
+
 
             #if WINDOWS
             SetPipeSecurity();

+ 3 - 0
InABox.Server/RPC/Transports/Socket/RPCServerSocketTransport.cs

@@ -26,6 +26,8 @@ namespace InABox.Rpc
             var protocol = certificate != null ? "wss" : "ws";
 
             _server = new WebSocketServer($"{protocol}://0.0.0.0:{port}");
+            _server.ListenerSocket.NoDelay = true;
+            //_server.RestartAfterListenError = true;
 
             if (Certificate != null)
             {
@@ -54,6 +56,7 @@ namespace InABox.Rpc
             _server?.Start(socket =>
             {
                 var connection = new RpcServerSocketConnection2(socket);
+                
                 socket.OnOpen = () => ConnectionOpened(connection);
                 socket.OnClose = () => ConnectionClosed(connection);
                 socket.OnError = (e) => ConnectionException(connection, e);

+ 2 - 1
inabox.client.ipc/IPCClientTransport.cs

@@ -3,6 +3,7 @@ using InABox.Core;
 using InABox.IPC;
 using System.Collections.Concurrent;
 using H.Formatters;
+using InABox.Formatters;
 
 namespace InABox.Client.IPC
 {
@@ -31,7 +32,7 @@ namespace InABox.Client.IPC
 
         public IPCClientTransport(string pipeName)
         {
-            Client = new PipeClient<IPCMessage>(pipeName, new MessagePackFormatter<IPCMessage>());
+            Client = new PipeClient<IPCMessage>(pipeName, new MemoryPackFormatter<IPCMessage>());
             Client.Connected += Client_Connected;
             Client.Disconnected += Client_Disconnected;
             Client.MessageReceived += Client_MessageReceived;

+ 1 - 1
inabox.client.ipc/InABox.Client.IPC.csproj

@@ -7,13 +7,13 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="H.Formatters.BinaryFormatter" Version="15.0.0" />
     <PackageReference Include="H.Formatters.MessagePack" Version="15.0.0" />
     <PackageReference Include="H.Pipes" Version="15.0.0" />
     <PackageReference Include="System.Collections.Immutable" Version="9.0.6" />
   </ItemGroup>
 
   <ItemGroup>
+    <ProjectReference Include="..\InABox.Formatters.MemoryPack\InABox.Formatters.MemoryPack.csproj" />
     <ProjectReference Include="..\InABox.IPC.Shared\InABox.IPC.Shared.csproj" />
   </ItemGroup>
 

+ 11 - 3
inabox.ipc.shared/IPCMessage.cs

@@ -5,6 +5,7 @@ using System.Linq;
 using System.Net.NetworkInformation;
 using System.Text;
 using System.Threading.Tasks;
+using MemoryPack;
 
 namespace InABox.IPC
 {
@@ -36,7 +37,8 @@ namespace InABox.IPC
     }
 
     [Serializable]
-    public class IPCMessage
+    [MemoryPackable]
+    public partial class IPCMessage
     {
         public Guid RequestID;
         public Method Method;
@@ -47,7 +49,13 @@ namespace InABox.IPC
         [NonSerialized]
         public RequestError ErrorCode;
 
-        private IPCMessage(Guid requestID, Method method, string? type, string data, RequestError error = RequestError.NONE)
+        [MemoryPackConstructor]
+        public IPCMessage()
+        {
+            
+        }
+        
+        private IPCMessage(Guid requestID, Method method, string? type, string data, RequestError error = RequestError.NONE) : this()
         {
             RequestID = requestID;
             Method = method;
@@ -56,7 +64,7 @@ namespace InABox.IPC
             BinaryData = null;
             ErrorCode = error;
         }
-        private IPCMessage(Guid requestID, Method method, string? type, byte[] data, RequestError error = RequestError.NONE)
+        private IPCMessage(Guid requestID, Method method, string? type, byte[] data, RequestError error = RequestError.NONE) : this()
         {
             RequestID = requestID;
             Method = method;

+ 1 - 0
inabox.ipc.shared/InABox.IPC.Shared.csproj

@@ -12,6 +12,7 @@
 
   <ItemGroup>
     <PackageReference Include="H.Pipes" Version="15.0.0" />
+    <PackageReference Include="MemoryPack" Version="1.21.4" />
     <PackageReference Include="System.Collections.Immutable" Version="9.0.6" />
   </ItemGroup>