Skip to content

Commit

Permalink
简化IPacket使用
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 12, 2024
1 parent 288b13c commit eb16d39
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion NewLife.Core/Net/Handlers/MessageCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override Boolean Close(IHandlerContext context, String reason)
//!!! 处理结果的Packet需要拷贝一份,否则交给另一个线程使用会有冲突
// Match里面TrySetResult时,必然唤醒原来阻塞的Task,如果不是当前io线程执行后续代码,必然导致两个线程共用了数据区,因此需要拷贝
if (rs is IMessage msg4 && msg4.Payload != null && msg4.Payload == msg3.Payload)
msg4.Payload = (ArrayPacket)msg4.Payload.GetSpan().ToArray();
msg4.Payload = msg4.Payload.Clone();

Queue?.Match(context.Owner, msg, rs ?? msg, IsMatch);
}
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Core/Net/ReceivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public class ReceivedEventArgs : EventArgs, IData

/// <summary>获取当前事件的原始数据。避免用户错误使用Packet.Data</summary>
/// <returns></returns>
public Byte[]? GetBytes() => Packet?.GetSpan().ToArray();
public Byte[]? GetBytes() => Packet?.ToArray();
}
2 changes: 1 addition & 1 deletion NewLife.Core/Remoting/ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static HttpRequestMessage BuildRequest(HttpMethod method, String action,
{
var url = action;
url += url.Contains('?') ? "&" : "?";
url += pk.GetSpan().ToArray().ToUrlBase64();
url += pk.ToArray().ToUrlBase64();
request.RequestUri = new Uri(url, UriKind.RelativeOrAbsolute);
}
else if (args is Byte[] buf)
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Core/Serialization/Json/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ UInt32 or UInt64
WriteStringFast(Convert.ToBase64String(buf, 0, buf.Length, Base64FormattingOptions.None));
}
else if (obj is IPacket pk)
WriteStringFast(ByteArrayAsHex ? pk.ToHex(-1) : pk.GetSpan().ToArray().ToBase64());
WriteStringFast(ByteArrayAsHex ? pk.ToHex(-1) : pk.ToArray().ToBase64());
else if (obj is StringDictionary dictionary2)
WriteSD(dictionary2);

Expand Down
6 changes: 3 additions & 3 deletions XUnitTest.Core/Serialization/BinaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void Fast()
var pk = Binary.FastWrite(model);
Assert.Equal(8, pk.Length);
Assert.Equal("D2090553746F6E65", pk.ToHex());
Assert.Equal("0gkFU3RvbmU=", pk.GetSpan().ToArray().ToBase64());
Assert.Equal("0gkFU3RvbmU=", pk.ToArray().ToBase64());

var model2 = Binary.FastRead<MyModel>(pk.GetStream());
Assert.Equal(model.Code, model2.Code);
Expand All @@ -177,7 +177,7 @@ public void Accessor()
var pk = Binary.FastWrite(model);
Assert.Equal(10, pk.Length);
Assert.Equal("D20400000553746F6E65", pk.ToHex());
Assert.Equal("0gQAAAVTdG9uZQ==", pk.GetSpan().ToArray().ToBase64());
Assert.Equal("0gQAAAVTdG9uZQ==", pk.ToArray().ToBase64());

var model2 = Binary.FastRead<MyModelWithAccessor>(pk.GetStream());
Assert.Equal(model.Code, model2.Code);
Expand Down Expand Up @@ -220,7 +220,7 @@ public void MemberAccessor()
var pk = Binary.FastWrite(model);
Assert.Equal(8, pk.Length);
Assert.Equal("D2090553746F6E65", pk.ToHex());
Assert.Equal("0gkFU3RvbmU=", pk.GetSpan().ToArray().ToBase64());
Assert.Equal("0gkFU3RvbmU=", pk.ToArray().ToBase64());

var model2 = Binary.FastRead<MyModelWithMemberAccessor>(pk.GetStream());
Assert.Equal(model.Code, model2.Code);
Expand Down

0 comments on commit eb16d39

Please sign in to comment.