Due to the limited space and time, this article can only introduce some knowledge, mainly talk about how to design the basic modules of the server. In addition, read this article and require a certain UNIX programming basis.
This game server-side operating system uses UNIX because UNIX is a standard server operating system to ensure the stability of online games. Therefore, all of the following programming will be performed for UNIX.
The overall architecture of the server is as follows: Communication module, message delivery module, game rule module, thread management module, game world management module.
Communication module:
The communication module mainly implements communication features with the client. In fact, the communication module is a package of the set of snaps. Socket is a network communication base under UNIX. For a Socket we can read and write, the read data is from the client, and the written data is available to the client.
Socket mainly has two types of blocking sockets and no block sockets. For no blocking sleeve, after reading and writing, no matter whether the number of bytes read and written is required, it will return immediately; If the number of readscripts is not enough, the function will be blocked until all the data to be processed is handled back. It can be seen that if there is no blocking sleeve, it will make network transmission very unstable, and it is difficult to control the transmission when the network environment is not good. Therefore, for our system, blocking mode will be used.
What is the next question, how do you know when to read, if you read the data from the blocking socket when you are inappropriate, the thread is likely to be blocked, and SELECT - multiplexing technology, principle It is monitored for our socket. If there is a read event on the socket, send the message module to the portable object to Socket, read and write it.
The following is a simple package for Socket:
Class Ssocket
{
fd_set * sockset; // fd_set, that is, our SELECT listening collection
CHAR islistensocket; // Is it a listener?
INT serverport; // listen to the monitor port number of the socket
struct sockaddr_in addr; // address information
PUBLIC:
Ssocket ();
~ SSOCKET ();
Int socket; // Socket
INT CREATELISTENSOCKET (fd_set * sset, int port, char * addr); // initial // a listener SOCKET
INT Acceptsocket (int Listen_fd, fd_set * sset); // Initialize an in-laws SOCKET
INT CloseSocket (); // Close Socket
Int Sendbuf (Void * BUF, INT Size); // Send Data
Int Recvbuf (Void * buf, int size); // receive data
Int setsocketfd (); // Add Socket to the listening collection
INT CLRSocketfd (); // Clear Socket from the listening collection
}
Through the package of Socket, we completed the basic tasks of the communication module. The next step is to transmit a message on the network. At this time, you need to continue the package, first define a message structure, then read and write messages, the message structure is different according to each game, there is no more discussion. Thread management module:
Since we use blocking mode, this means that you must create a separate thread for each socket, otherwise it is likely to cause the server to stop working. Therefore, we need to package the thread, the package content mainly includes: thread function addresses, thread start time, thread last blocking time, thread maximum blocking time, thread START method, thread STOP method. The thread STOP method can achieve the purpose of killing the thread by sending a signal to the thread.
After the thread class package is complete, we can start writing the management module code. The management module is actually a thread. The first function is to monitor whether each thread is blocked, and the last blocking time and the maximum blocked time are completed by checking the thread. Once the current time exceeds the thread maximum blocking time plus the last blocking time, the thread blocking timeout can be determined, and the KILL thread is required at this time. In addition, some other root-related management methods are responsible for this module.
Message delivery module:
Objects, how to pass messages between modules, which is also the focus of server-side design. To give a simplified example, if a player has sent a message to another player, then receive data by the communication module, then use the message delivery module to notify another player, and then call the communication module of another player to call the communication module Send it back to the client, which is the role of the message delivery module.
How do you pack the message module? The first step is to do a MessageBox class, which is a stack that is used to install a message, mainly by the POP and PUSH methods, of course, you can't forget that a storage message data structure must be implemented. The second step is the HandleMessage class, which is the main implementation of our message module. There is a waitMessage method. After calling this method, the thread will be blocked until the message arrives. This can be implemented by an unknown signal, that is, SEM under UNIX, which can increase or reduce the amount of semaphore to implement mutual exclusion. However, someone will certainly ask, why do we have to have waitmesssage, is this not a thread blocking? In fact, the server side is a passive drive model, just like not going to step on the throttle, if there is no message to drive, the server will not run.
After the above package, the message has become very simple, we can use the SendMessage method, and the realization of SendMessage is also very simple. This allows this to receive the message.
other:
The rest of the two modules are related to the game, in fact they are two more complex modules, depending on the game to be written, the two modules are different. However, what are they doing, and the following will be explained by an example:
Suppose we are now developing a RPG game, our players let the game characters on the screen step forward, when sending the mobile request to the server side, the server-side communication module is received, notify the game world management module, game The World Management Module calls the game rule module, and determines whether the player requests if the rules are compliant (whether it is legal), if it can be moved, then change its coordinate by the game management module, and finally inform the related players. That is, the rule module is actually dedicated to the game business logic, and the management module is actually dedicated to the game object. summary
The development of online games is quite difficult. In fact, this article can only play the role of throwing bricks, only telling you a way to get started, the rest, there is also the annoying line to implement code.
Very interested in online game design with me SUNXING@cosco-logistics-km.com.cn