Browse Source

[fix] 修正例程错误,Packet全部改为使用IPacket。close: https://github.com/NewLifeX/NewLife.Net/issues/9#issuecomment-2728378297

master
智能大石头 4 months ago
parent
commit
8fd1e518f7
  1. 8
      Benchmark/Program.cs
  2. 7
      EchoTest/MyNetServer.cs
  3. 6
      HandlerTest/App.config
  4. 7
      HandlerTest/EchoHandler.cs
  5. 11
      HandlerTest/Program.cs
  6. 4
      HandlerTest/packages.config
  7. 10
      NewLife.Net/Application/EchoHandler.cs
  8. 11
      RpcTest/MyClient.cs
  9. 9
      RpcTest/MyController.cs
  10. 8
      RpcTest/Program.cs

8
Benchmark/Program.cs

@ -68,7 +68,7 @@ internal class Program
if (txt.IsNullOrEmpty()) txt = cfg.Content = "学无先后达者为师";
var buf = txt.StartsWith("0x") ? txt.TrimStart("0x").ToHex() : txt.GetBytes();
var pk = new Packet(buf);
var pk = new ArrayPacket(buf);
// 绑定集合
var binds = new List<(IPAddress, NetUri)>();
@ -104,7 +104,7 @@ internal class Program
Console.WriteLine("目标:{0}", uri);
Console.WriteLine("请求:{0:n0}", cfg.Times);
Console.WriteLine("并发:{0:n0}", cfg.ConcurrentLevel);
Console.WriteLine("内容:[{0:n0}] {1}", pk.Count, txt);
Console.WriteLine("内容:[{0:n0}] {1}", pk.Length, txt);
if (cfg.Interval > 0) Console.WriteLine("间隔:{0:n0}", cfg.Interval);
if (!cfg.Bind.IsNullOrEmpty())
@ -151,9 +151,9 @@ internal class Program
Console.WriteLine("速度:{0:n0}tps", total * 1000L / ms);
}
private static readonly ConcurrentHashSet<Type> _LastErrors = new ConcurrentHashSet<Type>();
private static readonly ConcurrentHashSet<Type> _LastErrors = new();
private static async Task<Int32> WorkOneAsync(IPEndPoint local, NetUri uri, Config cfg, Packet pk)
private static async Task<Int32> WorkOneAsync(IPEndPoint local, NetUri uri, Config cfg, IPacket pk)
{
var count = 0;
try

7
EchoTest/MyNetServer.cs

@ -1,5 +1,6 @@
using System;
using NewLife;
using NewLife.Data;
using NewLife.Net;
namespace EchoTest
@ -22,13 +23,13 @@ namespace EchoTest
}
/// <summary>客户端断开连接</summary>
protected override void OnDisconnected()
protected override void OnDisconnected(String reason)
{
#if DEBUG
WriteLog("客户端{0}已经断开连接啦", Remote);
WriteLog("客户端{0}已经断开连接啦,原因:{1}", Remote, reason);
#endif
base.OnDisconnected();
base.OnDisconnected(reason);
}
/// <summary>收到客户端数据</summary>

6
HandlerTest/App.config

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

7
HandlerTest/EchoHandler.cs

@ -4,8 +4,8 @@ using NewLife.Log;
using NewLife.Model;
using NewLife.Net;
namespace HandlerTest
{
namespace HandlerTest;
class EchoHandler : Handler
{
/// <summary>性能计数器</summary>
@ -19,7 +19,7 @@ namespace HandlerTest
// 性能计数
Counter?.Increment(1, 0);
var pk = message as Packet;
var pk = message as IPacket;
#if DEBUG
session.WriteLog("收到:{0}", pk.ToStr());
#endif
@ -30,4 +30,3 @@ namespace HandlerTest
return null;
}
}
}

11
HandlerTest/Program.cs

