nginx-0.0.1-2003-02-06-20:21:13 import
diff --git a/src/os/unix/freebsd/ngx_sendfile.c b/src/os/unix/freebsd/ngx_sendfile.c
index dcbf68a..8de85f6 100644
--- a/src/os/unix/freebsd/ngx_sendfile.c
+++ b/src/os/unix/freebsd/ngx_sendfile.c
@@ -5,25 +5,25 @@
 
 #include <ngx_core.h>
 #include <ngx_types.h>
-#include <ngx_file.h>
 #include <ngx_socket.h>
 #include <ngx_errno.h>
 #include <ngx_log.h>
+#include <ngx_connection.h>
 #include <ngx_sendv.h>
 #include <ngx_sendfile.h>
 
 /*
   CHECK:
        check sent if errno == EINTR then should return right sent.
+       EINTR should not occur according to man.
 */
 
 
-int ngx_sendfile(ngx_socket_t s,
+int ngx_sendfile(ngx_connection_t *c,
                  ngx_iovec_t *headers, int hdr_cnt,
                  ngx_fd_t fd, off_t offset, size_t nbytes,
                  ngx_iovec_t *trailers, int trl_cnt,
-                 off_t *sent,
-                 ngx_log_t *log)
+                 off_t *sent, u_int flags)
 {
     int             rc, i;
     ngx_err_t       err;
@@ -35,26 +35,29 @@
     hdtr.trl_cnt = trl_cnt;
 
 #if (HAVE_FREEBSD_SENDFILE_NBYTES_BUG)
-    for (i = 0; i < hdr_cnt; i++)
+    for (i = 0; i < hdr_cnt; i++) {
         nbytes += headers[i].iov_len;
+    }
 #endif
 
-    rc = sendfile(fd, s, offset, nbytes, &hdtr, sent, 0);
+    rc = sendfile(fd, c->fd, offset, nbytes, &hdtr, sent, flags);
 
     if (rc == -1) {
-        err = ngx_socket_errno;
+        err = ngx_errno;
         if (err != NGX_EAGAIN && err != NGX_EINTR) {
-            ngx_log_error(NGX_LOG_ERR, log, err,
-                         "ngx_sendfile: sendfile failed");
+            ngx_log_error(NGX_LOG_ERR, c->log, err, "sendfile failed");
+
             return NGX_ERROR;
 
         } else {
-            ngx_log_error(NGX_LOG_INFO, log, err,
-                         "ngx_sendfile: sendfile sent only %qd bytes", *sent);
+            ngx_log_error(NGX_LOG_INFO, c->log, err,
+                          "sendfile sent only %qd bytes", *sent);
+
+            return NGX_AGAIN;
         }
     }
 
-    ngx_log_debug(log, "ngx_sendfile: %d, @%qd %qd:%d" _
+    ngx_log_debug(c->log, "sendfile: %d, @%qd %qd:%d" _
                   rc _ offset _ *sent _ nbytes);
 
     return NGX_OK;
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h
index 62e1b07..a5fccab 100644
--- a/src/os/unix/ngx_errno.h
+++ b/src/os/unix/ngx_errno.h
@@ -15,6 +15,7 @@
 #define NGX_EINPROGRESS   EINPROGRESS
 #define NGX_EADDRINUSE    EADDRINUSE
 #define NGX_ETIMEDOUT     ETIMEDOUT
+#define NGX_ECANCELED     ECANCELED
 
 #define ngx_errno                  errno
 #define ngx_socket_errno           errno
diff --git a/src/os/unix/ngx_socket.c b/src/os/unix/ngx_socket.c
new file mode 100644
index 0000000..d213fa8
--- /dev/null
+++ b/src/os/unix/ngx_socket.c
@@ -0,0 +1,26 @@
+
+#include <ngx_socket.h>
+
+
+/* ioctl(FIONBIO) set blocking mode with one syscall only while
+   fcntl(F_SETFL, ~O_NONBLOCK) need to know previous state
+   using fcntl(F_GETFL).
+   On FreeBSD both are syscall */
+
+#ifdef __FreeBSD__
+
+int ngx_nonblocking(ngx_socket_t s)
+{
+    unsigned long  nb = 1;
+
+    return ioctl(s, FIONBIO, &nb);
+}
+
+int ngx_blocking(ngx_socket_t s)
+{
+    unsigned long  nb = 0;
+
+    return ioctl(s, FIONBIO, &nb);
+}
+
+#endif
diff --git a/src/os/unix/ngx_socket.h b/src/os/unix/ngx_socket.h
index b4be762..937c12d 100644
--- a/src/os/unix/ngx_socket.h
+++ b/src/os/unix/ngx_socket.h
@@ -4,6 +4,11 @@
 
 #include <ngx_config.h>
 
+#ifdef __FreeBSD__
+#include <sys/ioctl.h>
+#endif
+
+
 #define NGX_WRITE_SHUTDOWN SHUT_WR
 
 typedef int  ngx_socket_t;
@@ -11,9 +16,23 @@
 #define ngx_socket(af, type, proto, flags)   socket(af, type, proto)
 #define ngx_socket_n        "socket()"
 
+
+#ifdef __FreeBSD__
+
+int ngx_nonblocking(ngx_socket_t s);
+int ngx_blocking(ngx_socket_t s);
+
+#define ngx_nonblocking_n   "ioctl(FIONBIO)"
+#define ngx_blocking_n      "ioctl(!FIONBIO)"
+
+#else
+
 #define ngx_nonblocking(s)  fcntl(s, F_SETFL, O_NONBLOCK)
 #define ngx_nonblocking_n   "fcntl(O_NONBLOCK)"
 
+#endif
+
+
 #define ngx_shutdown_socket    shutdown
 #define ngx_shutdown_socket_n  "shutdown()"
 
diff --git a/src/os/unix/ngx_time.h b/src/os/unix/ngx_time.h
index fac71ad..5cd791d 100644
--- a/src/os/unix/ngx_time.h
+++ b/src/os/unix/ngx_time.h
@@ -5,7 +5,7 @@
 #include <ngx_config.h>
 
 typedef u_int          ngx_msec_t;
-#define NGX_MAX_MSEC   ~0
+#define NGX_MAX_MSEC   (u_int) ~0
 
 typedef struct tm      ngx_tm_t;
 
diff --git a/src/os/win32/ngx_sendfile.c b/src/os/win32/ngx_sendfile.c
index 560f50f..ce43b37 100644
--- a/src/os/win32/ngx_sendfile.c
+++ b/src/os/win32/ngx_sendfile.c
@@ -6,6 +6,7 @@
 #include <ngx_socket.h>
 #include <ngx_errno.h>
 #include <ngx_log.h>
+#include <ngx_connection.h>
 #include <ngx_sendv.h>
 #include <ngx_sendfile.h>
 
@@ -17,18 +18,51 @@
 
 #if (HAVE_WIN32_TRANSMITFILE)
 
-int ngx_sendfile(ngx_socket_t s,
+int ngx_sendfile(ngx_connection_t *c,
                  ngx_iovec_t *headers, int hdr_cnt,
                  ngx_fd_t fd, off_t offset, size_t nbytes,
                  ngx_iovec_t *trailers, int trl_cnt,
-                 off_t *sent,
-                 ngx_log_t *log)
+                 off_t *sent, u_int flags)
 {
     int                    tfrc, rc;
     ngx_err_t              tf_err, err;
     OVERLAPPED             olp;
     TRANSMIT_FILE_BUFFERS  tfb, *ptfb;
 
+#if 0
+    ev = c->write;
+
+    if (ev->timedout) {
+        ngx_set_socket_errno(NGX_ETIMEDOUT);
+        ngx_log_error(NGX_LOG_ERR, ev->log, 0, "TransmitFile() timed out");
+
+        return NGX_ERROR;
+    }
+
+    if (ev->ready) {
+        ev->ready = 0;
+
+#if (HAVE_IOCP_EVENT) /* iocp */
+
+        if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
+            if (ev->ovlp.error) {
+                ngx_log_error(NGX_LOG_ERR, ev->log, 0, "TransmitFile() failed");
+                return NGX_ERROR;
+            }
+
+            return ev->available;
+            }
+        }
+
+#endif
+
+        /* TODO: WSAGetOverlappedResult stuff */
+
+    }
+
+#endif
+
+
     tf_err = 0;
     err = 0;
 
@@ -49,40 +83,54 @@
         ptfb = NULL;
     }
 
