1. 程式人生 > >Google protobuf訊息巢狀c++實現

Google protobuf訊息巢狀c++實現

protobuf3.1.0的安裝見:https://blog.csdn.net/mircosheng/article/details/70141704

安裝完protobuf後,新建.proto檔案,本文命名為lm.helloworld. proto

在網路通訊系統中,protobuf能夠提升通訊效率。訊息巢狀可以實現傳輸較為複雜的訊息。

內容如下:

syntax = "proto2";//這裡改成proto3編譯不通過,原因不明。有知道的麻煩告知一下。

package lm;
message helloworld {
 required string name = 1;
 required int32 id = 2;        // Unique ID number for this person. 
 optional string email = 3;
//列舉
 enum PhoneType {
   MOBILE = 0;
   HOME = 1;
   WORK = 2;
 }
//個人電話資訊
 message PhoneNumber {
   required string number = 1;
   optional PhoneType type = 2 [default = HOME];
 }
 repeated PhoneNumber phones = 4;//個人可擁有多個電話
}
//電話本
message AddressBook{
        repeated helloworld  people=1;//電話本包含多個人的電話資訊
}
~                                                                               
~                    

執行命令:protoc -I=./ --cpp_out=./   lm.helloworld.proto

生成兩個檔案:lm.helloworld.pb.h  lm.helloworld.pb.cc

寫端writer:

#include "lm.helloworld.pb.h"
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include <fstream>

using namespace std;

void add_message(lm::helloworld *person )
{
        person->set_name("bruvin");
        person->set_id(123);
        person->set_email("
[email protected]
"); char phone_number[6][12]={"13209812233","17832923210","90323221098",\ "13432820164","90887321234","0976321233"}; for(int i=0;i<6;i++){ lm::helloworld::PhoneNumber *msg2=person->add_phones(); msg2->set_number(phone_number[i]); msg2->set_type(lm::helloworld::MOBILE); } } int main(void) { lm::AddressBook addressbook; // Write the new address book back to disk. add_message(addressbook.add_people()); fstream output("./log", ios::out | ios::trunc | ios::binary); if (!addressbook.SerializeToOstream(&output)) { cerr << "Failed to write msg." << endl; return -1; } return 0; }

讀端reader:

#include "lm.helloworld.pb.h"
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include <fstream>

using namespace std;

void ListMsg(const lm::AddressBook &address_book) {
        for(int i=0;i<address_book.people_size();i++){//電話本有幾個人的的資訊
                const lm::helloworld& person = address_book.people(i);
                cout << person.name() << endl;
                cout << person.id() << endl;
                if(person.email()!=""){
                cout<<"E-mail address: "<<person.email()<<endl;
                }
                for(int j=0;j<person.phones_size();j++){//該人有幾個電話號碼
                        const lm::helloworld::PhoneNumber& phone_number = person.phones(j);
                        switch(phone_number.type()){
                                case lm::helloworld::MOBILE:
                                        cout<<"Mobile phone#:";
                                        break;
                                case lm::helloworld::HOME:
                                        cout<<"Home phone#:";
                                        break;
                                case lm::helloworld::WORK:
                                        cout<<"Work phone#:";
                                        break;
                        }
                        cout<<phone_number.number()<<endl;
                }
        }
 }
int main(int argc, char* argv[]) {
    lm::AddressBook address_book;
    fstream input("./log", ios::in | ios::binary);
    if (!address_book.ParseFromIstream(&input)) {
      cerr << "Failed to parse address book." << endl;
      return -1;
    }

    ListMsg(address_book);
}