Mercurial > hg > nginx
comparison src/os/unix/ngx_recv.c @ 96:a23d010f356d
nginx-0.0.1-2003-05-27-16:18:54 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 27 May 2003 12:18:54 +0000 |
parents | 738fe44c70d5 |
children | 6dfda4cf5200 |
comparison
equal
deleted
inserted
replaced
95:b48066122884 | 96:a23d010f356d |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.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 | 4 |
5 | |
6 static int ngx_unix_recv_error(ngx_event_t *rev, ngx_err_t err); | |
7 | |
8 | |
9 #if (HAVE_KQUEUE) | |
9 | 10 |
10 ssize_t ngx_unix_recv(ngx_connection_t *c, char *buf, size_t size) | 11 ssize_t ngx_unix_recv(ngx_connection_t *c, char *buf, size_t size) |
11 { | 12 { |
12 ssize_t n; | 13 ssize_t n; |
13 ngx_err_t err; | 14 ngx_event_t *rev; |
14 ngx_event_t *ev; | |
15 | 15 |
16 ev = c->read; | 16 rev = c->read; |
17 | 17 |
18 #if (HAVE_KQUEUE) /* DEBUG */ | |
19 if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { | 18 if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { |
20 ngx_log_debug(c->log, "recv: eof:%d, avail:%d, err:%d" _ | 19 ngx_log_debug(c->log, "recv: eof:%d, avail:%d, err:%d" _ |
21 ev->eof _ ev->available _ ev->error); | 20 rev->eof _ rev->available _ rev->error); |
22 } | |
23 #endif | |
24 | 21 |
25 #if (HAVE_KQUEUE) | 22 if (rev->available == 0) { |
23 if (rev->eof) { | |
24 if (rev->error) { | |
25 rev->ready = 0; | |
26 ngx_set_socket_errno(rev->error); | |
27 return ngx_unix_recv_error(rev, rev->error); | |
28 } | |
29 return 0; | |
26 | 30 |
27 if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) | 31 } else { |
28 && ev->eof && ev->available == 0) { | 32 return NGX_AGAIN; |
29 | 33 } |
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 | |
41 ngx_log_debug(c->log, "recv: read:%d:%d" _ n _ size); | |
42 | |
43 if (n == -1) { | |
44 err = ngx_socket_errno; | |
45 } | 34 } |
46 } | 35 } |
47 | 36 |
48 #else /* not kqueue */ | 37 do { |
38 n = recv(c->fd, buf, size, 0); | |
49 | 39 |
50 n = recv(c->fd, buf, size, 0); | 40 ngx_log_debug(c->log, "recv: %d:%d" _ n _ size); |
51 | 41 |
52 ngx_log_debug(c->log, "recv: read:%d:%d" _ n _ size); | 42 if (n >= 0) { |
43 if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { | |
44 rev->available -= n; | |
45 if (rev->available == 0) { | |
46 rev->ready = 0; | |
47 } | |
53 | 48 |
54 if (n == -1) { | 49 return n; |
55 err = ngx_socket_errno; | 50 } |
56 } | |
57 | 51 |
58 #endif | 52 if ((size_t) n < size) { |
53 rev->ready = 0; | |
54 } | |
59 | 55 |
60 if (n == -1) { | 56 return n; |
61 ev->ready = 0; | |
62 | |
63 if (err == NGX_ECONNRESET && ev->ignore_econnreset) { | |
64 return 0; | |
65 } | 57 } |
66 | 58 |
67 if (err == NGX_EAGAIN) { | 59 rev->ready = 0; |
68 ngx_log_error(NGX_LOG_INFO, c->log, err, "recv() returned EAGAIN"); | 60 n = ngx_unix_recv_error(rev, ngx_socket_errno); |
69 return NGX_AGAIN; | |
70 } | |
71 | 61 |
72 ngx_log_error(NGX_LOG_ERR, c->log, err, "recv() failed"); | 62 } while (n == NGX_EINTR); |
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 | 63 |
93 return n; | 64 return n; |
94 } | 65 } |
66 | |
67 #else /* ! NAVE_KQUEUE */ | |
68 | |
69 ssize_t ngx_unix_recv(ngx_connection_t *c, char *buf, size_t size) | |
70 { | |
71 ssize_t n; | |
72 ngx_event_t *rev; | |
73 | |
74 rev = c->read; | |
75 | |
76 do { | |
77 n = recv(c->fd, buf, size, 0); | |
78 | |
79 ngx_log_debug(c->log, "recv: %d:%d" _ n _ size); | |
80 | |
81 if (n >= 0) { | |
82 if ((size_t) n < size) { | |
83 rev->ready = 0; | |
84 } | |
85 return n; | |
86 } | |
87 | |
88 rev->ready = 0; | |
89 n = ngx_unix_recv_error(rev, ngx_socket_errno); | |
90 | |
91 } while (n == NGX_EINTR); | |
92 | |
93 return n; | |
94 } | |
95 | |
96 #endif /* NAVE_KQUEUE */ | |
97 | |
98 | |
99 static int ngx_unix_recv_error(ngx_event_t *rev, ngx_err_t err) | |
100 { | |
101 if (err == NGX_ECONNRESET && rev->ignore_econnreset) { | |
102 return 0; | |
103 } | |
104 | |
105 if (err == NGX_EAGAIN) { | |
106 ngx_log_error(NGX_LOG_INFO, rev->log, err, "recv() returned EAGAIN"); | |
107 return NGX_AGAIN; | |
108 } | |
109 | |
110 if (err == NGX_EINTR) { | |
111 ngx_log_error(NGX_LOG_INFO, rev->log, err, "recv() returned EINTR"); | |
112 return NGX_EINTR; | |
113 } | |
114 | |
115 ngx_log_error(NGX_LOG_ERR, rev->log, err, "recv() failed"); | |
116 | |
117 return NGX_ERROR; | |
118 } |