nginx/src/os/unix/ngx_recv.c

165 lines
3.7 KiB
C
Raw Normal View History

2003-05-20 00:39:14 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2003-06-11 23:28:34 +08:00
#include <ngx_event.h>
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
#if (HAVE_KQUEUE)
2003-05-20 00:39:14 +08:00
ssize_t ngx_unix_recv(ngx_connection_t *c, char *buf, size_t size)
{
ssize_t n;
2004-02-09 15:46:43 +08:00
ngx_err_t err;
2003-05-27 20:18:54 +08:00
ngx_event_t *rev;
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
rev = c->read;
2003-05-20 00:39:14 +08:00
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
2004-01-06 04:55:48 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
"recv: eof:%d, avail:%d, err:%d",
rev->kq_eof, rev->available, rev->kq_errno);
2003-05-27 20:18:54 +08:00
if (rev->available == 0) {
2003-10-28 23:45:41 +08:00
if (rev->kq_eof) {
2003-10-17 04:19:16 +08:00
rev->ready = 0;
2003-10-28 23:45:41 +08:00
rev->eof = 1;
2004-02-09 15:46:43 +08:00
ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
"kevent() reported about an closed connection");
2003-10-28 23:45:41 +08:00
if (rev->kq_errno) {
rev->error = 1;
ngx_set_socket_errno(rev->kq_errno);
2004-01-06 04:55:48 +08:00
if (rev->kq_errno == NGX_ECONNRESET
2004-02-11 00:23:38 +08:00
&& c->log_error == NGX_ERROR_IGNORE_ECONNRESET)
2004-01-06 04:55:48 +08:00
{
return 0;
}
return NGX_ERROR;
2003-05-27 20:18:54 +08:00
}
2003-10-28 23:45:41 +08:00
2003-05-27 20:18:54 +08:00
return 0;
} else {
return NGX_AGAIN;
}
}
2003-05-20 00:39:14 +08:00
}
2003-05-27 20:18:54 +08:00
do {
n = recv(c->fd, buf, size, 0);
2003-05-20 00:39:14 +08:00
2004-02-09 15:46:43 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
"recv: fd:%d %d of %d", c->fd, n, size);
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
if (n >= 0) {
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
rev->available -= n;
2003-10-28 23:45:41 +08:00
/*
* rev->available can be negative here because some additional
* bytes can be received between kevent() and recv()
*/
2003-10-13 00:49:16 +08:00
if (rev->available <= 0) {
2003-10-28 23:45:41 +08:00
if (!rev->kq_eof) {
2003-10-17 04:19:16 +08:00
rev->ready = 0;
}
2003-10-13 00:49:16 +08:00
if (rev->available < 0) {
rev->available = 0;
}
2003-05-27 20:18:54 +08:00
}
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
return n;
}
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
if ((size_t) n < size) {
rev->ready = 0;
}
2003-05-20 00:39:14 +08:00
2003-10-28 23:45:41 +08:00
if (n == 0) {
rev->eof = 1;
}
2003-05-27 20:18:54 +08:00
return n;
2003-05-20 00:39:14 +08:00
}
2004-02-09 15:46:43 +08:00
err = ngx_socket_errno;
2003-05-27 20:18:54 +08:00
2004-02-09 15:46:43 +08:00
if (err == NGX_EAGAIN || err == NGX_EINTR) {
2004-02-12 01:08:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
2004-02-09 15:46:43 +08:00
"recv() not ready");
n = NGX_AGAIN;
} else {
n = ngx_connection_error(c, err, "recv() failed");
break;
}
2003-05-20 00:39:14 +08:00
2004-02-09 15:46:43 +08:00
} while (err == NGX_EINTR);
2003-11-18 16:04:34 +08:00
rev->ready = 0;
2003-11-14 15:20:34 +08:00
if (n == NGX_ERROR){
rev->error = 1;
}
2003-05-27 20:18:54 +08:00
return n;
}
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
#else /* ! NAVE_KQUEUE */
2003-05-21 21:28:21 +08:00
2003-05-27 20:18:54 +08:00
ssize_t ngx_unix_recv(ngx_connection_t *c, char *buf, size_t size)
{
ssize_t n;
2004-02-09 15:46:43 +08:00
ngx_err_t err;
2003-05-27 20:18:54 +08:00
ngx_event_t *rev;
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
rev = c->read;
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
do {
n = recv(c->fd, buf, size, 0);
2003-05-20 00:39:14 +08:00
2004-02-09 15:46:43 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
"recv: fd:%d %d of %d", c->fd, n, size);
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
if (n >= 0) {
if ((size_t) n < size) {
rev->ready = 0;
}
2003-10-28 23:45:41 +08:00
if (n == 0) {
rev->eof = 1;
}
2003-05-27 20:18:54 +08:00
return n;
2003-05-20 00:39:14 +08:00
}
2004-02-09 15:46:43 +08:00
err = ngx_socket_errno;
if (err == NGX_EAGAIN || err == NGX_EINTR) {
2004-02-12 01:08:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
2004-02-09 15:46:43 +08:00
"recv() not ready");
n = NGX_AGAIN;
2003-05-20 00:39:14 +08:00
2004-02-09 15:46:43 +08:00
} else {
n = ngx_connection_error(c, err, "recv() failed");
break;
}
2003-05-27 20:18:54 +08:00
2004-02-09 15:46:43 +08:00
} while (err == NGX_EINTR);
2003-11-18 16:04:34 +08:00
rev->ready = 0;
2003-11-14 15:20:34 +08:00
if (n == NGX_ERROR){
rev->error = 1;
}
2003-05-27 20:18:54 +08:00
return n;
}
#endif /* NAVE_KQUEUE */