T2FGame 0.0.1

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

T2FGame

NuGet .NET License

T2FGame 是一个基于 .NET 9 的高性能分布式游戏服务器框架,灵感来源于 Java 游戏框架 ioGame

特性

  • 高性能网络层:基于 SuperSocket 2.x 的 TCP 通信
  • 分布式架构:基于 Microsoft Orleans 的 Actor 模型
  • DDD 支持:完整的领域驱动设计基础设施
  • Pipeline 处理链:可扩展的请求处理管道(认证、限流、缓存、日志、指标)
  • 分布式缓存:支持内存缓存和 Redis 分布式缓存
  • 分布式限流:支持滑动窗口和令牌桶算法
  • 多数据库支持:MongoDB、Redis
  • 房间系统:内置游戏房间和匹配功能

架构概览

┌─────────────────────────────────────────────────────────────────┐
│                         客户端 (Client)                          │
└─────────────────────────────────────────────────────────────────┘
                                │
                                ▼
┌─────────────────────────────────────────────────────────────────┐
│                    T2FGame.Network.Socket                        │
│                      (外部网关 Gateway)                           │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐           │
│  │ TCP Server   │  │ 协议编解码    │  │ 会话管理      │           │
│  └──────────────┘  └──────────────┘  └──────────────┘           │
└─────────────────────────────────────────────────────────────────┘
                                │
                                ▼
┌─────────────────────────────────────────────────────────────────┐
│                   T2FGame.Cluster.Orleans                        │
│                      (消息代理 Broker)                            │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐           │
│  │ BrokerGrain  │  │ 路由分发      │  │ 负载均衡      │           │
│  └──────────────┘  └──────────────┘  └──────────────┘           │
└─────────────────────────────────────────────────────────────────┘
          │                   │                   │
          ▼                   ▼                   ▼
┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐
│ GameServerGrain │  │ GameServerGrain │  │ GameServerGrain │
│   (用户模块)     │  │   (战斗模块)     │  │   (房间模块)     │
└─────────────────┘  └─────────────────┘  └─────────────────┘
          │                   │                   │
          ▼                   ▼                   ▼
┌─────────────────────────────────────────────────────────────────┐
│                       T2FGame.Action                             │
│                     (业务处理 Pipeline)                           │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐           │
│  │ 认证/授权     │  │ Action 调用   │  │ 异常处理      │           │
│  └──────────────┘  └──────────────┘  └──────────────┘           │
└─────────────────────────────────────────────────────────────────┘
          │                   │                   │
          ▼                   ▼                   ▼
┌─────────────────────────────────────────────────────────────────┐
│                        T2FGame.Core                              │
│                     (DDD 基础设施)                                │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐           │
│  │ Entity       │  │ Result 模式   │  │ 领域事件      │           │
│  │ AggregateRoot│  │ Error 处理    │  │ EventBus     │           │
│  └──────────────┘  └──────────────┘  └──────────────┘           │
└─────────────────────────────────────────────────────────────────┘
                                │
                                ▼
┌─────────────────────────────────────────────────────────────────┐
│                 T2FGame.Persistence.MongoDB                      │
│                      (数据持久化)                                 │
└─────────────────────────────────────────────────────────────────┘

模块说明

核心模块

模块 说明 NuGet
T2FGame 元包,一次性引用所有运行时模块 NuGet
T2FGame.Core 核心库,DDD 构建块、Result 模式、领域事件 NuGet
T2FGame.Protocol 协议层,命令路由定义、Protobuf 消息 NuGet
T2FGame.Action Action 处理层,Pipeline、Handler 链、广播服务 NuGet
T2FGame.Network.Socket 网络层,TCP 服务器、会话管理 NuGet
T2FGame.Cluster.Orleans 集群层,Broker、GameServer、负载均衡 NuGet
T2FGame.Room 房间模块,游戏房间、匹配系统 NuGet
T2FGame.Cache.Redis Redis 缓存,分布式缓存和限流 NuGet
T2FGame.Persistence.MongoDB MongoDB 持久化,Repository 实现 NuGet

代码生成模块

模块 说明
T2FGame.CodeGen Source Generator 基础框架,提供代码构建器和分析工具
T2FGame.CodeGen.Action Action 源生成器,编译时生成零反射调用器和注册表
T2FGame.CodeGen.Protocol 协议源生成器,自动生成 protobuf-net 配置和 .proto 文件

开发工具