-#if 1
-    tfrc = TransmitFile(s, fd, nbytes, 0, &olp, ptfb, 0);
-#else
-    tfrc = TransmitFile(s, fd, nbytes, 0, NULL, ptfb, 0);
+#if 0
+    flags = TF_DISCONNECT|TF_REUSE_SOCKET;
 #endif
 
-    if (tfrc == 0)
+    tfrc = TransmitFile(c->fd, fd, nbytes, 0, &olp, ptfb, flags);
+
+#if 0
+#if 1
+    tfrc = TransmitFile(c->fd, fd, nbytes, 0, &olp, ptfb, 0);
+#else
+    tfrc = TransmitFile(c->fd, fd, nbytes, 0, NULL, ptfb, 0);
+#endif
+#endif
+
+    if (tfrc == 0) {
         tf_err = ngx_socket_errno;
+        ngx_log_error(NGX_LOG_NOTICE, c->log, tf_err,
+                      "ngx_sendfile: TransmitFile failed");
+        if (tf_err == WSA_IO_PENDING) {
+            return NGX_AGAIN;
+        }
+    }
 
     /* set sent */
 #if 0
-    rc = WSAGetOverlappedResult(s, &olp, (unsigned long *) sent, 0, NULL);
+    rc = WSAGetOverlappedResult(c->fd, &olp, (unsigned long *) sent, 0, NULL);
 #else
     *sent = olp.InternalHigh;
     rc = 1;
 #endif
 
