Network Protocol Design

Source: Internet
Author: User

 

An article:

The focus is
1. Prevent message loss
(1) The recipient receives a response
(2) After sending and sending, if you fail to receive a response for a period of time, resend the message. If you do not receive a response for multiple resend attempts, exit
2. Prevent response loss
(1) If the response is lost, the sender resends the message. The receiver must determine whether the received message is repeated (the serial number is added to the frame)
3. Identity confirmation
(1) identify with an address
4 Transfer Efficiency
(1) limit the size of each message

The above is very basic. From: http://blog.csdn.net/ybdesire/article/details/6859582

 

An article:

Network Protocol Design for real-time games

Games like SLG do not require high real-time interaction between games. Therefore, we generally choose to use the HTTP protocol for frontend and backend data interaction. However, as users demand more and more games, MMORPG has gradually emerged in webgames, such as the back-to-integrate MMORPG-webgame such as Kunlun and lottu. Because HTTP is a transient connection, it cannot meet the network requirements of this persistent connection. Therefore, you must use socket for network connection. Although comet can achieve a similar goal, it is still difficult to achieve the socket level in terms of performance, so it is rarely considered in commercial applications.
This article does not cover the language used to implement socket connections for data transmission. The main content is to discuss the data transmitted between the server and client and the data structure based on actual experience.

The first few terms:
1. Packets
When everyone is using the Internet, all the data is packaged into a package and sent out. This package is not a compressed package (ZIP \ RAR), but a binary data with a certain data structure. The main content of a packet is data information, which includes the destination IP address to which the information is sent, the source IP address to which the information is sent, and some related control information. When a router receives an IP packet, it searches for the route table based on the destination IP address in the packet and sends the IP packet to the corresponding port based on the search result. The next IP router continues forwarding after receiving the packet until it is sent to the destination. Routers can exchange route information through the routing protocol to update the route table.
In designing game packages, We only care about data information, while others do not.

2. bytes)
Everyone is familiar with MB and kb, and we must know that B in MB or KB is byte. Byte is the basic unit of measurement for a bucket. Generally, an ascii code occupies one byte, for example, 'A' or 1.

3. Bit
Now that we know the byte, what is the composition of the byte? It is a bit. Is the smallest data unit in a computer. A byte consists of eight binary digits, such as 00001111. ASCII codes can all be converted to binary. The following table
Binary-decimal hexadecimal character
00110001 49 31 1
00110010 50 32 2
00110011 51 33 3
00110100 52 34 4
00110101 53 35 5
00110110 54 36 6
00110111 55 37 7
00111000 56 38 8
00111001 57 39 9

1. Protocol Design
In general, we will design a protocol that specifies the role of the data in this package. Generally, numbers are used to represent the protocol.
For example:
Login logout 1
Chat 2
Move 3

So we can design data protocols like this.
--------------------------------
Controller login logout 1
--------------------------------
Action login 1
Action logout 2

----------------------------------
Controller chat 2
----------------------------------
Action chat 1
Action World 2
Action Team 3

-----------------------------------
Controller mobile 3
-----------------------------------
Action 1

This forms the data structure: controller, action, Data
For example, World chat: 2, 2, data; Mobile: 3, 1, data

In this way, the client and server can call different programs to process data according to the Controller and action.
Data also has its own structure, which is designed based on actual functions.
For example, private chat is designed as: 2, 1, from, to, MSG

If we use socket to transmit the string, the client and the server can process the data after receiving the data.

2. Packaging Design
In protocol design, we use a string-based protocol. To reduce network overhead, binary is generally used to transmit data. This greatly reduces the length of the package. Saves network bandwidth.

Previously, we designed controllers and actions in the Protocol. Now we add a return value, and the return value represents the results of controllers and actions, rather than the data itself. This constitutes the data structure: controller, action, return value, and data.

We can put the controller, action, and return value in two bytes, which requires bitwise operations.

A byte has eight bits. We can allocate bits as follows:
First byte
+ -------- 4--------8
+ Controller + Action +
+ --------- + ------ +
1-4 bits of a byte are used with the Controller, and 5-8 bits are used for the action.

Second byte
+ -------- 4--------8
+ Return value + retention +
+ --------- + ------ +
Use 1-4 bits of a byte and return values, and retain 5-8 bits for use.

If no value is returned, the second byte is 00000000.


The data in data also uses a binary structure, which requires writing different unpacking programs based on unused protocols. You can also write a general unpacking program, so you need to think carefully when designing the data structure. The design of the data structure will be described later.

We have constructed the data subject of the packet, and we have to mark the Data Length. We will add a separator between the length and the data subject. I use 0x88 or 0x86 here.
+ -------- 16--------8--------8-----------4--------8---------4 ---------- n
+ Package Length + 0x88 + controller + Action + return value + retention + Data +
+ -------- + --------- + ---------- +
Depending on the actual situation, it is determined whether the package length is 1 byte or 2 bytes. one byte can represent the length of 255 bytes, and two bytes can indicate the length of 65535.



During socket communication, packets are often combined and pasted. With the data structure we designed previously, we cannot solve the problem of packaging and sticking packets. Therefore, we must also distinguish controllers from packages.

For example, our package composition is:
+ -------- 16--------8--------8-----------4--------8---------4 ---------- n
+ Package Length + 0x88 + controller + Action + return value + retention + Data +
+ -------- + --------- + ---------- +


We can add a byte at the front end of the package for differentiation.
+ -------- 8--------16--------8--------8-----------4--------8---------4 ---------- n
+ 0x86 + package Length + 0x88 + controller + Action + return value + retention + Data +
+ -------- + --------- + ---------- +


More elements can be added to the entire package, such as encryption and parity. The structure of the package should be designed according to the actual situation, these need to be realized and discovered in actual projects.

This article only gives an overview of packets. The deficiencies, vulnerabilities, and irrationality are everywhere. I hope you can point out and discuss them together. I hope you can see better ideas and provide them to you.

 

Network Protocol Design

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.