protobuf的C简单的代码例子(二)

创建一个cmessage.proto 文件:

message CMessage
{
    repeated int32 c=1;
}

源代码生成的方法请参考  《protobuf的C简单的代码例子(一)》

下面给出打包代码:

#include<stdio.h>
#include<stdlib.h>
#include"cmessage.pb-c.h"

int main (int argc,constchar* argv[])
{
    CMessage msg = CMESSAGE__INIT;  // CMessage(repeated int32)
    void*buf;                     // Buffer to storeserialized data
    unsigned len,i;                // Length of serialized data
    msg.n_c = argc -1;                     // Save number ofrepeated int32
    msg.c = malloc (sizeof(int)* msg.n_c);// Allocate memoryto store int32
    for(i =0; i < msg.n_c; i++)
        msg.c[i]= atoi (argv[i+1]);        // Access msg.c[] as array
    len =cmessage__get_packed_size (&msg);  // This iscalculated packing length
    buf = malloc (len);                     // Allocaterequired serialized buffer length
    cmessage__pack (&msg, buf);             // Pack the data

    fprintf(stderr,"Writing %dserialized bytes\n",len);// See the lengthof message
    fwrite (buf, len,1, stdout);           // Write to stdout to allow direct command line piping

    free (msg.c);// Free storage forrepeated int32            
    free (buf);   // Free serializedbuffer
    return0;
}

注意

  • 使用 C_MESSAGE__INIT宏创建信息架构
  • the n_XXX member is generated for a repeated field XXX.
  • n_XXX 成员是通过复域XXX(可简单理解为类似数组的结构)产生的。

下面给出解包的代码

#include<stdio.h>
#include"cmessage.pb-c.h"
#define MAX_MSG_SIZE  4096

int main (int argc,constchar* argv[])
{
    CMessage*msg;
    uint8_t buf[MAX_MSG_SIZE];
    size_t msg_len = read_buffer (MAX_MSG_SIZE, buf);

    msg = cmessage__unpack (NULL, len, buf);// Deserialize theserialized input
    if(msg == NULL)
    {// Something failed
        fprintf(stderr,"errorunpacking incoming message\n");
        return1;
    }
   
    for(i =0; i < msg->n_c; i++)
    {// Iterate through all repeated int32
        if(i >0)
            printf (", ");
        printf ("%d", msg->c[i]);
    }
    printf ("\n");
   
    cmessage__free_unpacked(msg,NULL);// Free the messagefrom unpack()
    return0;
}


$ ./cmessage_serialize 12 3 4|./ cmessage_deserialize   --->管道命令
Writing:6 serialized bytes   ---->打印信息
12,3,4   ---->打印信息

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用protobuf-c,你可以按照以下步骤进行操作: 1. 首先,使用protoc-c工具编译你的.proto文件,生成对应的C代码文件(头文件和源文件)。使用命令:protoc-c --c_out=. yourfile.proto。 2. 然后,编写你的C程序来使用生成的C代码。在你的C程序中,包含生成的头文件,并使用相应的函数来序列化和反序列化数据。 3. 如果你想使用protobuf-c-rpc,可以参考以下步骤: 1. 首先,从GitHub上下载protobuf-c-rpc项目,并编译。使用命令:./autogen.sh && ./configure && make && make install(注意,最后一条命令可能需要使用sudo)。 2. 根据你的需求,在你的C程序中引入相关的头文件。 3. 根据你的.proto文件,使用protoc-c工具生成相应的C代码。 4. 编译你的C程序,链接protobuf-c-rpc库文件和protobuf-c库文件。使用命令:gcc yourfile.c generated.pb-c.c -o yourprogram -lprotobuf-c -lprotobuf-c-rpc。 4. 最后,运行你的C程序,测试你的protobuf-c代码。 请注意,上述步骤是一个基本的使用例子,具体的步骤可能会因为你的需求和环境而有所不同。但是,通过这些步骤,你应该能够使用protobuf-c来序列化和反序列化数据,并使用protobuf-c-rpc来实现RPC功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [protobuf-c之嵌入式平台使用](https://blog.csdn.net/weixin_38293850/article/details/121361877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [protobuf-c-rpc跑起来正确步骤](https://blog.csdn.net/u013863751/article/details/113531482)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值