回调函数相关 核心模块定义的回调函数:
typedef struct { //模块名,即ngx_core_module_ctx结构体对象的 ngx_str_t name; //解析配置项茜,nginx框架会调用create_conf方法 void *(*create_conf)(ngx_cycle_t *cycle); //解析配置项完成后,nginx框架会调用init_conf方法 char *(*init_conf)(ngx_cycle_t *cycle, void *conf); } ngx_core_module_t; ngx_core_module_t module;module->create_conf(cycle);如何调用到相应的钩子函数ngx_core_module_create_conf,的呢?
是这样做的
static ngx_core_module_t ngx_core_module_ctx = { ngx_string("core"), ngx_core_module_create_conf, ngx_core_module_init_conf };使用ngx_core_module_t结构体定义一个变量,在变量中实现函数名,在后面实现函数体。
模块定义结构体中的回调函数如何调用:
ngx_module_s{ . . . ngx_int_t (*init_master)(ngx_log_t *log);//初始化master ngx_int_t (*init_module)(ngx_cycle_t *cycle);//初始化模块 ngx_int_t (*init_process)(ngx_cycle_t *cycle);//初始化工作进程 ngx_int_t (*init_thread)(ngx_cycle_t *cycle);//初始化线程 void (*exit_thread)(ngx_cycle_t *cycle);//退出线程 void (*exit_process)(ngx_cycle_t *cycle);//退出工作进程 void (*exit_master)(ngx_cycle_t *cycle);//退出master . . . }同样的用定义变量的方式,来看模块这个结构体中的回调函数
ngx_module_t ngx_event_core_module = { NGX_MODULE_V1, &ngx_event_core_module_ctx, /* module context */ ngx_event_core_commands, /* module directives */ NGX_EVENT_MODULE, /* module type */ NULL, /* init master */ ngx_event_module_init, /* init module */ ngx_event_process_init, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING };像下面一样,都是采用直接调用的方式来处理各自不同的回调函数。 ngx_modules[i]->init_module(cycle),用这个语句可以实现所有模块,初始化module时的各种不同的调用函数,如在ngx_event_core_module模块中调用ngx_event_module_init函数,在ngx_conf_module模块离开工作进程时调用ngx_conf_flush_files等。