Development Program: Crazy Bombs
Development progress: Phase Fourth
The main point of this issue: Design server-side request processing capabilities
Development platform: Java Platform
After the previous 3 period of explanation, our mobile games "crazy bomb" has been largely completed, in addition to the development of the request processing function, OK. Come and try our "crazy Bombs" (Game Download Address: http://www.shudoo.com/bzsoft).
Since the mobile games, the number of natural clients will be more, so multiple clients at the same time send requests to the server is very common. To better run, the server side needs to handle multiple client requests at the same time, which is using concurrent processing. Here we will learn how to deal with the request of multiple clients concurrently with the mobile games.
Design idea of request processing
In the "Crazy Bomb" program's server-side request processing function, not using the "thread pool" technology, but directly when a client connection request arrives, start a new thread to process, after the connection is completed to close the line connector.
By setting up new threads for each connected client, it is easy to implement the requirement to handle multiple clients, enabling multiple clients to work simultaneously (Figure 1). In the internal of each thread, the Client network protocol format is first resolved to resolve the request data sent by clients and then processed separately according to different request commands.
How to handle concurrency
Because the server side needs to handle the requests sent by multiple clients at the same time, it requires concurrent processing of the core data in order to solve the problem of data synchronization. The following is an example of assigning a user ID to introduce the process of server-side concurrency processing. When the client first connects to the server side, the server side feedback a unique serial number to the client. The server side adds 1 for each id,id value.
If multiple clients send requests at the same time, the server-side count can be faulted. This problem can be resolved by using the Synchronized keyword in the Java language to modify processing logic. The UserID (Figure 2), which is used to feedback the function, guarantees synchronization of the data in case of simultaneous access by multiple clients.
Designing service-Side network protocols
Server-side network protocol, the most important function is the server-side processing results of the data feedback to the client, thus controlling the client's logical behavior. When designing a server-side network protocol, you need to consider several feedback directives and the parameters that each feedback needs to be feedback. In addition, consider how to reduce duplication of data.
The success command in the server-side network protocol is used in response to the client's preparation instructions, after the client login succeeds, sends the start instruction to the server side, if the server side cannot match the war user, then the feedback waits for the instruction, so the client will continue to wait for other players. If there are other players that match or have been matched by the server to other players, feedback the initial data instructions and feedback the server-generated room number, initial map data, role data, etc.
After the beginning of the game, the client sends out data instructions at certain times, server-side feedback game Refresh instructions, the current game data feedback to the client. The mobile instructions sent by the client are processed directly on the server side, without feedback instructions.
Request Processing Design Steps
First step: Create a project
Start Eclipse, select "File→new→java Project", set the project name "Bomberserver", and select "Next" to complete the project setup. Then create a new entry class, select "File→new→class", set the name "Server", and click "Finish". The code download address for creating the project is Http://www.shudoo.com/bzsoft.