如何使用协议缓冲区转储结构类型的数据?

How to dump struct type data using protocol buffer?(如何使用协议缓冲区转储结构类型的数据?)
本文介绍了如何使用协议缓冲区转储结构类型的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gRPC,我要使用protocol bufferstruct type objectserver传输到client。我希望将结构数据类型直接转储到消息字段中。

此处给出了我的一些示例代码

server.cpp我从函数获取struct类型的对象。


// sample struct
struct Dummy_Info{
    int age;
    bool presence;
};

// sample function to return struct
Dummy_Info Pass_Dummy_Info()
{
    Dummy_Info obj;
    obj.age = 54;
    obj.presence = 1;
    return obj;
}

sample.proto我想制作的文件(我在这里只使用将用于服务器目的的消息)

syntax = "proto3";

package abc_xyz;

// Response message from server to client
message Server_Response {
    float val = 1;
   // The following is my desire
    struct variable_for_struct_object = 2;
}

// Request message from client to server
message Client_Request {
    string name = 1;
}

service Service {
    rpc GetAddress(Client_Request) returns (Server_Response) {}
}

server.cpp

// omitted the unnecessary part intentionally. Suppose I have passed the Server_Response as pointer type object and it is `response`
Dummy_Info result;
result = Pass_Dummy_Info();

// The following works flawlessly
response -> set_val(0.5);

// The following I want to prepare
response -> set_variable_for_struct_object(result);

client.cpp

// Here I need to fetch the struct type data
auto data = response_.variable_for_struct_object();
cout << data.presence << endl;
cout << data.age << endl;

我不知道这种直接转储是可能的还是不可能的,或者我可能错过了概念。

我现在正在做的事情:

sample.proto

syntax = "proto3";

package abc_xyz;

// The following I have added which mimics my struct type object
message DATA_TYPE{
    int32 age = 1;
    bool presence = 2;
}

// Response message from server to client
message Server_Response {
    float val = 1;
   // The following is my desire
    DATA_TYPE variable_for_struct_object = 2;
}

// Request message from client to server
message Client_Request {
    string name = 1;
}

service Service {
    rpc GetAddress(Client_Request) returns (Server_Response) {}
}

之后,我将CPP文件的格式设置为如下所示,这是可行的

server.cpp

Dummy_Info result;
result = Pass_Dummy_Info();
response->mutable_variable_for_struct_object()->set_age(result.age);

client.cpp和以前一样。

我希望的原因是避免两次声明相同的结构(在cpp中声明一次,在proto message format中声明一次)。如果这里有任何解决办法,希望得到指导。

推荐答案

Protobuf为结构保存类似的数据。有关PB与结构的更多信息,请访问:https://developers.google.com/protocol-buffers/docs/cpptutorial

syntax = "proto3";

package communication;

message YourData {
  int32 d1 = 1;
  string d2 = 2; 
} 

// Response message from server to client
message Response {
    int32 result = 1;
    // struct obj_data = 2; // I know this will not work and which pushes me to ask the question.
    YourData data = 2;  
}

// Request message from client to server
message Request {
    string name = 1
}

service Dummy_Service {
    rpc GetAddress(Request) returns (Response) {}
}

服务器相关代码

   YourData d; 
   d.set_d1(1);
   d.set_d2("hello");
   response->mutable_data()->CopyFrom(d)

与客户端相关的代码

 auto data = response_.data();
 cout << data.d1() << ":" << data.d2() << endl;

这篇关于如何使用协议缓冲区转储结构类型的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

socks4 with asynchronous boost::asio(带有异步Boost::ASIO的socks4)
Boost ASIO HTTP client POST(增强ASIO HTTP客户端开机自检)
asio::strandlt;asio::io_context::executor_typegt; vs io_context::strand(Asio::strandlt;asio::io_context::executor_typegt;VS io_CONTEXT::Strand)
What#39;s the difference between asio::io_context and asio::thread_pool?(ASIO::IO_CONTEXT和ASIO::THREAD_POOL有什么区别?)
How to use boost::asio with Linux GPIOs(如何在Linux GPIO中使用Boost::ASIO)
Boost asio io_service vs io_context(增强ASIO io_服务与io_Context)