First, what is Protobuf
Protocolbuffer (PB) is a format for Google's data interchange, which is independent of the language and is platform independent. It is a kind of interactive format similar to XML, JSON and other similar functions. Because it is a binary format, it is much faster than using XML for data exchange.
Google offers implementations in multiple languages: Java, C #, C + +, go and Python, each of which contains compilers and library files for the appropriate language. It can be used in data communication between distributed applications or in heterogeneous environments.
As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas.
With the emergence of the mobile market and its own advantages, PB is the most kind of data exchange format completely fire.
Second, the PROTOBUF version
PB has three versions:
First, Google official version: Https://github.com/google/protobuf/tree/master/csharp (Google official development, more obscure, the main library name: Google.ProtoBuf.dll)
Two. NET Community version: Https://github.com/mgravell/protobuf-net (. NET community enthusiasts, writing more in line with the grammar habits on. NET, Main Library name: Protobuf-net.dll)
Three. NET Community version (ii): https://github.com/jskeet/ Protobuf-csharp-port (said to be a. NET development by Google's. NET staff, developed at the time of the official absence of CSharp, and maintained when the blog was sent, the main library name: Google.ProtocolBuffers.dll)
As for the choice of that version, cross-platform demand, you can use version two, big words can choose one or three. (for example, the second choice of this article)
Iii. What is the use of protobuf?
(1) Making network communication protocol.
(2) data storage (serialization and deserialization), similar to XML, JSON, etc.;
Let's look first (1). PB can be used in one language (the author abbreviation for the PB language source code, the file suffix is. proto) into a communication class (. cs file, other languages generate the corresponding suffix file, such as. h,.cpp). In this procedure, you need to generate the. bin with the Protoc.exe command-line tool, and then use
ProtoGen.exe generate. cs files, as for how to invoke the command line, here is no longer to repeat. It is possible to put these three files (some may need ProtoGen.exe.config) into a directory, and then write a command-line file. Bat is generated directly.
Echo on Set Path=protogen\protogen.exe%Path% -i:message.proto -o:openapimodel\message.cspause
Note: Different versions of the command line are not the same. (If you have trouble, you can hand the agreement, I do so, but pay attention to each modifier field, but the hand is only applicable to version two, because the other way of writing is not the same, the generation of. cs files need to generate some methods at the same time)
One of my proto files
Message message{RequiredstringMessagetype=1;//message type, contract link for establishing a connection message, command for a message, file for files, filetoclient for files to the clientOptionalstringsourceip=2; Optionalstringsourceport=3; OptionalstringContent=4;//Message ContentOptional STIRNG filename=5; Optional bytes bytes=9999;//file ArrayOptionalstringother =6;//reservedOptional Int32 filetotallength =7;//Total file LengthOptional Int32 FileIndex =8;//Subscript of the current transferOptional Int32 filetotalcount =9;//total number of file splits}
My own hand-over files
usingProtobuf;usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Linq;usingSystem.Text;namespaceandroidsockettest{[Protocontract] Public classMessage {[Protomember (1)] Public stringMessageType {Get;Set; } [Protomember (2)] Public stringsourceIp {Get;Set; } [Protomember (3)] Public stringSourceport {Get;Set; } [Protomember (4)] Public stringContent {Get;Set; }//Message Content[Protomember (5)] Public stringFileName {Get;Set; } Private byte[] _bytes =NULL; [Global::P Rotobuf.protomember (9999, isrequired =false, Name =@"bytes", DataFormat =Global::P RotoBuf.DataFormat.Default)] [Global:: System.ComponentModel.DefaultValue (NULL)] Public byte[] bytes {Get{return_bytes;} Set{_bytes =value;} } [Protomember (6)] Public stringOther {Get;Set; }//reserved[Protomember (7)] Public intFiletotallength {Get;Set; }//Total file Length[Protomember (8)] Public intFileIndex {Get;Set; }//file Current number of segments[Protomember (9)] Public intfiletotalcount{Get;Set; }//total pieces of documents } Public Static classMessagehelp {//methods for serializing messages into binary//< param name= "model" > Objects to serialize </param> Public Static byte[] Serialize (Message model) {Try { //involves a format conversion, requires a stream, and serializes a binary into a stream using(MemoryStream ms =NewMemoryStream ()) { //serialization method using the Protobuf toolProtobuf.serializer.serialize<message>(MS, model); //define a binary array, save the serialized result byte[] result =New byte[Ms. Length]; //set the position of the stream to 0, starting pointMs. Position =0; //reads the contents of a stream into a binary arrayMs. Read (Result,0, result. Length); returnresult; } } Catch(Exception ex) {return NULL; } } //Deserializes a received message into an object//< returns>the serialize.</returns>//< param name= "MSG" > Received message .</param> Public StaticMessage Deserialize (byte[] msg) { Try { using(MemoryStream ms =NewMemoryStream ()) { //writes a message to the streamMs. Write (MSG,0, Msg. Length); //return the position of the stream to 0Ms. Position =0; //deserializing objects using toolsMessage result = protobuf.serializer.deserialize<message>(MS); returnresult; } } Catch(Exception ex) {return NULL; } } }}
Even the following serialization and deserialization are all written.
Iv. Proto Syntax, citing a peer blog
http://blog.csdn.net/u014308482/article/details/52958148
. NET Environment Call Protobuf