Talking about the design of online games
- Server programming (2)
Keywords: UNIX, Socket, Communication Module, Thread Management Module, Message Delivery Module, Game Rule Module, Game World Management Module
Thank you very much for your support of the previous article. Under everyone's support, I decided to launch a brief talk about online game development (2).
First, once again, online game development is extremely difficult, high technical content, programming knowledge involves network programming, operating system processes, thread programming, graphic image programming (DirectX / OpenGL), Win32 API programming (developing under Windows), And various algorithms and data structures, while the design staff planning capacity is also quite high. If you can't conceive a game world that attracts players, it will definitely lead to failure.
At present, the domestic online game market is occupied by Korean games, and the situation is cold. In an environment where domestic online game programming resources, I hope to say some of my experiences and ideas, for everyone to refer, and give a throwing jade.
For the architecture of me in shallow (1), if you have better modifications, you are welcome to discuss the revision, and my country's online game development is welcome to the world-class standard. At least, it is necessary to be in the domestic market!
Ok, I don't say much, the text begins.
In shallow (1), I have not described in detail about the game world management module and communication module. This article will be added.
Game World Management Module:
This module specifically manages the data model in the game world, that is, it means that the objects in all games are basically managed by him, so this module is extremely complex, even in large systems, it can also divide it into many children. Modules work together.
How can this module package? First, it is naturally a message processing class because the game World Management Module is also a message-driven. After each of the modules receive a message, the message type is checked, and the forwarding type or the management type message, if it is a forward type In order to forward the message to the message destination module. If it is a manager of the management type, the object of management is checked, and the method of management, and then perform the management method. Therefore, we also need is a method of identifying messages, as well as some data and operational data.
Game rule module
This module performs business logic according to the rules set by the game planner. Similarly, the first need to package is also a message processing class. Then it is the discrimination message, according to the message prompt, then encapsulate the processing result into a message, send the management module, basically the same as the game world management module mode.
The above is talked about the package idea of two modules. However, in fact, these two modules are impossible to use it as that, many friends also talk about this architecture and not suitable for large online games, then true large networks What kind of architecture is it?
Just like our OSI models and TCP / IP models, only the latter can truly use in industrial standards, the former is alive, but he has been packaged too thin, too complicated, not suitable for the current situation. In the real online game, the above two modules are together! I am collectively referred to as the game world module. Please pay attention to this model (send a few pictures, so I use the text, please understand)
Client Communication Module Game World Module | | | | |
| Send Internet Message | |
| --------------------- à | |
| | ─ ┐ |
| | | Unpacking |
| | <─ ┘ |
| | Send Message |
| | ---------------------> |
| | (Via message module) |
| | | | ┐┐
| | | | | Rule determination
| | | | <─ ┘
| | | | |
| | | | ┐┐
| | | | Modify the game world
| | Send Message | <┘
| || <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Send Internet Message | (via message module) |
| <------------------------- | |
| | | | |
As can be seen from the figure, in fact, the game rule module and the game management module are combined together, that is, the two modules do not need messaging, they are just a simple function call relationship. The rules determine that the main job to do is to distinguish the news, translate our messages into objects to objects.
Our game world has a lot of objects, and an object can also carry multiple objects, and the object can be increasing, expanded. Whenever we add or expand a new object, we can bring it include, and add to his method to the rule module.
One of the points to explain here is, in fact, all objects are not different. Everyone is a data model, whether you are a person, or a tree, or a props, or even magic, they are just some attributes. These attributes, all exist in our profile, and can even exist in the database. Bring the property when you create an object. Of course, about the idea of the game world object, there are a lot of good proposals, I even thought about the way you can do, as long as it's attribute, anyway, we just change the properties of the object, and how to change these properties to write in a certain format In a file or database, such as INC XX 0.3 represents the XX property 30%, which can make it easy to change the rules and objects.
Below, I propose some views for everyone:
First of all, there may be questions, such a system architecture seems to be very slow, how to maintain so many players online? How can I keep the speed if there are so many objects to handle?
1. The server is not a PC that you use, as long as you can use the server and PC machine, you know their difference, use UNIX development to ensure that the game can run on the server.
2. Many people think that online games should support hundreds of thousands of players at the same time, such as Legends, Lianzhong, they are indeed 30-4 million people online! In fact, everyone looks carefully, how many people per server, some servers are only 6xx people, this is the true situation of online games.
3. UDP and TCP controversial, some friends may think that UDP should be used, because it is fast, in fact, this problem is nothing better, why don't our FTP not use UDP? If you use udp, we have to pack a set of TCP confirmation mechanisms.
4. For synchronous problems, in general, the client is really what is hairdry, but to control the transmission time, this is also to prevent shift gears and solve synchronization problems. For example, once the client issues a moving command, the client must first determine if the time is intervals, and then determine whether it can move, can move the message to the server side, start moving, if the server is sent back to the message, then Just succeed, otherwise, the characters on the screen will be pulled back.
Connection pool technology
A number of friends concerned is: assign a thread for each connection, is it too wasteful! In fact, everyone knows it, he is allocated for each connection! The thread is much smaller than the process overhead. If the number of users is not a lot, it is no problem. But do the number of users don't understand how to understand: Linux6.2 test, each process can create up to 1000 threads (actually 1024), Win2000 Advance Server SP3 test, each process can create up to 2000 threads (The actual should be 2048), let me understand. However, how to say that the connection thread is just in the network communication, to support more users, too disappointment, so you will introduce the connection pool technology.
The so-called connection pool, in practice, we have created some threads in advance. When the communication module is sent, find an idle thread, then give it the message you want to send, let it send it. In this way, create a small thread is enough. This is a bit similar to the Apache process pretreatment, and more like JDBC database connection pools. This requires encapsulating a connection pool control class.
In summary, if you want to use the connection pool technology, it will make the communication module, but it can save a large amount of system resources. It is recommended that you can choose whether to use the connection pool technology when you design, so according to different systems, There can be different configurations.
Section
This discussion is here, I don't know how much against opinion this time, but I really hope to see everyone's opinion, because any software development itself is the benevolent people see the benevolence, the problem of wise, here, there is no absolute Standard, there is no absolute truth, we want to realize the way, I hope everyone can give good advice while giving opinions.