protobuf 嵌套协议使用方法

本文介绍了如何使用Protobuf生成C++及C#代码,并通过案例展示了如何处理嵌套协议及图像数据的序列化与反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

protobuf 使用方法

protobuf 文件产生:

主要是使用windows .bat脚本。
对于C++来说:
“……\Tools\protoc.exe” –proto_path =.\Protocol –cpp_out=.\Protocol\protocol_common.proto

其中 protoc.exe 是产生C++ proto类文件的可执行程序, –proto_path 表示 产生的c++文件的存放路径。
–cpp_out 是指proto文件的地址

对于C#来说:
“……\Tools\ProtoGenerator\ProtoGen.exe” –proto_path=.\Protocol .\Protocol\protocol_common.proto
如果想在VS中自动执行这样的脚本,则还需要:增加(SolutionDir)(SolutionDir)..\..\Tools\ProtoGenerator\ProtoGen.exe" --proto_path=(SolutionDir)Protocol(SolutionDir)Protocol\protocol_common.proto

Case1: proto协议中嵌套使用

对于Protobuf产生的协议,网络上已经有很多文章介绍。但是,对于Proto协议中有嵌套的proto协议的使用,较少。
因此,记录下来。

Proto协议如下:
message MsgCommomAction
{
required int32 action_id = 1;
required string action_name = 2;
required bytes action_context = 3;
}

message MsgCommonDouble
{
required double value = 1;
}

其中,action_context 中的嵌套 MsgCommonDouble生成的string.
代码使用方法如下:

    MsgCommonDouble  msgCommonDouble;
    msgCommonDouble.set_value(dValue);
    int iSize = msgCommonDouble.ByteSize();
    sValue.resize(iSize);

    if ( ! msgCommonDouble.SerializeToArray(const_cast<char*>(sValue.data()), iSize) )
    {
        return false;
    }
 ```
 这样,就可以将double 类型的dValue 赋值给 msgCommonDouble类

##Case2: proto协议将将图像数据序列化 
Proto的协议如下
// Protocol of JPG Image 
message AppMsgResult
{
    required AppMsgResultImage      app_msg_result_image = 1;          
}
message AppMsgResultImage
{   
    // One Image and its cell id
    message MsgOneImage
    {
        required int32   image_cell_id = 1;
        required bytes   image_content = 2; 
    }

    required int32         image_number = 1 [default=0]; 
    repeated MsgOneImage   image_result_array = 2;        // JPG image Data array   
}

将图像的数据代码序列化
```C++
        // ImgResult 为数据与数据长度的结构体
        AppMsgResult            AppResult;
        AppMsgResultImage       AppImage;
        AppImage.set_image_number(ImgResultArray.size());
        for(int iIndex = 0; iIndex < ImgResultArray.size(); iIndex ++ )
        {
            ImgResult temp = ImgResultArray[iIndex];
            AppMsgResultImage_MsgOneImage   JpgImage;

            JpgImage.set_image_cell_id(temp._cellID);
            JpgImage.set_image_content(temp._data, temp._len);

            AppMsgResultImage_MsgOneImage* pJpgImage
                = AppImage.add_image_result_array();
            * pJpgImage = JpgImage;
        }

        // std::string ResultString = AppImage.SerializeAsString();




<div class="se-preview-section-delimiter"></div>

将图像数据从Proto协议中反序列化

    std::string sOutputFilePath = "D:\\"; 
    AppMsgResultImage   AppImageDe;
    AppImageDe.ParseFromString(ResultString);
    int iNumber = AppImageDe.image_number();

    int iIndex = 0;
    std::string sImageType(".JPG");
    for(iIndex = 0; iIndex < iNumber; iIndex ++)
    {
        AppMsgResultImage_MsgOneImage OneImageData = AppImageDe.image_result_array(iIndex);
        std::string sPath = sOutputFilePath;
        int iCellId = OneImageData.image_cell_id();
        printf( "CellID %d", iCellId );

        std::string sImageContent = OneImageData.image_content();

        std::stringstream ss;
        ss << iCellId << sImageType;
        sPath.append(ss.str());
        WriteJPEGSimple(sPath, sImageContent);  // 此方法是将数据保存为JPEG 文件
    }

以上基本上就是对于嵌套使用 proto 协议的总结

Protocol Buffers,简称protobuf,是一种轻便高效的数据交换格式,类似于XML和JSON,但是protobuf更加小巧、快速、简单,同时也支持多种编程语言。protobuf主要包括三个部分:语言无关的格式定义语言、编译器和库。 protobuf的格式定义语言类似于XML Schema和JSON Schema,用于定义数据结构和消息格式。protobuf使用类似于IDL的语法来定义数据结构,可以定义消息类型、字段等,同时还支持嵌套和继承等特性。 protobuf的编译器可以将格式定义语言编译成多种编程语言的代码,包括C++、Java、Python、Go等等,这样我们就可以在不同的编程语言之间进行数据交换,而不必担心数据格式的兼容性问题。 protobuf的库提供了序列化和反序列化消息的方法,同时也提供了其他一些辅助方法,例如解析消息、验证消息等。 protobuf的优点包括: 1. 轻便高效:protobuf使用二进制编码,相比于XML和JSON,它更加小巧、快速、简单。 2. 多语言支持:protobuf支持多种编程语言,可以方便地进行跨语言数据交换。 3. 可扩展性:protobuf支持消息的嵌套和继承,可以方便地进行数据结构的扩展和修改。 4. 兼容性:protobuf支持数据格式的向前和向后兼容,可以方便地进行升级和降级。 5. 安全性:protobuf支持消息的加密和签名,可以保证数据的安全性。 总之,protobuf是一种优秀的数据交换格式,可以帮助我们解决跨语言数据交换的问题,同时还具有轻便高效、多语言支持、可扩展性、兼容性和安全性等优点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值