In my first web game project, the server used the plug-in mechanism, but the plug-ins were all written in C # at that time, and it was not clear how to implement it. In the next several browser game projects, you have taken over the server by yourself, so there is no need to write plug-ins. The server of the next web game project requires the cooperation of many people. Therefore, I want to build other modules into a plug-in mode independently, which is also in the exploration phase. A simple version of plug-in mechanism is implemented by searching online materials and organizing your own documents. ImplementationCodeAs follows:
The object. HPP file implements the base classes of all plug-in classes. All plug-ins must inherit this class.
1 # Ifndef _ object_hpp __ 2 # Define _ Object_hpp __ 3 4 Class Object 5 { 6 Public : 7 Object (){} 8 Virtual ~ Object (){} 9 Virtual Int Dump () = 0 ; 10 }; 11 12 # Endif // _ Object_hpp __
The testobject. HPP file and testobject. cpp file are the implementation classes of the plug-in.
1 # Ifndef _ testobject_hpp __ 2 # Define _ Testobject_hpp __ 3 4 # Include " Object. HPP " 5 6 Class Testobject:Public Object 7 { 8 Public : 9 Virtual Int Dump (); 10 }; 11 12 # Endif // _ Testobject_hpp __
1 # Include " Testobject. HPP " 2 # Include <iostream> 3 4 Extern " C " 5 { 6 Void * Instance () 7 { 8 Return New Testobject; 9 } 10 } 11 12 Int Testobject: dump () 13 { 14 STD: cout <" Class Name: testobject function name: dump () " < STD: Endl; 15 Return 0 ; 16 }
The file main. cpp loads and uses plug-ins. If you have more details, you can write a class for plug-in Management, which is responsible for plug-in registration and object generation. At present, we have not made further research only to demonstrate the function.
1 # Include <dlfcn. h> 2 # Include <iostream> 3 # Include " Object. HPP " 4 5 Typedef object * Instance (); 6 7 Int Main () 8 { 9 Void * Handle = dlopen ( " ./Libto. So " , Rtld_lazy ); 10 If (! Handle) 11 { 12 STD: cout <dlerror () < STD: Endl; 13 Return 1 ; 14 } 15 16 Instance * getinstance = (instance *) dlsym (handle, " Instance " ); 17 If (! Getinstance) 18 { 19 STD: cout < " Error instance Function " < STD: Endl; 20 Return 1 ; 21 } 22 23 Object * OBJ = Getinstance (); 24 If ( 0 ! = OBJ) 25 { 26 OBJ-> Dump (); 27 } 28 29 Delete OBJ; 30 Dlclose (handle ); 31 32 Return 0 ; 33 }
The last is the MAKEFILE file.
1Source =Testobject. cpp2 Makeso: $ {source}3G ++-FPIC-shared $ ^-O libto. So4 5 Main:6G ++-O main-LDL main. cpp7 8 Clean:9Rm-RF main *. O *. So10 11ALL: makeso main
ProgramThe compilation method is very simple, as long as make all. The running process is as follows:
1 [Kiven @ localhost plugin] $ make all2G ++-FPIC-shared testobject. cpp-O libto. So3G ++-O main-LDL main. cpp4[Kiven @ localhost plugin] $ ./Main5 ClassName: testobject function name: dump ()6[Kiven @ localhost plugin] $
The entire program is very simple. The object. HPP main. cpp files are the main program, that is, the functions to be implemented in the framework of the game server. Testobject. HPP and testobject. cpp are the specific implementations of plug-ins. They can be understood as the implementations of specific game modules. The plug-in mechanism implemented here is a prototype and can only be used to demonstrate functions. When using the game server, the plug-in also needs to access the classes, objects, and methods in the main program.