谷歌协议缓冲工具。
pbtools的Python项目详细描述
安装
pipinstallpbtools
示例用法
C源代码
在本例中,我们使用简单的proto文件hello_world.proto。
syntax="proto3";packagehello_world;messageFoo{int32bar=1;}
接下来,我们从proto文件生成c源代码。
$ pbtools generate_c_source examples/hello_world/hello_world.proto Successfully generated hello_world.h and hello_world.c.
请参阅hello_world.h和hello_world.c了解 生成的文件。
我们将使用下面生成的类型和函数。
structhello_world_foo_t{structhello_world_heap_t*heap_p;int32_tbar;};structhello_world_foo_t*hello_world_foo_new(void*workspace_p,size_tsize);inthello_world_foo_encode(structhello_world_foo_t*self_p,void*encoded_p,size_tsize);inthello_world_foo_decode(structhello_world_foo_t*self_p,constuint8_t*encoded_p,size_tsize);
对foo消息进行编码和解码。
#include<string.h> #include<stdio.h> #include"hello_world.h"intmain(intargc,constchar*argv[]){intsize;uint8_tworkspace[64];uint8_tencoded[16];structhello_world_foo_t*foo_p;/* Encode. */foo_p=hello_world_foo_new(&workspace[0],sizeof(workspace));if(foo_p!=NULL){foo_p->bar=78;size=hello_world_foo_encode(foo_p,&encoded[0],sizeof(encoded));if(size>=0){printf("Successfully encoded Foo into %d bytes.\n",size);}}/* Decode. */foo_p=hello_world_foo_new(&workspace[0],sizeof(workspace));if(foo_p!=NULL){size=hello_world_foo_decode(foo_p,&encoded[0],sizeof(encoded));if(size>=0){printf("Successfully decoded %d bytes into Foo.\n",size);printf("Foo.bar: %d\n",foo_p->bar);}}return(0);}
有关此示例中使用的所有文件,请参见hello_world。