Google Protocol Buffer Usage C #

Source: Internet
Author: User

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:

    1. Message SearchRequest {
    2. Required String query = 1;
    3. Optional Int32 page_number = 2;
    4. Optional Int32 result_per_page = 3;
    5. }

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 #

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.