I saw the Mudo network library recommended by my friends last month. After the code is completed, I learned that it is an open-source file from my colleagues in China, and I admire it very much. I checked the structure of the Mudo code using the hands-on and 035 versions. It seems that it is a mature and easy-to-use network library than the slave. I also have my own network library. Although I do not dare to say how powerful a network library is, after all, it has been operating stably on the Internet for several years. When multiple groups are at the same time, I have been running online for about 30 million RMB, A single group of servers can also run on W people. After several years of experience in game server, I feel that stability is the core and most important part of the online game server, and the efficiency is still the second. This is also determined by the group architecture commonly used by online game servers, from the operational point of view, sometimes even if a group of servers can run up to W people, the Operation will force the upper limit to be set to several k people, which is related to the gameplay and operation methods, I won't go into detail!
Muduo listener uses the reactor mode based on the message callback mechanism, which is frequently used by network libraries. The usage of the entire network library is more convenient than that of the listener, and only a few TCP events need to be concerned: TCP connection, data collection, TCP shutdown, etc. The callback functions can be used simply by adding the callback function to these events. The specific usage depends on Chen Shuo's muduo network library.
If muduo needs to be integrated into a general game server, what is the most important? Group package and package. If it is for external communication, the encryption and decryption module is also required. The following methods are used for the secondary exam:
The first step is to define a base class, such as gameserver, which serves as the game TCP server (both gateway server and Logic Server may need to use TCP server), so it must include tcpserver, and the TCP accept connection and receive data, close the connection and other events into the tcpserver callback.
Step 2: After the TCP connection calls back the onconnection, a new logicsession is required (this logicsession is a class responsible for processing data group packets, encryption and decryption, and Message Queue ), can establish a tcpconnection pointer to logicsession ing. After the TCP connection calls back the onmessage, locate the logicsession Based on the tcpconnection key value and push the data to the logicsession class.
Step 3: After receiving data, logicsession must define a Message Queue class, such as msgqueqe class, which is responsible for decrypting the received binary data into a group package, in addition, the group of packages is stored in the message queue for later game logic.
After completing the preceding steps, you can start the job. For example, to create a game gateway, you only need to define a clientserver class to inherit from gameserver (the reason for defining clientserver is that every TCP Service may do different things, therefore, you need to implement your own sub-classes based on your own needs. For example, if the game gateway must count the number of connected users, You need to expose the interface for obtaining the number of logicsessions in the clientserver ), and a new clientserver incoming port, such as port20000, first listens for TCP connections from 20000port. After receiving the connection data, logicsession automatically processes and saves it to each Message Queue connected to itself. Assume that the logic of the game server is single-threaded, then you only need to start a thread to traverse all the logicsessions from the 20000port, and pop the message queue of each logicsession, and distributed to the corresponding message processing module for unified processing. In this way, a simple TCP data transmission and processing from the client to the game gateway can be completed.
(Unfinished, to be continued)
Muduo network library usage experience