"SLIGHTTPD" server project based on LIGHTTPD Architecture Combat (10)-Plugins & Dynamic Libraries

Source: Internet
Author: User

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.

Plug - ins

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: Jiange > Mail: [email protected] > Created time:2016 February 10 Wednesday 18:46 22 seconds ********************************* ***************************************/#ifndef _plugin_h#define _PLUGIN_HclassWorker;classConnection;typedef enum{plugin_ready, Plugin_not_ready, plugin_error} plugin_state_t;classplugin{ Public: Plugin ();Virtual~plugin ();Virtual BOOLInit (Connection *con,intindex);Virtual BOOLRequeststart (Connection *con,intindex);Virtual BOOLRead (Connection *con,intindex);Virtual BOOLRequestend (Connection *con,intindex);Virtual BOOLResponsestart (Connection *con,intindex);Virtualplugin_state_t Write (Connection *con,intindex);Virtual BOOLResponseend (Connection *con,intindex);Virtual voidClose (Connection *con,intindex);Virtual BOOLTrigger (worker* Worker,intindex);Virtual BOOLLoadplugin (worker* Worker,intindex);Virtual voidFreeplugin (worker* Worker,intindex);typedefplugin* (*setupplugin) ();typedef void(*removeplugin)        (Plugin *plugin);        Setupplugin Setup_plugin; Removeplugin Remove_plugin;void* PLUGIN_DATA;void* PLUGIN_SO;intPlugin_index;BOOLplugin_is_loaded;};#endif
Dynamic Library Loading
const CHAR*Path=So_path;//The path of the dynamic library soThe /* 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:: SetuppluginSetup_plugin=(Plugin:: Setupplugin) Dlsym (So,"Setupplugin"); Plugin:: RemovepluginRemove_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, gets the plug-in objectPlugin*Plugin=Setup_plugin ();if(!Plugin) {//Unload an open libraryDlclose (SO);return false;}//...return true;

"SLIGHTTPD" server project based on LIGHTTPD Architecture Combat (10)-Plugins & Dynamic Libraries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.