protobuf动态解析

2018-12-17  本文已影响0人  hexg1016

enum command

{

// 登录请求

CMD_LOGIN_REQ = 501 [(message_name) = "LoginReq"];

// 登录回复

CMD_LOGIN_RSP = 502 [(message_name) = "LoginRsp"];

}

extend google.protobuf.MessageOptions

{

// 协议的协议号

optional gateproto.command protocolid = 51550;

}

// CMD_LOGIN_REQ = 501

message LoginReq

{

option (gateproto.protocolid) = CMD_LOGIN_REQ;

required string account = 1;

required string name = 2;

required bytes token = 3;

required uint64 roleid = 4;

required uint32 is_reconnect = 5[ default = 0];

optional uint32 imgid = 6[ default = 0];

optional string device_id = 7;

}

include <stdio.h>

include <vector>

include <map>

include <iostream>

include "google/protobuf/message.h"

include "google/protobuf/descriptor.h"

include "google/protobuf/descriptor.pb.h"

include "google/protobuf/dynamic_message.h"

include "google/protobuf/compiler/importer.h"

include "google/protobuf/extension_set.h"

include <stdlib.h>

include <stdint.h>

include <ctype.h>

include "stable_test.h"

using namespace std;

using namespace google::protobuf;

using namespace google::protobuf::compiler;

using namespace google::protobuf::internal;

static DiskSourceTree srctree_;

static Importer *importer_;

static DynamicMessageFactory MsgFactory_;

struct MsgDesc

{

const FileDescriptor *filedesc;

const Descriptor *desc;

const Message *prototype;

};

std::map<string, MsgDesc *> descmap_;

std::vector< MsgDesc *> vecDesc_;

const FieldDescriptor * protocolid_des;

class FileErrorCollector : public MultiFileErrorCollector {

public:

void AddError(const string& filename, int line, int column,

const string& message)

{

cout << filename << endl;

cout << line << endl;

cout << column << endl;

cout << message << endl;

}

};

void AddSearchPath(vector<string>& paths)

{

for (size_t i = 0; i < paths.size(); i++)

{

srctree_.MapPath("", paths[i]);

}

FileErrorCollector err;

importer_ = new Importer(&srctree_, &err);

}

void AddMsgDesc(const string& proto_file)

{

const FileDescriptor *file_desc = importer_->Import(proto_file);

int count = file_desc->message_type_count();

if (count <= 0)

return;

int extensiopn_couny = file_desc->extension_count();

printf("extensiopn_couny type: %d\n", extensiopn_couny);

for (int i = 0; i < extensiopn_couny; i++)

{

const FieldDescriptor * msg_desc = file_desc->extension(i);

printf("extension type: %s\n", msg_desc->name().c_str());

printf("extension cpp type: %s\n", msg_desc->cpp_type_name());

printf("extension type: %s\n", msg_desc->type_name());

if ( msg_desc->name() == "protocolid")

{

protocolid_des = msg_desc;

}

}

// string key = proto_file.substr(0, proto_file.find_last_of("."));

for (int i = 0; i < count; i++)

{

const Descriptor * msg_desc = file_desc->message_type(i);

printf("message type: %s\n", msg_desc->name().c_str());

MsgDesc *msgdesc = new MsgDesc;

msgdesc->filedesc = file_desc;

msgdesc->desc = msg_desc;

msgdesc->prototype = MsgFactory_.GetPrototype(msg_desc);

descmap_[msg_desc->name()] = msgdesc;

printf("message type: %s\n", msg_desc->options().DebugString().c_str());

/*bool m = msg_desc->options().GetExtension(gateproto::protocolid);

if (m) {

printf("message type: %s\n", msg_desc->name().c_str());

vecDesc_.push_back(msgdesc);

}*/

}

}

void SendGsRandomMessage()

{

uint32_t index = 10;

auto iter = descmap_.find("EnterPvePassReq");

if (iter!= descmap_.end() ){

printf("SendGsRandomMessage");

MsgDesc *msgdesc = iter->second;

Message *msg = msgdesc->prototype->New();

printf("SendGsRandomMessage field count type: %d\n",msg->GetDescriptor()->field_count());

printf("SendGsRandomMessage extension count type: %d\n",msg->GetDescriptor()->extension_count());

printf("SendGsRandomMessage one count type: %d\n",msg->GetDescriptor()->oneof_decl_count());

printf("SendGsRandomMessage enum count type: %d\n",msg->GetDescriptor()->enum_type_count());

const Descriptor * option_Desc = msg->GetDescriptor()->options().GetDescriptor();

for (int i=0;i<option_Desc->field_count();i++) {

const FieldDescriptor * filed_des = option_Desc->field(i);

printf("FieldDescriptor type: %s\n", filed_des->name().c_str());

}

for (int i=0;i<msg->GetDescriptor()->field_count();i++) {

const FieldDescriptor * filed_des = msg->GetDescriptor()->field(i);

printf("FieldDescriptor type: %s\n", filed_des->name().c_str());

}

MessageOptions msgop= msg->GetDescriptor()->options();

const ::google::protobuf::UnknownFieldSet& unset = msgop.unknown_fields();

for (int i=0;i<unset.field_count();i++) {

UnknownField filed = unset.field(i);

printf("UnknownField %d",filed.varint());

}

}

}

int main(int argc, char *argv[])

{

vector<string> v;

v.push_back("../protocol");

v.push_back("../include");

AddSearchPath(v);

AddMsgDesc("gate.pb/gate.proto");

AddMsgDesc("rank.pb/rank.proto");

AddMsgDesc("immortaldb.pb/immortaldb.proto");

AddMsgDesc("dbproxy.pb/dbproxy.proto");

AddMsgDesc("online.pb/online.proto");

AddMsgDesc("login.pb/login.proto");

SendGsRandomMessage();

return 0;

}
</pre>

上一篇下一篇

猜你喜欢

热点阅读