简体   繁体   English

Google协议缓冲区-对编码解码base64 char * c字符串协议缓冲区数据感到困惑

[英]Google Protocol Buffers - Confused about encode decode base64 char * c string protocol buffer data

I am using Google Protocol Buffers to send a message to a server. 我正在使用Google协议缓冲区将消息发送到服务器。 My confusion comes in about how I send an image vs how I receive the image. 我对如何发送图像与如何接收图像感到困惑。 See code below for details but my question is: 有关详细信息,请参见下面的代码,但我的问题是:

Do I need to base64_decode the returned string that has never been base64 encoded, because it was sent using char * and size? 我是否需要base64_decode从未使用base64编码的返回字符串,因为它是使用char *和size发送的? Perhaps Google Protocol Buffers handles this but I can't find any evidence in the generated class. 也许Google Protocol Buffers可以解决这个问题,但是我在生成的类中找不到任何证据。

I may have found the answer here but I want to be sure. 我可能在这里找到了答案但是我想确定。


Here is the code to send the data. 这是发送数据的代码。 Clearly I am sending an unsigned char * and a const long unsigned int to set the image. 显然,我正在发送一个无符号char *和一个const long unsigned int来设置图像。

std::string message::myMessaging::generateMessage(unsigned char *imageData, const long unsigned int elemSize, long unsigned int *msgSize)
{
    ImageMessage message;
    message.set_ismaster(true);
    message.set_personname("Lucy");
    message.set_image(imageData, elemSize);

    string out;
    message.SerializeToString(&out);
    int size = message.ByteSize();
    memcpy(msgSize, &size, sizeof(int));
    return out;
}

Here is the code to receive the data. 这是接收数据的代码。 Coming back from the server I receive the messageData as a const string. 从服务器返回时,我以常量字符串形式接收messageData。 I create my Google Protocol Buffer message and parse it to get the information out of it. 我创建了我的Google Protocol Buffer消息并进行了解析,以获取其中的信息。 Here, my image is a string. 在这里,我的图像是一个字符串。

std::string message::myMessaging::parseMessage(const string messageData, long unsigned int *msgSize)
{
    ImageMessage message;
    message.Clear();
    message.ParseFromString(messageData);

    string personname = message.personname();
    string image = message.image();

    // do I need to decode it ?
    std::string decoded = base64_decode(image);

    // or can I just return the string image ?
    return decoded;
}

No, you do not need to base64 decode. 不,您不需要base64解码。 The image field should have type bytes in your .proto file, so you're allowed to put arbitrary bytes in it without encoding. image字段在.proto文件中应该具有类型bytes ,因此您可以在其中输入任意字节而无需编码。

Note that a C++ std::string is allowed to contain arbitrary bytes, not just text. 请注意,C ++ std::string允许包含任意字节,而不仅仅是文本。 Just make sure that you use image.size() and image.data() to access the contents. 只要确保您使用image.size()image.data()来访问内容即可。 Do not use image.c_str() as that is for text strings, and definitely do not use strlen() or any other C string function on the image data. 请勿将image.c_str()用作文本字符串,并且绝对不要在图像数据上使用strlen()或任何其他C字符串函数。

Also: 也:

 int size = message.ByteSize();

You can delete this line and just use out.size() instead. 您可以删除此行,而只需使用out.size() If you call ByteSize() , protobufs has to recompute the size of the whole message because it doesn't know if you've changed it. 如果调用ByteSize() ,则protobufs必须重新计算整个消息的大小,因为它不知道您是否已更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM