comparison src/os/unix/ngx_freebsd_sendfile_chain.c @ 94:8220378432a8

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