nginx-0.0.1-2003-06-06-18:59:20 import
diff --git a/src/event/modules/ngx_iocp_module.c b/src/event/modules/ngx_iocp_module.c
index 46f09a6..44c155e 100644
--- a/src/event/modules/ngx_iocp_module.c
+++ b/src/event/modules/ngx_iocp_module.c
@@ -1,28 +1,80 @@
 
+/*
+ * Copyright (C) 2002-2003 Igor Sysoev, http://sysoev.ru
+ */
+
+
 #include <ngx_config.h>
-
 #include <ngx_core.h>
-#include <ngx_log.h>
-#include <ngx_errno.h>
-#include <ngx_time.h>
-#include <ngx_connection.h>
 #include <ngx_event.h>
-#include <ngx_event_timer.h>
-
-#include <ngx_iocp_module.h>
 
 
-int ngx_iocp_threads = 0;;
+typedef struct {
+    int  threads;
+} ngx_iocp_conf_t;
 
 
-static HANDLE        iocp;
-static ngx_event_t  *timer_queue;
+static int ngx_iocp_init(ngx_log_t *log);
+static void ngx_iocp_done(ngx_log_t *log);
+static int ngx_iocp_add_event(ngx_event_t *ev, int event, u_int key);
+static int ngx_iocp_process_events(ngx_log_t *log);
+static void *ngx_iocp_create_conf(ngx_pool_t *pool);
+static char *ngx_iocp_init_conf(ngx_pool_t *pool, void *conf);
 
 
-int ngx_iocp_init(int max_connections, ngx_log_t *log)
+static ngx_str_t      iocp_name = ngx_string("iocp");
+
+static ngx_command_t  ngx_iocp_commands[] = {
+
+    {ngx_string("iocp_threads"),
+     NGX_EVENT_CONF|NGX_CONF_TAKE1,
+     ngx_conf_set_num_slot,
+     0,
+     offsetof(ngx_iocp_conf_t, threads),
+     NULL},
+
+    ngx_null_command
+};
+
+
+ngx_event_module_t  ngx_iocp_module_ctx = {
+    &iocp_name,
+    ngx_iocp_create_conf,                  /* create configuration */
+    ngx_iocp_init_conf,                    /* init configuration */
+
+    {
+        ngx_iocp_add_event,                /* add an event */
+        NULL,                              /* delete an event */
+        NULL,                              /* enable an event */
+        NULL,                              /* disable an event */
+        NULL,                              /* add an connection */
+        NULL,                              /* delete an connection */
+        ngx_iocp_process_events,           /* process the events */
+        ngx_iocp_init,                     /* init the events */
+        ngx_iocp_done                      /* done the events */
+    }
+
+};
+
+ngx_module_t  ngx_iocp_module = {
+    NGX_MODULE,
+    &ngx_iocp_module_ctx,                  /* module context */
+    ngx_iocp_commands,                     /* module directives */
+    NGX_EVENT_MODULE,                      /* module type */
+    NULL                                   /* init module */
+};
+
+
+static HANDLE  iocp;
+
+
+static int ngx_iocp_init(ngx_log_t *log)
 {
-    iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
-                                  NULL, 0, ngx_iocp_threads);
+    ngx_iocp_conf_t  *cf;
+
+    cf = ngx_event_get_conf(ngx_iocp_module);
+
+    iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, cf->threads);
 
     if (iocp == NULL) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
@@ -30,12 +82,11 @@
         return NGX_ERROR;
     }
 
-    timer_queue = ngx_event_init_timer(log);
-    if (timer_queue == NULL) {
+    if (ngx_event_timer_init(log) == NGX_ERROR) {
         return NGX_ERROR;
     }
 
-    ngx_event_actions.process = ngx_iocp_process_events;
+    ngx_event_actions = ngx_iocp_module_ctx.actions;
 
     ngx_event_flags = NGX_HAVE_AIO_EVENT|NGX_HAVE_IOCP_EVENT;
 
@@ -43,15 +94,26 @@
 }
 
 
