Qt +原型缓冲区,类型?

发布于 2024-10-04 10:02:58 字数 188 浏览 1 评论 0原文

我想在 Qt 开发中深入研究 Google 的协议缓冲区,但我无法弄清楚如何最好地合并它们。

最终,我想使用协议缓冲区通过 QUdpSocketQTcpSocket 发送。

在协议缓冲区消息和通过套接字 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒岛晴空 2024-10-11 10:02:58

从 protobuf 对象创建 QByteArray

Person person; // a protobuf object
person.set_id(123);
person.set_name("Bob");
person.set_email("[email protected]");

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());
sendSerializedPersonOverQTcpSocket(byteArray);

QByteArray 读回 protobuf 对象:

QByteArray byteArray = readSerializedPersonFromQTcpSocket();
Person person;
if (!person.ParseFromArray(byteArray, byteArray.size())) {
  std::cerr << "Failed to parse person.pb." << std::endl;
}

Creating a QByteArray from a protobuf object:

Person person; // a protobuf object
person.set_id(123);
person.set_name("Bob");
person.set_email("[email protected]");

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());
sendSerializedPersonOverQTcpSocket(byteArray);

Reading back a protobuf object from a QByteArray:

QByteArray byteArray = readSerializedPersonFromQTcpSocket();
Person person;
if (!person.ParseFromArray(byteArray, byteArray.size())) {
  std::cerr << "Failed to parse person.pb." << std::endl;
}
彩扇题诗 2024-10-11 10:02:58

而不是:

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());

你也可以写:

QByteArray byteArray(person.SerializeAsString().c_str());

编辑:以上两个给出相同的结果,但我不确定它是否正确。这个似乎工作得更好:

QByteArray byteArray(QString::fromStdString(person.SerializeAsString()));

编辑2:好的,现在我知道它是如何工作的:如果序列化中有 \0 字符,前两种方法是错误的 - 之后的所有内容都会丢失。要纠正它,可以写:

QByteArray byteArray(person.SerializeAsString().c_str(), person.ByteSize());

Instead of:

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());

you can also write:

QByteArray byteArray(person.SerializeAsString().c_str());

EDIT: Above two gives the same result, but I'm not sure wether it's correct. This one seems to work better:

QByteArray byteArray(QString::fromStdString(person.SerializeAsString()));

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:

QByteArray byteArray(person.SerializeAsString().c_str(), person.ByteSize());
枫以 2024-10-11 10:02:58

使用下面的代码确实很危险

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());
sendSerializedPersonOverQTcpSocket(byteArray);

您可以在这里找到一个很好的解释 在 protobuf-c 中,可选的 uint32 变量的值可以为 0

从 protobuf 消息创建 QByteArray 的正确方法是

QByteArray byteArray;
byteArray.resize(message.ByteSize());
message.SerializeToArray(byteArray.data(), byteArray.size());

Using the code below is really dangerous

std::ostringstream out;
person.SerializeToOstream(&out);
QByteArray byteArray(out.str().c_str());
sendSerializedPersonOverQTcpSocket(byteArray);

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

QByteArray byteArray;
byteArray.resize(message.ByteSize());
message.SerializeToArray(byteArray.data(), byteArray.size());
橘香 2024-10-11 10:02:58

@James:您可以使用 ParseFromArray(),例如,如下所示:(请注意,ParseFromArray() 仅在 libs 的 proto-buf-lite 版本上可用)。

void convertQByteArrayToUser(QByteArray& aByteArray)
{
    com::your::name_space::User user;
    if(!user.ParseFromArray(aByteArray.data(), aByteArray.size())) 
    {
        //could not parse
    }
    else { //yayyyyy            
        if(user.has_userid())
        {
            //...
        }
    }
}

@James: You can use ParseFromArray(), for example, as below: (Please note that ParseFromArray() is available only on proto-buf-lite versions of the libs).

void convertQByteArrayToUser(QByteArray& aByteArray)
{
    com::your::name_space::User user;
    if(!user.ParseFromArray(aByteArray.data(), aByteArray.size())) 
    {
        //could not parse
    }
    else { //yayyyyy            
        if(user.has_userid())
        {
            //...
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文