protobuf 嵌套协议使用方法

来源:互联网 发布:多益网络邮编地址 编辑:程序博客网 时间:2024/06/05 21:57

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 协议的总结

0 0