Proto-gen-lua 与 C# 对 Extension 的不同处理

来源:互联网 发布:测亩仪软件免费下载 编辑:程序博客网 时间:2024/06/05 23:15

按照我的习惯,网络消息结构应该是 MSDID +  数据 ,发送到服务器的。


一直奇怪为什么项目中,发送网络消息的时候根本没有指定MSGID,却还能正常工作。

开始使用 Proto-gen-lua 之后,碰到一系列的问题,直到刚才 才发现其中的奥秘。


之前注意到项目中的 Proto 很奇怪,使用了 Extension,而且 MsgId 和 消息结构体的 Field.number 一致。



下面是我们使用的一个 Proto 文件

message Student{  extensions 10 to max;}message Phone{extend Student{required Phone phone = 12;}        required int32 phonenum=1;}enum MSGID{MSGID_REQUEST_Login = 12;}

使用了 Extension ,而且 MSGID 和 对应的Phone 的 Field.number 一致 ,都是12.


所以我们在项目中使用的时候,是这样的

//序列化byte[] bs = null;using (System.IO.MemoryStream ms = new System.IO.MemoryStream()){    Student.Student student = new Student.Student();    Student.Phone content = new Student.Phone();    content.phonenum = 123;    ProtoBuf.Extensible.AppendValue<Student.Phone>(student, 12, content);    ProtoBuf.Serializer.Serialize(ms, student);    bs = ms.ToArray();}string result = "";for (int index = 0; index < bs.Length; index++){    result += bs[index].ToString() + " ";}Debug.Log(result);//反序列化using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bs)){    Student.Student student = ProtoBuf.Serializer.Deserialize<Student.Student>(ms);    Student.Phone phone = ProtoBuf.Extensible.GetValue<Student.Phone>(student, 12);}



注意这一句

ProtoBuf.Extensible.AppendValue<Student.Phone>(student, 12, content);

是的,使用了 Extension 后,需要手动传入 Field.number 到API中进行组包!!


然后我们就把这个数据包发送到了服务器。


想到 我们的MsgId 和 消息结构体的 Field.number 一致。

猜测可能服务器是直接取了  Field.number 作为了 MSGID,看了服务器代码之后,的确如此!!!



之前的疑问 就烟消云散了!!!


但是,在Lua 中我可没有把 Field.number 传入,而且 Lua 对Extension的使用也不是C# 这样的。

local student= Student_pb.Student()local phone = student.Extensions[Student_pb.Phone.phone]phone.phonenum=123ProtobufStringData.data = student:SerializeToString()

为什么还是能正常工作?

打开 转换后的 lua 文件来看

-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"module('Student_pb')local STUDENT = protobuf.Descriptor();local PHONE = protobuf.Descriptor();local PHONE_PHONENUM_FIELD = protobuf.FieldDescriptor();local PHONE_PHONE_FIELD = protobuf.FieldDescriptor();STUDENT.name = "Student"STUDENT.full_name = ".Student"STUDENT.nested_types = {}STUDENT.enum_types = {}STUDENT.fields = {}STUDENT.is_extendable = trueSTUDENT.extensions = {}PHONE_PHONENUM_FIELD.name = "phonenum"PHONE_PHONENUM_FIELD.full_name = ".Phone.phonenum"PHONE_PHONENUM_FIELD.number = 1PHONE_PHONENUM_FIELD.index = 0PHONE_PHONENUM_FIELD.label = 2PHONE_PHONENUM_FIELD.has_default_value = falsePHONE_PHONENUM_FIELD.default_value = 0PHONE_PHONENUM_FIELD.type = 5PHONE_PHONENUM_FIELD.cpp_type = 1PHONE_PHONE_FIELD.name = "phone"PHONE_PHONE_FIELD.full_name = ".Phone.phone"PHONE_PHONE_FIELD.number = 12PHONE_PHONE_FIELD.index = 0PHONE_PHONE_FIELD.label = 2PHONE_PHONE_FIELD.has_default_value = falsePHONE_PHONE_FIELD.default_value = nilPHONE_PHONE_FIELD.message_type = PHONEPHONE_PHONE_FIELD.type = 11PHONE_PHONE_FIELD.cpp_type = 10PHONE.name = "Phone"PHONE.full_name = ".Phone"PHONE.nested_types = {}PHONE.enum_types = {}PHONE.fields = {PHONE_PHONENUM_FIELD}PHONE.is_extendable = falsePHONE.extensions = {PHONE_PHONE_FIELD}Phone = protobuf.Message(PHONE)Student = protobuf.Message(STUDENT)Student.RegisterExtension(PHONE_PHONE_FIELD)

发现 Lua 已经把 Field.number 自动包含进来了


至此!!!!

Proto-gen-lua 与 C#  对 Extension 的处理是不同的,在C#中需要手动指定 Extend 结构的 Field.Numer  ,但是在 Lua中是不需要的。

直接使用 Field.Number 替代MSGID,这样能节省一个 Int32 。。

0 0