T2FGame 0.0.2
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 T2FGame --version 0.0.2
NuGet\Install-Package T2FGame -Version 0.0.2
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.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="T2FGame" Version="0.0.2" />
<PackageReference Include="T2FGame" />
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.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: T2FGame, 0.0.2"
#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.2
#: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.2
#tool nuget:?package=T2FGame&version=0.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
T2FGame
T2FGame 是一个基于 .NET 9 的高性能分布式游戏服务器框架,灵感来源于 Java 游戏框架 ioGame。
特性
- 高性能网络层:基于 SuperSocket 2.x 的 TCP 通信
- 分布式架构:基于 Microsoft Orleans 的 Actor 模型
- DDD 支持:完整的领域驱动设计基础设施
- Pipeline 处理链:可扩展的请求处理管道
- 多数据库支持:MongoDB、SQL Server、PostgreSQL、MySQL
- 房间系统:内置游戏房间和匹配功能
架构概览
┌─────────────────────────────────────────────────────────────────┐
│ 客户端 (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 │
│ (数据持久化) │
└─────────────────────────────────────────────────────────────────┘
模块说明
| 模块 | 说明 |
|---|---|
| T2FGame.Core | 核心库,DDD 构建块、Result 模式、领域事件 |
| T2FGame.Protocol | 协议层,命令路由定义、Protobuf 消息 |
| T2FGame.Action | Action 处理层,Pipeline、Handler 链 |
| T2FGame.Network.Socket | 网络层,TCP 服务器、会话管理 |
| T2FGame.Cluster.Orleans | 集群层,Broker、GameServer、负载均衡 |
| T2FGame.Room | 房间模块,游戏房间、匹配系统 |
| T2FGame.Persistence.MongoDB | MongoDB 持久化,Repository 实现 |
快速开始
1. 安装
# 创建新项目
dotnet new console -n MyGameServer
cd MyGameServer
# 添加 T2FGame 依赖
dotnet add package T2FGame.Core
dotnet add package T2FGame.Protocol
dotnet add package T2FGame.Action
dotnet add package T2FGame.Network.Socket
dotnet add package T2FGame.Cluster.Orleans
dotnet add package T2FGame.Persistence.MongoDB
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:高性能网络库
- MongoDB Driver 3.x:MongoDB 官方驱动
- Google.Protobuf:高性能序列化
环境要求
- .NET 9.0 SDK
- MongoDB 4.4+(推荐副本集模式)
- 或其他支持的数据库
项目结构
T2FGame/
├── src/
│ ├── T2FGame.Core/ # 核心库
│ ├── T2FGame.Protocol/ # 协议层
│ ├── T2FGame.Action/ # Action 层
│ ├── T2FGame.Network.Socket/ # 网络层
│ ├── T2FGame.Cluster.Orleans/ # 集群层
│ ├── T2FGame.Room/ # 房间模块
│ └── T2FGame.Persistence.MongoDB/ # MongoDB 持久化
├── tests/ # 测试项目
├── examples/ # 示例项目
├── Directory.Packages.props # 中央包管理
├── Directory.Build.props # 公共构建配置
└── T2FGame.sln # 解决方案文件
文档
每个模块都有独立的 README 文档,详细说明用法:
- T2FGame.Core - DDD 基础设施
- T2FGame.Protocol - 协议定义
- T2FGame.Action - Action 框架
- T2FGame.Network.Socket - 网络通信
- T2FGame.Cluster.Orleans - 分布式集群
- T2FGame.Room - 房间系统
- T2FGame.Persistence.MongoDB - MongoDB 持久化
构建
# 还原依赖
dotnet restore
# 构建
dotnet build
# 运行测试
dotnet test
致谢
- ioGame - 灵感来源
- Microsoft Orleans - 分布式 Actor 框架
- SuperSocket - 网络通信库
License
MIT License
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- T2FGame.Action (>= 0.0.2)
- T2FGame.Cluster.Orleans (>= 0.0.2)
- T2FGame.Core (>= 0.0.2)
- T2FGame.Network.Socket (>= 0.0.2)
- T2FGame.Persistence.MongoDB (>= 0.0.2)
- T2FGame.Protocol (>= 0.0.2)
- T2FGame.Room (>= 0.0.2)
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.