nginx学习(二)动态加载各模块

其实应该叫做静态加载各模块,因为此加载的过程实际上是在编译的时候完成的,而非运行时根据配置文件动态加载.

假设这样的场景,程序有很多模块,每种模块的配置写在各自的代码中(可能为.cpp,也可能为.h),在调用处只是将其简单申明,然后加载进来,若某一天不需要此模块,只需简单的在调用处屏蔽代码即可.

此代码从nginx代码中抽取,为了简单修改了部分代码,以免引入过多的概念.

  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
 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   ngx_uint_t            ctx_index;
 36   ngx_uint_t            index;
 37 
 38   ngx_uint_t            spare0;
 39   ngx_uint_t            spare1;
 40   ngx_uint_t            spare2;
 41   ngx_uint_t            spare3;
 42 
 43   ngx_uint_t            version;
 44 
 45   void                 *ctx;
 46   ngx_command_t        *commands;
 47   ngx_uint_t            type;
 48 
 49   uintptr_t             spare_hook11;
 50   uintptr_t             spare_hook12;
 51   uintptr_t             spare_hook13;
 52   uintptr_t             spare_hook14;
 53   uintptr_t             spare_hook15;
 54   uintptr_t             spare_hook16;
 55   uintptr_t             spare_hook17;
 56 
 57   uintptr_t             spare_hook0;
 58   uintptr_t             spare_hook1;
 59   uintptr_t             spare_hook2;
 60   uintptr_t             spare_hook3;
 61   uintptr_t             spare_hook4;
 62   uintptr_t             spare_hook5;
 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 // core模块定义处
 70 ngx_module_t  ngx_core_module = {
 71   NGX_MODULE_V1,
 72   NULL,                  /* module context */
 73   NULL,                     /* module directives */
 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 // errlog模块定义处
 86 ngx_module_t  ngx_errlog_module = {
 87   NGX_MODULE_V1,
 88   NULL,                /* module context */
 89   NULL,                   /* module directives */
 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 ngx_module_t *ngx_modules[] = {
105   &ngx_core_module,         //引入core模块
106   &ngx_errlog_module,       //引入errlog模块
107   NULL
108 };
109 
110 
111 int _tmain(int argc, _TCHAR* argv[])
112 {
113   //编历加载各模块
114   for (int i = 0; ngx_modules[i]; i++)
115   {
116     cout<<i<<" type:"<<ngx_modules[i]->type<<endl;
117   }
118     return 0;
119 }

 

posted @ 2012-09-19 14:29  绿色的麦田  阅读(1093)  评论(0编辑  收藏  举报