CodeWF.NetWrapper
2.1.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CodeWF.NetWrapper --version 2.1.0
NuGet\Install-Package CodeWF.NetWrapper -Version 2.1.0
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="CodeWF.NetWrapper" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeWF.NetWrapper" Version="2.1.0" />
<PackageReference Include="CodeWF.NetWrapper" />
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 CodeWF.NetWrapper --version 2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CodeWF.NetWrapper, 2.1.0"
#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 CodeWF.NetWrapper@2.1.0
#: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=CodeWF.NetWrapper&version=2.1.0
#tool nuget:?package=CodeWF.NetWrapper&version=2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CodeWF.NetWeaver
CodeWF.NetWeaver 是一个简洁而强大的 C# 核心序列化库,支持 AOT,用于处理 TCP 和 UDP 数据包的组包和解包操作。
CodeWF.NetWeaver is a concise and powerful C# core serialization library that supports AOT for handling TCP and UDP packet grouping and unpacking operations.
框架组成
| 项目 | 说明 |
|---|---|
| CodeWF.NetWeaver | 核心序列化库,负责数据包的组包和解包(本文档) |
| CodeWF.NetWrapper | TCP/UDP Socket 封装库,基于 NetWeaver 实现高级网络通信功能 |
安装(Installer)
NuGet\Install-Package CodeWF.NetWeaver -Version 1.3.0
核心概念
数据包结构
┌────────────────────────────────────────────┐
│ Header (23 bytes) │
│ ├── BufferLen: int │
│ ├── SystemId: long │
│ ├── ObjectId: ushort │
│ ├── ObjectVersion: byte │
│ └── UnixTimeMs: long │
├────────────────────────────────────────────┤
│ Body (可变长度) │
└────────────────────────────────────────────┘
特性系统
| 特性 | 用途 |
|---|---|
NetHead(id, version) |
标记网络对象类型和版本 |
NetIgnoreMember |
序列化时忽略该成员 |
NetFieldOffset(offset, size) |
位字段打包 |
定义通信对象(Define Net Object)
[NetHead(10, 1)] // ObjectId = 10, Version = 1
public class ResponseProcessList : INetObject
{
public int TaskId { get; set; }
public int TotalSize { get; set; }
public List<ProcessItem>? Processes { get; set; }
}
public record ProcessItem
{
public int Pid { get; set; }
public string? Name { get; set; }
public byte Type { get; set; }
public uint LastUpdateTime { get; set; }
}
使用(Usage)
序列化
var netObject = new ResponseProcessList
{
TaskId = 3,
TotalSize = 200,
Processes = new List<ProcessItem>
{
new ProcessItem { Pid = 1, Name = "CodeWF.NetWeaver", Type = 1 }
}
};
// 序列化:对象 -> 字节数组
var buffer = netObject.Serialize(systemId: 32);
反序列化
// 反序列化:字节数组 -> 对象
var desObject = buffer.Deserialize<ResponseProcessList>();
Assert.Equal(netObject.TotalSize, desObject.TotalSize);
与 CodeWF.NetWrapper 的关系
// CodeWF.NetWeaver 只负责序列化和反序列化
var buffer = myObject.Serialize(systemId);
var obj = buffer.Deserialize<MyObject>();
// CodeWF.NetWrapper 提供完整的 TCP/UDP Socket 封装
var server = new TcpSocketServer();
await server.StartAsync("Server", "0.0.0.0", 8888);
// Socket 收到数据后自动序列化为对象
EventBus.Default.Subscribe<SocketCommand>(async (sender, cmd) =>
{
var myObject = cmd.GetCommand<MyObject>();
// 处理业务逻辑
});
详细设计原理请参阅:CodeWF-NetWeaver-Design-Principles.md
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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.
-
net10.0
- CodeWF.EventBus (>= 3.4.5.1)
- CodeWF.Log.Core (>= 11.3.14)
- CodeWF.NetWeaver (>= 2.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CodeWF.NetWrapper:
| Package | Downloads |
|---|---|
|
CodeWF.EventBus.Socket
Distributed event bus implemented using Socket, independent of third-party MQ. |
GitHub repositories
This package is not used by any popular GitHub repositories.