comparison src/event/ngx_event_write.c @ 0:4eff17414a43

nginx-0.0.1-2002-08-06-20:39:45 import The first code that uses "ngx_" prefix, the previous one used "gx_" prefix. At that point the code is not yet usable. The first draft ideas are dated back to 23.10.2001.
author Igor Sysoev <igor@sysoev.ru>
date Tue, 06 Aug 2002 16:39:45 +0000
parents
children d220029ac7f3
comparison
equal deleted inserted replaced
-1:000000000000 0:4eff17414a43
1
2 #include <ngx_config.h>
3 #include <ngx_types.h>
4 #include <ngx_alloc.h>
5 #include <ngx_array.h>
6 #include <ngx_hunk.h>
7 #include <ngx_connection.h>
8 #include <ngx_sendv.h>
9 #include <ngx_sendfile.h>
10 #include <ngx_event_write.h>
11
12
13 ngx_chain_t *ngx_event_writer(ngx_connection_t *cn, ngx_chain_t *in,
14 off_t flush)
15 {
16 int rc;
17 char *last;
18 off_t sent;
19 ngx_iovec_t *iov;
20 ngx_array_t *header, *trailer;
21 ngx_hunk_t *file;
22 ngx_chain_t *ch;
23
24 ch = in;
25 file = NULL;
26
27 ngx_test_null(header, ngx_create_array(cn->pool, 10, sizeof(ngx_iovec_t)),
28 (ngx_chain_t *) -1);
29
30 ngx_test_null(trailer, ngx_create_array(cn->pool, 10, sizeof(ngx_iovec_t)),
31 (ngx_chain_t *) -1);
32
33 do {
34 header->nelts = 0;
35 trailer->nelts = 0;
36
37 if (ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)) {
38 last = NULL;
39 iov = NULL;
40
41 while (ch
42 && (ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)))
43 {
44 if (ch->hunk->type & NGX_HUNK_FLUSH)
45 continue;
46
47 if (last == ch->hunk->pos.p) {
48 iov->ngx_iov_len += ch->hunk->last.p - ch->hunk->pos.p;
49
50 } else {
51 ngx_test_null(iov, ngx_push_array(header),
52 (ngx_chain_t *) -1);
53 iov->ngx_iov_base = ch->hunk->pos.p;
54 iov->ngx_iov_len = ch->hunk->last.p - ch->hunk->pos.p;
55 last = ch->hunk->last.p;
56 }
57
58 ch = ch->next;
59 }
60 }
61
62 if (ch && (ch->hunk->type & NGX_HUNK_FILE)) {
63 file = ch->hunk;
64 ch = ch->next;
65 }
66
67 #if (HAVE_MAX_SENDFILE_IOVEC)
68 if (file && header->nelts > HAVE_MAX_SENDFILE_IOVEC) {
69 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts, header->nelts,
70 &sent);
71 } else {
72 #endif
73 if (ch && ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)) {
74 last = NULL;
75 iov = NULL;
76
77 while (ch
78 && (ch->hunk->type & (NGX_HUNK_IN_MEMORY | NGX_HUNK_FLUSH)))
79 {
80 if (ch->hunk->type & NGX_HUNK_FLUSH)
81 continue;
82
83 if (last == ch->hunk->pos.p) {
84 iov->ngx_iov_len += ch->hunk->last.p - ch->hunk->pos.p;
85
86 } else {
87 ngx_test_null(iov, ngx_push_array(trailer),
88 (ngx_chain_t *) -1);
89 iov->ngx_iov_base = ch->hunk->pos.p;
90 iov->ngx_iov_len = ch->hunk->last.p - ch->hunk->pos.p;
91 last = ch->hunk->last.p;
92 }
93
94 ch = ch->next;
95 }
96 }
97
98 if (file) {
99 rc = ngx_sendfile(cn->fd,
100 (ngx_iovec_t *) header->elts, header->nelts,
101 file->fd, file->pos.f,
102 (size_t) (file->last.f - file->pos.f),
103 (ngx_iovec_t *) trailer->elts, trailer->nelts,
104 &sent, cn->log);
105 } else {
106 rc = ngx_sendv(cn->fd, (ngx_iovec_t *) header->elts,
107 header->nelts, (size_t *) &sent);
108 }
109 #if (HAVE_MAX_SENDFILE_IOVEC)
110 }
111 #endif
112 /* save sent for logging */
113
114 if (rc == -1)
115 return (ngx_chain_t *) -1;
116
117 flush -= sent;
118
119 for (ch = in; ch && !(ch->hunk->type & NGX_HUNK_LAST); ch = ch->next) {
120 if (sent >= ch->hunk->last.f - ch->hunk->pos.f) {
121 sent -= ch->hunk->last.f - ch->hunk->pos.f;
122 ch->hunk->last.f = ch->hunk->pos.f;
123 continue;
124 }
125
126 ch->hunk->pos.f += sent;
127 break;
128 }
129
130 } while (flush > 0);
131
132 ngx_destroy_array(trailer);
133 ngx_destroy_array(header);
134
135 return ch;
136 }