Protobuf c++ extensions use

最后都变了- 提交于 2019-12-12 01:07:59

问题


I'm using google protobuf library version 2.61 and want to use extensions,

I have the following proto files:

package communication;

message BaseMessage {
  required uint64 server_id     = 1;
  required string uuid          = 2;
  required uint64 message_id    = 3;

  extensions 100 to max;
}

message GetIdentify {

  extend BaseMessage {
    optional GetIdentify message = 100;
  }

  required string hostname = 1;
}

I am able to build the message using the following code:

communication::BaseMessage base_message;
base_message.set_message_id(123456);
base_message.set_server_id(112313123);
base_message.set_uuid("asdaskdjasd213123123asd");
base_message.MutableExtension(communication::GetIdentify::message)->set_hostname("http://test123123123ing");

However I would like to do the opposite action and get message with unknown extension and parse it and find which extension it is and parse according to it.

I've used nanopb for my c project and python version. but I find it really hard to write protobuf code in c++ because I can't find good enough documentation and code examples.

Is there a way to do this without adding additional variable of type of extension?

Also what is the most elegant way to do so in c++


回答1:


The library parse message and all extensions. You can check presence of the extension using HasExtension method.

Java implementation needs to register extensions in parser, before parsing. But in C++ everything done automatically. Please see following code. (I tested it with protobuf 2.5.0)

Create and write message:

#include <iostream>
#include <fstream>
#include <string>
#include <communication/p.pb.h> 
using namespace std;

int
main(int, char **)
{
    communication::BaseMessage base_message;
    base_message.set_message_id(123456);
    base_message.set_server_id(112313123);
    base_message.set_uuid("asdaskdjasd213123123asd");
    base_message.MutableExtension(communication::GetIdentify::message)->set_hostname("http://test123123123ing");
    base_message.SerializeToOstream(&cout);
    return 0;
}

Read message, test extension and print it:

#include <iostream>
#include <fstream>
#include <string>
#include <communication/p.pb.h>
#include <google/protobuf/text_format.h>
using namespace google::protobuf;
using namespace std;

int
main(int, char **)
{
    communication::BaseMessage base_message;
    base_message.ParseFromIstream(&cin);

    if (base_message.HasExtension(communication::GetIdentify::message)) {
        const communication::GetIdentify &ii = base_message.GetExtension(communication::GetIdentify::message);
        cout << "yes, msg has extension: " << ii.hostname() << endl << endl;
    } else {
        cout << "no, msg has no extension" << endl << endl;
    }

    string res;
    TextFormat::PrintToString(base_message, &res);

    cout << res << endl;
    return 0;
}


来源:https://stackoverflow.com/questions/38962972/protobuf-c-extensions-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!