nginx/src/os/unix/ngx_writev_chain.c

99 lines
2.2 KiB
C
Raw Normal View History

2003-05-21 21:28:21 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
ngx_chain_t *ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in)
{
char *prev;
2003-06-11 23:28:34 +08:00
ssize_t n, size;
2003-05-21 21:28:21 +08:00
off_t sent;
struct iovec *iov;
ngx_err_t err;
2003-06-11 23:28:34 +08:00
ngx_array_t iovecs;
2003-05-21 21:28:21 +08:00
ngx_chain_t *ce;
2003-06-11 23:28:34 +08:00
ngx_init_array(iovecs, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
2003-05-21 21:28:21 +08:00
prev = NULL;
iov = NULL;
/* create the iovec and coalesce the neighbouring chain entries */
for (ce = in; ce; ce = ce->next) {
if (prev == ce->hunk->pos) {
iov->iov_len += ce->hunk->last - ce->hunk->pos;
prev = ce->hunk->last;
} else {
2003-06-11 23:28:34 +08:00
ngx_test_null(iov, ngx_push_array(&iovecs), NGX_CHAIN_ERROR);
2003-05-21 21:28:21 +08:00
iov->iov_base = ce->hunk->pos;
iov->iov_len = ce->hunk->last - ce->hunk->pos;
prev = ce->hunk->last;
}
}
2003-06-11 23:28:34 +08:00
n = writev(c->fd, iovecs.elts, iovecs.nelts);
2003-05-21 21:28:21 +08:00
if (n == -1) {
err = ngx_errno;
if (err == NGX_EAGAIN) {
ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EAGAIN");
} else if (err == NGX_EINTR) {
ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EINTR");
} else {
ngx_log_error(NGX_LOG_CRIT, c->log, err, "writev() failed");
return NGX_CHAIN_ERROR;
}
}
sent = n > 0 ? n : 0;
#if (NGX_DEBUG_WRITE_CHAIN)
ngx_log_debug(c->log, "writev: %qd" _ sent);
#endif
c->sent += sent;
for (ce = in; ce && sent > 0; ce = ce->next) {
size = ce->hunk->last - ce->hunk->pos;
2003-05-22 23:23:47 +08:00
ngx_log_debug(c->log, "SIZE: %d" _ size);
2003-05-21 21:28:21 +08:00
if (sent >= size) {
sent -= size;
if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
ce->hunk->pos = ce->hunk->last;
}
2003-05-22 23:23:47 +08:00
#if 0
2003-05-21 21:28:21 +08:00
if (ce->hunk->type & NGX_HUNK_FILE) {
ce->hunk->file_pos = ce->hunk->file_last;
}
2003-05-22 23:23:47 +08:00
#endif
2003-05-21 21:28:21 +08:00
continue;
}
if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
ce->hunk->pos += sent;
}
2003-05-22 23:23:47 +08:00
#if 0
2003-05-21 21:28:21 +08:00
if (ce->hunk->type & NGX_HUNK_FILE) {
ce->hunk->file_pos += sent;
}
2003-05-22 23:23:47 +08:00
#endif
2003-05-21 21:28:21 +08:00
break;
}
2003-06-11 23:28:34 +08:00
ngx_destroy_array(&iovecs);
2003-05-21 21:28:21 +08:00
return ce;
}