Basics of Network Synchronization (Frame Synchronization & State Synchronization)

1. What is network synchronization

The so-called synchronization means that the performance effects of multiple clients are consistent. For example, when we play King of Glory, the screens of ten players need to display the same hero position, the same skill release angle, and the same release time. This is synchronization. 

2. How to achieve network synchronization, comparison of different solutions

2.1 State Synchronization

2.1.1 Definition

If the client needs the state of the object, it will request the state of the object, or the service will push the state information of this object, and then update the display of this object [the way to synchronize the state behavior of other players (request the state of other players and displayed on the NPC), under normal circumstances, AI logic, skill logic, and battle calculations are all calculated by the server, and the results of the calculation are synchronized to the client. The client only needs to accept the status changes sent by the server, and then update its local action status , Buff status, location, etc., but in order to give players a good experience and reduce the amount of data to be synchronized, the client will also do a lot of local calculations to reduce the frequency of server synchronization and the amount of data]

2.1.2 Advantages

(1) The security is very high, and the plug-in basically has no ability to benefit from it.

(2) Stronger adaptability to network bandwidth and jitter packets [ Even if there is an input delay of 200 or 300 and then returns to normal, players will not feel uncomfortable]

(3) In the process of developing the game, it is faster to disconnect and reconnect [ If the game crashes, after the client restarts, the server only needs to synchronize the status of all important objects again, and recreate it again]

(4) Logical performance optimization has advantages, and the advantages of client-side performance optimization are also more obvious [ for example, you can cut out characters during optimization, and you don’t need to create characters that players can’t see, and you don’t need to calculate them, saving consumption]

2.1.3 Disadvantages

(1) The development efficiency is low (manpower consumption) , which is worse than frame synchronization. In many cases, it is necessary to ensure that the state of each role object between the server and the client is consistent, but in fact it is difficult to achieve consistency [such as client The update frequency of the client and the server, some tailoring for optimization, network jitter, etc., it is difficult to synchronize each state on the client, and it is difficult to debug these things to optimize the loopholes and inconsistencies it brings. Phenomena, the cost cycle will be longer, and it is more difficult to achieve a good level of optimization]

(2) Poor sense of strike (performance effect) , it is difficult to make the sense of strike and accuracy of action games [For example, if you want to make a shooting character, his bullets will produce dozens of bullets per second, based on state synchronization It is more difficult to do it, because the system will generate a lot of data in a short period of time, and it must be synchronized through creation, destruction, and position calculation]

