PROTOBUF full Name protocol buffers, is a Google launched an efficient, fast data interchange format, and Xml,thrift, as is a data exchange protocol (of course, thrift also provides RPC functionality). Protobuf relative to XML structured text data format, it is a binary data format, with higher transmission, packaging and packet efficiency, which is why PROTOBUF is very popular reasons.
Protobuf through its own compiler, the protocol file to compile, generate the corresponding language code, convenient data packaging and reconciliation package. Currently, Google offers implementations of three languages: Java, C + +, and Python, each of which contains compilers for the language and library files.
The following describes the syntax of Protobuf, where Protobuf IDL is a file that is saved as *.proto, and the data types in proto files can be grouped into two broad categories : Composite data types and standard data types. composite data types include: enumerations and message types , and standard data types include: integer, Floating-point, string, and so on, which are described in detail later. Message
The most commonly used data format is the message, for example, an order data can be expressed as follows:
Message order
{
required UInt64 uid = 1;
Required float = 2;
Optional string tag = 3;
}
It is compiled into C + + code through PROTOBUF, which generates corresponding Xxx.pb.h and XXX.pb.cc. The message corresponds to a class, which holds the corresponding data members, the functions that deal with it, and the corresponding package and reconciliation functions.
Class Order:public:: Google::p rotobuf::message {public
:
...
Accessors-------------------------------------------------------
...
:: Google::p rotobuf::uint64 uid_
:: std::string* tag_;
float cost_;
};
What you need to know in the message data format:
1. Tag at the end of each field: The tag is used to mark the field in the serialized binary data, and the tag for each field is unique within the message. Can not be changed, otherwise the data will not be able to solve the correct package.
2. Previous modifiers for data types:
Required: Must be assigned a value, cannot be empty, otherwise the message will be considered as "uninitialized". Build a "uninitialized" message throws a RuntimeException exception, parsing a "uninitialized" "message" throws a IOException exception. In addition, the "required" field does not differ from the "optional" field. Optional: A field can be assigned or not assigned a value. If no value is assigned, the default value is assigned. Repeated: This field can be repeated any number of times, including 0 times. The order of the duplicate data will be stored in protocol buffer, imagining the field as an array that can automatically set the size. Enumeration
Enumeration types in enumerations and C++,java are a meaning:
Enum Corpus {
universal = 0;
WEB = 1;
IMAGES = 2;
local = 3;
NEWS = 4;
Products = 5;
Video = 6;
}
Perform Protoc--cpp_out=. Enum_test.proto, the following C + + code will be generated
Enum Corpus {
Universal = 0,
WEB = 1,
IMAGES = 2, local
= 3,
NEWS = 4, Products
= 5,
VID EO = 6
};
Basic data Types
The basic data types supported by PROTOBUF are shown below:
Message Detailed:
The message data format is protobuf automatically compiled in C + + including content:
Xxx.proto message Order {Required UInt64 uid = 1;
Required float = 2;
Optional string tag = 3; //xxx.pb.h <pre name= "code" class= "CPP" >class:: Google::p order:public {public: ...//AC
Cessors-------------------------------------------------------//Required UInt64 uid = 1;
inline BOOL Has_uid () const;
inline void clear_uid ();
static const int kuidfieldnumber = 1;
Inline:: Google::p rotobuf::uint64 uid () const;
inline void Set_uid (:: Google::p rotobuf::uint64 value);
//required float cost = 2;
inline BOOL Has_cost () const;
inline void clear_cost ();
static const int kcostfieldnumber = 2;
Inline float cost () const;
inline void Set_cost (float value);
//Optional String tag = 3;
inline BOOL Has_tag () const;
inline void Clear_tag ();
static const int ktagfieldnumber = 3;
Inline Const::std::string& tag () const; INLine void Set_tag (const::std::string& value);
inline void Set_tag (const char* value);
inline void Set_tag (const char* value, size_t size);
Inline:: std::string* mutable_tag ();
Inline:: std::string* release_tag ();
inline void Set_allocated_tag (:: std::string* tag);
//@ @protoc_insertion_point (Class_scope:order) private: inline void set_has_uid ();
inline void clear_has_uid ();
inline void set_has_cost ();
inline void clear_has_cost ();
inline void Set_has_tag ();
inline void Clear_has_tag ();
:: Google::p rotobuf::uint32 _has_bits_[1];
:: Google::p rotobuf::uint64 uid_;
:: std::string* tag_;
FLOAT Cost_; };
For each message the data member,protobuf automatically generates the associated handler function, for each field the main processing functions are: Has_uid (), Clear_uid (), UID (), Set_uid (), They are used to determine whether the field is set, clear the field settings record, get the field, and set the field. For the UID field in the example, the corresponding function is implemented as follows:
Xxx.pb.h
//Required UInt64 uid = 1;
inline bool Order::has_uid () const {return
(_has_bits_[0] & 0x00000001u)!= 0;
}
inline void Order::set_has_uid () {
_has_bits_[0] |= 0x00000001u;
}
inline void Order::clear_has_uid () {
_has_bits_[0] &= ~0x00000001u;
}
inline void Order::clear_uid () {
uid_ = google_ulonglong (0);
Clear_has_uid ();
}
Inline:: Google::p rotobuf::uint64 order::uid () const {
//@ @protoc_insertion_point (field_get:Order.uid)
return uid_;
}
inline void Order::set_uid (:: Google::p rotobuf::uint64 value) {
set_has_uid ();
Uid_ = value;
@ @protoc_insertion_point (field_set:Order.uid)
}
By the implementation code, the code is _HAS_BITS_ to mark whether the field has been set, _HAS_BITS_ is defined as follows:
:: Google::p rotobuf::uint32 _has_bits_[1];
The _has_bits_ bits are used to express whether each field is set. respectively through 0x01, 0x02, 0x04 ... To mark 1,2,3 separately, and whether each field has been set.
For PROTOBUF, the interface that serializes protocol data into binary data is as follows:
Serialization---------------------------------------------------//Methods for serializing in protocol buffer forma
T. Most of these//are just simple wrappers around bytesize () and serializewithcachedsizes (). Write a protocol buffer to the given output. Returns//False on a write error.
IF the message is missing required fields,//it may google_check-fail.
BOOL Serializetocodedstream (io::codedoutputstream* output) const;
Like Serializetocodedstream (), but allows missing required fields.
BOOL Serializepartialtocodedstream (io::codedoutputstream* output) const; //Write The message to the given zero-copy output stream.
All required//fields must is set.
BOOL Serializetozerocopystream (io::zerocopyoutputstream* output) const;
BOOL Serializepartialtozerocopystream (io::zerocopyoutputstream* output) const; //Serialize the message and store it in the given string.
All required//fields must is set. BOOL SerializeToString (string* output) const;
BOOL Serializepartialtostring (string* output) const; //Serialize the message and store it in the given byte array.
All required//fields must is set.
BOOL Serializetoarray (void* data, int size) const;
BOOL Serializepartialtoarray (void* data, int size) const;
String serializeasstring () const;
String serializepartialasstring () const; Like Serializetostring (), but appends to the "the" string ' s existing//contents.
All required fields must is set.
BOOL Appendtostring (string* output) const;
BOOL Appendpartialtostring (string* output) const;
Serialize the message and write it to the given file descriptor. all //required fields must is set.
BOOL Serializetofiledescriptor (int file_descriptor) const;
BOOL Serializepartialtofiledescriptor (int file_descriptor) const;
//Serialize the message and write it to the given C + + ostream. all //required fields must is set. &nbSp
BOOL Serializetoostream (ostream* output) const;
BOOL Serializepartialtoostream (ostream* output) const;