Custom header protocol

Time:2020-1-1

Custom header protocol

After learning the course of computer network, we know that at the beginning, computers work alone and offline. Without networking, the information sharing ability and computing ability of computers are very limited, and then computer network came into being. With those networks, 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 The data of computer B. But when different computers exchange data, they need to be transmitted through the network. In the process of transmission, different computers need to follow certain rules to assemble data and transmit information. Then such rules are called protocols

1. agreement

There are many protocols in the computer network. These protocols are located in different layers of OSI, such as TCP / IP, UDP, SMTP, FTP, etc. the reason why the protocol is called protocol is that it has constraint effect. In the end-to-end transmission process of information, the same protocol rules are used between the same layers, so that the sender processes data according to the protocol agreement in this layer, and the receiver processes data according to the protocol agreement in this layer Protocol agreement parsing data. Exists in pairs

Custom header protocol
Custom header protocol

2. Custom agreement

In daily development, it may be necessary to define message protocol for some reasons. This protocol is based on TCP connection. For example, when the mobile terminal is doing APM, it divides the function into two modules, one is APM monitoring module, the other is a data reporting component for convenience of expansion. It has the configuration of dynamic sending and reporting strategy

So the report component involves efficient communication with the server, so the client and the server agree on a set of customized message protocol, as shown below

  • Overall structure of pack
    | HEAD | META | BATCH_PAYLOAD |
  • Pack:: head structure

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

  • Pack:: meta structure

    JSON string separated by line break

    crypto(deflate(JSONnJSON…))

  • Pack:: batch  payload structure

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

    crypto(deflate(JSON) | crypto(deflate(JSON) | …

In fact, in the process of computer network transmission is binary data, so take IOS for example, the purpose of our custom message protocol is to customize and assemble the data of nsdata according to the protocol, and the header in the message specifies the assembly format of various information

  • There are three messages to be carried in the head. Message 1: the size information of meta, and the protocol stipulates that the data type of unsigned int of 2 bytes must be used. Message 2: the number of payload messages, the data type of unsigned int of 1 byte must be used. Message 3: the size of each message, the data type of unsigned int of 4 bytes must be used
  • One message needs to be carried in meta. multiple metadata are spliced with “n” line breaks. In addition, the whole spliced data is compressed and then encrypted
  • In payload, each data is compressed first, and then encrypted as a whole

So the core is the three points above. It’s not difficult at all, but the first time you do it, you may step on the pit

  • The protocol clearly specifies what kind of data type should be used. In addition, the data size is clearly limited. If you don’t do this, the server will make an error in parsing the data according to the byte length, and the return result of the corresponding client interface will be failure
  • When designing data processing, we should pay attention to byte order problems, such as the small end order adopted by IOS and the large end order used by network specified data transmission. If you don’t know this problem, you are likely to fail to call the interface, so you need to convert your data into the large end order data. For the problem of large and small end order, please refer to my article

The Objective-C language processes the data of unsigned int, so you need to directly operate the unsigned short to nsdata. The interface of nsdata is very convenient,[NSData dataWithBytes:&metaLength length:sizeof(metaLength)]]It can be processed into 2 bytes of unsigned int data