(3) Traffic will gradually increase with the complexity of the game (traffic consumption) [such as the number of characters. We hope that PvP can also be played under 3G and 4G network conditions, so we hope that its consumption of paid traffic can be controlled at a reasonable level, and we don't want to consume tens of megabytes of data traffic for a single game. (For example, if a character puts an AOE, the status of all characters must be updated]

2.2 Frame Synchronization

2.2.1 Definition

Frame synchronization is a way to synchronize front-end and back-end data , and it is generally used in online games that require high real-time performance.

Simply put, it is the same state + the same instruction + executed in frame order = the same result .
Status: All clients ensure logical consistency and receive the same random seed (randomseed) and room information;
Instructions: The server is only responsible for collecting and receiving each client operation instruction (cmd), forwarding instructions , and the server runs at a constant frame rate (30 frames 1 Seconds) to dispatch instructions , no instruction or no change in the instruction also needs to be dispatched;
execution: the real game logic is calculated by each client separately, the client needs to receive the instruction sent by the server to advance the logic, and cannot advance the logic when the instruction is not received (LockStep )

2.2.2 Advantages

(1) Its development efficiency is relatively high (manpower consumption), but if the overall framework of the development idea is verified and feasible, and its shortcomings are solved, then only need to follow this idea, try to ensure performance, how to write the program Just how to write, the logic of the server is simple, it only needs to be responsible for forwarding instructions, and the pressure is also small [ For example, if you want to make a complex skill under state synchronization, there are many levels of skills, it may take several days to develop a slightly passable one As a result, under the frame synchronization, the hero's multi-level skills may be done in half a day]

(2) It can achieve a stronger sense of percussion (expression effect) . In addition to various feedback, special effects and sound effects, the strong percussion sense also has its accuracy . Using frame synchronization, seeing these waving movements in the game can generate feedback at a more accurate moment, and the density of the movement itself can also achieve a high frequency, which is more difficult to do under state synchronization.

(3) Its flow consumption is stable (flow consumption) . Frame synchronization will only increase the traffic as the number of players increases. If the number of players is fixed, no matter how complex your game is or how many characters you have, the traffic consumption will basically be stable.

(4) Watching games , video storage, playback, and follow-up processing based on video files can be realized more conveniently .

2.2.3 Disadvantages

(1) The most fatal disadvantage is that the network requirements are relatively high , and the frame synchronization is frame-locked. If there is network jitter and the number of calls is unstable for a period of time, the delay of network commands will be squeezed, causing freezes.

(2) The anti-cheat ability is weaker, and the logic of frame synchronization is in the client , and you can modify it relatively easily. But why "Glory of the King" dared to use frame synchronization , on the one hand, it was because the development cycle was very short when the project was established, and it would take half a year to go online, with dozens of heroes, and there was time pressure. On the other hand, MOBA games did not Like numerical growth games, its gameplay is based on a single round, and the cheating modification of a single round will at most affect the outcome of this round. Moreover, we can easily detect cheating and give due punishment, so we don't think this is a fatal shortcoming.

(3) It takes a long time to recover from disconnection. For example, it took a very long time to reload after a flashback, and even after loading, the game was almost over. This is a fatal problem of frame synchronization.

(4) Its logic performance optimization is under great pressure. Every logical object in a large game needs to be calculated on the client side. If you make a main city, there are thousands of people in the main city. Although the players cannot see the thousands of people, the game still needs to perform effective logic operations on them, so frame synchronization cannot be done . Many objects need to be updated . game scene.

(5) Debugging is difficult. In different situations, it is difficult to find the problem. Generally, the problem is located by outputting key change information through debug, but the problem may be within 1-20 functions, but only the debug information is typed in the 20th function, and then it needs to be layered . To find out where the problem is, consider recording the game where the problem occurred, and then replay and debug continuously to find the problem.

2.2.4 Scope of application

RTS real-time strategy, ACT multiplayer real-time combat, and MOBA provide frame synchronization solution suggestions. The characteristics of the above-mentioned types of online battle games are: extremely high real-time requirements, the pursuit of fair competition, and a sense of attack.

2.2.5 Solutions realized by core principles

In frame synchronization game development, there are mainly three types:

1.0 frame lockstep [Lockstep]

  • When the network delay of client A causes the server to fail to collect the X-th frame input instruction packet of A in the X-th frame,
  • The server needs to wait for the collection of X frame instructions of all clients to complete, and then it will issue all client packet data of X frame to keep in sync.
  • It also means that one person is delayed, and all clients have to wait. Obviously, this synchronization concept is not suitable for highly competitive games, and the player experience will be poor.

2.0 optimistic frame

  • Other optimistic frames are improved on the basis of frame lockstep. In order to improve the game experience, the server no longer requires to collect all client X frame operation instructions before distributing them.
  • Instead, each client performs local input command snapshot storage, storing its own input commands (with frame numbers).
  • If client A loses packets from the second frame (logical frame) until the transmission of the fifth frame is normal due to delay, then the server will continue to distribute even if the server does not receive the input command of the 2-5 frames of the client A,
  • Until the 5th frame, the local snapshots of the 2-5 frames of the A client will be distributed together, and at the same time, the input commands of the 2-5 frames of other clients will be synchronized to the local client. Of course, the lost data of 3 frames will be directly synchronized in one frame.
  • To the client will cause freeze and teleportation, so the A client needs to perform accelerated operations to execute command operations, usually doing difference calculations, and making the synchronization smoother by means of tweening animation.

3.0 Predictive rollback 

  • Predictive rollback is a synchronization mechanism and a solution to optimistic frames. The biggest disadvantage of the optimistic frames mentioned above is that once client A loses packets, A of other clients will not have packets during the packet loss period. any
  • The operation instructions are synchronized, which will affect the game experience, so the prediction mechanism is used here. If the client does not have instruction data in X frame, the server will give it a predicted input instruction in X frame. When A client connects,
  • The server will reconcile and roll back the predicted data and the real lost snapshot data.

3. How does frame synchronization ensure consistency (the client calculates correctly)

Different call sequences, timing, deviations in floating-point calculations, container sorting uncertainty, uncertainty caused by writing logic in coroutines, physical floating-point numbers, uncertainties caused by random values, etc.

The most basic : Update the entire battle logic through a unified logic tick entry, instead of updating each logic by itself. Ensure that each tick is from top to bottom, and the order of each execution is consistent.

Random value : Just make a random seed.

Pay attention to code specifications : For example, in the battle of frame synchronization, the logic part does not use Coroutine, and does not rely on the loop of indeterminate order containers such as Dictionary.

Physics : Check whether the combat logic requires physics, and whether the collisions are all collision logics made by ourselves, and then deal with them.

Fixed-point numbers instead of floating-point numbers : Regarding the implementation of fixed-point numbers, the simpler way is to multiply the original floating-point number by 1000 or 10000, and divide the corresponding place by 1000 or 10000. , can solve some problems and reduce the probability of calculation inconsistencies, but this method is a temporary solution, and there are some hidden dangers (for example, if an int is multiplied by a float, if the original value is *1000, then The final calculated value may be very large, and there is a risk of crossing the boundary). The best solution : Use a proven fixed-point math library that implements more precision and rigor.

Guess you like

Origin blog.csdn.net/Luoxiaobaia/article/details/129905551