[英]C++ protobuf, ZMQ. Functions of client-server interface
While I doing program server-client on C++, using ZMQ and protobufs from Google, touched functions related with variance of types.当我在 C++ 上使用 ZMQ 和 Google 的 protobufs 编写服务器客户端程序时,触及了与类型变化相关的功能。
Problem: functions ParseFromString
and SerializeToString
use string type as parameter, but I need write program using zmq::message_t
such as a parameter.问题:函数
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
Program is a client-server execution using sockets Need to coordinate variables string type and message between each other.程序是使用 sockets 的客户端-服务器执行 需要协调变量之间的字符串类型和消息。 Functions ParseFromString() и SerializeToString().
函数 ParseFromString() 和 SerializeToString()。 В Python все просто: socket.send(reqt.SerializeToString()) и class_id=reqt.ParseFromString(socket.recv)
В Python все просто: socket.send(reqt.SerializeToString()) и class_id=reqt.ParseFromString(socket.recv)
You should use ParseFromArray()
: 你应该使用
ParseFromArray()
:
reqt.ParseFromArray(msg.data(), msg.size());
On the serializing side, use ByteSize()
to get the size of the protobuf message, allocate space for it, then SerializeToArray()
to write the bytes into the space. 在序列化方面,使用
ByteSize()
获取protobuf消息的大小,为其分配空间,然后使用SerializeToArray()
将字节写入该空间。
Full documentation on these methods and others can be found at: 有关这些方法和其他方法的完整文档,请访问:
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message_lite https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message_lite
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.