From 9283e3652701a2eb468729da2765c4f1ca3ea9dd Mon Sep 17 00:00:00 2001 From: lindexi Date: Thu, 9 Mar 2023 09:54:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E5=A4=B4=E8=80=8C=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/GeneratedProxyMemberInvokeModel.cs | 18 ++++++------ .../Models/GeneratedProxyMemberReturnModel.cs | 18 ++++++------ .../Utils/Extensions/IpcMessageExtension.cs | 29 +++++++++++++++++++ 3 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 src/dotnetCampus.Ipc/Utils/Extensions/IpcMessageExtension.cs diff --git a/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberInvokeModel.cs b/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberInvokeModel.cs index 890bb418..946a3689 100644 --- a/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberInvokeModel.cs +++ b/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberInvokeModel.cs @@ -8,6 +8,7 @@ using dotnetCampus.Ipc.Context; using dotnetCampus.Ipc.Messages; using dotnetCampus.Ipc.Serialization; +using dotnetCampus.Ipc.Utils.Extensions; namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies { @@ -86,20 +87,19 @@ public static IpcMessage Serialize(GeneratedProxyMemberInvokeModel model) /// public static bool TryDeserialize(IpcMessage message, [NotNullWhen(true)] out GeneratedProxyMemberInvokeModel? model) { - var header = BitConverter.ToUInt64(message.Body.Buffer, message.Body.Start); - if (header != (ulong) KnownMessageHeaders.RemoteObjectMessageHeader) + if (message.TryReadBusinessHeader(out var header) && header == (ulong) KnownMessageHeaders.RemoteObjectMessageHeader) + { + // 跳过业务头的消息内容 + var deserializeMessage = message.Skip(sizeof(ulong)); + + return JsonIpcMessageSerializer.TryDeserialize(deserializeMessage, out model); + } + else { // 如果业务头不对,那就不需要解析了 model = null; return false; } - - // 跳过业务头的消息内容 - var deserializeMessage = new IpcMessage(message.Tag, - new IpcMessageBody(message.Body.Buffer, message.Body.Start + sizeof(long), - message.Body.Length - sizeof(long))); - - return JsonIpcMessageSerializer.TryDeserialize(deserializeMessage, out model); } /// diff --git a/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberReturnModel.cs b/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberReturnModel.cs index bc5816c3..d2603a99 100644 --- a/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberReturnModel.cs +++ b/src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Models/GeneratedProxyMemberReturnModel.cs @@ -6,6 +6,7 @@ using dotnetCampus.Ipc.Context; using dotnetCampus.Ipc.Messages; using dotnetCampus.Ipc.Serialization; +using dotnetCampus.Ipc.Utils.Extensions; namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies { @@ -65,20 +66,19 @@ public static IpcMessage Serialize(GeneratedProxyMemberReturnModel model) public static bool TryDeserialize(IpcMessage message, [NotNullWhen(true)] out GeneratedProxyMemberReturnModel? model) { - var header = BitConverter.ToUInt64(message.Body.Buffer, message.Body.Start); - if (header != (ulong) KnownMessageHeaders.RemoteObjectMessageHeader) + if (message.TryReadBusinessHeader(out var header) && header == (ulong) KnownMessageHeaders.RemoteObjectMessageHeader) + { + // 跳过业务头的消息内容 + var deserializeMessage = message.Skip(sizeof(ulong)); + + return JsonIpcMessageSerializer.TryDeserialize(deserializeMessage, out model); + } + else { // 如果业务头不对,那就不需要解析了 model = null; return false; } - - // 跳过业务头的消息内容 - var deserializeMessage = new IpcMessage(message.Tag, - new IpcMessageBody(message.Body.Buffer, message.Body.Start + sizeof(long), - message.Body.Length - sizeof(long))); - - return JsonIpcMessageSerializer.TryDeserialize(deserializeMessage, out model); } } } diff --git a/src/dotnetCampus.Ipc/Utils/Extensions/IpcMessageExtension.cs b/src/dotnetCampus.Ipc/Utils/Extensions/IpcMessageExtension.cs new file mode 100644 index 00000000..248ef51b --- /dev/null +++ b/src/dotnetCampus.Ipc/Utils/Extensions/IpcMessageExtension.cs @@ -0,0 +1,29 @@ +using System; +using dotnetCampus.Ipc.Messages; + +namespace dotnetCampus.Ipc.Utils.Extensions; + +static class IpcMessageExtension +{ + public static IpcMessage Skip(this IpcMessage ipcMessage, int length) + { + return new IpcMessage(ipcMessage.Tag, new IpcMessageBody(ipcMessage.Body.Buffer, + ipcMessage.Body.Start + length, + ipcMessage.Body.Length - length)); + } + + public static bool TryReadBusinessHeader(this in IpcMessage ipcMessage, out ulong header) + { + var length = sizeof(ulong); + if (ipcMessage.Body.Length < length) + { + header = 0; + return false; + } + else + { + header = BitConverter.ToUInt64(ipcMessage.Body.Buffer, ipcMessage.Body.Start); + return true; + } + } +}