-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathbase.cc
421 lines (363 loc) Β· 12 KB
/
base.cc
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "swoole_socket.h"
#include "swoole_signal.h"
#include "swoole_reactor.h"
#include "swoole_api.h"
#include "swoole_c_api.h"
namespace swoole {
using network::Socket;
#ifdef SW_USE_MALLOC_TRIM
#ifdef __APPLE__
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#endif
static void reactor_begin(Reactor *reactor);
#ifdef HAVE_EPOLL
ReactorImpl *make_reactor_epoll(Reactor *_reactor, int max_events);
#endif
#ifdef HAVE_POLL
ReactorImpl *make_reactor_poll(Reactor *_reactor, int max_events);
#endif
#ifdef HAVE_KQUEUE
ReactorImpl *make_reactor_kqueue(Reactor *_reactor, int max_events);
#endif
ReactorImpl *make_reactor_select(Reactor *_reactor);
void ReactorImpl::after_removal_failure(Socket *_socket) {
if (!_socket->silent_remove) {
swoole_sys_warning("failed to delete events[fd=%d#%d, type=%d, events=%d]",
_socket->fd,
reactor_->id,
_socket->fd_type,
_socket->events);
}
}
Reactor::Reactor(int max_event, Type _type) {
if (_type == TYPE_AUTO) {
#ifdef HAVE_EPOLL
type_ = TYPE_EPOLL;
#elif defined(HAVE_KQUEUE)
type_ = TYPE_KQUEUE;
#elif defined(HAVE_POLL)
type_ = TYPE_POLL;
#else
type_ = TYPE_SELECT;
#endif
} else {
type_ = _type;
}
switch (type_) {
#ifdef HAVE_EPOLL
case TYPE_EPOLL:
impl = make_reactor_epoll(this, max_event);
break;
#endif
#ifdef HAVE_KQUEUE
case TYPE_KQUEUE:
impl = make_reactor_kqueue(this, max_event);
break;
#endif
#ifdef HAVE_POLL
case TYPE_POLL:
impl = make_reactor_poll(this, max_event);
break;
#endif
case TYPE_SELECT:
default:
impl = make_reactor_select(this);
break;
}
if (!impl->ready()) {
running = false;
return;
}
running = true;
idle_task = {};
future_task = {};
write = _write;
writev = _writev;
close = _close;
default_write_handler = _writable_callback;
if (swoole_isset_hook(SW_GLOBAL_HOOK_ON_REACTOR_CREATE)) {
swoole_call_hook(SW_GLOBAL_HOOK_ON_REACTOR_CREATE, this);
}
set_end_callback(PRIORITY_DEFER_TASK, [](Reactor *reactor) {
CallbackManager *cm = reactor->defer_tasks;
if (cm) {
reactor->defer_tasks = nullptr;
cm->execute();
delete cm;
}
});
set_exit_condition(EXIT_CONDITION_DEFER_TASK,
[](Reactor *reactor, size_t &event_num) -> bool { return reactor->defer_tasks == nullptr; });
set_end_callback(PRIORITY_IDLE_TASK, [](Reactor *reactor) {
if (reactor->idle_task.callback) {
reactor->idle_task.callback(reactor->idle_task.data);
}
});
set_end_callback(PRIORITY_SIGNAL_CALLBACK, [](Reactor *reactor) {
if (sw_unlikely(reactor->singal_no)) {
swoole_signal_callback(reactor->singal_no);
reactor->singal_no = 0;
}
swoole_signal_dispatch();
});
set_end_callback(PRIORITY_TRY_EXIT, [](Reactor *reactor) {
if (reactor->wait_exit && reactor->if_exit()) {
reactor->running = false;
}
});
#ifdef SW_USE_MALLOC_TRIM
set_end_callback(PRIORITY_MALLOC_TRIM, [](Reactor *reactor) {
time_t now = ::time(nullptr);
if (reactor->last_malloc_trim_time < now - SW_MALLOC_TRIM_INTERVAL) {
malloc_trim(SW_MALLOC_TRIM_PAD);
reactor->last_malloc_trim_time = now;
}
});
#endif
set_exit_condition(EXIT_CONDITION_DEFAULT,
[](Reactor *reactor, size_t &event_num) -> bool { return event_num == 0; });
}
bool Reactor::set_handler(int _fdtype, ReactorHandler handler) {
int fdtype = get_fd_type(_fdtype);
if (fdtype >= SW_MAX_FDTYPE) {
swoole_warning("fdtype > SW_MAX_FDTYPE[%d]", SW_MAX_FDTYPE);
return false;
}
if (isset_read_event(_fdtype)) {
read_handler[fdtype] = handler;
} else if (isset_write_event(_fdtype)) {
write_handler[fdtype] = handler;
} else if (isset_error_event(_fdtype)) {
error_handler[fdtype] = handler;
} else {
swoole_warning("unknown fdtype");
return false;
}
return true;
}
bool Reactor::if_exit() {
size_t _event_num = get_event_num();
for (auto &kv : exit_conditions) {
if (kv.second(this, _event_num) == false) {
return false;
}
}
return true;
}
void Reactor::activate_future_task() {
onBegin = reactor_begin;
}
static void reactor_begin(Reactor *reactor) {
if (reactor->future_task.callback) {
reactor->future_task.callback(reactor->future_task.data);
}
}
int Reactor::_close(Reactor *reactor, Socket *socket) {
swoole_trace_log(SW_TRACE_CLOSE, "fd=%d", socket->fd);
socket->free();
return SW_OK;
}
using SendFunc = std::function<ssize_t(void)>;
using AppendFunc = std::function<void(Buffer *buffer)>;
static ssize_t write_func(
Reactor *reactor, Socket *socket, const size_t __len, const SendFunc &send_fn, const AppendFunc &append_fn) {
ssize_t retval;
Buffer *buffer = socket->out_buffer;
int fd = socket->fd;
if (socket->buffer_size == 0) {
socket->set_memory_buffer_size(Socket::default_buffer_size);
}
if (socket->nonblock == 0) {
socket->set_fd_option(1, -1);
}
if ((uint32_t) __len > socket->buffer_size) {
swoole_error_log(SW_LOG_WARNING,
SW_ERROR_PACKAGE_LENGTH_TOO_LARGE,
"data packet is too large, cannot exceed the buffer size");
return SW_ERR;
}
if (Buffer::empty(buffer)) {
#ifdef SW_USE_OPENSSL
if (socket->ssl_send_) {
goto _alloc_buffer;
}
#endif
_do_send:
retval = send_fn();
if (retval > 0) {
if ((ssize_t) __len == retval) {
return retval;
} else {
goto _alloc_buffer;
}
} else if (socket->catch_write_error(errno) == SW_WAIT) {
_alloc_buffer:
if (!socket->out_buffer) {
buffer = new Buffer(socket->chunk_size);
if (!buffer) {
swoole_warning("create worker buffer failed");
return SW_ERR;
}
socket->out_buffer = buffer;
}
if (!socket->isset_writable_event()) {
reactor->add_write_event(socket);
}
goto _append_buffer;
} else if (errno == EINTR) {
goto _do_send;
} else {
swoole_set_last_error(errno);
return SW_ERR;
}
} else {
_append_buffer:
if (buffer->length() > socket->buffer_size) {
if (socket->dontwait) {
swoole_set_last_error(SW_ERROR_OUTPUT_BUFFER_OVERFLOW);
return SW_ERR;
} else {
swoole_error_log(
SW_LOG_WARNING, SW_ERROR_OUTPUT_BUFFER_OVERFLOW, "socket#%d output buffer overflow", fd);
sw_yield();
socket->wait_event(SW_SOCKET_OVERFLOW_WAIT, SW_EVENT_WRITE);
}
}
append_fn(buffer);
}
return __len;
}
ssize_t Reactor::_write(Reactor *reactor, Socket *socket, const void *buf, size_t n) {
ssize_t send_bytes = 0;
auto send_fn = [&send_bytes, socket, buf, n]() -> ssize_t {
send_bytes = socket->send(buf, n, 0);
return send_bytes;
};
auto append_fn = [&send_bytes, buf, n](Buffer *buffer) {
ssize_t offset = send_bytes > 0 ? send_bytes : 0;
buffer->append((const char *) buf + offset, n - offset);
};
return write_func(reactor, socket, n, send_fn, append_fn);
}
ssize_t Reactor::_writev(Reactor *reactor, Socket *socket, const iovec *iov, size_t iovcnt) {
#ifdef SW_USE_OPENSSL
if (socket->ssl) {
swoole_error_log(SW_LOG_WARNING, SW_ERROR_OPERATION_NOT_SUPPORT, "does not support SSL");
return SW_ERR;
}
#endif
ssize_t send_bytes = 0;
size_t n = 0;
SW_LOOP_N(iovcnt) {
n += iov[i].iov_len;
}
auto send_fn = [&send_bytes, socket, iov, iovcnt]() -> ssize_t {
send_bytes = socket->writev(iov, iovcnt);
return send_bytes;
};
auto append_fn = [&send_bytes, iov, iovcnt](Buffer *buffer) {
ssize_t offset = send_bytes > 0 ? send_bytes : 0;
buffer->append(iov, iovcnt, offset);
};
return write_func(reactor, socket, n, send_fn, append_fn);
}
int Reactor::_writable_callback(Reactor *reactor, Event *ev) {
int ret;
Socket *socket = ev->socket;
Buffer *buffer = socket->out_buffer;
while (!Buffer::empty(buffer)) {
BufferChunk *chunk = buffer->front();
if (chunk->type == BufferChunk::TYPE_CLOSE) {
return reactor->close(reactor, ev->socket);
} else if (chunk->type == BufferChunk::TYPE_SENDFILE) {
ret = socket->handle_sendfile();
} else {
ret = socket->handle_send();
}
if (ret < 0) {
if (socket->close_wait) {
return reactor->trigger_close_event(ev);
} else if (socket->send_wait) {
return SW_OK;
}
}
}
if (socket->send_timer) {
swoole_timer_del(socket->send_timer);
socket->send_timer = nullptr;
}
// remove EPOLLOUT event
if (Buffer::empty(buffer)) {
reactor->remove_write_event(ev->socket);
}
return SW_OK;
}
void Reactor::drain_write_buffer(Socket *socket) {
Event event = {};
event.socket = socket;
event.fd = socket->fd;
while (!Buffer::empty(socket->out_buffer)) {
if (socket->wait_event(Socket::default_write_timeout, SW_EVENT_WRITE) == SW_ERR) {
break;
}
_writable_callback(this, &event);
if (socket->close_wait || socket->removed) {
break;
}
}
}
void Reactor::add_destroy_callback(Callback cb, void *data) {
destroy_callbacks.append(cb, data);
}
void Reactor::set_end_callback(enum EndCallback id, const std::function<void(Reactor *)> &fn) {
end_callbacks[id] = fn;
}
void Reactor::erase_end_callback(enum EndCallback id) {
end_callbacks.erase(id);
}
/**
* Returns false, the reactor cannot be exited, the next condition is skipped
* Returns true, the reactor can exit and will continue to execute the next conditional function
*/
void Reactor::set_exit_condition(enum ExitCondition id, const std::function<bool(Reactor *, size_t &)> &fn) {
exit_conditions[id] = fn;
}
void Reactor::defer(Callback cb, void *data) {
if (defer_tasks == nullptr) {
defer_tasks = new CallbackManager;
}
defer_tasks->append(cb, data);
}
void Reactor::execute_end_callbacks(bool timedout) {
for (auto &kv : end_callbacks) {
kv.second(this);
}
}
Reactor::~Reactor() {
destroyed = true;
destroy_callbacks.execute();
delete impl;
if (swoole_isset_hook(SW_GLOBAL_HOOK_ON_REACTOR_DESTROY)) {
swoole_call_hook(SW_GLOBAL_HOOK_ON_REACTOR_DESTROY, this);
}
}
} // namespace swoole