基于位掩码序列化技术的多语言网络通信协议框架。
- 多语言支持: C#、Python、C++
- 位掩码序列化: 高效的网络传输
- 代码生成: 从协议定义文件自动生成目标语言代码
- 客户端/服务端框架: 自动生成RPC客户端和服务端代码
- 类型安全: 强类型的数据结构定义
BitRPC/
├── Docs/ # 文档
│ └── protocol-framework-design.md
├── Examples/ # 示例协议定义
│ └── user_service.pdl
├── Generated/ # 生成的代码
├── Src/ # 源代码
│ ├── C++Core/ # C++运行时库
│ ├── Client/ # 客户端框架
│ ├── Generator/ # 代码生成器
│ │ ├── CodeGeneratorCore.cs
│ │ ├── CSharpCodeGenerator.cs
│ │ ├── PythonCodeGenerator.cs
│ │ └── CppCodeGenerator.cs
│ ├── Parser/ # 协议解析器
│ │ └── PDLParser.cs
│ ├── Python/ # Python运行时库
│ │ └── bitrpc/
│ ├── Serialization/ # 序列化框架
│ │ └── BitMaskSerialization.cs
│ ├── Server/ # 服务端框架
│ │ └── RpcServer.cs
│ └── GeneratorApp/ # 生成器应用程序
│ ├── Program.cs
│ └── GeneratorApp.csproj
└── *.bat # 批处理脚本
namespace Example.Protocol
// 消息定义
message MessageName {
field_type field_name = field_id;
repeated item_type field_name = 2; // 列表类型
}
// 服务定义
service ServiceName {
rpc MethodName(RequestType) returns (ResponseType);
}
// 选项定义
option version = "1.0.0"
- 基本类型:
int32,int64,float,double,bool,string - 复合类型:
repeated(列表) - 特殊类型:
DateTime,Vector3
创建 .pdl 文件定义你的协议:
namespace MyService.Protocol
message Request {
string message = 1;
int32 value = 2;
}
message Response {
bool success = 1;
string result = 2;
}
service MyService {
rpc Process(Request) returns (Response);
}
有几种方式可以生成代码:
# 生成所有语言的代码
generate-all.bat
# 只生成C#代码
generate-csharp.bat
# 只生成Python代码
generate-python.bat
# 只生成C++代码
generate-cpp.bat# 生成所有语言的代码
dotnet run --project Src/GeneratorApp -- generator-config.json
# 生成特定语言的代码 (需要先修改generator-config.json)
dotnet run --project Src/GeneratorApp -- generator-config.jsonGenerated/
├── csharp/ # C# 代码
│ ├── Data/ # 数据结构
│ ├── Serialization/ # 序列化代码
│ ├── Client/ # 客户端代码
│ ├── Server/ # 服务端代码
│ └── Factory/ # 工厂类
├── python/ # Python 代码
│ ├── data/ # 数据模型
│ ├── serialization/ # 序列化器
│ ├── client/ # 客户端
│ ├── server/ # 服务端
│ └── factory/ # 工厂
└── cpp/ # C++ 代码
├── include/ # 头文件
├── src/ # 源文件
└── CMakeLists.txt
框架使用 generator-config.json 配置文件来控制代码生成:
{
"protocolFile": "Examples/user_service.pdl",
"outputDirectory": "Generated",
"languages": [
{
"name": "csharp",
"enabled": true,
"namespace": "Example.Protocol",
"runtimePath": "Src"
},
{
"name": "python",
"enabled": true,
"namespace": "example.protocol",
"runtimePath": "Src/Python"
},
{
"name": "cpp",
"enabled": true,
"namespace": "example.protocol",
"runtimePath": "Src/C++Core"
}
]
}本框架使用位掩码序列化技术来优化网络传输:
- 每32个字段分为一组
- 使用位掩码标识哪些字段需要序列化
- 只传输有变化的字段数据,减少传输量
var user = new User
{
UserId = 123,
Username = "john_doe",
IsActive = true
};
var serializer = BufferSerializer.Instance;
var data = serializer.Serialize(user);var client = new TcpRpcClient("localhost", 8080);
var userService = new UserServiceClient(client);
await client.ConnectAsync();
var response = await userService.GetUserAsync(new GetUserRequest { UserId = 123 });var server = new RpcServer(8080);
server.RegisterService("UserService", new UserServiceImplementation());
server.Start();var options = new GenerationOptions
{
Language = TargetLanguage.CSharp,
OutputDirectory = "./output",
Namespace = "My.Protocol",
GenerateSerialization = true,
GenerateClientServer = true,
GenerateFactories = true
};dotnet build Src/GeneratorApp/GeneratorApp.csproj# 使用配置文件
dotnet run --project Src/GeneratorApp -- generator-config.json
# 直接指定协议文件和输出目录
dotnet run --project Src/GeneratorApp -- Examples/user_service.pdl Generated查看 Examples/user_service.pdl 了解完整的协议定义示例。
对于每种目标语言,需要将以下文件复制到生产项目中:
Src/Serialization/BitMaskSerialization.cs- 序列化核心库Src/Client/RpcClient.cs- 客户端框架Src/Server/RpcServer.cs- 服务端框架
Src/Python/bitrpc/目录下的所有文件 - Python运行时库
Src/C++Core/目录下的所有文件 - C++运行时库
每次更新协议定义后,需要将生成的代码复制到对应项目中:
Generated/csharp/Example/Protocol/Data/- 数据结构类Generated/csharp/Example/Protocol/Serialization/- 序列化代码Generated/csharp/Example/Protocol/Client/- 客户端代码Generated/csharp/Example/Protocol/Server/- 服务端代码Generated/csharp/Example/Protocol/Factory/- 工厂类
Generated/python/data/- 数据模型Generated/python/serialization/- 序列化器Generated/python/client/- 客户端Generated/python/server/- 服务端Generated/python/factory/- 工厂
Generated/cpp/include/- 头文件Generated/cpp/src/- 源文件
本项目采用 MIT 许可证。