nginx listen

     nginx中使用ngx_listening 来监听外部的连接请求

     struct ngx_listening_s { ngx_socket_t fd; //socket struct sockaddr *sockaddr; //地址 socklen_t socklen; /* size of sockaddr */ size_t addr_text_max_len; //地址文本形式长度 ngx_str_t addr_text; //地址文本 198.168.1.1:80 int type; //类型 ,sock_stream 或者 sock_dgram ,前者连接,后者无连接 int backlog; int rcvbuf; int sndbuf; /* handler of accepted connection */ ngx_connection_handler_pt handler; //处理函数 void *servers; /* array of ngx_http_in_addr_t, for example */ ngx_log_t log; ngx_log_t *logp; size_t pool_size; /* should be here because of the AcceptEx() preread */ size_t post_accept_buffer_size; /* should be here because of the deferred accept */ ngx_msec_t post_accept_timeout; ngx_listening_t *previous; ngx_connection_t *connection; //对应连接 unsigned open:1; unsigned remain:1; unsigned ignore:1; unsigned bound:1; /* already bound */ unsigned inherited:1; /* inherited from previous process */ unsigned nonblocking_accept:1; unsigned listen:1; unsigned nonblocking:1; unsigned shared:1; /* shared between threads or processes */ unsigned addr_ntop:1; #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) unsigned ipv6only:2; #endif #if (NGX_HAVE_DEFERRED_ACCEPT) unsigned deferred_accept:1; unsigned delete_deferred:1; unsigned add_deferred:1; #ifdef SO_ACCEPTFILTER char *accept_filter; #endif #endif };

 

     这个的handler就是接受后的处理函数,在初始化的时候先赋值,连接过来的时候直接调用。

 

 

     一 listen 空间分配

        在ngx_cycle.c中,分配了空间,实际就是分配一个数组,但是里面的元素为空。 cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t)); if (cycle->listening.elts == NULL) { ngx_destroy_pool(pool); return NULL; } cycle->listening.nelts = 0; cycle->listening.size = sizeof(ngx_listening_t); cycle->listening.nalloc = n; cycle->listening.pool = pool;  

 

 

     二 新建listen

         在ngx_connection.c中的ngx_create_listening函数中,从 cycle->listening中新建一个listening, 接着对数据的成员进行赋值

 

      三 赋值

           ngx_http.c 中的 ngx_http_add_listening 函数给创建的listening赋上处理函数  ls->handler = ngx_http_init_connection;

 

      四 使用

           在 ngx_event_accept.c中的  ngx_event_accept函数中   ls->handler(c);

你可能感兴趣的:(nginx,struct,Stream,socket,null,delete)