最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
信息传递工具protobuf通过pb的反射来赋值
时间:2022-06-25 07:20:44 编辑:袖梨 来源:一聚教程网
最近因为项目逻辑方面都做的差不多了,于是赶紧去做做测试工作,免得项目上线之后出问题,于是打算用配置的形式做一个类似与白盒测试工具的东西出来。
因为项目使用pb来做协议通讯,所以配置的xml也是类似于pb,将pb的字段和类型配置进去,然后加上值,一个协议结构就可以了,现在只能通过修改值来做测试,后面会改动的更智能化一些,例如某个行为的次数,某个行为更随机等等。
去读了一下陈硕的关于pb处理协议的反射,学到了不少东西,同时对pb的一些东西理解更深刻了,google还是大牛很多。
1.如何处理pb的反射,通过协议字串动态生成一个协议
pb提供了一个强大的DescriptorPool::generated_pool()
代码如下:
| 代码如下 | 复制代码 |
| inline google::protobuf::Message* CreateMessage(const std::string& msg) { google::protobuf::Message* message = NULL; const google::protobuf::Descriptor* descriptor = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName("ProtoMsg." + msg); if (descriptor) { const google::protobuf::Message* prototype = google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor); if (prototype) { message = prototype->New(); } } return message; } inline void ReleaseMessage(google::protobuf::Message* pMsg) { if (NULL != pMsg) { pMsg->Clear(); delete pMsg; } } | |
这两个函数可以动态生成pb的message,其中ProtoMsg是你pb package的名字
2.通过反射将配置中的值设置进pb字段
pb的Message基类提供了一个Reflection,这个类非常强大
代码如下
| 代码如下 | 复制代码 |
| // mstrCurMsg 当前正在执行的协议 google::protobuf::Message* pMsg = Test::CreateMessage(mstrCurMsg); const google::protobuf::Descriptor* pDescriptor = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName("ProtoMsg." + mstrCurMsg); assert(NULL != pDescriptor); // 这一个可以获取到反射类,然后可以将配置中值赋值进去 const google::protobuf::Reflection* pReflection = pMsg->GetReflection(); assert(NULL != pReflection); for (int i = 0; i < pDescriptor->field_count(); ++i) { const google::protobuf::FieldDescriptor* pFieldDescriptor = pDescriptor->field(i); assert(NULL != pFieldDescriptor); const std::string& strFieldName = pFieldDescriptor->name(); const TestConfigModule::MsgEntry* pMsgEntry = pMsgStruct->GetMsgEntry(strFieldName); assert(NULL != pMsgEntry); // 读取字段类型,顺带可以做类型检查 assert(pMsgEntry->mnType == pFieldDescriptor->type()); // 设置值 switch (pMsgEntry->mnType) { case Test::TYPE_STRING: pReflection->SetString(pMsg, pFieldDescriptor, pMsgEntry->mstrValue); break; // ... default: break; } } std::string strData; if (!pMsg->SerializeToString(&strData)) { m_pLogModule->LogNormal("Test stop, cannot SerializeToString ", mstrCurMsg, __FUNCTION__, __LINE__); return; } Test::ReleaseMessage(pMsg); | |
通过这样的步骤,就可以自动创建message和对field赋值了
相关文章
- dota2骑士之倚有什么用 骑士之倚效果介绍 12-06
- 致命公司雷达增强器怎么用 雷达发射器使用教学 12-06
- 恋爱吗我家的猫会后空翻第一章全流程攻略 12-06
- 星球重启球球宠物心愿解锁条件一览 12-06
- 星球重启冰霜琥珀在哪 冰霜琥珀位置一览 12-06
- 星球重启伊甸城全鱼类分布位置一览 12-06