CodeWF.NetWrapper 2.1.0

There is a newer version of this package available.
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" />
                    
Directory.Packages.props
<PackageReference Include="CodeWF.NetWrapper" />
                    
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.NetWrapper --version 2.1.0
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=CodeWF.NetWrapper&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.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CodeWF.NetWrapper:

Package Downloads
CodeWF.EventBus.Socket

基于 Socket 实现的分布式事件总线,不依赖第三方 MQ。

CodeWF.NetWrapper.FileSystem

File system management and file transfer extension package for CodeWF.NetWrapper.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.9 37 7/30/2026
3.0.8 101 7/27/2026
3.0.7 97 7/24/2026
3.0.6 108 7/23/2026
3.0.5 128 7/23/2026
3.0.4 136 7/14/2026
3.0.3 127 6/28/2026
3.0.1 123 6/28/2026
3.0.0 147 6/24/2026
2.1.2.10 123 6/24/2026
2.1.2.8 101 6/23/2026
2.1.2.7 103 6/22/2026
2.1.2.6 106 6/22/2026
2.1.2.4 103 6/22/2026
2.1.2.3 127 6/8/2026
2.1.2.1 122 6/5/2026
2.1.1 125 5/3/2026
2.1.0 125 4/17/2026
2.0.24 137 2/13/2026
2.0.23 128 1/23/2026
Loading failed