Skip to content

Commit

Permalink
使用OwnerPacket申请空间,简化数据包克隆
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 7, 2024
1 parent 1e73ee3 commit 66a4888
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 18 deletions.
5 changes: 1 addition & 4 deletions NewLife.Remoting/ApiNetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ protected override void OnReceive(ReceivedEventArgs e)
{
if (msg.Payload is ArrayPacket ap2 && ap.Buffer == ap2.Buffer)
{
var ap3 = new ArrayPacket(ap2.Length);
ap2.GetSpan().CopyTo(ap3.GetSpan());

msg.Payload = ap3;
msg.Payload = ap2.Clone();
}
}

Expand Down
2 changes: 1 addition & 1 deletion NewLife.Remoting/IEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public virtual IOwnerPacket Encode(String action, Int32? code, IPacket? value)
//ms.Seek(8, SeekOrigin.Begin);

var len = 8 + 1 + Encoding.UTF8.GetByteCount(action) + 4 + 4;
var pk = new ArrayPacket(len);
var pk = new OwnerPacket(len);

// 请求:action + args
// 响应:action + code + result
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Remoting/NewLife.Remoting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.0.2024.1001" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1006-beta1717" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.0.2024.1001" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1006-beta1717" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 10 additions & 10 deletions XUnitTest/JsonEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void EncodeRequest()
// 简洁请求
{
value = null;
var pk = (ArrayPacket)encoder.Encode(name, null, value);
var pk = (OwnerPacket)encoder.Encode(name, null, value);
Assert.Equal(1 + name.Length, pk.Total);
Assert.Null(pk.Next);
Assert.Equal(8, pk.Offset);
Expand All @@ -38,7 +38,7 @@ public void EncodeRequest()
// 简洁请求,带空数据
{
value = new Byte[0];
var pk = (ArrayPacket)encoder.Encode(name, null, value);
var pk = (OwnerPacket)encoder.Encode(name, null, value);
Assert.Equal(1 + name.Length + 4, pk.Total);
Assert.NotNull(pk.Next);
Assert.Equal(value, pk.Next);
Expand All @@ -52,15 +52,15 @@ public void EncodeRequest()
// 标准请求,带数据体
{
value = Rand.NextBytes(64);
var pk = (ArrayPacket)encoder.Encode(name, null, value);
var pk = (OwnerPacket)encoder.Encode(name, null, value);
Assert.Equal(1 + name.Length + 4 + value.Count, pk.Total);
Assert.NotNull(pk.Next);
Assert.Equal(value, pk.Next);
Assert.Equal(8, pk.Offset);

// 拷贝一次,拉平。因为SpanReader不支持跨包读取
pk = (ArrayPacket)pk.ToArray();
var reader = new SpanReader(pk.GetSpan());
var pk2 = (ArrayPacket)pk.ToArray();
var reader = new SpanReader(pk2.GetSpan());

Assert.Equal(name, reader.ReadString());

Expand All @@ -83,7 +83,7 @@ public void EncodeResponse()
{
// 错误码200等同于0,表示成功
value = null;
var pk = (ArrayPacket)encoder.Encode(name, 200, value);
var pk = (OwnerPacket)encoder.Encode(name, 200, value);
Assert.Equal(1 + name.Length, pk.Total);
Assert.Null(pk.Next);
Assert.Equal(8, pk.Offset);
Expand All @@ -96,7 +96,7 @@ public void EncodeResponse()
// 简洁响应,带异常
{
value = new Byte[0];
var pk = (ArrayPacket)encoder.Encode(name, 500, value);
var pk = (OwnerPacket)encoder.Encode(name, 500, value);
Assert.Equal(1 + name.Length + 4 + 4 + value.Count, pk.Total);
Assert.NotNull(pk.Next);
Assert.Equal(value, pk.Next);
Expand All @@ -111,15 +111,15 @@ public void EncodeResponse()
// 标准响应,带数据体
{
value = Rand.NextBytes(64);
var pk = (ArrayPacket)encoder.Encode(name, 0, value);
var pk = (OwnerPacket)encoder.Encode(name, 0, value);
Assert.Equal(1 + name.Length + 4 + value.Count, pk.Total);
Assert.NotNull(pk.Next);
Assert.Equal(value, pk.Next);
Assert.Equal(8, pk.Offset);

// 拷贝一次,拉平。因为SpanReader不支持跨包读取
pk = (ArrayPacket)pk.ToArray();
var reader = new SpanReader(pk.GetSpan());
var pk2 = (ArrayPacket)pk.ToArray();
var reader = new SpanReader(pk2.GetSpan());

Assert.Equal(name, reader.ReadString());
// 正常响应不需要错误码,直接写数据体长度
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest/XUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1001" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1006-beta1717" />
<PackageReference Include="NewLife.UnitTest" Version="1.0.2024.1006" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
Expand Down

0 comments on commit 66a4888

Please sign in to comment.