Skip to content

Commit

Permalink
修复不存在业务头而失败
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Mar 9, 2023
1 parent ce30b77 commit 9283e36
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -86,20 +87,19 @@ public static IpcMessage Serialize(GeneratedProxyMemberInvokeModel model)
/// <returns></returns>
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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
}
29 changes: 29 additions & 0 deletions src/dotnetCampus.Ipc/Utils/Extensions/IpcMessageExtension.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 9283e36

Please sign in to comment.