EasyTcp4Net 1.0.0.1
dotnet add package EasyTcp4Net --version 1.0.0.1
NuGet\Install-Package EasyTcp4Net -Version 1.0.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyTcp4Net" Version="1.0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyTcp4Net" Version="1.0.0.1" />
<PackageReference Include="EasyTcp4Net" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasyTcp4Net --version 1.0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasyTcp4Net, 1.0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EasyTcp4Net@1.0.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EasyTcp4Net&version=1.0.0.1
#tool nuget:?package=EasyTcp4Net&version=1.0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
这是一个基于c# Pipe,ReadonlySequence的高性能Tcp通信库,旨在提供稳定,高效,可靠的tcp通讯服务。
- 基础的消息通讯
- 重试机制
- 超时机制
- SSL加密通信支持
- KeepAlive
- 流量背压控制
- 粘包和断包处理 (支持固定头处理,固定长度处理,固定字符处理)
- 日志支持
Pipe & ReadOnlySequence
为什么选择 Pipe & ReadOnlySequence
TCP 是一个流式面向连接的传输协议,所以源源不断地处理数据,并且在合适的地方进行数据分包,才是我们所关心的。Pipe本身是流水线一样的处理管道,我们只需要把我们收到的数据源源不断地扔到管道里,管道的消费端会帮我们进行数据处理
ReadOnlySequence 是多组数据的链表结构,更加符合了Tcp的流式传输的特征,并且它强大的多组数据切割能力,可以让我们非常方便的在多数据包中获取正确的数据。
Link:
https://learn.microsoft.com/zh-cn/dotnet/api/system.buffers.readonlysequence-1?view=net-7.0 https://learn.microsoft.com/zh-cn/dotnet/api/system.io.pipes?view=net-8.0
客户端配置(EasyTcpClientOptions)
| Key | Description |
|---|---|
| NoDelay | 是否不使用 Nagle's算法 避免了过多的小报文的过大TCP头所浪费的带宽 |
| BufferSize | 流数据缓冲区大小 |
| ConnectTimeout | 连接超时时间 |
| ConnectRetryTimes | 连接失败尝试次数 |
| ReadTimeout | 从socket缓冲区读取数据的超时时间 |
| WriteTimeout | 向socket缓冲区写入数据的超时时间 |
| IsSsl | 是否使用ssl连接 |
| PfxCertFilename | ssl证书 |
| PfxPassword | ssl证书密钥 |
| AllowingUntrustedSSLCertificate | 是否允许不受信任的ssl证书 |
| LoggerFactory | 日志工厂 |
| KeepAlive | 是否启动操作系统的tcp keepalive机制 |
| KeepAliveTime | KeepAlive的空闲时长,或者说每次正常发送心跳的周期,默认值为3600s(1小时) |
| KeepAliveProbes | KeepAlive之后设置最大允许发送保活探测包的次数,到达此次数后直接放弃尝试,并关闭连接 |
| KeepAliveIntvl | 没有接收到对方确认,继续发送KeepAlive的发送频率,默认值为60s |
| MaxPipeBufferSize | 待处理数据队列最大缓存,如果有粘包断包的过滤器,要大于单个包的大小,防止卡死 |
服务端配置(EasyTcpServerOptions)
| Key | Description |
|---|---|
| NoDelay | 是否不使用 Nagle's算法 避免了过多的小报文的过大TCP头所浪费的带宽 |
| BufferSize | 流数据缓冲区大小 |
| ConnectionsLimit | 最大连接数 |
| BacklogCount | 连接等待/挂起连接数量 |
| IsSsl | 是否使用ssl连接 |
| PfxCertFilename | ssl证书 |
| PfxPassword | ssl证书密钥 |
| AllowingUntrustedSSLCertificate | 是否允许不受信任的ssl证书 |
| MutuallyAuthenticate | 是否双向的ssl验证,标识了客户端是否需要提供证书 |
| CheckCertificateRevocation | 是否检查整数的吊销列表 |
| LoggerFactory | 日志工厂 |
| IdleSessionsCheck | 是否开启空闲连接检查 |
| CheckSessionsIdleMs | 空闲连接检查时间阈值 |
| KeepAlive | 是否启动操作系统的tcp keepalive机制 |
| KeepAliveTime | KeepAlive的空闲时长,或者说每次正常发送心跳的周期,默认值为3600s(1小时) |
| KeepAliveProbes | KeepAlive之后设置最大允许发送保活探测包的次数,到达此次数后直接放弃尝试,并关闭连接 |
| KeepAliveIntvl | 没有接收到对方确认,继续发送KeepAlive的发送频率,默认值为60s |
| MaxPipeBufferSize | 待处理数据队列最大缓存,如果有粘包断包的过滤器,要大于单个包的大小,防止卡死 |
开启一个Server
EasyTcpServer _server = new EasyTcpServer(_serverPort);
_server.StartListen();
开启一个Server并且配置SSL证书
EasyTcpServer easyTcpServer = new EasyTcpServer(7001, new EasyTcpServerOptions()
{
IsSsl = true,
PfxCertFilename = "xxx",
PfxPassword = "xxx",
IdleSessionsCheck = false,
KeepAlive = true,
CheckSessionsIdleMs = 10 * 1000
});
开启一个Server,配置数据过滤器处理粘包断包
固定头处理
//参数分别为:数据包头长度,数据包体长度所在头下标,数据包体长度字节数,是否小端在前
easyTcpServer.SetReceiveFilter(new FixedHeaderPackageFilter(7, 5, 4, true));
固定长度处理
//参数分别为:数据包固定长度
easyTcpServer.SetReceiveFilter(new FixedLengthPackageFilter(50));
固定字符处理(不推荐)
//参数分别为:截取的字符
easyTcpServer.SetReceiveFilter(new FixedCharPackageFilter('\n'));
客户端收到数据的回调
easyTcpClient.OnReceivedData += (obj, e) =>
{
Console.WriteLine(string.Join(',', e.Data));
Console.WriteLine("\n");
};
服务端收到数据的回调
easyTcpServer.OnReceivedData += async (obj, e) =>
{
Console.WriteLine($"数据来自:{e.Session.RemoteEndPoint}");
Console.WriteLine(string.Join(',', e.Data));
};
日志配置
var _server = new EasyTcpServer(_serverPort, new EasyTcpServerOptions()
{
ConnectionsLimit = 2,
LoggerFactory = LoggerFactory.Create(options =>
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()//最小的记录等级
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)//对其他日志进行重写,除此之外,目前框架只有微软自带的日志组件
.WriteTo.Console()//输出到控制台
.CreateLogger();
options.AddSerilog();
})
});
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- CommunityToolkit.HighPerformance (>= 8.2.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- System.IO.Pipelines (>= 8.0.0)
-
net7.0
- CommunityToolkit.HighPerformance (>= 8.2.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- System.IO.Pipelines (>= 8.0.0)
-
net8.0
- CommunityToolkit.HighPerformance (>= 8.2.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- System.IO.Pipelines (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0.1 | 263 | 4/28/2024 |