comparison src/os/unix/ngx_freebsd_write_chain.c @ 63:36d2c25cc9bb

nginx-0.0.1-2003-02-26-23:21:43 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 26 Feb 2003 20:21:43 +0000
parents
children 5a7d1aaa1618
comparison
equal deleted inserted replaced
62:8ccba41a678e 63:36d2c25cc9bb
1
2 #include <ngx_config.h>
3
4 #include <ngx_core.h>
5 #include <ngx_types.h>
6 #include <ngx_alloc.h>
7 #include <ngx_array.h>
8 #include <ngx_hunk.h>
9 #include <ngx_connection.h>
10 #include <ngx_sendv.h>
11 #include <ngx_sendfile.h>
12
13
14 ngx_chain_t *ngx_freebsd_write_chain(ngx_connection_t *c, ngx_chain_t *in)
15 {
16 int rc;
17 char *prev;
18 size_t hsize;
19 off_t sent;
20 struct iovec *iov;
21 struct sf_hdtr hdtr;
22 ngx_err_t err;
23 ngx_array_t header, trailer;
24 ngx_hunk_t *file;
25 ngx_chain_t *ce;
26
27 ce = in;
28 file = NULL;
29 hsize = 0;
30
31 ngx_init_array(header, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
32 ngx_init_array(trailer, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
33
34 /* create the header iovec */
35 if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
36 prev = NULL;
37 iov = NULL;
38
39 /* create the iovec and coalesce the neighbouring chain entries */
40 while (ce && (ce->hunk->type & NGX_HUNK_IN_MEMORY))
41 {
42 if (prev == ce->hunk->pos.mem) {
43 iov->iov_len += ce->hunk->last.mem - ce->hunk->pos.mem;
44 prev = ce->hunk->last.mem;
45
46 } else {
47 ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
48 iov->iov_base = ce->hunk->pos.mem;
49 iov->iov_len = ce->hunk->last.mem - ce->hunk->pos.mem;
50 prev = ce->hunk->last.mem;
51 }
52
53 #if (HAVE_FREEBSD_SENDFILE_NBYTES_BUG)
54 hsize += ce->hunk->last.mem - ce->hunk->pos.mem;
55 #endif
56 ce = ce->next;
57 }
58 }
59
60 /* TODO: coalesce the neighbouring shadow file hunks */
61 if (ce && (ce->hunk->type & NGX_HUNK_FILE)) {
62 file = ce->hunk;
63 ce = ce->next;
64 }
65
66 /* create the trailer iovec */
67 if (ce && ce->hunk->type & NGX_HUNK_IN_MEMORY) {
68 prev = NULL;
69 iov = NULL;
70
71 /* create the iovec and coalesce the neighbouring chain entries */
72 while (ce && (ce->hunk->type & NGX_HUNK_IN_MEMORY)) {
73
74 if (prev == ce->hunk->pos.mem) {
75 iov->iov_len += ce->hunk->last.mem - ce->hunk->pos.mem;
76 prev = ce->hunk->last.mem;
77
78 } else {
79 ngx_test_null(iov, ngx_push_array(&trailer), NGX_CHAIN_ERROR);
80 iov->iov_base = ce->hunk->pos.mem;
81 iov->iov_len = ce->hunk->last.mem - ce->hunk->pos.mem;
82 prev = ce->hunk->last.mem;
83 }
84
85 ce = ce->next;
86 }
87 }
88
89 if (file) {
90 hdtr.headers = (struct iovec *) header.elts;
91 hdtr.hdr_cnt = header.nelts;
92 hdtr.trailers = (struct iovec *) trailer.elts;
93 hdtr.trl_cnt = trailer.nelts;
94
95 rc = sendfile(file->file->fd, c->fd, file->pos.file,
96 (size_t) (file->last.file - file->pos.file) + hsize,
97 &hdtr, &sent, 0);
98
99 if (rc == -1) {
100 err = ngx_errno;
101 if (err == NGX_EAGAIN || err == NGX_EINTR) {
102 ngx_log_error(NGX_LOG_INFO, c->log, err,
103 "sendfile() sent only %qd bytes", sent);
104
105 } else {
106 ngx_log_error(NGX_LOG_CRIT, c->log, err, "sendfile() failed");
107 return NGX_CHAIN_ERROR;
108 }
109 }
110
111 #if (NGX_DEBUG_WRITE_CHAIN)
112 ngx_log_debug(c->log, "sendfile: %d, @%qd %qd:%d" _
113 rc _ file->pos.file _ *sent _
114 (size_t) (file->last.file - file->pos.file) + hsize);
115 #endif
116
117 } else {
118 rc = writev(c->fd, (struct iovec *) header.elts, header.nelts);
119
120 if (rc == -1) {
121 err = ngx_errno;
122 if (err == NGX_EAGAIN) {
123 ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EAGAIN");
124
125 } else if (err == NGX_EINTR) {
126 ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EINTR");
127
128 } else {
129 ngx_log_error(NGX_LOG_CRIT, c->log, err, "writev() failed");
130 return NGX_CHAIN_ERROR;
131 }
132 }
133
134 sent = rc > 0 ? rc : 0;
135 }
136
137 #if (NGX_DEBUG_WRITE_CHAIN)
138 ngx_log_debug(c->log, "sendv: %qd" _ sent);
139 #endif
140
141 c->sent += sent;
142
143 for (ce = in; ce; ce = ce->next) {
144
145 #if (NGX_DEBUG_WRITE_CHAIN)
146 ngx_log_debug(c->log, "write chain: %x %qx %qd" _
147 ce->hunk->type _
148 ce->hunk->pos.file _
149 ce->hunk->last.file - ce->hunk->pos.file);
150 #endif
151
152 if (sent >= ce->hunk->last.file - ce->hunk->pos.file) {
153 sent -= ce->hunk->last.file - ce->hunk->pos.file;
154 ce->hunk->pos.file = ce->hunk->last.file;
155
156 #if (NGX_DEBUG_WRITE_CHAIN)
157 ngx_log_debug(c->log, "write chain done: %qx %qd" _
158 ce->hunk->pos.file _ sent);
159 #endif
160 continue;
161 }
162
163 ce->hunk->pos.file += sent;
164
165 #if (NGX_DEBUG_WRITE_CHAIN)
166 ngx_log_debug(c->log, "write chain rest: %qx %qd" _
167 ce->hunk->pos.file _
168 ce->hunk->last.file - ce->hunk->pos.file);
169 #endif
170
171 break;
172 }
173
174 ngx_destroy_array(&trailer);
175 ngx_destroy_array(&header);
176
177 return ce;
178 }