2003-05-21 21:28:21 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-05-21 21:28:21 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-10-13 00:49:16 +08:00
|
|
|
#include <ngx_event.h>
|
2003-05-21 21:28:21 +08:00
|
|
|
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#define NGX_IOVS 16
|
2004-09-18 00:07:35 +08:00
|
|
|
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_chain_t *ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
2003-05-21 21:28:21 +08:00
|
|
|
{
|
2004-09-18 00:07:35 +08:00
|
|
|
u_char *prev;
|
2004-11-11 22:07:14 +08:00
|
|
|
ssize_t n, size, sent;
|
2004-12-03 02:40:46 +08:00
|
|
|
off_t send, prev_send;
|
2004-09-18 00:07:35 +08:00
|
|
|
ngx_uint_t eintr, complete;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_array_t vec;
|
|
|
|
ngx_chain_t *cl;
|
|
|
|
ngx_event_t *wev;
|
|
|
|
struct iovec *iov, iovs[NGX_IOVS];
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
wev = c->write;
|
|
|
|
|
|
|
|
if (!wev->ready) {
|
2003-10-13 00:49:16 +08:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_KQUEUE)
|
2004-02-12 01:08:49 +08:00
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
|
|
|
|
"kevent() reported about an closed connection");
|
|
|
|
|
|
|
|
wev->error = 1;
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
/* the maximum limit size is the maximum size_t value - the page size */
|
|
|
|
|
|
|
|
if (limit == 0 || limit > MAX_SIZE_T_VALUE - ngx_pagesize) {
|
|
|
|
limit = MAX_SIZE_T_VALUE - ngx_pagesize;
|
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
send = 0;
|
|
|
|
complete = 0;
|
|
|
|
|
2004-09-18 00:07:35 +08:00
|
|
|
vec.elts = iovs;
|
|
|
|
vec.size = sizeof(struct iovec);
|
|
|
|
vec.nalloc = NGX_IOVS;
|
|
|
|
vec.pool = c->pool;
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for ( ;; ) {
|
2003-11-28 03:01:37 +08:00
|
|
|
prev = NULL;
|
|
|
|
iov = NULL;
|
|
|
|
eintr = 0;
|
2004-12-03 02:40:46 +08:00
|
|
|
prev_send = send;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-09-18 00:07:35 +08:00
|
|
|
vec.nelts = 0;
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* create the iovec and coalesce the neighbouring bufs */
|
2003-11-14 15:20:34 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for (cl = in; cl && vec.nelts < IOV_MAX && send < limit; cl = cl->next)
|
|
|
|
{
|
|
|
|
if (ngx_buf_special(cl->buf)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if 1
|
|
|
|
if (!ngx_buf_in_memory(cl->buf)) {
|
|
|
|
ngx_debug_point();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
size = cl->buf->last - cl->buf->pos;
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (prev == cl->buf->pos) {
|
2004-06-21 23:59:32 +08:00
|
|
|
iov->iov_len += size;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
} else {
|
2004-09-18 00:07:35 +08:00
|
|
|
if (!(iov = ngx_array_push(&vec))) {
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
iov->iov_base = (void *) cl->buf->pos;
|
2004-06-21 23:59:32 +08:00
|
|
|
iov->iov_len = size;
|
2003-11-28 03:01:37 +08:00
|
|
|
}
|
2004-06-21 23:59:32 +08:00
|
|
|
|
|
|
|
prev = cl->buf->pos + size;
|
|
|
|
send += size;
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
n = writev(c->fd, vec.elts, vec.nelts);
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
if (n == -1) {
|
|
|
|
err = ngx_errno;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"writev() not ready");
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
} else {
|
|
|
|
wev->error = 1;
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_connection_error(c, err, "writev() failed");
|
2003-11-28 03:01:37 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
sent = n > 0 ? n : 0;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %z", sent);
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (send - prev_send == sent) {
|
2004-06-21 23:59:32 +08:00
|
|
|
complete = 1;
|
|
|
|
}
|
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
c->sent += sent;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
for (cl = in; cl; cl = cl->next) {
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
size = cl->buf->last - cl->buf->pos;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
if (sent >= size) {
|
|
|
|
sent -= size;
|
2004-06-21 23:59:32 +08:00
|
|
|
cl->buf->pos = cl->buf->last;
|
2003-11-28 03:01:37 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
cl->buf->pos += sent;
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
break;
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (eintr) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (!complete) {
|
|
|
|
wev->ready = 0;
|
|
|
|
return cl;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send >= limit || cl == NULL) {
|
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
|
|
|
in = cl;
|
|
|
|
}
|
2003-05-21 21:28:21 +08:00
|
|
|
}
|