1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_types.h>
#include <ngx_alloc.h>
#include <ngx_array.h>
#include <ngx_hunk.h>
#include <ngx_connection.h>
#include <ngx_sendv.h>
#include <ngx_sendfile.h>
#include <ngx_event_write.h>
ngx_chain_t *ngx_event_write(ngx_connection_t *c, ngx_chain_t *in,
off_t flush)
{
int rc;
char *last;
off_t sent;
ngx_iovec_t *iov;
ngx_array_t *header, *trailer;
ngx_hunk_t *file;
ngx_chain_t *ch;
ch = in;
file = NULL;
ngx_test_null(header, ngx_create_array(c->pool, 10, sizeof(ngx_iovec_t)),
(ngx_chain_t *) -1);
ngx_test_null(trailer, ngx_create_array(c->pool, 10, sizeof(ngx_iovec_t)),
(ngx_chain_t *) -1);
do {
header->nelts = 0;
trailer->nelts = 0;
if (ch->hunk->type & NGX_HUNK_IN_MEMORY) {
last = NULL;
iov = NULL;
while (ch && (ch->hunk->type & NGX_HUNK_IN_MEMORY))
{
if (last == ch->hunk->pos.mem) {
iov->ngx_iov_len += ch->hunk->last.mem - ch->hunk->pos.mem;
} else {
ngx_test_null(iov, ngx_push_array(header),
(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;
}
ch = ch->next;
}
}
if (ch && (ch->hunk->type & NGX_HUNK_FILE)) {
file = ch->hunk;
ch = ch->next;
}
#if (HAVE_MAX_SENDFILE_IOVEC)
if (file && header->nelts > HAVE_MAX_SENDFILE_IOVEC) {
rc = ngx_sendv(c->fd, (ngx_iovec_t *) header->elts, header->nelts,
&sent);
} else {
#endif
if (ch && ch->hunk->type & NGX_HUNK_IN_MEMORY) {
last = NULL;
iov = NULL;
while (ch && (ch->hunk->type & NGX_HUNK_IN_MEMORY)) {
if (last == ch->hunk->pos.mem) {
iov->ngx_iov_len +=
ch->hunk->last.mem - ch->hunk->pos.mem;
} else {
ngx_test_null(iov, ngx_push_array(trailer),
(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;
}
ch = ch->next;
}
}
if (file) {
rc = ngx_sendfile(c->fd,
(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);
} else {
rc = ngx_sendv(c, (ngx_iovec_t *) header->elts,
header->nelts);
sent = rc > 0 ? rc: 0;
ngx_log_debug(c->log, "sendv: " QD_FMT _ sent);
}
#if (HAVE_MAX_SENDFILE_IOVEC)
}
#endif
if (rc == NGX_ERROR)
return (ngx_chain_t *) NGX_ERROR;
c->sent += sent;
flush -= sent;
for (ch = in; ch; ch = ch->next) {
ngx_log_debug(c->log, "ch event write: %x %qx %qd" _
ch->hunk->type _
ch->hunk->pos.file _
ch->hunk->last.file - ch->hunk->pos.file);
if (sent >= ch->hunk->last.file - ch->hunk->pos.file) {
sent -= ch->hunk->last.file - ch->hunk->pos.file;
ch->hunk->pos.file = ch->hunk->last.file;
ngx_log_debug(c->log, "event write: " QX_FMT " 0 " QD_FMT _
ch->hunk->pos.file _ sent);
/*
if (ch->hunk->type & NGX_HUNK_LAST)
break;
*/
continue;
}
ch->hunk->pos.file += sent;
ngx_log_debug(c->log, "event write: %qx %qd" _
ch->hunk->pos.file _
ch->hunk->last.file - ch->hunk->pos.file);
break;
}
/* flush hunks if threaded state */
} while (c->write->context && flush > 0);
ngx_destroy_array(trailer);
ngx_destroy_array(header);
return ch;
}
|