@ -6,8 +6,8 @@ using NewLife.Net;
using NewLife.Net.Handlers;
using NewLife.Threading;
namespace HandlerTest
{
namespace HandlerTest;
class Program
{
static void Main(String[] args)
@ -74,7 +74,7 @@ namespace HandlerTest
client.LogReceive = true;
client.Received += (s, e) =>
{
var pk = e.Message as Packet;
var pk = e.Message as IPacket;
XTrace.WriteLine("收到:{0}", pk.ToStr());
};
//client.Add(new LengthFieldCodec { Size = 4 });
@ -89,8 +89,8 @@ namespace HandlerTest
for (var i = 0; i < 5; i++)
{
var str = "你好" + (i + 1);
var pk = new Packet(str.GetBytes());
client.SendMessageAsync(pk);
//var pk = new Packet(str.GetBytes());
client.SendMessageAsync(str);
}
}
@ -116,4 +116,3 @@ namespace HandlerTest
XTrace.WriteLine(msg);
}
}
}

4
HandlerTest/packages.config

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NewLife.Core" version="7.3.6755.22250" targetFramework="net461" />
</packages>

10
NewLife.Net/Application/EchoHandler.cs

@ -1,10 +1,9 @@
using System;
using NewLife.Data;
using NewLife.Data;
using NewLife.Log;
using NewLife.Model;
namespace NewLife.Net.Application
{
namespace NewLife.Net.Application;
/// <summary>回声处理器</summary>
public class EchoHandler : Handler
{
@ -17,7 +16,7 @@ namespace NewLife.Net.Application
var ctx = context as NetHandlerContext;
var session = ctx.Session;
if (message is Packet pk)
if (message is IPacket pk)
{
var len = pk.Total;
if (len > 100)
@ -33,4 +32,3 @@ namespace NewLife.Net.Application
return null;
}
}
}

11
RpcTest/MyClient.cs

@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using NewLife.Data;
using NewLife.Remoting;
namespace RpcTest
{
namespace RpcTest;
/// <summary>自定义业务客户端</summary>
class MyClient : ApiClient
{
@ -24,9 +22,9 @@ namespace RpcTest
/// <summary>RC4加解密,高速业务服务,二进制收发不经序列化</summary>
/// <param name="pk"></param>
/// <returns></returns>
public async Task<Packet> RC4Async(Packet pk)
public async Task<IPacket> RC4Async(IPacket pk)
{
return await InvokeAsync<Packet>("My/RC4", pk);
return await InvokeAsync<IPacket>("My/RC4", pk);
}
public async Task<User> FindUserAsync(Int32 uid, Boolean enable)
@ -34,4 +32,3 @@ namespace RpcTest
return await InvokeAsync<User>("User/FindByID", new { uid, enable });
}
}
}

9
RpcTest/MyController.cs

@ -2,8 +2,8 @@
using NewLife;
using NewLife.Data;
namespace RpcTest
{
namespace RpcTest;
/// <summary>自定义控制器。包含多个服务</summary>
class MyController
{
@ -16,12 +16,11 @@ namespace RpcTest
/// <summary>RC4加解密,高速业务服务,二进制收发不经序列化</summary>
/// <param name="pk"></param>
/// <returns></returns>
public Packet RC4(Packet pk)
public IPacket RC4(IPacket pk)
{
var data = pk.ToArray();
var pass = "NewLife".GetBytes();
return data.RC4(pass);
}
return (ArrayPacket)data.RC4(pass);
}
}

8
RpcTest/Program.cs

@ -3,12 +3,11 @@ using NewLife;
using NewLife.Data;
using NewLife.Log;
using NewLife.Net;
using NewLife.Net.Handlers;
using NewLife.Remoting;
using NewLife.Threading;
namespace RpcTest
{
namespace RpcTest;
class Program
{
static void Main(String[] args)
@ -93,7 +92,7 @@ namespace RpcTest
// 高速服务,二进制
var buf = "Hello".GetBytes();
var pk = await client.RC4Async(buf);
var pk = await client.RC4Async((ArrayPacket)buf);
XTrace.WriteLine("RC4: {0}", pk.ToHex());
// 返回对象
@ -122,4 +121,3 @@ namespace RpcTest
Console.Title = msg;
}
}
}
Loading…
Cancel
Save