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; + } + } +}