In the previous section we introduced the state machine, this section we will add the plug-in module, then we can develop the plug-in according to the public interface, and our server only need to call the corresponding stage of the public function through the state machine, do not care about the implementation details of the plug-in. Our plugin will be loaded in the form of dynamic library so. Plugins
Our plug-in class will act as a base class with member functions as virtual functions, which are then inherited and implemented by plug-in developers.
The plug-in interface for this project corresponds to the stage of the state machine, and each stage provides a function:
/************************************************************************* > File name:plugin.h > Author:j Iange > Mail:jiangezh@qq.com > Created time:2016 February 10 Wednesday 18:46 22 seconds **********************************
/#ifndef _plugin_h #define _PLUGIN_H class Worker;
Class Connection;
typedef enum {Plugin_ready, Plugin_not_ready, plugin_error} plugin_state_t;
Class Plugin {public:plugin ();
Virtual ~plugin ();
virtual bool Init (Connection *con, int index);
virtual bool Requeststart (Connection *con, int index);
virtual bool Read (Connection *con, int index);
virtual bool Requestend (Connection *con, int index);
virtual bool Responsestart (Connection *con, int index);
Virtual plugin_state_t Write (Connection *con, int index);
virtual bool Responseend (Connection *con, int index); virtual void Close (Connection *con, int index);
virtual bool Trigger (worker* Worker, int index);
virtual bool Loadplugin (worker* Worker, int index);
virtual void Freeplugin (worker* Worker, int index);
typedef plugin* (*setupplugin) ();
typedef void (*removeplugin) (Plugin *plugin);
Setupplugin Setup_plugin;
Removeplugin Remove_plugin;
void* Plugin_data;
void* Plugin_so;
int plugin_index;
BOOL plugin_is_loaded;
}; #endif
Dynamic Library Loading
const char *path = So_path;
The path to the dynamic library so/* Dlopen () function opens the specified dynamic-link library file in the specified mode, * and returns a handle to the calling process for Dlsym ().
* Use Dlclose () to uninstall the Open library.
* Header file # include <dlfcn.h> * Rtld_lazy: Before Dlopen returns, no parsing is performed for undefined symbols in the dynamic library * (valid only for function references, always parse immediately for variable references).
*/void *so = Dlopen (path, rtld_lazy);
if (!so) {std::cerr << dlerror () << Std::endl;
return false;
}/* void* dlsym (void* handle,const char* symbol) * Returns the address of the symbol according to the dynamic Link library operation handle (handle) and symbol.
* Use this function not only to get the function address, but also to get the variable address.
* Handle: The pointer returned after opening the dynamic link library by Dlopen; * Symbol: The name of the function or global variable required to get it.
* Setupplugin and Removeplugin are functions in the plugin module; * Setupplugin returns a dynamically allocated plug-in object; * Removeplugin frees the memory space of the plug-in object.
*/Plugin::setupplugin Setup_plugin = (plugin::setupplugin) dlsym (So, "setupplugin");
Plugin::removeplugin Remove_plugin = (plugin::removeplugin) dlsym (So, "removeplugin");
if (!setup_plugin | |!remove_plugin) {std::cerr << dlerror () << Std::endl;
Dlclose (SO);
return false;
}//Based on the function handle obtained above, get the plug-in object Plugin *plugin = Setup_plugin ();
if (!plugin) { Unload Open Library Dlclose (SO);
return false; }//... return true;