comparison src/event/ngx_event_write.c @ 8:708f8bb772ec

nginx-0.0.1-2002-09-02-18:48:24 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 02 Sep 2002 14:48:24 +0000
parents 34a521b1a148
children 6f58641241bb
comparison
equal deleted inserted replaced
7:b5481d6fbbd4 8:708f8bb772ec
1 1
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 #include <ngx_core.h>
3 #include <ngx_types.h> 4 #include <ngx_types.h>
4 #include <ngx_alloc.h> 5 #include <ngx_alloc.h>
5 #include <ngx_array.h> 6 #include <ngx_array.h>
6 #include <ngx_hunk.h> 7 #include <ngx_hunk.h>
7 #include <ngx_connection.h> 8 #include <ngx_connection.h>
8 #include <ngx_sendv.h> 9 #include <ngx_sendv.h>
9 #include <ngx_sendfile.h> 10 #include <ngx_sendfile.h>
10 #include <ngx_event_write.h> 11 #include <ngx_event_write.h>
11 12
12 13
13 ngx_chain_t *ngx_event_write(ngx_connection_t *cn, ngx_chain_t *in, 14 ngx_chain_t *ngx_event_write(ngx_connection_t *c, ngx_chain_t *in,
14 off_t flush) 15 off_t flush)
15 { 16 {
16 int rc; 17 int rc;
17 char *last; 18 char *last;
18 off_t sent; 19 off_t sent;
22 ngx_chain_t *ch; 23 ngx_chain_t *ch;
23 24
24 ch = in; 25 ch = in;
25 file = NULL; 26 file = NULL;
26 27
27 ngx_test_null(header, ngx_create_array(cn->pool, 10, sizeof(ngx_iovec_t)), 28 ngx_test_null(header, ngx_create_array(c->pool, 10, sizeof(ngx_iovec_t)),
28 (ngx_chain_t *) -1); 29 (ngx_chain_t *) -1);
29 30
30 ngx_test_null(trailer, ngx_create_array(cn->pool, 10, sizeof(ngx_iovec_t)), 31 ngx_test_null(trailer, ngx_create_array(c->pool, 10, sizeof(ngx_iovec_t)),
31 (ngx_chain_t *) -1); 32 (ngx_chain_t *) -1);
32 33
33 do { 34 do {
34 header->nelts = 0; 35 header->nelts = 0;
35 trailer->nelts = 0; 36 trailer->nelts = 0;
60 ch = ch->next; 61 ch = ch->next;
61 } 62 }
62 63
63 #if (HAVE_MAX_SENDFILE_IOVEC) 64 #if (HAVE_MAX_SENDFILE_IOVEC)
64 if (file && header->nelts > HAVE_MAX_SENDFILE_IOVEC) { 65 if (file && header->nelts > HAVE_MAX_SENDFILE_IOVEC) {
65 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts, header->nelts, 66 rc = ngx_sendv(c->fd, (ngx_iovec_t *) header->elts, header->nelts,
66 &sent); 67 &sent);
67 } else { 68 } else {
68 #endif 69 #endif
69 if (ch && ch->hunk->type & NGX_HUNK_IN_MEMORY) { 70 if (ch && ch->hunk->type & NGX_HUNK_IN_MEMORY) {
70 last = NULL; 71 last = NULL;
88 ch = ch->next; 89 ch = ch->next;
89 } 90 }
90 } 91 }
91 92
92 if (file) { 93 if (file) {
93 rc = ngx_sendfile(cn->fd, 94 rc = ngx_sendfile(c->fd,
94 (ngx_iovec_t *) header->elts, header->nelts, 95 (ngx_iovec_t *) header->elts, header->nelts,
95 file->fd, file->pos.file, 96 file->file->fd, file->pos.file,
96 (size_t) (file->last.file - file->pos.file), 97 (size_t) (file->last.file - file->pos.file),
97 (ngx_iovec_t *) trailer->elts, trailer->nelts, 98 (ngx_iovec_t *) trailer->elts, trailer->nelts,
98 &sent, cn->log); 99 &sent, c->log);
99 } else { 100 } else {
100 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts, 101 size_t sendv_sent;
101 header->nelts, (size_t *) &sent); 102
102 ngx_log_debug(cn->log, "sendv: %d" _ sent); 103 sendv_sent = 0;
104 rc = ngx_sendv(c->fd, (ngx_iovec_t *) header->elts,
105 header->nelts, &sendv_sent);
106 sent = sendv_sent;
107 ngx_log_debug(c->log, "sendv: " QD_FMT _ sent);
103 } 108 }
104 #if (HAVE_MAX_SENDFILE_IOVEC) 109 #if (HAVE_MAX_SENDFILE_IOVEC)
105 } 110 }
106 #endif 111 #endif
107 /* save sent for logging */ 112 /* save sent for logging */
108 113
109 if (rc == -1) 114 if (rc == NGX_ERROR)
110 return (ngx_chain_t *) -1; 115 return (ngx_chain_t *) -1;
111 116
117 c->sent = sent;
112 flush -= sent; 118 flush -= sent;
113 119
114 for (ch = in; ch; ch = ch->next) { 120 for (ch = in; ch; ch = ch->next) {
121
122 ngx_log_debug(c->log, "ch event write: %x %qx %qd" _
123 ch->hunk->type _
124 ch->hunk->pos.file _
125 ch->hunk->last.file - ch->hunk->pos.file);
126
115 if (sent >= ch->hunk->last.file - ch->hunk->pos.file) { 127 if (sent >= ch->hunk->last.file - ch->hunk->pos.file) {
116 sent -= ch->hunk->last.file - ch->hunk->pos.file; 128 sent -= ch->hunk->last.file - ch->hunk->pos.file;
117 ch->hunk->last.file = ch->hunk->pos.file; 129 ch->hunk->pos.file = ch->hunk->last.file;
118 130
119 ngx_log_debug(cn->log, "event write: %qx 0" _ 131 ngx_log_debug(c->log, "event write: " QX_FMT " 0 " QD_FMT _
120 ch->hunk->pos.file); 132 ch->hunk->pos.file _ sent);
121 133
134 /*
122 if (ch->hunk->type & NGX_HUNK_LAST) 135 if (ch->hunk->type & NGX_HUNK_LAST)
123 break; 136 break;
137 */
124 138
125 continue; 139 continue;
126 } 140 }
127 141
128 ch->hunk->pos.file += sent; 142 ch->hunk->pos.file += sent;
129 143
130 ngx_log_debug(cn->log, "event write: %qx %qd" _ 144 ngx_log_debug(c->log, "event write: %qx %qd" _
131 ch->hunk->pos.file _ 145 ch->hunk->pos.file _
132 ch->hunk->last.file - ch->hunk->pos.file); 146 ch->hunk->last.file - ch->hunk->pos.file);
133 147
134 break; 148 break;
135 } 149 }
136 150
137 /* flush hunks if threaded state */ 151 /* flush hunks if threaded state */
138 } while (cn->write->context && flush > 0); 152 } while (c->write->context && flush > 0);
139 153
140 ngx_destroy_array(trailer); 154 ngx_destroy_array(trailer);
141 ngx_destroy_array(header); 155 ngx_destroy_array(header);
142 156
143 return ch; 157 return ch;