1.介绍
protocol buffers 是一种语言无关、平台无关、可扩展的序列化结构数据的方法,它可用于(数据)通信协议、数据存储等。
2.解析proto生成原理
1.Proto2CS.cs
将proto文件生成cs文件供c#使用
public static void Proto2CS()
{
msgOpcode.Clear();
//生成cs脚本
Proto2CS("ET", "../Proto/InnerMessage.proto", serverMessagePath, "InnerOpcode", 10000);
//生成opcode脚本
GenerateOpcode("ET", "InnerOpcode", serverMessagePath);
Proto2CS("ET", "../Proto/OuterMessage.proto", serverMessagePath, "OuterOpcode", 20000);
GenerateOpcode("ET", "OuterOpcode", serverMessagePath);
Proto2CS("ET", "../Proto/OuterMessage.proto", clientMessagePath, "OuterOpcode", 20000);
GenerateOpcode("ET", "OuterOpcode", clientMessagePath);
}
3.优化
现在框架所有的proto都在一个proto中,这在开发过程中极为不便,所以我重写了proto2cs方法,就可以将proto拆分无数个。
private const string protoSourcePath = "../Proto/";//proto目录
private const int opcodeIncrement = 200;//每个功能的opcode增量
public static void Proto2CS()
{
//读取proto下所有文件
DirectoryInfo dir = new DirectoryInfo(protoSourcePath);
FileInfo[] files = dir.GetFiles();
int count = 0;
foreach (FileInfo file in files)
{
if (file.Name.Contains("Inner"))
{
//生成cs脚本
Proto2CS("ET", protoSourcePath + file.Name, serverMessagePath, "InnerOpcode", 10000);
//生成opcode脚本
GenerateOpcode("ET", "InnerOpcode", serverMessagePath);
Log.Console(file.Name);
}
else if(file.Name.Contains("Outer"))
{
string opcodeName = Path.GetFileNameWithoutExtension(file.Name) + "Opcode";
Proto2CS("ET", protoSourcePath + file.Name, serverMessagePath, opcodeName, 20000 + count * opcodeIncrement);
GenerateOpcode("ET", opcodeName, serverMessagePath);
Proto2CS("ET", protoSourcePath + file.Name, clientMessagePath, opcodeName, 20000 + count * opcodeIncrement);
GenerateOpcode("ET", opcodeName, clientMessagePath);
count++;
Log.Console(file.Name);
}
}
}
4.使用
1.//开头为注释,//ResponseType开头要如果是有返回的要填写,message后面的//为继承接口,必须是ET框架实现的接口
//注释
//ResponseType M2C_TestResponse
message C2M_TestRequest // IActorLocationRequest
{
int32 RpcId = 90; //测试90
int64 ActorId = 93;//测试93
string request = 1;//测试1
}
message M2C_TestResponse // IActorLocationResponse
{
int32 RpcId = 90;
int32 Error = 91;
string Message = 92;
string response = 1;
}
2.创建的proto文件的名字要包含Inner和Outer才会被生成,Inner只会生成服务端,Outer会双端都生成
3.运行win_startProtoExport.bat生成cs文件