Check on the Internet, although there are many articles introduced protocol Buffer, but the actual use, or will encounter a lot of problems, so I think there should be a guide to the same thing, so that the novice will be able to use it soon.
Protocol buffer shorthand is protobuf, which is a way for Google to store data, with the same functionality as XML, but more convenient, with smaller data volumes and faster speeds, with a great advantage when serializing and deserializing. For example, the communication protocol of the network game is written. More importantly, it is an open source project.
Gossip not much to say:
Http://code.google.com/p/protobuf/downloads/list
Since protobuf cannot generate C # code, it is necessary to use another open source project protobuf-net:
http://code.google.com/p/protobuf-net/
@echo off
rem Find File
for/f "delims="%%i in (' dir/b ". \*.proto" ') do echo%%i
for/f "delims="%%i in (' dir/b/a "*.proto" ') do protoc-i=. --cpp_out=. %%i
for/f "delims="%%i in (' dir/b/a "*.proto" ') do Protogen-i:%%i-o:%%~ni.cs
Pause
Use Protoc.exe to generate a. cpp file from the command line using the. proto file that you define yourself.
Using the Protobuf-net Protogen.exe to generate a. cs file, you can write a batch that reads as follows:
Protoc-i=. --cpp_out=. Net.proto//In the current directory, the CPP is compiled Net.proto
Protogen-i:net.proto-o:net.cs//In the current directory, generate Net.cs with Net.proto
In fact, the two functions are the same, just generate the Net.proto CPP file and CS file separately, so that you can communicate between applications written in two languages.
The contents of the Net.proto file are as follows:
- Message SearchRequest {
- Required String query = 1;
- Optional Int32 page_number = 2;
- Optional Int32 result_per_page = 3;
- }
The data types are as follows:
. Proto Type |
Notes |
C + + Type |
Java Type |
Double |
|
Double |
Double |
Float |
|
Float |
Float |
Int32 |
Uses variable-length encoding. Inefficient for encoding negative numbers–if your field was likely to has negative values, use Sint32 instead. |
Int32 |
Int |
Int64 |
Uses variable-length encoding. Inefficient for encoding negative numbers–if your field was likely to has negative values, use Sint64 instead. |
Int64 |
Long |
UInt32 |
Uses variable-length encoding. |
UInt32 |
INT[1] |
UInt64 |
Uses variable-length encoding. |
UInt64 |
LONG[1] |
Sint32 |
Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. |
Int32 |
Int |
Sint64 |
Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. |
Int64 |
Long |
Fixed32 |
Always four bytes. More efficient than uint32 if values is often greater than 228. |
UInt32 |
INT[1] |
Fixed64 |
Always eight bytes. More efficient than UInt64 if values is often greater than 256. |
UInt64 |
LONG[1] |
Sfixed32 |
Always four bytes. |
Int32 |
Int |
Sfixed64 |
Always eight bytes. |
Int64 |
Long |
bool |
|
bool |
Boolean |
String |
A string must always contain UTF-8 encoded or 7-bit ASCII text. |
String |
String |
bytes |
may contain any arbitrary sequence of bytes. |
String |
ByteString |
http://blog.csdn.net/wangpei421/article/details/8012295
Google Protocol Buffer Usage C #