comparison src/event/ngx_event_write.c @ 1:d220029ac7f3

nginx-0.0.1-2002-08-15-21:20:26 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 15 Aug 2002 17:20:26 +0000
parents 4eff17414a43
children 34a521b1a148
comparison
equal deleted inserted replaced
0:4eff17414a43 1:d220029ac7f3
8 #include <ngx_sendv.h> 8 #include <ngx_sendv.h>
9 #include <ngx_sendfile.h> 9 #include <ngx_sendfile.h>
10 #include <ngx_event_write.h> 10 #include <ngx_event_write.h>
11 11
12 12
13 ngx_chain_t *ngx_event_writer(ngx_connection_t *cn, ngx_chain_t *in, 13 ngx_chain_t *ngx_event_write(ngx_connection_t *cn, ngx_chain_t *in,
14 off_t flush) 14 off_t flush)
15 { 15 {
16 int rc; 16 int rc;
17 char *last; 17 char *last;
18 off_t sent; 18 off_t sent;
19 ngx_iovec_t *iov; 19 ngx_iovec_t *iov;
32 32
33 do { 33 do {
34 header->nelts = 0; 34 header->nelts = 0;
35 trailer->nelts = 0; 35 trailer->nelts = 0;
36 36
37 if (ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)) { 37 if (ch->hunk->type & NGX_HUNK_IN_MEMORY) {
38 last = NULL; 38 last = NULL;
39 iov = NULL; 39 iov = NULL;
40 40
41 while (ch 41 while (ch && (ch->hunk->type & NGX_HUNK_IN_MEMORY))
42 && (ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)))
43 { 42 {
44 if (ch->hunk->type & NGX_HUNK_FLUSH) 43 if (last == ch->hunk->pos.mem) {
45 continue; 44 iov->ngx_iov_len += ch->hunk->last.mem - ch->hunk->pos.mem;
46
47 if (last == ch->hunk->pos.p) {
48 iov->ngx_iov_len += ch->hunk->last.p - ch->hunk->pos.p;
49 45
50 } else { 46 } else {
51 ngx_test_null(iov, ngx_push_array(header), 47 ngx_test_null(iov, ngx_push_array(header),
52 (ngx_chain_t *) -1); 48 (ngx_chain_t *) -1);
53 iov->ngx_iov_base = ch->hunk->pos.p; 49 iov->ngx_iov_base = ch->hunk->pos.mem;
54 iov->ngx_iov_len = ch->hunk->last.p - ch->hunk->pos.p; 50 iov->ngx_iov_len = ch->hunk->last.mem - ch->hunk->pos.mem;
55 last = ch->hunk->last.p; 51 last = ch->hunk->last.mem;
56 } 52 }
57 53
58 ch = ch->next; 54 ch = ch->next;
59 } 55 }
60 } 56 }
68 if (file && header->nelts > HAVE_MAX_SENDFILE_IOVEC) { 64 if (file && header->nelts > HAVE_MAX_SENDFILE_IOVEC) {
69 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts, header->nelts, 65 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts, header->nelts,
70 &sent); 66 &sent);
71 } else { 67 } else {
72 #endif 68 #endif
73 if (ch && ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)) { 69 if (ch && ch->hunk->type & NGX_HUNK_IN_MEMORY) {
74 last = NULL; 70 last = NULL;
75 iov = NULL; 71 iov = NULL;
76 72
77 while (ch 73 while (ch && (ch->hunk->type & NGX_HUNK_IN_MEMORY)) {
78 && (ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)))
79 {
80 if (ch->hunk->type & NGX_HUNK_FLUSH)
81 continue;
82 74
83 if (last == ch->hunk->pos.p) { 75 if (last == ch->hunk->pos.mem) {
84 iov->ngx_iov_len += ch->hunk->last.p - ch->hunk->pos.p; 76 iov->ngx_iov_len +=
77 ch->hunk->last.mem - ch->hunk->pos.mem;
85 78
86 } else { 79 } else {
87 ngx_test_null(iov, ngx_push_array(trailer), 80 ngx_test_null(iov, ngx_push_array(trailer),
88 (ngx_chain_t *) -1); 81 (ngx_chain_t *) -1);
89 iov->ngx_iov_base = ch->hunk->pos.p; 82 iov->ngx_iov_base = ch->hunk->pos.mem;
90 iov->ngx_iov_len = ch->hunk->last.p - ch->hunk->pos.p; 83 iov->ngx_iov_len =
91 last = ch->hunk->last.p; 84 ch->hunk->last.mem - ch->hunk->pos.mem;
85 last = ch->hunk->last.mem;
92 } 86 }
93 87
94 ch = ch->next; 88 ch = ch->next;
95 } 89 }
96 } 90 }
97 91
98 if (file) { 92 if (file) {
99 rc = ngx_sendfile(cn->fd, 93 rc = ngx_sendfile(cn->fd,
100 (ngx_iovec_t *) header->elts, header->nelts, 94 (ngx_iovec_t *) header->elts, header->nelts,
101 file->fd, file->pos.f, 95 file->fd, file->pos.file,
102 (size_t) (file->last.f - file->pos.f), 96 (size_t) (file->last.file - file->pos.file),
103 (ngx_iovec_t *) trailer->elts, trailer->nelts, 97 (ngx_iovec_t *) trailer->elts, trailer->nelts,
104 &sent, cn->log); 98 &sent, cn->log);
105 } else { 99 } else {
106 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts, 100 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts,
107 header->nelts, (size_t *) &sent); 101 header->nelts, (size_t *) &sent);
115 return (ngx_chain_t *) -1; 109 return (ngx_chain_t *) -1;
116 110
117 flush -= sent; 111 flush -= sent;
118 112
119 for (ch = in; ch && !(ch->hunk->type & NGX_HUNK_LAST); ch = ch->next) { 113 for (ch = in; ch && !(ch->hunk->type & NGX_HUNK_LAST); ch = ch->next) {
120 if (sent >= ch->hunk->last.f - ch->hunk->pos.f) { 114 if (sent >= ch->hunk->last.file - ch->hunk->pos.file) {
121 sent -= ch->hunk->last.f - ch->hunk->pos.f; 115 sent -= ch->hunk->last.file - ch->hunk->pos.file;
122 ch->hunk->last.f = ch->hunk->pos.f; 116 ch->hunk->last.file = ch->hunk->pos.file;
123 continue; 117 continue;
124 } 118 }
125 119
126 ch->hunk->pos.f += sent; 120 ch->hunk->pos.file += sent;
127 break; 121 break;
128 } 122 }
129 123
130 } while (flush > 0); 124 /* flush hunks if threaded state */
125 } while (cn->write->context && flush > 0);
131 126
132 ngx_destroy_array(trailer); 127 ngx_destroy_array(trailer);
133 ngx_destroy_array(header); 128 ngx_destroy_array(header);
134 129
135 return ch; 130 return ch;