System design, protocol first. Most people do not understand the design details of the protocol, and tend to use existing protocols for application layer design, such as:
In any case, understanding the principles of protocol design is very helpful for a deep understanding of system communication. 1. Layered design of the protocolThe so-called "agreement" is a rule that both parties abide by, such as a divorce agreement or a ceasefire agreement. An agreement has three elements: syntax, semantics, and timing:
Voice-over: The following text mainly talks about grammar design. Protocol design is usually divided into three layers: application layer protocol, security layer protocol, and transport layer protocol. Let’s take a look at how to select the protocols for these three layers. 2. Application layer protocol designThere are three common application layer protocol selections: text protocol, binary protocol, and streaming XML protocol. (1) Text Agreement A text protocol refers to a communication transmission protocol that is "close to human written language expression". A typical protocol is the HTTP protocol. An example of an HTTP protocol request message is as follows: GET / HTTP / 1.1 The characteristics of the text protocol are:
(2) Binary protocol A binary protocol is a binary protocol, typically the IP protocol. The following is a diagram of the IP protocol: Binary protocols generally include: Generally include:
The characteristics of the binary protocol are:
This is an example of a typical 16-byte binary fixed-length header: //sizeof(cs_header)=16 in: (1) The first 4 bytes represent the version number; (2) The next 4 bytes represent the magic number magic_num, which is used to solve the problem of data misalignment or packet loss; Voice-over: For example, the magic number is agreed to be 0x01020304. If the received message matches the magic number, it is considered to be a normal message. Otherwise, it is considered to be an abnormal message and the connection is disconnected. (3) The next 4 bytes represent the command number. Different command numbers correspond to different variable-length packets. (4) The last 4 bytes represent the length of the packet body, which determines how many bytes the variable-length packet body has; This is an actual binary variable-length package body: message CUserLoginReq { It uses Google's Protobuf protocol, which is easy to see:
PB is a very popular binary variable-length packet protocol with the following advantages:
Streaming XML protocol Streaming XML seems to be a special case of text protocol, and can also be regarded as a separate category. For example, xmpp is a typical streaming XML protocol. The following is a typical message of the xmpp protocol: < message From the XML tags, we can roughly judge that this is a chat message sent by Romeo to Juliet. The XML protocol has several characteristics:
3. Security layer protocol designIn addition to using SSL, there are three common solutions for implementing the security layer protocol by yourself. Voiceover: SSL key management is a problem. (1) Fixed key The server and the client agree on a key and an encryption algorithm (for example, AES). Each time before the client sends a message, it uses the agreed algorithm and key to encrypt and transmit it. After the server receives the message, it uses the agreed algorithm and key to decrypt it. Voice-over: Security is low, and security is based on the professional ethics of programmers. (2) One person, one password In simple terms, a person's key is fixed, but different for each person. Common implementation methods are:
(3) One-time pad That is, dynamic keys, one session one key, which is more secure, and keys are negotiated before each session. The key negotiation process requires two random asymmetric key generation and one random symmetric encryption key generation. The specific details are not expanded here. 4. Transport layer protocol designThe optional protocols are TCP and UDP. TCP is basically used now. With the advent of technologies such as epoll, multiple connections are no longer a bottleneck, and there is no problem with hundreds of thousands of connections on a single machine. |
<<: What is 6G and when can we expect it?
>>: Let’s talk about the technological advances needed to realize the 6G vision
retinaface face detection algorithm dessert I hav...
With the popularity of WiFi and mobile devices, w...
In the wave of full digitalization, enterprises n...
Last month, the blog shared that RAKsmart Los Ang...
According to the survey results of Cisco and a th...
Today I will share with you some knowledge about ...
We have read many articles before about IP addres...
At the "Joining Inspur's Thinking and In...
October 13, 2020, Beijing - The COVID-19 pandemic...
With the support of "new infrastructure"...
According to foreign media CNET, Sir Tim Berners-...
In most cases, choosing an Ethernet cable is rela...
A few years ago, I bought a K2P (A2) for my frien...
AlphaVPS is a foreign hosting company founded in ...
Convergence between wired and wireless networks i...