nginx/src/os/unix/ngx_solaris_sendfilev_chain.c

167 lines
4.2 KiB
C
Raw Normal View History

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>
ngx_chain_t *ngx_solaris_sendfilev_chain(ngx_connection_t *c, ngx_chain_t *in)
{
int fd;
2004-03-16 15:10:12 +08:00
u_char *prev;
2003-11-27 15:45:22 +08:00
off_t fprev;
2003-11-28 03:01:37 +08:00
size_t sent, size;
2003-11-27 15:45:22 +08:00
ssize_t n;
ngx_int_t eintr;
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;
}
do {
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;
2003-11-28 03:01:37 +08:00
ngx_init_array(vec, c->pool, 10, sizeof(sendfilevec_t),
NGX_CHAIN_ERROR);
/* create the sendfilevec and coalesce the neighbouring bufs */
2003-11-27 15:45:22 +08:00
2004-02-18 23:45:21 +08:00
for (cl = in; cl && vec.nelts < IOV_MAX; cl = cl->next) {
2003-11-27 15:45:22 +08:00
if (ngx_hunk_special(cl->hunk)) {
continue;
}
if (ngx_hunk_in_memory_only(cl->hunk)) {
fd = SFV_FD_SELF;
if (prev == cl->hunk->pos) {
sfv->sfv_len += cl->hunk->last - cl->hunk->pos;
} else {
ngx_test_null(sfv, ngx_push_array(&vec), NGX_CHAIN_ERROR);
sfv->sfv_fd = SFV_FD_SELF;
sfv->sfv_flag = 0;
2003-11-28 03:01:37 +08:00
sfv->sfv_off = (off_t) (uintptr_t) cl->hunk->pos;
2003-11-27 15:45:22 +08:00
sfv->sfv_len = cl->hunk->last - cl->hunk->pos;
}
prev = cl->hunk->last;
} else {
prev = NULL;
if (fd == cl->hunk->file->fd && fprev == cl->hunk->file_pos) {
sfv->sfv_len += cl->hunk->file_last - cl->hunk->file_pos;
} else {
ngx_test_null(sfv, ngx_push_array(&vec), NGX_CHAIN_ERROR);
fd = cl->hunk->file->fd;
sfv->sfv_fd = fd;
sfv->sfv_flag = 0;
sfv->sfv_off = cl->hunk->file_pos;
sfv->sfv_len = cl->hunk->file_last - cl->hunk->file_pos;
}
fprev = cl->hunk->file_last;
}
}
2004-02-18 23:45:21 +08:00
/*
* the tail is the rest of the chain that exceedes a single
* sendfilev() capability, IOV_MAX in Solaris is limited by 16
2004-02-18 23:45:21 +08:00
*/
tail = cl;
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
c->sent += sent;
for (cl = in; cl; cl = cl->next) {
if (ngx_hunk_special(cl->hunk)) {
continue;
}
if (sent == 0) {
break;
}
size = ngx_hunk_size(cl->hunk);
if (sent >= size) {
sent -= size;
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
cl->hunk->pos = cl->hunk->last;
}
if (cl->hunk->type & NGX_HUNK_FILE) {
cl->hunk->file_pos = cl->hunk->file_last;
}
continue;
}
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
cl->hunk->pos += sent;
}
if (cl->hunk->type & NGX_HUNK_FILE) {
cl->hunk->file_pos += sent;
}
break;
}
in = cl;
2004-02-18 23:45:21 +08:00
/* "tail == in" means that a single sendfilev() is complete */
} while ((tail && tail == in) || eintr);
2003-11-27 15:45:22 +08:00
if (in) {
wev->ready = 0;
}
return in;
}