nginx/src/os/unix/ngx_recv.c

147 lines
3.1 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
static int ngx_unix_recv_error(ngx_event_t *rev, ngx_err_t err);
#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;
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) {
2003-05-21 21:28:21 +08:00
ngx_log_debug(c->log, "recv: eof:%d, avail:%d, err:%d" _
2003-10-28 23:45:41 +08:00
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;
if (rev->kq_errno) {
rev->error = 1;
ngx_set_socket_errno(rev->kq_errno);
return ngx_unix_recv_error(rev, rev->kq_errno);
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
2003-05-27 20:18:54 +08:00
ngx_log_debug(c->log, "recv: %d:%d" _ 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
}
2003-05-27 20:18:54 +08:00
rev->ready = 0;
2003-10-28 23:45:41 +08:00
rev->error = 1;
2003-05-27 20:18:54 +08:00
n = ngx_unix_recv_error(rev, ngx_socket_errno);
} while (n == NGX_EINTR);
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
#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;
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
2003-05-27 20:18:54 +08:00
ngx_log_debug(c->log, "recv: %d:%d" _ 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
}
2003-05-27 20:18:54 +08:00
rev->ready = 0;
2003-10-28 23:45:41 +08:00
rev->error = 1;
2003-05-27 20:18:54 +08:00
n = ngx_unix_recv_error(rev, ngx_socket_errno);
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
} while (n == NGX_EINTR);
return n;
}
#endif /* NAVE_KQUEUE */
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
static int ngx_unix_recv_error(ngx_event_t *rev, ngx_err_t err)
{
if (err == NGX_ECONNRESET && rev->ignore_econnreset) {
return 0;
2003-05-20 00:39:14 +08:00
}
2003-05-27 20:18:54 +08:00
if (err == NGX_EAGAIN) {
ngx_log_error(NGX_LOG_INFO, rev->log, err, "recv() returned EAGAIN");
return NGX_AGAIN;
}
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
if (err == NGX_EINTR) {
ngx_log_error(NGX_LOG_INFO, rev->log, err, "recv() returned EINTR");
return NGX_EINTR;
2003-05-20 00:39:14 +08:00
}
2003-05-27 20:18:54 +08:00
ngx_log_error(NGX_LOG_ERR, rev->log, err, "recv() failed");
return NGX_ERROR;
2003-05-20 00:39:14 +08:00
}