Protocol buffers (Google-specific communications protocol)

Source: Internet
Author: User

turn from: Http://www.cellopoint.com/cn/media_resources/blogs/2011/05/Protocol_Buffers

Introduction Introduction
The Protocol buffers is a flexible, efficient, automated mechanism (possibly coding) tool for serializing structural data.
Like XML, but smaller, faster, and simpler. You define your structured data, and then you can use the special code generated by the tool to easily use various languages (c + + python java)
Read and write your structured data from a variety of data streams. You can even renew your data structure (hot deployment) without interrupting a program that has already been deployed.

Why not just use XML? Why not XML?
The protocol buffer has many advantages that XML does not have:
1. Simple;
2. Small: 3-10 times
3. High efficiency: 20-100 times
4. Matchless meaning
5. Automatic tool generation

How do they work?
You just need to install Google's protocol buffer
Edit. Proto File
Perform protoc–cpp_out=. File name


Example: Example 1
Package tutorial;
Message person {
Required String name = 1;
Required Int32 id = 2;
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 phone = 4;
}
Message AddressBook {
Repeated person person = 1;
}

Three type three data types
Required: Field values must be supplied, otherwise the corresponding message will be considered "uninitialized." “
Optional: Either the field value is specified or not. If you do not specify an optional field value, it uses the default value.
Repeated: The field repeats n times (can be 0). The order of the duplicate values will be saved in the buffer in the protocol.

Required is permanent: you should be particularly careful when labeling a field as Required.

start working protoc-i= $SRC _dir--cpp_out= $DST _dir $SRC _dir/addressbook.proto

This will produce the following files
Addressbook.pb.h: Declares the header file of the class you are generating.
addressbook.pb.cc: The actual file of the class you generated.

Example: Example 2
Person of person;
Person.set_name ("John Doe");
PERSON.SET_ID (1234);
Person.set_email ("jdoe@example.com");
FStream output ("MyFile", Ios::out | ios::binary);
Person. Serializetoostream (&output);

FStream input ("myfile", Ios::in | ios::binary);
Person of person;
Person. Parsefromistream (&input);
cout << "Name:" << person.name () << Endl;
cout << "e-mail:" << person.email () << Endl;

Entire message, including: internal API
BOOL IsInitialized () Const: Checks whether all required fields are set (set) values.
String debugstring () const;: Returns a readable message representation that is particularly useful for reading.
void CopyFrom (const person& from);: Overwrite the value inside the caller's message with the value of the external message.
void Clear (): Returns all items to the empty state (empty).
BOOL Serializetostring (string* output) const;: Serializes the message and stores it in the specified string. Note that the contents are binary; we just use strings as a convenient container.
BOOL Parsefromstring (const string& data): Parsing a message from a given string.
BOOL Serializetoostream (ostream* output) const;: Writes the message to the given C + + Ostream.
BOOL Parsefromistream (istream* Input): Parsing the message from the given C + + istream.

Http://code.google.com/intl/zh-TW/apis/protocolbuffers/docs/overview.html
Http://www.cppprog.com/2010/0908/207_4.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.