模块 说明
T2FGame.Tools 开发工具库,提供 Proto 导出等功能
T2FGame.Tools.Cli CLI 工具,命令行导出 .proto 文件(t2f-proto

快速开始

1. 安装

# 创建新项目
dotnet new console -n MyGameServer
cd MyGameServer

# 方式一:使用元包(推荐,一次性引用所有模块)
dotnet add package T2FGame

# 方式二:按需引用单独模块
dotnet add package T2FGame.Core
dotnet add package T2FGame.Action
dotnet add package T2FGame.Network.Socket
dotnet add package T2FGame.Cluster.Orleans
dotnet add package T2FGame.Persistence.MongoDB
dotnet add package T2FGame.Cache.Redis  # 可选:Redis 分布式缓存

2. 定义 Action

[ActionController(cmd: 1)] // 用户模块
public class UserController
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }

    [ActionMethod(subCmd: 1, Description = "用户登录")]
    public async Task<LoginResponse> Login(LoginRequest request, FlowContext context)
    {
        var result = await _userService.LoginAsync(request.Username, request.Password);

        context.SetAuthenticated(result.UserId);

        return new LoginResponse
        {
            UserId = result.UserId,
            Token = result.Token
        };
    }

    [ActionMethod(subCmd: 2, RequireAuth = true)]
    public async Task<UserInfo> GetUserInfo(FlowContext context)
    {
        return await _userService.GetByIdAsync(context.UserId);
    }
}

3. 配置服务器

var host = Host.CreateDefaultBuilder(args)
    // 配置 Orleans 集群
    .UseT2FGameCluster(options =>
    {
        options.ClusterId = "MyGameCluster";
        options.ServiceId = "MyGame";

        // 生产环境使用 MongoDB
        options.ClusteringProvider = ClusteringProviderType.MongoDB;
        options.PersistenceProvider = PersistenceProviderType.MongoDB;
        options.MongoConnectionString = "mongodb://localhost:27017";
        options.MongoDatabaseName = "MyGame";
    })
    .ConfigureServices((context, services) =>
    {
        // 注册 Action
        services.AddActionMethodRegistry(options =>
        {
            options.ScanAssemblies(typeof(UserController).Assembly);
        });
        services.AddActionPipeline();

        // 注册 MongoDB 持久化
        services.AddMongoDbPersistence(options =>
        {
            options.ConnectionString = "mongodb://localhost:27017";
            options.DatabaseName = "MyGame";
        });

        // 注册游戏服务器
        services.AddT2FGameServer(options =>
        {
            options.ServerId = "game-server-1";
            options.ServerName = "游戏服务器1";
        });

        // 注册网络服务
        services.AddT2FGameSocket(options =>
        {
            options.Port = 9000;
        });
    })
    .Build();

await host.RunAsync();

4. 定义 Proto 消息

syntax = "proto3";
package myproject;

message LoginRequest {
    string username = 1;
    string password = 2;
}

message LoginResponse {
    int64 user_id = 1;
    string token = 2;
}

message UserInfo {
    int64 user_id = 1;
    string nickname = 2;
    int32 level = 3;
}

与 ioGame 对比

ioGame 概念 T2FGame 对应 说明
BarSkeleton ActionPipeline 业务处理骨架
ActionController ActionController Action 控制器
ActionMethod ActionMethod Action 方法
FlowContext FlowContext 请求上下文
BrokerServer BrokerGrain 消息代理
BrokerClient GameServerGrain 游戏逻辑服务器
ExternalServer Network.Socket 外部网关
CmdMerge CmdKit.Merge 命令路由合并
RoomKit T2FGame.Room 房间模块

技术栈

  • .NET 9:最新 LTS 版本
  • Microsoft Orleans 9.x:分布式 Actor 框架
  • SuperSocket 2.x:高性能网络库
  • StackExchange.Redis:高性能 Redis 客户端
  • MongoDB Driver 3.x:MongoDB 官方驱动
  • Google.Protobuf:高性能序列化

环境要求

  • .NET 9.0 SDK
  • MongoDB 4.4+(推荐副本集模式)
  • Redis 6.0+(可选,用于分布式缓存和限流)

项目结构

T2FGame/
├── src/
│   ├── T2FGame/                      # 元包
│   ├── T2FGame.Core/                 # 核心库
│   ├── T2FGame.Protocol/             # 协议层
│   ├── T2FGame.Action/               # Action 层
│   ├── T2FGame.Network.Socket/       # 网络层
│   ├── T2FGame.Cluster.Orleans/      # 集群层
│   ├── T2FGame.Room/                 # 房间模块
│   ├── T2FGame.Cache.Redis/          # Redis 缓存
│   ├── T2FGame.Persistence.MongoDB/  # MongoDB 持久化
│   ├── T2FGame.CodeGen/              # 源生成器基础框架
│   ├── T2FGame.CodeGen.Action/       # Action 源生成器
│   ├── T2FGame.CodeGen.Protocol/     # 协议源生成器
│   ├── T2FGame.Tools/                # 开发工具库
│   └── T2FGame.Tools.Cli/            # CLI 工具
├── samples/                          # 示例项目
├── tests/                            # 测试项目
├── Directory.Packages.props          # 中央包管理
├── Directory.Build.props             # 公共构建配置
└── T2FGame.sln                       # 解决方案文件

文档

每个模块都有独立的 README 文档,详细说明用法:

运行时模块

代码生成模块

开发工具

构建

# 还原依赖
dotnet restore

# 构建
dotnet build

# 运行测试
dotnet test

致谢

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on T2FGame:

Package Downloads
T2FGame.Samples.GameServer

A high-performance game server framework

GitHub repositories

This package is not used by any popular GitHub repositories.