当我在 C++ 上使用 ZMQ 和来自 Google 的 protobufs 编写服务器-客户端程序时,接触了与类型变化相关的函数。
问题:ParseFromString
和 SerializeToString
函数使用字符串类型作为参数,但我需要使用 zmq::message_t
编写程序作为参数.
Proto file:
package core;
{ message Request
required uint32 id=1;
required uint32 class_id=2;
...}
zmq::message_t msg; // beginning
core::Request reqt;
…
socket.recv(&msg) ;// receiving messgae, socket.recv(reqt) doesn't work
ParseFromString (msg); //serialization to structure doesn’t work
程序是使用套接字的客户端-服务器执行 需要协调变量字符串类型和消息之间的相互关系。 函数 ParseFromString() 和 SerializeToString()。 В Python все просто: socket.send(reqt.SerializeToString()) è class_id=reqt.ParseFromString(socket.recv)
最佳答案
你应该使用 ParseFromArray()
:
reqt.ParseFromArray(msg.data(), msg.size());
在序列化方面,使用 ByteSize()
获取 protobuf 消息的大小,为其分配空间,然后使用 SerializeToArray()
将字节写入空间。
可以在以下位置找到有关这些方法和其他方法的完整文档:
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message_lite
关于C++ protobuf,ZMQ。客户端-服务器接口(interface)的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36180651/