1. 程式人生 > >ProtoBuf練習(三)

ProtoBuf練習(三)

mes 練習 from esp lose serialize networker errors 檢測

任意類型

protobuf語言的任意字段類型相當於Boost庫的boost::any類型數據,google.protobuf.Any是對protobuf語言的message進行封裝,所以需要使用message來封裝任意類型的數據,而不能像boost::any一樣直接使用基礎數據類型

工程目錄結構

$ ls proto/
ErrorStatus.proto

proto文件

$ cat proto/ErrorStatus.proto
syntax = "proto3";

import "google/protobuf/any.proto";

message NetworkErrorDetails
{
    sint32 err_no = 1;
}

message IoErrorDetails
{
    sint32 err_no = 1;
}

message ErrorStatus {
  string message = 1;
  repeated google.protobuf.Any details = 2;
}

讀寫源文件

$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "ErrorStatus.pb.h"

using namespace std;

int main(int argc, char *argv[])
{
    fstream input("./log", ios::in | ios::binary);
    cout << "Deserialize start." << endl;

    ErrorStatus status;
    if (!status.ParseFromIstream(&input))
    {
        cout << "Deserialize failed." << endl;
        return -1;
    }
    cout << status.message() << endl;
    for (const google::protobuf::Any& detail : status.details())
    {
        // 檢測Any類型具體對應的消息類型
        if (detail.Is<IoErrorDetails>())
        {
            cout << "IoErrorStatus:" << endl;
            IoErrorDetails io_error;
            if (detail.UnpackTo(&io_error))
                io_error.PrintDebugString();
            else
                cout << "Parse fails." << endl;
        }

        if (detail.Is<NetworkErrorDetails>())
        {
            cout << "NetworkErrorDetails:" << endl;
            NetworkErrorDetails network_error;
            if (detail.UnpackTo(&network_error))
                network_error.PrintDebugString();
            else
                cout << "Parse fails." << endl;
        }

    }

    cout << "Deserialize end." << endl;
    input.close();
    return 0;
}

$ cat writer.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "ErrorStatus.pb.h"

using namespace std;

int main(int argc, char *argv[])
{
    IoErrorDetails details;
    details.set_err_no(1);

    ErrorStatus status;
    // 自動生成對象,並打包消息
    status.add_details()->PackFrom(details);
    status.set_message("File read operation");

    fstream output("./log", ios::out | ios::trunc | ios::binary);
    cout << "Serialize start." << endl;
    if (!status.SerializeToOstream(&output))
    {
        cout << "Serialize failed." << endl;
         return -1;
    }
    output.close();
    cout << "Serialize end." << endl;
    return 0;
}

ProtoBuf練習(三)