Real-time synchronization scheme of mobile games

Source: Internet
Author: User

Tag:os    use    sp   for   on    data    div    problem    bs   

Network latency is the problem of all real-time synchronized games, and here are some ideas and ways to deal with real-time synchronization problems. The specific solution may be special, first here the server does not run the timer (in addition to a game End countdown timer), driven by the front-end, the case of delay is mainly by the front-end to predict or correct, server-assisted, processing and forwarding, as far as I know it seems that no one is doing it. So after reading if I think that there is no consideration, or better ideas, welcome to make bricks/exchange   First of all, this is a two-exchanges game, only 2 players, and the number of soldiers/heroes on the battlefield will not be too much, no more than 50, the soldiers are AI, not controlled by the player. Players can do a limited amount of action, this is only the operation as an example. At the beginning of the game, there will be a school time operation, which will be said later.   Player A in the Tick1 point, play a variety of troop-contributing effects, Tick1 + Delay = Tick2, in Tick2 this time will actually send troops, delay equals to the time of lifting, that is, a permissible delay time, not equal to the network delay, It should be understood as the playback time of a pre-shaking picture, the longer the better, without affecting the player experience (for example, to get a soldier production queue, from the quality of the production to the soldiers, this can take some time).   This time the player requests the server, the content of the request contains TICK1 + Troop command, the server receives the instruction, to the server, at this time there may be two cases, the first is received before Tick2, the second is received after Tick2   before Tick2 received, The server can forward operations, tell all the clients, we send troops in Tick2 (the contents of the package contains Tick2 and troop instructions), this time for another player, he may not see the special effects of the troops, but directly see the troops, this is no problem, or he saw the effects in the time later, This is acceptable and does not affect the game logic.   received after Tick2, the server can discard this operation, or immediately perform this operation, this time is already TICK3, broadcast to all players, in Tick3 out a soldier, because then the server is already TICK3, then the client received the command of the troop is already passed, This will cause the client to make a correction, so we can postpone the instruction to execute again at Tick4, Tick3 + Delay2 = Tick4. Discard, the result of the Tick3,tick4 execution is, the player's operation is invalid, the battle needs to be corrected once (correct will cause the player to see strange things), and the player's operation is delayed, but the thing that sees is normal, only my operation late for a period of time only.   All instructions from trigger to effectiveare not immediately, all through the delay, even if my network latency within 1ms, I this instruction will be executed after the delay time, and the front and back end will have a command queue, to record at which frame should execute which instruction.   All instructions, as long as the implementation of the first few frames of unity, you can guarantee the unity of results.   When the client receives the instruction, there are 2 possibilities, that is, before Tick2, and after Tick2   received before Tick2, so happy. Received after Tick2, then our operation is delayed, may have to be corrected, this is said to be possible, because there are chance. Is the player point troops, not waiting for the server to return, but in the Tick2 when the automatic troop, such a benefit is that the client in the network is poor, all things can get feedback. Although these feedback may be wrong, but it does not matter, the key is the player experience of the smooth, wrong end will certainly be corrected, as long as the correct time to deal with the performance is OK.   And this does not have to be wrong, if the server receives my request before Tick2, then the server will execute the logic of sending troops in Tick2, and I receive the response of the server after Tick2, but I also executed the logic of sending troops in Tick2, this is the same frame, In this case there is no need to correct it, because it is delayed, but we have correctly predicted the result. There are two situations that need to be corrected, the first is that B players receive after Tick2, because B players are unable to predict a player in Tick1 click on the troops, in Tick2 out a soldier. The second is that the server is received after the Tick2, then the two end may need to be corrected, it is possible, because there is another chance.   If we do not predict, but wait for the results of the server, according to the results of the server to execute, in this case, the client's performance is smooth, the battle is smooth, but my click did not react immediately. Server in Tick3 issued (content contains TICK4 + Troop command), in Tick4 execution, the client as long as the Tick4 can be received before the TICK4 can execute troops, then the result is correct. This even we can be received by the server time, the server each time received on the basis of the current time delay to execute, completely disregard the player click Time, as long as the client in this delay time to receive results, but also do not need to correct.   The difference between the two chance is that one ignores the delay of the packet coming back from the server, one is to ignore the packet-to-server delay, one is to ensure that the client is fluent, and one is to avoid correcting it as much as possible. Specifically in the actual environment to test to know, which is more suitable for our game. Since the execution of all instructions is placed in a queue, the switching of these methods requires only a small number of generations to be changed.Code. When we are not in a grip, let this part be flexibly adjusted as much as possible. The tick mechanism on this side of the server can be flexibly adjusted (because I don't run the timer).   Finally is correct, the first correction is initiated by the front end, when the end also know that the front-end extension, the back end of the processing is more arbitrary, irrelevant, said before, discard, immediately execute, or delay execution, are OK, but it seems to delay the implementation is a better idea, this also see the specific game bar. How the front end knows that it's delayed, the problem is simple, at the beginning of the battle at the time of the school, then both sides at a consistent frequency, such as 10 frames per second, starting from the No. 0 frame (actually the server just record a time, do not run the timer).   In fact, the front and back end as long as the No. 0 frame of the start time, it is easy to calculate the current is in the first few frames, the front and rear end of this start time is not equal, just logically relative. Of course, this time there will be errors, the result of the error is, one side faster or slower. But it doesn't matter, we are not according to the time to calculate, we are in accordance with the frame to calculate, we do not want the same time two end of the content is completely the same, we only require the same results. For example, a device performance is poor, 5 frames per second, but his results will not be wrong, the game ends in 10 seconds, his side of 20 seconds after the end, but the end of the result is the same as the line, as for the operation, if there is such a possibility, the server will delay the operation of the front end can be pressed to wait several seconds But this time is not the problem of network delay, is the problem of equipment lag. The front end has been running, but can't run over, to solve this problem, can only be changed mobile phones, of course, we have to ensure that the performance of the mobile phone can run, for example, two or three years ago the machine, but actually I do not need to spend too much attention on this, the player so rotten mobile phone are not willing to change, How willing to fill in the game inside the money? gave him up straight away.   Servers don't run timers, what's the reason? There are a number of reasons for performance, each bullet, monster, buff these may all need to hang the timer, and I don't want the server to hang too many timers. The bigger part is to make this part of the grip more manageable. For example, this game does not do real-time synchronization, so that things become not so bad. Only a few changes, you can implement server verification.   Do not run the timer how to do it? Very simple, first you still need to have a timing management, like schedule, the game logic to add the timer to put it all in here, when the client request, we first from the last calculated time simulation to the current time, the current time minus the last time, calculated that there are n frames, and then the direct loop N times the timer, this loop and the client's loop, is less than a few to determine whether the elapsed time is less than the minimum interval, if it is to sleep thisCode, instead of a for loop, loop n times. Of course, loop is not necessarily just a timer, but it must contain a timer, and the main thing is the timer. After the  loop is finished, the last time is recorded as the current time, and then the verification, forwarding instructions. Instead of executing the instruction, the instruction is placed in the command queue, waiting for the next execution. The result of the instruction execution is that the server is now unknown. You can see the client's request to drive the server, not the timer to drive the server to run the logic. This is a bit like a turn-based system. Then there is a problem, assuming that the client does not send the request, the server will not be able to move? The server will run a timer, this timer only do one thing, that is, the game is over, the simulation players send an empty command to the server side, the server loop to the end of the game, and then issued the battle results.   Do not run timers, how to make this game from real-time synchronization into non-real-time synchronization becomes simple? In this way, all the instructions sent by the client run directly on the client, but recorded, and then at the end of the game, request the server once, send the command queue to the server, the server only need to set the command queue, and then perform a loop to the end of the game call, naturally can verify the combat results. This non-real-time synchronization needs, itself is existing, such as a single-brush copy, which we need to verify. So just a layer of packaging outside, you can replace them. And the amount of code changed is not much.   Then the problem of correction, when the client found that the server down the packet delay, more than the time to receive, the client needs to request the latest data from the server to refresh, the process, the client is normal operation, and then when the data down, about 0.5-1 seconds to refresh the data. The local server does not have the object to kill, the local no server has the creation of objects, both sides of the data to perform a smooth interpolation calculation, let him during this time to transition to the latest data. There will certainly be some strange images of the player, plus a reconnect on top of it ..., the player should be able to accept this little seemingly strange picture.   During the transition time, the local real-time logical frame is running normally, it records the server is currently running to the first frame, there is another frame variable, this variable represents the current logical frame, these two frames are logical frames, normally, these two frames are equal, but when the correction occurs, The current frame is smaller than the live frame, for example, the No. 200 frame found that I need to correct the local, and then request the server, the server issued the latest, that is, 201 frame results down, to the client side, is already 207 frames, but reset to the latest data, that is, 201 frames, and then began to speed up execution. Executes to two logical frames equal, i.e. resumes normal speed execution.   First, acceleration is possible because the server can instantaneouslyAfter execution, why can't the client speed up the execution? Most crucially, the result of each frame is the same, before 200 frames, the client has a part of the result is wrong, the essence of the correction is to discard the wrong results, and then reset the correct results. And then continue running.   also assume that the client is running too slow to keep up with the server, that is, in fact, the way the server uses deferred execution, basically there is no need to correct, because all instructions for this device is not happening, the client can only run slowly, slowly demonstrating the results of the battle. On such a device, the animation is one card, the problem has shifted from the problem of network latency to the performance of the device, this is another optimization topic.   logical frame and display frame separation, there are several purposes, logical frames to ensure the accuracy of the logic, that is, the game ran 2000 frames, the execution of 2000 logic processing. This part is shared between the front and back ends. As for the front-end display refreshed 8000 frames, or 6000 frames, the effect is only the smoothness of the animation, does not affect the results, the front end to spend 100 seconds or 200 seconds to run the game, but also does not affect the results. The second is the convenience of error correction and acceleration, logical frame and display frame interaction process, each time the logical frame execution, will be modified similar to the location of such properties, or change some state. The display frame is responsible for smoothing the position and state of the change from the current position to the logical frame, and playing the corresponding animation. Logical frames do not directly alter these properties, but instead place them in a data component that is unique to each object, and when logically judged, it is the data that is taken to judge it, not the data of the layer. Each time an update is made, the display frame will make a transition from the current to that data logic. The frequency of the logical frame is accelerated, and the display frame is not affected, the two are independent, logical frame only logical processing and write data, display frame just take out the data to display.   Client side, both the logical frame and the display frame are driven by the same schedule, but the frequency is different, they are larger than the difference is that the logical frame of each frame of the DT is a fixed value, such as 10 times per second logical frames, Then this dt is fixed at 0.1, and the display frame is based on the actual elapsed time as DT. The DT here refers to the elapsed time each time the update is passed in. The data written by each logical frame, in addition to data such as position state, also contains a data that needs to be shown to simulate how long the frame is being simulated, and this is a fixed value of 0.1. Display frame to get the location data, I want to move to where, and then get the time data, how long to move to, then you can perform the logic of smooth display.   What is the difference between this and PC latency for 2G 3G network latency? One is that the delay is bigger and the other one is instability. What is the latency of the data, this data is not too big, because this data only a few reference meaning, the actualThe delay is not based on this data, but is very unstable. Perhaps you squat a toilet, the door of the toilet, the signal is a lot worse, this is very common. Because mobile devices are mobile, moving to different places can have different delays, this time there may be two data will be useful, one is 2G 3G faster speed is how, the other is 2G 3G slower speed is how. In such a poor network, there must be a lot of delay correction, but as long as we can receive the message, the game can be run. Client-side impersonation is a much better experience to get high latency, because every click reacts, and although the actual time is delayed, your operation is still in effect. 2G 3G Another problem is the disconnection, the disconnection is actually another slightly egg-ache topic.   2G 3G, in the high latency of the user to bring a good experience, this is the real-time synchronization is difficult to do, the key to see the game instructions can be delayed, real-time requirements, the frequency of player operation, and error correction experience, whether the player can accept. If not, then you need to weaken it, for example, before entering the game, check the ping value, if too high, then remind the player that your current network latency is high, let the player decide to play in the environment is not high latency. In fact, for the network delay, the vast majority of players are not unfamiliar, after a dose of preventive needle, the reaction to the back of the experience is not timely inclusive will be a little higher. In the case of high latency, it is also feasible to suggest that the player brush a single copy of the machine. In addition, there is a scheme, although a bit deceptive players, but as long as the players feel that the game is smooth, is to send AI robots with high-latency players to play.   The idea of an entire synchronization is currently being tested, but theoretically feasible and may seem somewhat complex, but after the actual code framework is set up, the code is actually very concise, since all logic does not need to focus on latency. And the parts that are likely to change are isolated, and everything is as independent as possible. Of course, in the process of implementation will definitely encounter more problems, problem solving is, the key is to have ideas, to solve the problem of the direction.   The whole idea of the landing is probably the case, the front and back end are used C + +, the frontend is 2DX, the back end is Kxserver, back end of a set of simulation 2DX framework, to achieve a simplified version of the director,schedule,node,component, Then make rules to write logic-related components, using their own message mechanism to pass messages, when some logic needs to be executed to display related content, can be handled with events, the client exists this listener, and the server does not exist. Alternatively, you can use preprocessing to preprocess some code based on whether a macro such as running in server is defined. The   design is divided into a number of layers and modules to ensure that in the event of which does not, without affecting the other code. In the process of landing will continue to improve the code, polishing, validation ideas. It is hoped that after the completion of this project, a set of Cocos2d-x network real-time synchronization specifications and the front and back of the simple framework, to be used in other projects with similar requirements.

Real-time synchronization scheme for mobile online games

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.