2003-11-27 15:45:22 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-11-27 15:45:22 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_chain_t *ngx_solaris_sendfilev_chain(ngx_connection_t *c, ngx_chain_t *in,
|
|
|
|
off_t limit)
|
2003-11-27 15:45:22 +08:00
|
|
|
{
|
|
|
|
int fd;
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *prev;
|
2004-06-21 23:59:32 +08:00
|
|
|
off_t fprev, sprev, send, aligned;
|
|
|
|
size_t size, sent;
|
2003-11-27 15:45:22 +08:00
|
|
|
ssize_t n;
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_int_t eintr, complete;
|
2003-11-28 03:01:37 +08:00
|
|
|
ngx_err_t err;
|
2003-11-27 15:45:22 +08:00
|
|
|
sendfilevec_t *sfv;
|
|
|
|
ngx_array_t vec;
|
|
|
|
ngx_event_t *wev;
|
2004-02-18 23:45:21 +08:00
|
|
|
ngx_chain_t *cl, *tail;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
wev = c->write;
|
|
|
|
|
|
|
|
if (!wev->ready) {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
send = 0;
|
|
|
|
complete = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
2003-11-27 15:45:22 +08:00
|
|
|
fd = SFV_FD_SELF;
|
|
|
|
prev = NULL;
|
2003-11-28 03:01:37 +08:00
|
|
|
fprev = 0;
|
2003-11-27 15:45:22 +08:00
|
|
|
sfv = NULL;
|
|
|
|
eintr = 0;
|
|
|
|
sent = 0;
|
2004-06-21 23:59:32 +08:00
|
|
|
sprev = send;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
ngx_init_array(vec, c->pool, 10, sizeof(sendfilevec_t),
|
|
|
|
NGX_CHAIN_ERROR);
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* create the sendfilevec and coalesce the neighbouring bufs */
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for (cl = in; cl && vec.nelts < IOV_MAX && send < limit; cl = cl->next)
|
|
|
|
{
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-27 15:45:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory_only(cl->buf)) {
|
2003-11-27 15:45:22 +08:00
|
|
|
fd = SFV_FD_SELF;
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
size = cl->buf->last - cl->buf->pos;
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (prev == cl->buf->pos) {
|
2004-06-21 23:59:32 +08:00
|
|
|
sfv->sfv_len += size;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_test_null(sfv, ngx_push_array(&vec), NGX_CHAIN_ERROR);
|
|
|
|
sfv->sfv_fd = SFV_FD_SELF;
|
|
|
|
sfv->sfv_flag = 0;
|
2004-06-01 14:04:46 +08:00
|
|
|
sfv->sfv_off = (off_t) (uintptr_t) cl->buf->pos;
|
2004-06-21 23:59:32 +08:00
|
|
|
sfv->sfv_len = size;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
prev = cl->buf->pos + size;
|
|
|
|
send += size;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
prev = NULL;
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
size = (size_t) (cl->buf->file_last - cl->buf->file_pos);
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
|
|
|
|
aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
|
|
|
|
& ~(ngx_pagesize - 1);
|
|
|
|
|
|
|
|
if (aligned <= cl->buf->file_last) {
|
|
|
|
size = aligned - cl->buf->file_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (fd == cl->buf->file->fd && fprev == cl->buf->file_pos) {
|
2004-06-21 23:59:32 +08:00
|
|
|
sfv->sfv_len += size;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_test_null(sfv, ngx_push_array(&vec), NGX_CHAIN_ERROR);
|
2004-06-01 14:04:46 +08:00
|
|
|
fd = cl->buf->file->fd;
|
2003-11-27 15:45:22 +08:00
|
|
|
sfv->sfv_fd = fd;
|
|
|
|
sfv->sfv_flag = 0;
|
2004-06-01 14:04:46 +08:00
|
|
|
sfv->sfv_off = cl->buf->file_pos;
|
2004-06-21 23:59:32 +08:00
|
|
|
sfv->sfv_len = size;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
fprev = cl->buf->file_pos + size;
|
|
|
|
send += size;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
n = sendfilev(c->fd, vec.elts, vec.nelts, &sent);
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
if (n == -1) {
|
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
2004-02-12 01:08:49 +08:00
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
|
2003-11-27 15:45:22 +08:00
|
|
|
"sendfilev() sent only " SIZE_T_FMT " bytes",
|
|
|
|
sent);
|
2004-02-12 01:08:49 +08:00
|
|
|
|
2003-11-27 15:45:22 +08:00
|
|
|
} else {
|
|
|
|
wev->error = 1;
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_connection_error(c, err, "sendfilev() failed");
|
2003-11-27 15:45:22 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"sendfilev: %d " SIZE_T_FMT, n, sent);
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send - sprev == sent) {
|
|
|
|
complete = 1;
|
|
|
|
}
|
|
|
|
|
2003-11-27 15:45:22 +08:00
|
|
|
c->sent += sent;
|
|
|
|
|
|
|
|
for (cl = in; cl; cl = cl->next) {
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-27 15:45:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
size = ngx_buf_size(cl->buf);
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
if (sent >= size) {
|
|
|
|
sent -= size;
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
|
|
|
cl->buf->pos = cl->buf->last;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos = cl->buf->file_last;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
|
|
|
cl->buf->pos += sent;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos += sent;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (eintr) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (!complete) {
|
|
|
|
wev->ready = 0;
|
|
|
|
return cl;
|
|
|
|
}
|
2004-02-18 23:45:21 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send >= limit || cl == NULL) {
|
|
|
|
return cl;
|
|
|
|
}
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
in = cl;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
}
|