A wave of communication on heterogeneous platforms (sticky packet, size end mode, network byte order)

Source: Internet
Author: User

A. Intro

A time ago with Striveengine do a information acquisition system, the server is Windows, the client is a variety of MCU, as well as UNIX and other platforms. These heterogeneous platforms were convened by me to "strengthen dialogue, broaden consensus and deepen cooperation". All say that there is a place where there are rivers and lakes, truth, Machine World is the same! These heterogeneous platforms, weekdays in the fragmented, squarely, but also peaceful. Now the wooed, to discuss plans, as I expected, will be staged a wave of real bloody!

As is often said in the news broadcast, "strengthen dialogue, broaden consensus, deepen cooperation", first of all, "strengthen dialogue."

Looking at you, I have a little clear throat, liberty, said:

"Each road heroes, this time calls everybody to come, is wants to let everybody strong-hand join together, makes a big news!" ”

The field becomes quiet, and each is absorbed. So I went on and said:

"What the hell is going on here?" is to engage in an "information collection system"! ”

Started whispering on the pitch. I went on to say:

"We have any good advice, what needs to communicate with each other, please speak freely!" ”

The applause rang.

The applause settled.

The delegates began to scramble to speak.

So the question is--they don't speak Mandarin, okay? --a type of single-chip computer operating in Hunan dialect, Windows Server is a New York cavity-The scene of the instantaneous chaos! There is a indescribable a terminal, forgive me ignorant, you say is ... Is it a Javanese word? Why do you still smell cumin? More than two people seem to be a statement, ready to do! I am still wondering, these two languages do not pass, how does a word disagree?! The scene gradually loses control, has not allowed me to think more! I'm a loud shout! 30 guards armed with a gun from the side entrance to the array stage, the audience quiet! Then i a ring, outside the temple to Gallop, sent on a red silk covered with a large plaque, the full attention. I am lucky silk fall, gilt large plaque on the impressively written gold glittering shining several characters:

two. Byte-stream-oriented TCP communication

It all has to start with the TCP communication.

TCP is not unfamiliar to everyone, is one of the most important protocols in the network protocol stack. The striveengine communication engine supports both TCP and UDP, and we choose TCP based on the needs of the business scenario. TCP is a byte-stream oriented.

      

three. Problems caused by word-throttling communication

It is because TCP traffic is oriented to byte stream, but also raises a series of related problems need us to solve.

1. Sticky bag

One of the first, but also we are most familiar with is sticky bag problem.

The byte stream is the same as the current, and when two messages are read together, you cannot separate the boundaries of the two.

2. Byte order/size End mode

TCP is a byte stream oriented, and this byte stream is essentially a memory fragment. So the problem is, the sending host and the receiving host can be stored differently, that is, the host byte order is inconsistent.

To make a simple analogy, the books in China used to read and write from right to left, and now they read and write from left to right. If modern people read past books according to their current habits, problems will arise because of inconsistent host byte-order.

As read and write can from left to right, can also from right to left, the road can be defined on the left to drive can also be defined on the right side of the road-storage way There are two-"big-endian" and "Small End way."

"Gulliver's Travels" in the small country in the formation of the two political forces, the idea of eating eggs from the big-endian start stripping, the other side advocated to start stripping from the beginning, a word of battlefields, the war frequency years.

Computer science borrowed this allusion to name two ways of storing:

Unfortunately, the differences in the size and the end of the way in the computer world also failed to agree, so the heterogeneous platform communication process in the host byte sequence inconsistency problem.

four. How to solve?

The first thing to make clear, these problems are "application layer" problem! Because the transport layer, or the entire underlying communication is the responsibility of "communication", the other things should not control it! In particular, TCP communication, we know that TCP is reliable transmission-the sender of what I promise to send you intact, as for you to receive a look, oops, the bag stuck together how can I share clearly? Oops, this sent the data I parse out how is garbled? --It's not about TCP communications! This is your programmer to do the work of the message processing, the message transmission TCP to help you dry, and TCP is the name of the "Iron teeth sweet basil, honest and reliable little husband", has been every opportunity, processing messages, parsing the message of the work will rely on as a programmer you to show off!

1. Sticky bag problem

The striveengine communication engine offers two solutions:

Text Protocol mode

Binary Protocol mode

One of the more fundamental is the binary protocol pattern. The binary protocol bounds the message by adding a header to the message.

    //Summary://Binary Protocol Helper interface.      Public InterfaceIstreamcontracthelper {//Summary://the length of the message header.         intMessageheaderlength {Get; } //Summary://The length of the message body is resolved from the message header (note that it is not the length of the entire message, but the length of the body that does not contain the message header). //        //Parameters://Head://complete message header, fixed length of messageheaderlength        intParsemessagebodylength (byte[] head); }

This interface needs to be implemented by itself. The interface only requires two things: 1. What is the length of the message header? 2. How to remove the message body length from the message header, thereby indirectly removing the message body. As to how the specific protocol is designed, this is not fixed pattern.

2. Byte order problem

the problem with the byte order is much simpler. It has been said that communication is responsible for transferring data intact from end to end, and now that the data is given to me, I find that the byte order is inconsistent. What about inconsistencies? Convert to Chant!

void byte [] bmsg) {     ······ }

This byte array is the received data, how to convert it in the method body. And these transformations have already been encapsulated into the API, so there is no difficulty, this article will not repeat.

——————————————— attached project source code ———————————————————

Five. the

For the people of Sichuan to make the guide, to proclaim the words of the citizens. Since then, the exchanges between those machines have deepened, and some have established deep friendships. I often discipline them, although you are machines, but also want someone to taste, to put down those so-called stereotypes, differences. Everyone's way of thinking, cultural background, education level may not be the same, but as long as everyone with an open heart to understand each other, communication, exchange of positions, empathy, can carry out cooperation, can rid out a small personal life, and into a larger life. To complete a data acquisition system, either one of you can not be alone, only by the concerted efforts of everyone. To do together, we must be eclectic, not action. For example, you are small-end mode storage, it is not to exclude the big-endian way, as they are heresy, want to wipe out, this is the most dangerous! We must understand each other, to try to understand each other, to learn each other's words. Some advanced systems look down on the microcontroller and feel that they do not have a common language, which is also wrong. Ruler has a long inch is short, to complement each other. Pipe said: "And so can learn, Xi so can Kai, Kai Xi to learn, MO can hurt also", only and together with the XI, can really powerful!

later, there are machine messages to me, saying that learned a lot of the truth of life. For that I am gratified.  

A wave of communication on heterogeneous platforms (sticky packet, size end mode, network byte order)

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.