First, preface
This game server architecture is a single-service form, that is, all game logic in a project, no distinction between landing server, combat server, the world server and so on. This architecture has been successfully applied to a variety of page tour servers. There is no relevant business logic implemented in this framework, only simple testing with the registered login feature.
Server Engineering---gameserver (https://github.com/yongzhidai/GameServer.git)
Test Client---testclient, simulate client-to-server communication for testing server functions (HTTPS://GITHUB.COM/YONGZHIDAI/TESTCLIENT.GIT)
Project Tool---Tools, jar packages for server building, and related Eclipse plug-ins (https://github.com/yongzhidai/Tools.git)
Second, the server operating environment
This server is based on Tomcat, so Gameserver is a Web project, but this game server is also based on socket communication and does not use Tomcat for HTTP communication. The game server is started by adding a listener to Web. XML in the Web-inf directory. This listener is used to listen for Tomcat start and stop, and when Tomcat starts, the game server starts listening on the port, and when Tomcat stops, it does the corresponding destruction operation.
Many people may wonder why they are based on Tomcat.
The Tomcat-based operation has many advantages:
1, convenient debugging. While we were debugging the server program, we took advantage of Tomcat's hot load function so that the modified code could take effect without restarting it.
2. Easy to pack and deploy. Deploying a Web program to Tomcat is an easy thing to do, relying on third-party jar packages Tomcat will also help.
3. Use the data sources provided by Tomcat. This may not be an advantage, but at least it's pretty handy.
4, to facilitate the development of GM. We can easily add an opportunity to the sub-architecture of the Web management system, directly manage the current game server online players
Third, the communication layer
Java Development socket server is most commonly used Mina and Netty these two NIO frameworks. Online test said Netty performance slightly better. However, because Mina in the production environment did not encounter any problems, and I am familiar with Mina source code, or the use of Mina as a communication layer framework. (Netty3 and Mina code structure is similar, but netty4 change relatively large, I did not understand the source slightly)
Communication protocol: Flag (1 byte) +length (4 byte, message number plus the length of message content) +protocol code (4 byte) +content
Flag: is a reserved identity
Length: Indicates the message number and the duration of the message content
Protocol code: Custom message number, select the appropriate message processor with the second message number, the natural message number is not repeatable, an int indicates that the range is sufficient to use
Content: The contents of the message, an array of ordered data. Protocol code and content are defined in the ' Message Protocol ' document when developing features, such as "message protocol. xls" in the Gameserver project.
The message content is not encrypted in this project and it is easy to add it to encrypt.
Message processing:
When the server receives a message from the client, Msgdispatcher chooses the corresponding msgprocessor for its message number to process. Msgprocessor will read the content to do the appropriate processing.
Iii. Persistence layer
The persistence layer of this schema uses MyBatis. But in the production environment Ibatis has not been a problem, or can be re-use. And the use of MyBatis must each open a connection use must close off, how much feeling a bit troublesome (if you use spring AOP to avoid this problem). In Ibatis, it provides a sqlmapclient class, which is thread-safe, and we just need to get the database operation to Sqlmapclient, which will help us connect the open and close databases.
Do game development all know that the game on the database requirements are relatively low, we do not need any complex database operations, so in the persistence layer we use Ibator to do code generation tools (the corresponding plug-ins in the Tools project), can save a lot of development work.
Java Game Server Setup