CodeWF.NetWeaver 2.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeWF.NetWeaver --version 2.1.0
                    
NuGet\Install-Package CodeWF.NetWeaver -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.NetWeaver" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeWF.NetWeaver" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CodeWF.NetWeaver" />
                    
Project file
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.NetWeaver --version 2.1.0
                    
#r "nuget: CodeWF.NetWeaver, 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.NetWeaver@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.NetWeaver&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CodeWF.NetWeaver&version=2.1.0
                    
Install as a Cake Tool

CodeWF.NetWeaver

NuGet NuGet License

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 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

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CodeWF.NetWeaver:

Package Downloads
CodeWF.NetWrapper

CodeWF.NetWrapper builds on CodeWF.NetWeaver and provides TCP/UDP helpers, command dispatching, and file transfer or file management capabilities.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.1 108 5/3/2026
2.1.0 125 4/17/2026
2.0.24 153 2/13/2026
2.0.23 144 1/23/2026
2.0.20 143 1/5/2026
2.0.14 217 12/25/2025
2.0.7 204 12/14/2025
2.0.6 208 12/13/2025
2.0.3 488 12/10/2025
2.0.2 470 12/10/2025
2.0.1 465 12/10/2025
2.0.0.1 260 11/4/2025
2.0.0 264 9/5/2025
1.3.1 288 5/8/2025
1.3.0 902 9/21/2024
1.2.1 184 9/19/2024
1.2.0 222 9/19/2024
1.1.1.1 210 9/13/2024
1.1.1 208 7/27/2024
1.1.0 213 7/9/2024
Loading failed