Nginx Learning (2) Dynamic Loading of modules

Source: Internet
Author: User

In fact, it should be called static loading of each module, because the loading process is actually completed during compilation, rather than dynamic loading according to the configuration file during runtime.

In such a scenario, a program has many modules, and the configuration of each module is written in its own code (may be. CPP, which may also be. h), simply declare it at the call and load it in. If this module is not needed one day, simply block the code at the call.

This code is extracted from nginx code. In order to modify some code, too many concepts are introduced.

1 // ngx_module_load_test.cpp: defines the entry point for the console application. 2 // 3 4 # include "stdafx. H "5 # include <iostream> 6 using namespace STD; 7 8 typedef int intptr_t; 9 typedef unsigned int uintptr_t; 10 typedef unsigned char u_char; 11 12 typedef intptr_t ngx_int_t; 13 typedef uintptr_t ngx_uint_t; 14 15 # define ngx_module_v1 0, 0, 0, 0, 0, 0, 1 16 # define ngx_module_v1_padding 0, 0, 0, 0, 0, 0, 0, 0, 0 17 18 typedef struct {19 size_t Len; 20 u_char * data; 21} ngx_str_t; 22 23 // typedef struct ngx_conf_s ngx_conf_t; 24 struct ngx_command_s {25 ngx_str_t name; 26 ngx_uint_t type; 27 // char * (* Set) (ngx_conf_t * Cf, ngx_command_t * cmd, void * conf); 28 ngx_uint_t conf; 29 ngx_uint_t offset; 30 void * post; 31}; 32 typedef struct ngx_command_s ngx_command_t; 33 34 struct ngx_module_s {35 bytes ctx_index; 36 bytes index; 37 38 bytes spare0; 39 ngx_uint_t spare1; 40 bytes spare2; 41 bytes spare3; 42 43 ngx_uint_t version; 44 45 void * CTX; 46 ngx_command_t * commands; 47 ngx_uint_t type; 48 49 bytes spare_hook11; 50 bytes spare_hook12; 51 bytes spare_hook13; 52 bytes spare_hook14; 53. spare_hook15; 54. spare_hook16; 55. spare_hook17; 56 57. spare_hook0; 58. spare_hook1; 59. spare_hook2; 60. spare_hook3; 61. spare_hook4; 62; 63 uintptr_t spare_hook6; 64 uintptr_t spare_hook7; 65}; 66 67 typedef struct ngx_module_s ngx_module_t; 68 # define ngx_core_module 0x45524f43/* "core" */69 // 70 ngx_module_t ngx_core_module = {71 ngx_module_v1, 72 null,/* module context */73 null, /* module ctictives */74 1,/* module type */75 null,/* init master */76 null,/* init module */77 null, /* INIT process */78 null,/* init thread */79 null,/* exit thread */80 null,/* exit process */81 null, /* exit master */82 ngx_module_v1_padding 83}; 84 85 // the definition of the errlog module 86 ngx_module_t ngx_errlog_module = {87 ngx_module_v1, 88 null,/* module context */89 null, /* module ctictives */90 2,/* module type */91 null,/* init master */92 null,/* init module */93 null, /* INIT process */94 null,/* init thread */95 null,/* exit thread */96 null,/* exit process */97 null, /* exit master */98 ngx_module_v1_padding 99}; 100 101 102 103 104 // module introduction section 105 ngx_module_t * ngx_modules [] = {& ngx_core_module, // introduce core module 106 & ngx_errlog_module, // introduce errlog module 107 null108}; 109 110 111 int _ tmain (INT argc, _ tchar * argv []) 112 {113 // calendar loaded modules 114 for (INT I = 0; ngx_modules [I]; I ++) 115 {116 cout <I <"type: "<ngx_modules [I]-> type <Endl; 117} 118 return 0; 119}

 

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.