1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
void
nxt_conn_wait(nxt_conn_t *c)
{
nxt_event_engine_t *engine;
const nxt_conn_state_t *state;
nxt_debug(c->socket.task, "conn wait fd:%d rdy:%d",
c->socket.fd, c->socket.read_ready);
engine = c->socket.task->thread->engine;
state = c->read_state;
if (c->socket.read_ready) {
nxt_work_queue_add(&engine->fast_work_queue, state->ready_handler,
c->socket.task, c, c->socket.data);
return;
}
c->socket.read_handler = state->ready_handler;
c->socket.error_handler = state->error_handler;
nxt_conn_timer(engine, c, state, &c->read_timer);
nxt_fd_event_enable_read(engine, &c->socket);
}
void
nxt_conn_io_read(nxt_task_t *task, void *obj, void *data)
{
ssize_t n;
nxt_conn_t *c;
nxt_work_queue_t *wq;
nxt_event_engine_t *engine;
nxt_work_handler_t handler;
const nxt_conn_state_t *state;
c = obj;
nxt_debug(task, "conn read fd:%d rdy:%d cl:%d",
c->socket.fd, c->socket.read_ready, c->socket.closed);
engine = task->thread->engine;
state = c->read_state;
if (c->socket.read_ready) {
if (state->io_read_handler == NULL) {
n = c->io->recvbuf(c, c->read);
} else {
n = state->io_read_handler(c);
/* The state can be changed by io_read_handler. */
state = c->read_state;
}
if (n > 0) {
c->nbytes = n;
nxt_recvbuf_update(c->read, n);
nxt_fd_event_block_read(engine, &c->socket);
if (state->timer_autoreset) {
nxt_timer_disable(engine, &c->read_timer);
}
wq = c->read_work_queue;
handler = state->ready_handler;
nxt_work_queue_add(wq, handler, task, c, data);
return;
}
if (n != NXT_AGAIN) {
nxt_fd_event_block_read(engine, &c->socket);
nxt_timer_disable(engine, &c->read_timer);
wq = &engine->fast_work_queue;
handler = (n == 0) ? state->close_handler : state->error_handler;
nxt_work_queue_add(wq, handler, task, c, data);
return;
}
}
/*
* Here c->io->read() is assigned instead of direct nxt_conn_io_read()
* because the function can be called by nxt_kqueue_conn_io_read().
*/
c->socket.read_handler = c->io->read;
c->socket.error_handler = state->error_handler;
if (c->read_timer.state == NXT_TIMER_DISABLED
|| nxt_fd_event_is_disabled(c->socket.read))
{
/* Timer may be set or reset. */
nxt_conn_timer(engine, c, state, &c->read_timer);
if (nxt_fd_event_is_disabled(c->socket.read)) {
nxt_fd_event_enable_read(engine, &c->socket);
}
}
return;
}
ssize_t
nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b)
{
ssize_t n;
nxt_err_t err;
nxt_uint_t niov;
struct iovec iov[NXT_IOBUF_MAX];
nxt_recvbuf_coalesce_t rb;
rb.buf = b;
rb.iobuf = iov;
rb.nmax = NXT_IOBUF_MAX;
rb.size = 0;
niov = nxt_recvbuf_mem_coalesce(&rb);
if (niov == 1) {
/* Disposal of surplus kernel iovec copy-in operation. */
return nxt_conn_io_recv(c, iov->iov_base, iov->iov_len, 0);
}
for ( ;; ) {
n = readv(c->socket.fd, iov, niov);
err = (n == -1) ? nxt_socket_errno : 0;
nxt_debug(c->socket.task, "readv(%d, %ui): %z", c->socket.fd, niov, n);
if (n > 0) {
if ((size_t) n < rb.size) {
c->socket.read_ready = 0;
}
return n;
}
if (n == 0) {
c->socket.closed = 1;
c->socket.read_ready = 0;
return n;
}
/* n == -1 */
switch (err) {
case NXT_EAGAIN:
nxt_debug(c->socket.task, "readv() %E", err);
c->socket.read_ready = 0;
return NXT_AGAIN;
case NXT_EINTR:
nxt_debug(c->socket.task, "readv() %E", err);
continue;
default:
c->socket.error = err;
nxt_log(c->socket.task, nxt_socket_error_level(err),
"readv(%d, %ui) failed %E", c->socket.fd, niov, err);
return NXT_ERROR;
}
}
}
ssize_t
nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags)
{
ssize_t n;
nxt_err_t err;
for ( ;; ) {
n = recv(c->socket.fd, buf, size, flags);
err = (n == -1) ? nxt_socket_errno : 0;
nxt_debug(c->socket.task, "recv(%d, %p, %uz, 0x%ui): %z",
c->socket.fd, buf, size, flags, n);
if (n > 0) {
if ((size_t) n < size) {
c->socket.read_ready = 0;
}
return n;
}
if (n == 0) {
c->socket.closed = 1;
c->socket.read_ready = 0;
return n;
}
/* n == -1 */
switch (err) {
case NXT_EAGAIN:
nxt_debug(c->socket.task, "recv() %E", err);
c->socket.read_ready = 0;
return NXT_AGAIN;
case NXT_EINTR:
nxt_debug(c->socket.task, "recv() %E", err);
continue;
default:
c->socket.error = err;
nxt_log(c->socket.task, nxt_socket_error_level(err),
"recv(%d, %p, %uz, %ui) failed %E",
c->socket.fd, buf, size, flags, err);
return NXT_ERROR;
}
}
}
|