Custom header protocols may not be that hard

Having studied the course of computer network, we know that at the beginning, computers all work offline alone. Without networking, the information sharing ability and computing ability of computers are very limited. Later, computer networks were born. , the information and data of computer A can be transmitted to computer B through the network, and the same computer A can obtain the data from computer B. However, when data is exchanged between different computers, it must be transmitted through the network. The transmission process requires different All computers follow certain rules to assemble data and transmit information, so such rules are called protocols.

1. Agreement

There are many protocols in computer networks, these protocols are located in different layers of OSI, such as TCP/IP, UDP, SMTP, FTP, etc. The reason why the protocol is called a protocol is that it has a binding effect, and the information is transmitted in the end-to-end process. In the same layer, the same protocol rules are used, so that the sender processes the data according to the protocol agreement at this level, and the receiver parses the data according to the protocol agreement at this level. It exists in pairs.

OSI seven-layer model TCP protocol

2. Custom Protocol

In daily development, for some reasons, it may be necessary to customize the message protocol. This protocol is based on the TCP connection. For example, when the mobile terminal is doing APM, the function is divided into two modules, one is APM monitoring Module, a separate data reporting component for the convenience of expansion, with the configuration of dynamic delivery reporting strategy.

Therefore, the reporting component involves efficient communication with the server, so the client and the server have agreed on a set of custom message protocols, as shown below.

  • PACKET overall structure | HEAD | META | BATCH_PAYLOAD |

  • PACKET::HEAD structure

    | META_SIZE (2bytes unsigned int) | BATCH_COUNT (1 bytes unsigned int) | PAYLOAD_SIZE (4bytes unsigned int) | PAYLOAD_SIZE (4bytes unsigned int) | ... |

  • PACKET::META structure

    Newline-delimited JSON string

    crypto(deflate(JSON\nJSON...))

  • PACKET::BATCH_PAYLOAD structure

    The value of each field in the JSON body is BASE64 encoded

    crypto(deflate(JSON) | crypto(deflate(JSON) | ...

In fact, what is transmitted in the computer network process is binary data, so taking iOS as an example, the purpose of our custom message protocol is to custom assemble the data of a NSData according to the agreement, and the header in the message specifies various The assembled format of the information.

  • HEAD needs to carry 3 pieces of information. Information 1: The size information of meta, and the protocol stipulates that the unsigned int data type of 2 bytes must be used. Information 2: The number of payload packets, the unsigned int data type of 1 byte must be used. Information 3 : The size of each message, must use 4byte unsigned int data type.
  • META needs to carry one piece of information. Multiple pieces of metadata are spliced ​​with "\n" line breaks, and the spliced ​​overall data is compressed and then encrypted
  • In PAYLOAD, each piece of data is compressed first, and then encrypted as a whole

So the core is the above 3 points, it is not difficult at all, but you may step on the pit when you do it for the first time. The column is as follows

  • The protocol clearly stipulates what data type should be used, and the data size is clearly limited. If you do not do this, the server will make an error when parsing the data according to the byte length, and the corresponding client interface will return a failure. asking for trouble
  • When designing data processing, you should pay attention to the byte order, such as the little endian used by iOS, and the network stipulates that data transmission uses big endian. If you do not know this problem, then you are likely to fail the interface call, so you need Convert your data to big-endian data, you can check my article about big-endian order

Objective-C language handles unsigned int data, so you need to directly manipulate unsigned short to NSData. The interface of NSData is very convenient, and [NSData dataWithBytes:&metaLength length:sizeof(metaLength)]]it can be processed into 2byte unsigned int data

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324098305&siteId=291194637