Galosys.Foundation.DotNetty 26.9.14.1

dotnet add package Galosys.Foundation.DotNetty --version 26.9.14.1
                    
NuGet\Install-Package Galosys.Foundation.DotNetty -Version 26.9.14.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="Galosys.Foundation.DotNetty" Version="26.9.14.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.DotNetty" Version="26.9.14.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.DotNetty" />
                    
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 Galosys.Foundation.DotNetty --version 26.9.14.1
                    
#r "nuget: Galosys.Foundation.DotNetty, 26.9.14.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 Galosys.Foundation.DotNetty@26.9.14.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=Galosys.Foundation.DotNetty&version=26.9.14.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.DotNetty&version=26.9.14.1
                    
Install as a Cake Tool

Galosys.Foundation.DotNetty

成熟度: 🔴 实验性 — 依赖 preview 版本,API 可能变更

⚠️ 本模块依赖 preview/不稳定版本包,API 可能随时变更。

基于 DotNetty 的高性能 TCP RPC 服务器模块,提供低延迟的网络通信能力。

简介

DotNetty 是 Netty 的 .NET 移植版本,是一个高性能的异步事件驱动网络编程框架。Galosys.Foundation.DotNetty 模块封装了 DotNetty 的核心功能,提供简洁的 API 用于快速构建 TCP 服务器,支持自定义编解码器、会话管理和消息路由等特性。

特性

  • 高性能 TCP 服务器:基于 NIO/Libuv 两种模式
  • 自定义消息协议:内置 Packet 编解码,支持请求/响应类型
  • 会话管理:SessionManager 维护客户端连接状态
  • 灵活的编解码器:支持自定义 IPacketCodec 实现
  • 多线程事件循环:充分利用多核处理器
  • 服务标记:通过 NettyServiceAttribute 标记服务接口

安装

dotnet add package Galosys.Foundation.DotNetty

配置

appsettings.json 中配置 DotNetty 服务器端口:

{
  "DotNetty": {
    "Port": 3000
  }
}

使用示例

定义服务接口

[NettyService("MyService")]
public interface IMyService
{
    Task<string> SayHelloAsync(string name);
}

实现 TCP 服务器

using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels.Sockets;

public class MyNettyServer : NettyServer
{
    protected override void AddHandlers(ISocketChannel channel)
    {
        channel.Pipeline
            .AddLast(new PacketDecoder())
            .AddLast(new PacketEncoder())
            .AddLast(new SessionManager())
            .AddLast(new MyServerHandler());
    }
}

服务注册与启动

// 注册 Netty 服务器
services.AddNettyServer<MyNettyServer>();

// 在后台服务中启动
public class MyHostService : BackgroundService
{
    private readonly INettyServer _server;

    public MyHostService(INettyServer server)
    {
        _server = server;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await _server.StartAsync();
    }
}

使用消息包

// 构建请求包
var request = Packet.Builder()
    .SessionId(sessionId)
    .Type(0) // 0 = 请求
    .Level(1)
    .Body(new { Name = "Alice" })
    .Build();

// 处理响应包
var response = Packet.Builder()
    .SessionId(sessionId)
    .Type(1) // 1 = 响应
    .Level(1)
    .Body(new { Message = "Hello, Alice!" })
    .Build();

自定义编解码器

public class CustomCodec : IPacketCodec
{
    public T Decode<T>(byte[] bytes)
    {
        // 自定义解码逻辑
        return JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(bytes));
    }

    public byte[] Encode<T>(T payload)
    {
        // 自定义编码逻辑
        return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(payload));
    }
}

核心类

类名 说明
NettyServer TCP 服务器基类,封装引导和事件循环
INettyServer 服务器接口,定义 StartAsync 和端口配置
Packet 消息包,包含 Header 和 Body,支持 Builder 模式
PacketDecoder 消息解码器,将字节流转换为 Packet
PacketEncoder 消息编码器,将 Packet 转换为字节流
IPacketCodec 消息编解码器接口
MicrosoftJsonCodec 默认 JSON 编解码器实现
SessionManager 会话管理器,维护客户端连接状态
NettyServiceAttribute 服务标记特性,用于标识服务接口
DotNettyOptions 配置选项类,定义服务器端口

消息包结构

public class Packet
{
    public PacketHeader Header { get; set; }
    public object Body { get; set; }
}

public class PacketHeader
{
    public uint Crc = 0xabef0101;      // 校验码
    public byte Type { get; set; }      // 0=请求, 1=响应
    public byte Level { get; set; }     // 级别
    public long SessionId { get; set; } // 会话ID
    public int Length { get; set; }     // 消息体长度
}

依赖

  • DotNetty.Transport (>= 0.7.5)
  • DotNetty.Codecs (>= 0.7.5)
  • DotNetty.Common (>= 0.7.5)
  • Galosys.Foundation.Core
  • Microsoft.Extensions.Options

相关文档

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 Galosys.Foundation.DotNetty:

Package Downloads
Galosys.Foundation.DotNetty.Server

Galosys.Foundation快速开发库

Galosys.Foundation.DotNetty.Client

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.9.14.1 6 9/14/2026
26.9.10.1 79 9/10/2026
26.9.3.1 107 9/3/2026
26.8.29.1 103 8/31/2026
26.8.26.1 110 8/26/2026
26.8.23.1 114 8/23/2026
26.8.21.1 115 8/21/2026
26.8.20.1 104 8/20/2026
26.8.18.1 114 8/18/2026
26.8.17.1 126 8/17/2026
26.8.13.2 108 8/13/2026
26.8.13.1 113 8/13/2026
26.8.12.2 110 8/12/2026
26.8.12.1 113 8/12/2026
26.8.10.1 110 8/10/2026
26.8.5.1 118 8/5/2026
26.8.4.1 130 8/4/2026
26.8.3.1 122 8/3/2026
26.7.31.1 129 7/31/2026
26.7.30.1 120 7/30/2026
Loading failed