我正在尝试在c-project中使用protobuf-c来传输一些数据。“字符串”和“字节”数据类型的示例缺少here
有人能为这些提供一个小例子吗?我的问题是,我不知道如何为具有这些数据类型的消息分配内存,因为它们的大小在编译时是未知的。
发布于 2014-03-27 03:34:58
请查看以下链接:
https://groups.google.com/forum/#!topic/protobuf-c/4ZbQwqLvVdw
https://code.google.com/p/protobuf-c/wiki/libprotobuf_c (虚拟缓冲区)
基本上,ProtobufCBinaryData是一个结构,您可以访问它的文件,如第一个链接中所述。下面我还展示了一个小例子(类似于官方维基https://github.com/protobuf-c/protobuf-c/wiki/Examples上的例子)。
您的proto文件:
message Bmessage {
optional bytes value=1;
}现在假设您收到了这样的消息,并希望将其提取出来:
BMessage *msg;
// Read packed message from standard-input.
uint8_t buf[MAX_MSG_SIZE];
size_t msg_len = read_buffer (MAX_MSG_SIZE, buf);
// Unpack the message using protobuf-c.
msg = bmessage__unpack(NULL, msg_len, buf);
if (msg == NULL) {
fprintf(stderr, "error unpacking incoming message\n");
exit(1);
}
// Display field's size and content
if (msg->has_value) {
printf("length of value: %d\n", msg->value.len);
printf("content of value: %s\n", msg->value.data);
}
// Free the unpacked message
bmessage__free_unpacked(msg, NULL);希望这能有所帮助。
发布于 2018-02-12 18:05:43
下面是一个使用字节构建消息的示例:
message MacAddress {
bytes b = 1; // 6 bytes size
}和C代码:
uint8_t mac[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
MacAddress ma = MAC_ADDRESS__INIT;
ma.b.data = mac;
ma.b.len = 6;https://stackoverflow.com/questions/18966233
复制相似问题