diff src/event/ngx_event_write.c @ 59:e8cdc2989cee

nginx-0.0.1-2003-02-06-20:21:13 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 06 Feb 2003 17:21:13 +0000
parents 0e81ac0bb3e2
children
line wrap: on
line diff
--- a/src/event/ngx_event_write.c
+++ b/src/event/ngx_event_write.c
@@ -11,11 +11,11 @@
 #include <ngx_event_write.h>
 
 
-ngx_chain_t *ngx_event_write(ngx_connection_t *c, ngx_chain_t *in,
-                             off_t flush)
+ngx_chain_t *ngx_event_write(ngx_connection_t *c, ngx_chain_t *in, off_t flush)
 {
-    int           rc;
-    char         *last;
+    int           rc, i, last;
+    u_int         flags;
+    char         *prev;
     off_t         sent;
     ngx_iovec_t  *iov;
     ngx_array_t  *header, *trailer;
@@ -24,6 +24,7 @@ ngx_chain_t *ngx_event_write(ngx_connect
 
     ch = in;
     file = NULL;
+    last = 0;
 
     ngx_test_null(header, ngx_create_array(c->pool, 10, sizeof(ngx_iovec_t)),
                   (ngx_chain_t *) -1);
@@ -36,12 +37,12 @@ ngx_chain_t *ngx_event_write(ngx_connect
         trailer->nelts = 0;
 
         if (ch->hunk->type & NGX_HUNK_IN_MEMORY) {
-            last = NULL;
+            prev = NULL;
             iov = NULL;
 
             while (ch && (ch->hunk->type & NGX_HUNK_IN_MEMORY))
             {
-                if (last == ch->hunk->pos.mem) {
+                if (prev == ch->hunk->pos.mem) {
                     iov->ngx_iov_len += ch->hunk->last.mem - ch->hunk->pos.mem;
 
                 } else {
@@ -49,7 +50,11 @@ ngx_chain_t *ngx_event_write(ngx_connect
                                   (ngx_chain_t *) -1);
                     iov->ngx_iov_base = ch->hunk->pos.mem;
                     iov->ngx_iov_len = ch->hunk->last.mem - ch->hunk->pos.mem;
-                    last = ch->hunk->last.mem;
+                    prev = ch->hunk->last.mem;
+                }
+
+                if (ch->hunk->type & NGX_HUNK_LAST) {
+                    last = 1;
                 }
 
                 ch = ch->next;
@@ -59,6 +64,10 @@ ngx_chain_t *ngx_event_write(ngx_connect
         if (ch && (ch->hunk->type & NGX_HUNK_FILE)) {
             file = ch->hunk;
             ch = ch->next;
+
+            if (ch->hunk->type & NGX_HUNK_LAST) {
+                last = 1;
+            }
         }
 
 #if (HAVE_MAX_SENDFILE_IOVEC)
@@ -68,12 +77,12 @@ ngx_chain_t *ngx_event_write(ngx_connect
         } else {
 #endif
             if (ch && ch->hunk->type & NGX_HUNK_IN_MEMORY) {
-                last = NULL;
+                prev = NULL;
                 iov = NULL;
 
                 while (ch && (ch->hunk->type & NGX_HUNK_IN_MEMORY)) {
 
-                    if (last == ch->hunk->pos.mem) {
+                    if (prev == ch->hunk->pos.mem) {
                         iov->ngx_iov_len +=
                                         ch->hunk->last.mem - ch->hunk->pos.mem;
 
@@ -83,7 +92,11 @@ ngx_chain_t *ngx_event_write(ngx_connect
                         iov->ngx_iov_base = ch->hunk->pos.mem;
                         iov->ngx_iov_len =
                                         ch->hunk->last.mem - ch->hunk->pos.mem;
-                        last = ch->hunk->last.mem;
+                        prev = ch->hunk->last.mem;
+                    }
+
+                    if (ch->hunk->type & NGX_HUNK_LAST) {
+                        last = 1;
                     }
 
                     ch = ch->next;
@@ -91,19 +104,47 @@ ngx_chain_t *ngx_event_write(ngx_connect
             }
 
             if (file) {
-                rc = ngx_sendfile(c->fd,
+                flags = ngx_sendfile_flags;
+#if (HAVE_SENDFILE_DISCONNECT)
+                if (last && c->close) {
+                    flags |= HAVE_SENDFILE_DISCONNECT;
+                }
+#endif
+                rc = ngx_sendfile(c,
                                   (ngx_iovec_t *) header->elts, header->nelts,
                                   file->file->fd, file->pos.file,
                                   (size_t) (file->last.file - file->pos.file),
                                   (ngx_iovec_t *) trailer->elts, trailer->nelts,
-                                  &sent, c->log);
+                                  &sent, flags);
+
+#if (HAVE_AIO_EVENT) && !(HAVE_IOCP_EVENT)
+            } else if (ngx_event_flags & NGX_HAVE_AIO_EVENT) {
+
+                sent = 0;
+                rc = NGX_AGAIN;
+                iov = (ngx_iovec_t *) header->elts;
+                for (i = 0; i < header->nelts; i++) {
+                    rc = ngx_event_aio_write(c, iov[i].ngx_iov_base,
+                                             iov[i].ngx_iov_len);
+
+                    if (rc > 0) {
+                        sent += rc;
+                    } else {
+                        break;
+                    }
+
+                    if (rc < (int) iov->ngx_iov_len) {
+                        break;
+                    }
+                }
+#endif
             } else {
                 rc = ngx_sendv(c, (ngx_iovec_t *) header->elts, header->nelts);
 
                 sent = rc > 0 ? rc: 0;
 
 #if (NGX_DEBUG_EVENT_WRITE)
-                ngx_log_debug(c->log, "sendv: " QD_FMT _ sent);
+                ngx_log_debug(c->log, "sendv: " OFF_FMT _ sent);
 #endif
             }
 #if (HAVE_MAX_SENDFILE_IOVEC)
@@ -118,7 +159,7 @@ ngx_chain_t *ngx_event_write(ngx_connect
         for (ch = in; ch; ch = ch->next) {
 
 #if (NGX_DEBUG_EVENT_WRITE)
-            ngx_log_debug(c->log, "event write: %x " QX_FMT " " QD_FMT _
+            ngx_log_debug(c->log, "event write: %x " QX_FMT " " OFF_FMT _
                           ch->hunk->type _
                           ch->hunk->pos.file _
                           ch->hunk->last.file - ch->hunk->pos.file);
@@ -129,7 +170,7 @@ ngx_chain_t *ngx_event_write(ngx_connect
                 ch->hunk->pos.file = ch->hunk->last.file;
 
 #if (NGX_DEBUG_EVENT_WRITE)
-                ngx_log_debug(c->log, "event write: " QX_FMT " 0 " QD_FMT _
+                ngx_log_debug(c->log, "event write: " QX_FMT " 0 " OFF_FMT _
                               ch->hunk->pos.file _ sent);
 #endif
 
@@ -144,7 +185,7 @@ ngx_chain_t *ngx_event_write(ngx_connect
             ch->hunk->pos.file += sent;
 
 #if (NGX_DEBUG_EVENT_WRITE)
-            ngx_log_debug(c->log, "event write: " QX_FMT " " QD_FMT _
+            ngx_log_debug(c->log, "event write: " QX_FMT " " OFF_FMT _
                           ch->hunk->pos.file _
                           ch->hunk->last.file - ch->hunk->pos.file);
 #endif