-int ngx_iocp_add_event(ngx_event_t *ev)
+static void ngx_iocp_done(ngx_log_t *log)
+{
+    if (CloseHandle(iocp) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
+        "iocp CloseHandle() failed");
+    }
+
+    ngx_event_timer_done(log);
+}
+
+
+static int ngx_iocp_add_event(ngx_event_t *ev, int event, u_int key)
 {
     ngx_connection_t  *c;
 
     c = (ngx_connection_t *) ev->data;
 
-    ngx_log_debug(ev->log, "iocp: %d, %08x:%08x" _ c->fd _ ev _ &ev->ovlp);
+    ngx_log_debug(ev->log, "iocp add: %d, %08x:%08x" _ c->fd _ key _ &ev->ovlp);
 
-    if (CreateIoCompletionPort((HANDLE) c->fd, iocp, (DWORD) ev, 0) == NULL) {
+    if (CreateIoCompletionPort((HANDLE) c->fd, iocp, key, 0) == NULL) {
         ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
                       "CreateIoCompletionPort() failed");
         return NGX_ERROR;
@@ -61,17 +123,16 @@
 }
 
 
-int ngx_iocp_process_events(ngx_log_t *log)
+static int ngx_iocp_process_events(ngx_log_t *log)
 {
     int                rc;
+    u_int              key;
     size_t             bytes;
     ngx_err_t          err;
     ngx_msec_t         timer, delta;
-    ngx_event_t       *ev, *e;
+    ngx_event_t       *ev;
     ngx_event_ovlp_t  *ovlp;
 
-    ngx_log_debug(log, "iocp");
-
     timer = ngx_event_find_timer();
 
     if (timer) {
@@ -84,15 +145,12 @@
 
     ngx_log_debug(log, "iocp timer: %d" _ timer);
 
-#if 1
-    rc = GetQueuedCompletionStatus(iocp, &bytes, (LPDWORD) &e,
+    rc = GetQueuedCompletionStatus(iocp, &bytes, (LPDWORD) &key,
                                    (LPOVERLAPPED *) &ovlp, timer);
-    ngx_log_debug(log, "iocp: %d, %d:%08x:%08x" _ rc _ bytes _ e _ ovlp);
+
+    ngx_log_debug(log, "iocp: %d, %d:%08x:%08x" _ rc _ bytes _ key _ ovlp);
+
     if (rc == 0) {
-#else
-    if (GetQueuedCompletionStatus(iocp, &bytes, (LPDWORD) &e,
-                                  (LPOVERLAPPED *) &ovlp, timer) == 0) {
-#endif
         err = ngx_errno;
 
         if (ovlp == NULL) {
@@ -118,16 +176,43 @@
 
 ngx_log_debug(log, "iocp ev: %08x" _ ev);
 
-        if (ev == e) {
-            /* it's not AcceptEx() completion */
+        switch (key) {
+        case NGX_IOCP_IO:
             ev->ready = 1;
             ev->available = bytes;
+            break;
+
+        case NGX_IOCP_ACCEPT:
+            break;
         }
 
-ngx_log_debug(log, "iocp ev: %08x" _ ev->event_handler);
+ngx_log_debug(log, "iocp ev handler: %08x" _ ev->event_handler);
 
         ev->event_handler(ev);
     }
 
     return NGX_OK;
 }
+
+
+static void *ngx_iocp_create_conf(ngx_pool_t *pool)
+{
+    ngx_iocp_conf_t  *cf;
+
+    ngx_test_null(cf, ngx_palloc(pool, sizeof(ngx_iocp_conf_t)),
+                  NGX_CONF_ERROR);
+
+    cf->threads = NGX_CONF_UNSET;
+
+    return cf;
+}
+
+
+static char *ngx_iocp_init_conf(ngx_pool_t *pool, void *conf)
+{
+    ngx_iocp_conf_t *cf = conf;
+
+    ngx_conf_init_value(cf->threads, 0);
+
+    return NGX_CONF_OK;
+}
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index f1688bb..3846d5f 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -6,11 +6,16 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_connection.h>
 #include <ngx_event.h>
 #include <ngx_kqueue_module.h>
 
 
+typedef struct {
+    int  changes;
+    int  events;
+} ngx_kqueue_conf_t;
+
+
 static int ngx_kqueue_init(ngx_log_t *log);
 static void ngx_kqueue_done(ngx_log_t *log);
 static int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags);
@@ -210,7 +215,7 @@
     c = ev->data;
 
 #if (NGX_DEBUG_EVENT)
-    ngx_log_debug(ev->log, "kqueue set event: %d: ft:%d f:%08x" _
+    ngx_log_debug(ev->log, "kqueue set event: %d: ft:%d fl:%08x" _
                   c->fd _ filter _ flags);
 #endif
 
@@ -322,13 +327,13 @@
 #if (NGX_DEBUG_EVENT)
         if (event_list[i].ident > 0x8000000) {
             ngx_log_debug(log,
-                          "kevent: %08x: ft:%d f:%08x ff:%08x d:%d ud:%08x" _
+                          "kevent: %08x: ft:%d fl:%08x ff:%08x d:%d ud:%08x" _
                           event_list[i].ident _ event_list[i].filter _
                           event_list[i].flags _ event_list[i].fflags _
                           event_list[i].data _ event_list[i].udata);
         } else {
             ngx_log_debug(log,
-                          "kevent: %d: ft:%d f:%08x ff:%08x d:%d ud:%08x" _
+                          "kevent: %d: ft:%d fl:%08x ff:%08x d:%d ud:%08x" _
                           event_list[i].ident _ event_list[i].filter _
                           event_list[i].flags _ event_list[i].fflags _
                           event_list[i].data _ event_list[i].udata);
diff --git a/src/event/modules/ngx_kqueue_module.h b/src/event/modules/ngx_kqueue_module.h
index 2f1757f..ab1a12b 100644
--- a/src/event/modules/ngx_kqueue_module.h
+++ b/src/event/modules/ngx_kqueue_module.h
@@ -2,12 +2,6 @@
 #define _NGX_KQUEUE_MODULE_H_INCLUDED_
 
 
-typedef struct {
-    int   changes;
-    int   events;
-} ngx_kqueue_conf_t;
-
-
 extern int                 ngx_kqueue;
 extern ngx_module_t        ngx_kqueue_module;
 extern ngx_event_module_t  ngx_kqueue_module_ctx;
diff --git a/src/event/modules/ngx_poll_module.c b/src/event/modules/ngx_poll_module.c
index adb147d..a49afea 100644
--- a/src/event/modules/ngx_poll_module.c
+++ b/src/event/modules/ngx_poll_module.c
@@ -6,7 +6,6 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_connection.h>
 #include <ngx_event.h>
 
 
diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c
index 4b8391c..6f12966 100644
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -6,7 +6,6 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_connection.h>
 #include <ngx_event.h>
 
 
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index be9af20..f028f5d 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -6,7 +6,7 @@
 #include <ngx_event.h>
 
 
-#define DEF_CONNECTIONS  1024
+#define DEF_CONNECTIONS  512
 
 
 extern ngx_module_t ngx_select_module;
@@ -367,15 +367,6 @@
 
 #if (HAVE_KQUEUE)
 
-#if 0
-    if (ecf->connections != NGX_CONF_UNSET) {
-        ecf->connections = (ngx_max_connections < DEF_CONNECTIONS) ?
-                                        ngx_max_connections : DEF_CONNECTIONS;
-
-    } else if (ecf->connections > ngx_max_connections) {
-    }
-#endif
-
     ngx_conf_init_value(ecf->connections, DEF_CONNECTIONS);
     ngx_conf_init_value(ecf->use, ngx_kqueue_module.ctx_index);
 
@@ -393,6 +384,14 @@
 
 #endif
 
+#if (WIN32)
+    /*
+     * Winsock assignes a socket number according to 4 * N + M,
+     * where M is the constant 32 (98SE), 88 (NT) or 100 (W2K).
+     * So to find a connection we divide a socket number by 4.
+     */
+#endif
+
     ngx_conf_init_value(ecf->timer_queues, 10);
 
     return NGX_CONF_OK;
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index 08119a3..f467cc7 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -9,16 +9,14 @@
 #define NGX_INVALID_INDEX  0x80000000
 
 
-#if 0
-typedef struct ngx_event_s       ngx_event_t;
-#endif
-
 #if (HAVE_IOCP)
+
 typedef struct {
     WSAOVERLAPPED    ovlp;
     ngx_event_t     *event;
     int              error;
 } ngx_event_ovlp_t;
+
 #endif
 
 
@@ -81,6 +79,10 @@
 
     unsigned         deferred_accept:1;
 
+#if (WIN32)
+    unsigned         accept_context_updated:1;
+#endif
+
 #if (HAVE_KQUEUE)
     unsigned         eof:1;
     int              error;
@@ -284,6 +286,13 @@
 
 #endif
 
+
+#if (HAVE_IOCP_EVENT)
+#define NGX_IOCP_ACCEPT      0
+#define NGX_IOCP_IO          1
+#endif
+
+
 #define ngx_del_timer        ngx_event_del_timer
 
 
diff --git a/src/event/ngx_event_acceptex.c b/src/event/ngx_event_acceptex.c
index 288fd2b..8ba56f3 100644
--- a/src/event/ngx_event_acceptex.c
+++ b/src/event/ngx_event_acceptex.c
@@ -5,37 +5,36 @@
 #include <ngx_listen.h>
 
 #include <ngx_event.h>
+#if 0
 #include <ngx_event_close.h>
 #include <ngx_iocp_module.h>
-
-#include <ngx_event_acceptex.h>
+#endif
 
 
-
-void ngx_event_acceptex(ngx_event_t *ev)
+void ngx_event_acceptex(ngx_event_t *rev)
 {
     ngx_connection_t  *c;
 
-    c = (ngx_connection_t *) ev->data;
+    c = (ngx_connection_t *) rev->data;
 
-    if (ev->ovlp.error) {
-        ngx_log_error(NGX_LOG_CRIT, ev->log, ev->ovlp.error,
-                      "AcceptEx() falied for %s", c->addr_text.data);
+ngx_log_debug(rev->log, "ADDR: %s" _ c->addr_text.data);
+
+    if (rev->ovlp.error) {
+        ngx_log_error(NGX_LOG_CRIT, rev->log, rev->ovlp.error,
+                      "AcceptEx() failed for %s", c->addr_text.data);
         return;
     }
 
-    /* TODO: can we do SO_UPDATE_ACCEPT_CONTEXT just before shutdown() ???
-       or AcceptEx's context will be lost ??? */
+    /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
 
-     /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
     if (setsockopt(c->fd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
                    (char *)&c->listening->fd, sizeof(ngx_socket_t)) == -1)
     {
         ngx_log_error(NGX_LOG_CRIT, ev->log, ngx_socket_errno,
                       "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed for %s",
                       c->addr_text.data);
-
-        /* non fatal - we can not only do lingering close */
+    } else {
+        accept_context_updated = 1;
     }
 
     getacceptexsockaddrs(c->data, 0,
@@ -75,8 +74,7 @@
 
         if (s == -1) {
             ngx_log_error(NGX_LOG_ALERT, ls->log, ngx_socket_errno,
-                          ngx_socket_n " for AcceptEx(%s) falied",
-                          ls->addr_text.data);
+                          ngx_socket_n " for AcceptEx() post failed");
 
             return NGX_ERROR;
         }
@@ -132,7 +130,7 @@
         ngx_memcpy(c->log, ls->log, sizeof(ngx_log_t));
         rev->log = wev->log = c->log;
 
-        if (ngx_iocp_add_event(rev) == NGX_ERROR) {
+        if (ngx_add_event(rev, 0, NGX_IOCP_IO) == NGX_ERROR) {
             return NGX_ERROR;
         }