nginx/src/os/unix/ngx_writev_chain.c

91 lines
2.0 KiB
C
Raw Normal View History

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
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-11-03 06:56:18 +08:00
ngx_array_t io;
ngx_chain_t *cl;
2003-05-21 21:28:21 +08:00
2003-10-13 00:49:16 +08:00
if (!c->write->ready) {
return in;
}
2003-11-03 06:56:18 +08:00
ngx_init_array(io, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
2003-05-21 21:28:21 +08:00
prev = NULL;
iov = NULL;
2003-11-03 06:56:18 +08:00
/* create the iovec and coalesce the neighbouring hunks */
2003-11-14 15:20:34 +08:00
2003-11-03 06:56:18 +08:00
for (cl = in; cl; cl = cl->next) {
2003-05-21 21:28:21 +08:00
2003-11-03 06:56:18 +08:00
if (prev == cl->hunk->pos) {
iov->iov_len += cl->hunk->last - cl->hunk->pos;
prev = cl->hunk->last;
2003-05-21 21:28:21 +08:00
} else {
2003-11-03 06:56:18 +08:00
ngx_test_null(iov, ngx_push_array(&io), NGX_CHAIN_ERROR);
iov->iov_base = cl->hunk->pos;
iov->iov_len = cl->hunk->last - cl->hunk->pos;
prev = cl->hunk->last;
2003-05-21 21:28:21 +08:00
}
}
2003-11-03 06:56:18 +08:00
n = writev(c->fd, io.elts, io.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)
2003-11-26 04:44:56 +08:00
ngx_log_debug(c->log, "writev: " OFF_T_FMT _ sent);
2003-05-21 21:28:21 +08:00
#endif
c->sent += sent;
2003-11-03 06:56:18 +08:00
for (cl = in; cl && sent > 0; cl = cl->next) {
2003-05-21 21:28:21 +08:00
2003-11-03 06:56:18 +08:00
size = cl->hunk->last - cl->hunk->pos;
2003-05-21 21:28:21 +08:00
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;
2003-11-03 06:56:18 +08:00
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
cl->hunk->pos = cl->hunk->last;
2003-05-21 21:28:21 +08:00
}
continue;
}
2003-11-03 06:56:18 +08:00
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
cl->hunk->pos += sent;
2003-05-21 21:28:21 +08:00
}
break;
}
2003-11-03 06:56:18 +08:00
return cl;
2003-05-21 21:28:21 +08:00
}