2003-05-20 00:39:14 +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-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
|
|
|
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_KQUEUE)
|
2003-05-27 20:18:54 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
ssize_t ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
2003-05-20 00:39:14 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if (ngx_event_flags & NGX_USE_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",
|
2004-07-02 23:54:34 +08:00
|
|
|
rev->pending_eof, rev->available, rev->kq_errno);
|
2003-05-27 20:18:54 +08:00
|
|
|
|
|
|
|
if (rev->available == 0) {
|
2004-07-02 23:54:34 +08:00
|
|
|
if (rev->pending_eof) {
|
2003-10-17 04:19:16 +08:00
|
|
|
rev->ready = 0;
|
2003-10-28 23:45:41 +08:00
|
|
|
rev->eof = 1;
|
|
|
|
|
|
|
|
if (rev->kq_errno) {
|
|
|
|
rev->error = 1;
|
|
|
|
ngx_set_socket_errno(rev->kq_errno);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2005-08-30 18:55:07 +08:00
|
|
|
return ngx_connection_error(c, rev->kq_errno,
|
|
|
|
"kevent() reported about an closed connection");
|
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) {
|
2004-10-21 23:34:38 +08:00
|
|
|
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
2003-05-27 20:18:54 +08:00
|
|
|
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) {
|
2004-07-02 23:54:34 +08:00
|
|
|
if (!rev->pending_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
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#else /* ! NGX_HAVE_KQUEUE */
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-04-02 23:13:20 +08:00
|
|
|
ssize_t ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
2003-05-27 20:18:54 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2004-04-28 14:14:50 +08:00
|
|
|
if (n == 0) {
|
|
|
|
rev->ready = 0;
|
|
|
|
rev->eof = 1;
|
|
|
|
return n;
|
2003-10-28 23:45:41 +08:00
|
|
|
|
2004-04-28 14:14:50 +08:00
|
|
|
} else if (n > 0) {
|
|
|
|
|
|
|
|
if ((size_t) n < size
|
2004-10-21 23:34:38 +08:00
|
|
|
&& !(ngx_event_flags & NGX_USE_GREEDY_EVENT))
|
2004-04-28 14:14:50 +08:00
|
|
|
{
|
|
|
|
rev->ready = 0;
|
2003-10-28 23:45:41 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#endif /* NGX_HAVE_KQUEUE */
|