Thoughts on the last Socket-related project

Source: Internet
Author: User
Tags parse error

Today, when I made a java report, I accidentally browsed a block and found a good thing: Google + protobuf. I really felt that there were some problems when I customized the protocol in the Socket project. The first is efficiency, and the second is performance problems.

Let's first introduce protobuf:

Protobuf isOpen SourceThe official site of the project is "Here"), and it is an open-source project with a hard background

It was developed by a famous Google company and has been tested within Google for a long time. It can be seen that its authors are not comparable to ordinary people.

Its role:

Its functions are basically similar to those of XML. All users who know XML know that it is to save the information of a certain data structure in a certain format. It is mainly used for data storage and transmission protocol formats.

Differences between him and XML:

You will be confused. You can't use XML, but it is not. XML is a little more inadequate than it. Its shortcomings are mainly in XML serialization and annoying serialization. When there is a large amount of redundant data in serialization, the efficiency and overhead are not objective, high overhead in deserialization.

 

The following is an example of a huge online player.

 

CodeGeneration Mechanism
In addition to good performance, the code generation mechanism is the main attraction. To illustrate the code generation mechanism, let's give an example.
For example, if an e-commerce system is implemented in C ++, module A needs to send A large amount of order information to Module B. socket is used for communication.
Assume that the Order includes the following attributes:
--------------------------------
Time: time is expressed as an integer)
Customer id: userid is expressed as an integer)
Transaction amount: price is represented by a floating point number)
Transaction Description: desc is represented by a string)
--------------------------------
If you use protobuf for implementation, you must first write a proto file called Order. proto) and add a message structure named "Order" to the file to describe the structured data in the communication protocol. The content of this file is roughly as follows:

 

--------------------------------

Message Order
{
Required int32 time = 1;
Required int32 userid = 2;
Required float price = 3;
Optional string desc = 4;
}

--------------------------------

 


Then, use the protobuf built-in compiler to compile the proto. Because the module in this example is C ++, you can use the command line parameter of the protobuf compiler to view "Here") to generate the "Order packaging class" in the C ++ language ". Generally, a message structure generates a packaging class)
Then you use code similar to the following to serialize/parse the order packaging class:


--------------------------------

// Sender

Order order;
Order. set_time (XXXX );
Order. set_userid (123 );
Order. set_price (100366f );
Order. set_desc ("a test order ");

String sOrder;
Order. SerailzeToString (& sOrder );

// Then call a socket library to send the serialized string
//......

--------------------------------

// Receiver

String sOrder;
// First receives data through the network communication library and stores the data to a string sOrder
//......

Order order;
If (order. ParseFromString (sOrder) // parse the string
{
Cout <"userid:" <order. userid () <endl
<"Desc:" <order. desc () <endl;
}
Else
{
Cerr <"parse error! "<Endl;
}

--------------------------------

 


With this code generation mechanism, developers no longer need to compile the protocol-Resolved code to do this kind of work is a typical thankless ).
You can also use it in the previous project to encapsulate custom protocols to avoid coding for protocol parsing.

 

 

Finally, I would like to add that he has recently handed over Google development to the open-source community. He does not have to worry about performance. At the same time, he supported C ++ java Paython (official) and soon had a variety of languages with great development efficiency and comparable performance)

 

 

This article is from the "Essay" blog, please be sure to keep this source http://bigmac.blog.51cto.com/4126355/1270745

Related Article

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.