Qt +原型缓冲区,类型?
我想在 Qt 开发中深入研究 Google 的协议缓冲区,但我无法弄清楚如何最好地合并它们。
最终,我想使用协议缓冲区通过 QUdpSocket
和 QTcpSocket
发送。
在协议缓冲区消息和通过套接字 (QByteArray) 发送数据然后在另一端返回的最佳方法是什么?
I would like to dip into Google's protocol buffers in Qt development, but I am having trouble figuring out how to incorporate them best.
Ultimately, I want to send with QUdpSocket
and QTcpSocket
using protocol buffers.
What is the best method for going between a protocol buffer message
to sending the data over a socket (QByteArray
) and then back again at the other side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 protobuf 对象创建
QByteArray
:从
QByteArray
读回 protobuf 对象:Creating a
QByteArray
from a protobuf object:Reading back a protobuf object from a
QByteArray
:而不是:
你也可以写:
编辑:以上两个给出相同的结果,但我不确定它是否正确。这个似乎工作得更好:
编辑2:好的,现在我知道它是如何工作的:如果序列化中有 \0 字符,前两种方法是错误的 - 之后的所有内容都会丢失。要纠正它,可以写:
Instead of:
you can also write:
EDIT: Above two gives the same result, but I'm not sure wether it's correct. This one seems to work better:
EDIT2: OK, now I know how it works: first two ways are wrong if there are \0 char in serialization - everything after it it's then lost. To correct it one can write:
使用下面的代码确实很危险
您可以在这里找到一个很好的解释 在 protobuf-c 中,可选的 uint32 变量的值可以为 0
从 protobuf 消息创建 QByteArray 的正确方法是
Using the code below is really dangerous
You can find a good explanation here In protobuf-c, can optional uint32 variable have value 0
A right way to create a QByteArray from a protobuf message is
@James:您可以使用
ParseFromArray()
,例如,如下所示:(请注意,ParseFromArray()
仅在 libs 的 proto-buf-lite 版本上可用)。@James: You can use
ParseFromArray()
, for example, as below: (Please note thatParseFromArray()
is available only on proto-buf-lite versions of the libs).