protobuf的C简单的代码例子(四)
2016-05-07 09:17
375 查看
构造 Repeated Submessages
构造一个 emessage.proto 文件
message Submessage
{
required int32 value=1;
}
message EMessage
{
repeated Submessage a=1;
}
下面给出打包代码:
#include<stdio.h>
#include<stdlib.h>
#include"dmessage.pb-c.h"
int main
(int argc,constchar*
argv[])
{
Submessage**subs;
void*buf;
unsigned len;
subs = malloc
(sizeof(Submessage*)*(argc-1));
for(i
=1; i
< argc; i++)
{
subs[i-1]=
malloc (sizeof(Submessage));
submessage__init (subs[i-1]);
subs[i-1]->value
= atoi(argv[i]);
}
msg.n_a
= argc-1;
msg.a
= subs;
len = emessage__get_packed_size
(&msg);// This is the calculated packing length
buf = malloc
(len);
// Allocate memory
emessage__pack (&msg, buf);
// Pack msg, including submessages
fprintf(stderr,"Writing %d serializedbytes\n",len);//
See the length of message
fwrite (buf, len,1,
stdout);
// Write to stdout to allow direct command line piping
free(buf);// Free the allocated serialized buffer
for(i
=1; i
< argc; i++)
free (subs[i]);
free (subs);
return0;
}
值得注意的是重复域创建了两个域,n_a submessagesd 的个数,另一个是submessages
自己。(理解这句话需要阅读.proto文件生成的源代码)
下面给出解包的源代码
#include <stdio.h>
#include"emessage.pb-c.h"
#define MAX_MSG_SIZE
4096
int main
(int argc,constchar*
argv[])
{
EMessage*msg;
char c;int i=0;
uint8_tbuf[MAX_MSG_SIZE];// Input data container for bytes
while(fread(&c,1,1,stdin)!=0)
{
if(i
>= MAX_MSG_SIZE)
{
fprintf(stderr,"message too long for allocatedbuffer\n");
return1;
}
buf[i++]= c;
}
msg = emessage__unpack(NULL,i,buf);//
Deserialize the serialized input
if(msg
== NULL)
{
fprintf(stderr,"error unpacking incomingmessage\n");
return1;
}
for(i
=0; i
< msg->n_a; i++)
printf ("%d\n", msg->a[i]->value);
emessage__free_unpacked(msg,NULL);
return0;
}
命令行运行:
./emessage_serialize
45|./emessage_deserialize
Writing:8 serialized bytes
4
5
./emessage_serialize
458|./emessage_deserialize
4
5
8
构造一个 emessage.proto 文件
message Submessage
{
required int32 value=1;
}
message EMessage
{
repeated Submessage a=1;
}
下面给出打包代码:
#include<stdio.h>
#include<stdlib.h>
#include"dmessage.pb-c.h"
int main
(int argc,constchar*
argv[])
{
Submessage**subs;
void*buf;
unsigned len;
subs = malloc
(sizeof(Submessage*)*(argc-1));
for(i
=1; i
< argc; i++)
{
subs[i-1]=
malloc (sizeof(Submessage));
submessage__init (subs[i-1]);
subs[i-1]->value
= atoi(argv[i]);
}
msg.n_a
= argc-1;
msg.a
= subs;
len = emessage__get_packed_size
(&msg);// This is the calculated packing length
buf = malloc
(len);
// Allocate memory
emessage__pack (&msg, buf);
// Pack msg, including submessages
fprintf(stderr,"Writing %d serializedbytes\n",len);//
See the length of message
fwrite (buf, len,1,
stdout);
// Write to stdout to allow direct command line piping
free(buf);// Free the allocated serialized buffer
for(i
=1; i
< argc; i++)
free (subs[i]);
free (subs);
return0;
}
值得注意的是重复域创建了两个域,n_a submessagesd 的个数,另一个是submessages
自己。(理解这句话需要阅读.proto文件生成的源代码)
下面给出解包的源代码
#include <stdio.h>
#include"emessage.pb-c.h"
#define MAX_MSG_SIZE
4096
int main
(int argc,constchar*
argv[])
{
EMessage*msg;
char c;int i=0;
uint8_tbuf[MAX_MSG_SIZE];// Input data container for bytes
while(fread(&c,1,1,stdin)!=0)
{
if(i
>= MAX_MSG_SIZE)
{
fprintf(stderr,"message too long for allocatedbuffer\n");
return1;
}
buf[i++]= c;
}
msg = emessage__unpack(NULL,i,buf);//
Deserialize the serialized input
if(msg
== NULL)
{
fprintf(stderr,"error unpacking incomingmessage\n");
return1;
}
for(i
=0; i
< msg->n_a; i++)
printf ("%d\n", msg->a[i]->value);
emessage__free_unpacked(msg,NULL);
return0;
}
命令行运行:
./emessage_serialize
45|./emessage_deserialize
Writing:8 serialized bytes
4
5
./emessage_serialize
458|./emessage_deserialize
4
5
8
相关文章推荐
- Linux C函数参考手册(PDF版)
- C# partial关键字说明
- Lua教程(十七):C API简介
- 简单谈谈lua和c的交互
- C#中的委托数据类型简介
- C#编写的艺术字类实例代码
- C#实现打造气泡屏幕保护效果
- 举例讲解C#编程中委托的实例化使用
- 使用C#代码获取存储过程返回值
- C++中explict关键字用法
- C/C++数据对齐详细解析
- 利用C语言来求最大连续子序列乘积的方法
- 字符串的组合算法问题的C语言实现攻略
- C 语言基础教程(我的C之旅开始了)[三]
- 学习C和C++的9点经验总结
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- 最大子矩阵问题实例解析
- C字符串操作函数实现方法小结