-    ngx_log_debug(log, "ngx_sendfile: %d, @%I64d %I64d:%d" _
+    ngx_log_debug(c->log, "TransmitFile: %d, @%I64d %I64d:%d" _
                   tfrc _ offset _ *sent _ nbytes);
 
     if (rc == 0) {
         err = ngx_socket_errno;
-        ngx_log_error(NGX_LOG_ERR, log, err,
+        ngx_log_error(NGX_LOG_ERR, c->log, err,
                      "ngx_sendfile: WSAGetOverlappedResult failed");
     }
 
     if (tfrc == 0) {
         if (tf_err != NGX_EAGAIN) {
-            ngx_log_error(NGX_LOG_ERR, log, tf_err,
+            ngx_log_error(NGX_LOG_ERR, c->log, tf_err,
                           "ngx_sendfile: TransmitFile failed");
             return NGX_ERROR;
         }
 
-        ngx_log_error(NGX_LOG_INFO, log, tf_err,
+        ngx_log_error(NGX_LOG_INFO, c->log, tf_err,
                       "ngx_sendfile: TransmitFile sent only %I64d bytes",
                       *sent);
     }
diff --git a/src/os/win32/ngx_socket.c b/src/os/win32/ngx_socket.c
index d0e547c..09cbddf 100644
--- a/src/os/win32/ngx_socket.c
+++ b/src/os/win32/ngx_socket.c
@@ -1,19 +1,75 @@
+
 #include <ngx_config.h>
 
+#include <ngx_core.h>
 #include <ngx_log.h>
 #include <ngx_errno.h>
 #include <ngx_socket.h>
 
 
-void ngx_init_sockets(ngx_log_t *log)
+/* These pointers should be per protocol ? */
+LPFN_ACCEPTEX              AcceptEx;
+LPFN_GETACCEPTEXSOCKADDRS  GetAcceptExSockaddrs;
+LPFN_TRANSMITFILE          TransmitFile;
+
+static GUID ae_guid = WSAID_ACCEPTEX;
+static GUID as_guid = WSAID_GETACCEPTEXSOCKADDRS;
+static GUID tf_guid = WSAID_TRANSMITFILE;
+
+
+int ngx_init_sockets(ngx_log_t *log)
 {
+    DWORD    bytes;
+    SOCKET   s;
     WSADATA  wsd;
 
-    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
+    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
-                      "ngx_init_sockets: WSAStartup failed");
+                      "WSAStartup failed");
+        return NGX_ERROR;
+    }
 
-    /* get AcceptEx(), TransmitFile() functions */
+    s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
+    if (s == -1) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
+                      ngx_socket_n " %s falied");
+        return NGX_ERROR;
+    }
+
+    if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &ae_guid, sizeof(GUID),
+                 &AcceptEx, sizeof(LPFN_ACCEPTEX), &bytes, NULL, NULL) == -1) {
+
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
+                      "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER, "
+                               "WSAID_ACCEPTEX) failed");
+        return NGX_ERROR;
+    }
+
+    if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &as_guid, sizeof(GUID),
+                 &GetAcceptExSockaddrs, sizeof(LPFN_GETACCEPTEXSOCKADDRS),
+                 &bytes, NULL, NULL) == -1) {
+
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
+                      "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER, "
+                               "WSAID_ACCEPTEX) failed");
+        return NGX_ERROR;
+    }
+
+    if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &tf_guid, sizeof(GUID),
+                 &TransmitFile, sizeof(LPFN_TRANSMITFILE), &bytes,
+                                                           NULL, NULL) == -1) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
+                      "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER, "
+                               "WSAID_TRANSMITFILE) failed");
+        return NGX_ERROR;
+    }
+
+    if (ngx_close_socket(s) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
+                      ngx_close_socket_n " failed");
+    }
+
+    return NGX_OK;
 }
 
 int ngx_nonblocking(ngx_socket_t s)
diff --git a/src/os/win32/ngx_socket.h b/src/os/win32/ngx_socket.h
index 98e1520..f94d45c 100644
--- a/src/os/win32/ngx_socket.h
+++ b/src/os/win32/ngx_socket.h
@@ -12,14 +12,17 @@
 typedef SOCKET  ngx_socket_t;
 typedef int     socklen_t;
 
-void ngx_init_sockets(ngx_log_t *log);
+int ngx_init_sockets(ngx_log_t *log);
 
 #define ngx_socket(af, type, proto, flags)                                    \
             WSASocket(af, type, proto, NULL, 0, flags)
 #define ngx_socket_n        "WSASocket()"
 
 int ngx_nonblocking(ngx_socket_t s);
+int ngx_blocking(ngx_socket_t s);
+
 #define ngx_nonblocking_n   "ioctlsocket(FIONBIO)"
+#define ngx_blocking_n      "ioctlsocket(!FIONBIO)"
 
 #define ngx_shutdown_socket    shutdown
 #define ngx_shutdown_socket_n  "shutdown()"
@@ -28,5 +31,9 @@
 #define ngx_close_socket_n  "closesocket()"
 
 
+extern LPFN_ACCEPTEX              acceptex;
+extern LPFN_GETACCEPTEXSOCKADDRS  getacceptexsockaddrs;
+extern LPFN_TRANSMITFILE          transmitfile;
+
 
 #endif /* _NGX_SOCKET_H_INCLUDED_ */