Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 1 | |
| 2 | #include <ngx_config.h> |
| 3 | #include <ngx_core.h> |
| 4 | #include <ngx_errno.h> |
| 5 | #include <ngx_log.h> |
| 6 | #include <ngx_recv.h> |
| 7 | #include <ngx_connection.h> |
| 8 | |
| 9 | |
| 10 | ssize_t ngx_unix_recv(ngx_connection_t *c, char *buf, size_t size) |
| 11 | { |
| 12 | ssize_t n; |
| 13 | ngx_err_t err; |
| 14 | ngx_event_t *ev; |
| 15 | |
| 16 | ev = c->read; |
| 17 | |
| 18 | #if (HAVE_KQUEUE) /* DEBUG */ |
| 19 | if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 20 | ngx_log_debug(c->log, "recv: eof:%d, avail:%d, err:%d" _ |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 21 | ev->eof _ ev->available _ ev->error); |
| 22 | } |
| 23 | #endif |
| 24 | |
| 25 | #if (HAVE_KQUEUE) |
| 26 | |
| 27 | if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) |
| 28 | && ev->eof && ev->available == 0) { |
| 29 | |
| 30 | if (ev->error == 0) { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | ngx_set_socket_errno(ev->error); |
| 35 | err = ev->error; |
| 36 | n = -1; |
| 37 | |
| 38 | } else { |
| 39 | n = recv(c->fd, buf, size, 0); |
| 40 | |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 41 | ngx_log_debug(c->log, "recv: read:%d:%d" _ n _ size); |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 42 | |
| 43 | if (n == -1) { |
| 44 | err = ngx_socket_errno; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #else /* not kqueue */ |
| 49 | |
| 50 | n = recv(c->fd, buf, size, 0); |
| 51 | |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 52 | ngx_log_debug(c->log, "recv: read:%d:%d" _ n _ size); |
| 53 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 54 | if (n == -1) { |
| 55 | err = ngx_socket_errno; |
| 56 | } |
| 57 | |
| 58 | #endif |
| 59 | |
| 60 | if (n == -1) { |
| 61 | ev->ready = 0; |
| 62 | |
| 63 | if (err == NGX_ECONNRESET && ev->ignore_econnreset) { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | if (err == NGX_EAGAIN) { |
| 68 | ngx_log_error(NGX_LOG_INFO, c->log, err, "recv() returned EAGAIN"); |
| 69 | return NGX_AGAIN; |
| 70 | } |
| 71 | |
| 72 | ngx_log_error(NGX_LOG_ERR, c->log, err, "recv() failed"); |
| 73 | return NGX_ERROR; |
| 74 | } |
| 75 | |
| 76 | #if (HAVE_KQUEUE) |
| 77 | |
| 78 | if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { |
| 79 | ev->available -= n; |
| 80 | if (ev->available == 0) { |
| 81 | ev->ready = 0; |
| 82 | } |
| 83 | |
| 84 | return n; |
| 85 | } |
| 86 | |
| 87 | #endif |
| 88 | |
| 89 | if ((size_t) n < size) { |
| 90 | ev->ready = 0; |
| 91 | } |
| 92 | |
| 93 | return n; |
| 94 | } |