简体   繁体   English

Protobuf C ++扩展使用

[英]Protobuf c++ extensions use

I'm using google protobuf library version 2.61 and want to use extensions, 我正在使用Google protobuf库2.61版,并希望使用扩展程序,

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. 我已经将nanopb用于我的c项目和python版本。 but I find it really hard to write protobuf code in c++ because I can't find good enough documentation and code examples. 但是我发现用c ++编写protobuf代码真的很困难,因为我找不到足够好的文档和代码示例。

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++ 还有什么是在c ++中最优雅的方法

The library parse message and all extensions. 库解析消息和所有扩展。 You can check presence of the extension using HasExtension method. 您可以使用HasExtension方法检查扩展名的存在。

Java implementation needs to register extensions in parser, before parsing. Java实现需要在解析之前在解析器中注册扩展。 But in C++ everything done automatically. 但是在C ++中,所有操作都是自动完成的。 Please see following code. 请参阅以下代码。 (I tested it with protobuf 2.5.0) (我用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM