When the game server starts, the server loads the dynamic link library of each game logic processing module based on the information in the configuration file, and then calls the dllcreate function of the module to initialize the module.
The configuration file can be as follows:
Modulecount = 30
Module1 = battlesys
Module2 = equipsys
Module3 = friendsys
Module4 = shopsys
Module5 = skilllsys
......
The prototype of the dllcreate function in the module may be as follows:
Imodule * dllcreate ()
{
Return new cmodule ();
}
The return value of a function is a pointer to the imodule interface class. This interface class is required by each game logic processing module, in addition, each module has a specific class cmodule that continues from the imodule interface class.
The returned imodule is saved to the game logic management class clogicmanager.
STD: Map <imodule *> mapallmodule;
After fully loaded modules, call the imodule: init function. The pseudo code is as follows:
Foreach imodule * P in mapallmodule
P-> Init (ilogicmanager *) This );
At this time, the initialization function of each module will be called. In the initialization function, each module registers the message numbers of interest to ilogicmanager and saves them
STD: hash_multimap <msgid, imodule *> mapmsghandler;
After initialization, the network module of the server sends the message to ilogicmanager whenever it receives the message. Then ilogicmanager finds the relevant imodule in mapmsghandler;
Imodule: recvmessage (MSG * PMSG, player * pplayer );
Each module receives messages of interest in cmodule: recvmessage. Then, you can generate the corresponding message processor cxxxhandler Based on the typeid in MSG, and then process the message.