-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathswoole_curl.cc
491 lines (434 loc) Β· 15.8 KB
/
swoole_curl.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
+----------------------------------------------------------------------+
| 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 "php_swoole_curl.h"
#include "swoole_socket.h"
#ifdef SW_USE_CURL
namespace swoole {
namespace curl {
static std::unordered_map<CURL *, Handle *> handle_buckets;
Handle *get_handle(CURL *cp) {
auto iter = handle_buckets.find(cp);
return iter == handle_buckets.end() ? nullptr : iter->second;
}
Handle *create_handle(CURL *cp) {
auto iter = handle_buckets.find(cp);
if (iter != handle_buckets.end()) {
return nullptr;
}
Handle *handle = new Handle(cp);
handle_buckets[cp] = handle;
swoole_trace_log(SW_TRACE_CO_CURL, SW_ECHO_MAGENTA " handle=%p, curl=%p", "[CREATE]", handle, cp);
return handle;
}
void destroy_handle(CURL *cp) {
auto iter = handle_buckets.find(cp);
if (iter == handle_buckets.end()) {
return;
}
auto handle = iter->second;
handle_buckets.erase(iter);
delete handle;
swoole_trace_log(SW_TRACE_CO_CURL, SW_ECHO_RED " handle=%p, curl=%p", "[DESTROY]", handle, cp);
}
static int execute_callback(Event *event, int bitmask) {
Handle *handle = (Handle *) event->socket->object;
auto it = handle->sockets.find(event->fd);
if (it != handle->sockets.end()) {
it->second->event_bitmask |= bitmask;
it->second->event_fd = event->fd;
}
handle->multi->callback(handle, bitmask, event->fd);
return 0;
}
void Handle::destroy_socket(curl_socket_t sockfd) {
auto it = sockets.find(sockfd);
if (it != sockets.end()) {
auto _socket = it->second;
sockets.erase(it);
_socket->socket->fd = -1;
_socket->socket->free();
delete _socket;
}
}
HandleSocket *Handle::create_socket(curl_socket_t sockfd) {
auto socket = new network::Socket();
socket->fd = sockfd;
socket->removed = 1;
socket->fd_type = (FdType) PHP_SWOOLE_FD_CO_CURL;
HandleSocket *handle_socket = new HandleSocket();
handle_socket->socket = socket;
sockets[sockfd] = handle_socket;
socket->object = this;
return handle_socket;
}
int Multi::cb_readable(Reactor *reactor, Event *event) {
return execute_callback(event, CURL_CSELECT_IN);
}
int Multi::cb_writable(Reactor *reactor, Event *event) {
return execute_callback(event, CURL_CSELECT_OUT);
}
int Multi::cb_error(Reactor *reactor, Event *event) {
return execute_callback(event, CURL_CSELECT_ERR);
}
int Multi::handle_socket(CURL *easy, curl_socket_t sockfd, int action, void *userp, void *socketp) {
Multi *multi = (Multi *) userp;
swoole_trace_log(
SW_TRACE_CO_CURL, SW_ECHO_CYAN "action=%d, userp=%p, socketp=%p", "[HANDLE_SOCKET]", action, userp, socketp);
switch (action) {
case CURL_POLL_IN:
case CURL_POLL_OUT:
case CURL_POLL_INOUT:
multi->set_event(easy, socketp, sockfd, action);
break;
case CURL_POLL_REMOVE:
if (socketp) {
multi->del_event(easy, socketp, sockfd);
}
break;
default:
abort();
}
return 0;
}
HandleSocket *Multi::create_socket(Handle *handle, curl_socket_t sockfd) {
if (!swoole_event_isset_handler(PHP_SWOOLE_FD_CO_CURL)) {
swoole_event_set_handler(PHP_SWOOLE_FD_CO_CURL | SW_EVENT_READ, cb_readable);
swoole_event_set_handler(PHP_SWOOLE_FD_CO_CURL | SW_EVENT_WRITE, cb_writable);
swoole_event_set_handler(PHP_SWOOLE_FD_CO_CURL | SW_EVENT_ERROR, cb_error);
}
auto _socket = handle->create_socket(sockfd);
if (curl_multi_assign(multi_handle_, sockfd, (void *) _socket) != CURLM_OK) {
handle->destroy_socket(sockfd);
return nullptr;
}
return _socket;
}
void Multi::del_event(CURL *cp, void *socket_ptr, curl_socket_t sockfd) {
HandleSocket *curl_socket = (HandleSocket *) socket_ptr;
curl_socket->socket->silent_remove = 1;
if (curl_socket->socket->events && swoole_event_is_available() && swoole_event_del(curl_socket->socket) == SW_OK) {
event_count_--;
}
curl_multi_assign(multi_handle_, sockfd, NULL);
Handle *handle = get_handle(cp);
if (handle) {
handle->destroy_socket(sockfd);
}
swoole_trace_log(SW_TRACE_CO_CURL, SW_ECHO_RED " handle=%p, curl=%p, fd=%d", "[DEL_EVENT]", handle, cp, sockfd);
}
void Multi::set_event(CURL *cp, void *socket_ptr, curl_socket_t sockfd, int action) {
auto handle = get_handle(cp);
if (!handle) {
return;
}
HandleSocket *curl_socket = socket_ptr ? (HandleSocket *) socket_ptr : create_socket(handle, sockfd);
int events = 0;
if (action != CURL_POLL_IN) {
events |= SW_EVENT_WRITE;
}
if (action != CURL_POLL_OUT) {
events |= SW_EVENT_READ;
}
assert(curl_socket->socket->fd > 0);
curl_socket->socket->fd = sockfd;
if (curl_socket->socket->events) {
swoole_event_set(curl_socket->socket, events);
} else {
if (swoole_event_add(curl_socket->socket, events) == SW_OK) {
event_count_++;
}
}
auto it = handle->sockets.find(sockfd);
if (it != handle->sockets.end()) {
it->second->action = action;
}
swoole_trace_log(SW_TRACE_CO_CURL,
SW_ECHO_GREEN " handle=%p, curl=%p, fd=%d, events=%d",
"[ADD_EVENT]",
handle,
cp,
sockfd,
events);
}
CURLMcode Multi::add_handle(Handle *handle) {
if (handle == nullptr) {
php_swoole_fatal_error(E_WARNING, "The given handle is not initialized in coroutine");
return CURLM_INTERNAL_ERROR;
}
auto retval = curl_multi_add_handle(multi_handle_, handle->cp);
if (retval == CURLM_OK) {
handle->multi = this;
swoole_trace_log(SW_TRACE_CO_CURL, SW_ECHO_GREEN " handle=%p, curl=%p", "[ADD_HANDLE]", handle, handle->cp);
}
return retval;
}
CURLMcode Multi::remove_handle(Handle *handle) {
handle->multi = nullptr;
swoole_trace_log(SW_TRACE_CO_CURL, SW_ECHO_RED " handle=%p, curl=%p", "[REMOVE_HANDLE]", handle, handle->cp);
return curl_multi_remove_handle(multi_handle_, handle->cp);
}
CURLcode Multi::exec(Handle *handle) {
if (add_handle(handle) != CURLM_OK) {
return CURLE_FAILED_INIT;
}
HandleSocket *curl_socket = nullptr;
bool is_canceled = false;
SW_LOOP {
for (auto it : handle->sockets) {
curl_socket = it.second;
if (curl_socket->socket && curl_socket->socket->removed) {
if (swoole_event_add(curl_socket->socket, get_event(curl_socket->action)) == SW_OK) {
event_count_++;
}
swoole_trace_log(SW_TRACE_CO_CURL,
"resume, handle=%p, curl=%p, fd=%d",
handle,
handle->cp,
curl_socket->socket->get_fd());
}
}
co = check_bound_co();
co->yield_ex(-1);
is_canceled = co->is_canceled();
co = nullptr;
if (is_canceled) {
swoole_set_last_error(SW_ERROR_CO_CANCELED);
break;
}
int sockfd = last_sockfd;
int bitmask = 0;
if (sockfd >= 0) {
auto it = handle->sockets.find(sockfd);
if (it != handle->sockets.end()) {
curl_socket = it->second;
bitmask = curl_socket->event_bitmask;
if (!curl_socket->socket->removed && swoole_event_del(curl_socket->socket) == SW_OK) {
event_count_--;
}
}
}
del_timer();
curl_multi_socket_action(multi_handle_, sockfd, bitmask, &running_handles_);
swoole_trace_log(SW_TRACE_CO_CURL,
"curl_multi_socket_action: handle=%p, sockfd=%d, bitmask=%d, running_handles_=%d",
handle,
sockfd,
bitmask,
running_handles_);
if (running_handles_ == 0) {
break;
}
set_timer();
if (sockfd >= 0) {
auto it = handle->sockets.find(sockfd);
if (it != handle->sockets.end()) {
curl_socket = it->second;
if (curl_socket->socket && curl_socket->socket->removed) {
if (swoole_event_add(curl_socket->socket, get_event(curl_socket->action)) == SW_OK) {
event_count_++;
}
}
}
}
if (!timer) {
bool removed = true;
for (auto it = handle->sockets.begin(); it != handle->sockets.end();) {
curl_socket = it->second;
if (curl_socket->socket) {
if (curl_socket->socket->removed) {
it = handle->sockets.erase(it);
delete curl_socket;
continue;
} else {
removed = false;
}
}
++it;
}
if (removed) {
break;
}
}
}
CURLcode retval = read_info();
remove_handle(handle);
return is_canceled ? CURLE_ABORTED_BY_CALLBACK : retval;
}
CURLcode Multi::read_info() {
CURLMsg *message;
int pending;
while ((message = curl_multi_info_read(multi_handle_, &pending))) {
switch (message->msg) {
case CURLMSG_DONE:
/* Do not use message data after calling curl_multi_remove_handle() and
curl_easy_cleanup(). As per curl_multi_info_read() docs:
"WARNING: The data the returned pointer points to will not survive
calling curl_multi_cleanup, curl_multi_remove_handle or
curl_easy_cleanup." */
return message->data.result;
default:
swoole_warning("CURLMSG default");
break;
}
}
return CURLE_OK;
}
int Multi::handle_timeout(CURLM *mh, long timeout_ms, void *userp) {
Multi *multi = (Multi *) userp;
swoole_trace_log(SW_TRACE_CO_CURL, SW_ECHO_BLUE " timeout_ms=%ld", "[HANDLE_TIMEOUT]", timeout_ms);
if (!swoole_event_is_available()) {
return -1;
}
if (timeout_ms < 0) {
if (multi->timer) {
multi->del_timer();
} else {
multi->add_timer(1000);
}
} else {
if (timeout_ms == 0) {
timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in a bit */
}
multi->add_timer(timeout_ms);
}
return 0;
}
long Multi::select(php_curlm *mh, double timeout) {
if (zend_llist_count(&mh->easyh) == 0) {
return 0;
}
if (curl_multi_socket_all(multi_handle_, &running_handles_) != CURLM_OK) {
return CURLE_FAILED_INIT;
}
network::Socket *socket = nullptr;
for (zend_llist_element *element = mh->easyh.head; element; element = element->next) {
zval *z_ch = (zval *) element->data;
php_curl *ch;
if ((ch = swoole_curl_get_handle(z_ch, false)) == NULL) {
continue;
}
Handle *handle = get_handle(ch->cp);
if (handle) {
for (auto it : handle->sockets) {
socket = it.second->socket;
swoole_trace_log(SW_TRACE_CO_CURL,
"handle=%p, socket=%p, socket->removed=%d",
handle,
socket,
socket ? socket->removed : 0);
if (socket && socket->removed) {
if (swoole_event_add(socket, get_event(it.second->action)) == SW_OK) {
event_count_++;
}
swoole_trace_log(
SW_TRACE_CO_CURL, "resume, handle=%p, curl=%p, fd=%d", handle, ch->cp, socket->get_fd());
}
}
}
}
set_timer();
// no events and timers, should not be suspended
if (!timer && event_count_ == 0) {
return 0;
}
co = check_bound_co();
co->yield_ex(timeout);
co = nullptr;
swoole_trace_log(SW_TRACE_CO_CURL, "yield timeout, count=%lu", zend_llist_count(&mh->easyh));
auto count = selector->active_handles.size();
for (zend_llist_element *element = mh->easyh.head; element; element = element->next) {
zval *z_ch = (zval *) element->data;
php_curl *ch;
if ((ch = swoole_curl_get_handle(z_ch, false)) == NULL) {
continue;
}
Handle *handle = get_handle(ch->cp);
if (handle) {
for (auto it : handle->sockets) {
socket = it.second->socket;
if (socket && !socket->removed && swoole_event_del(socket) == SW_OK) {
swoole_trace_log(
SW_TRACE_CO_CURL, "suspend, handle=%p, curl=%p, fd=%d", handle, ch->cp, socket->get_fd());
event_count_--;
}
}
}
}
del_timer();
if (selector->timer_callback) {
selector->timer_callback = false;
curl_multi_socket_action(multi_handle_, CURL_SOCKET_TIMEOUT, 0, &running_handles_);
swoole_trace_log(SW_TRACE_CO_CURL, "socket_action[timer], running_handles=%d", running_handles_);
}
for (auto iter = selector->active_handles.begin(); iter != selector->active_handles.end(); iter++) {
Handle *handle = *iter;
if (handle) {
for (auto it = handle->sockets.begin(); it != handle->sockets.end();) {
HandleSocket *handle_socket = it->second;
it++;
curl_multi_socket_action(
multi_handle_, handle_socket->event_fd, handle_socket->event_bitmask, &running_handles_);
swoole_trace_log(SW_TRACE_CO_CURL, "socket_action[socket], running_handles=%d", running_handles_);
}
}
}
selector->active_handles.clear();
return count;
}
void Multi::callback(Handle *handle, int event_bitmask, int sockfd) {
swoole_trace_log(
SW_TRACE_CO_CURL, "handle=%p, event_bitmask=%d, co=%p, sockfd=%d", handle, event_bitmask, co, sockfd);
if (handle) {
last_sockfd = sockfd;
} else {
last_sockfd = -1;
}
if (selector.get()) {
if (!handle) {
selector->timer_callback = true;
}
}
if (!co) {
if (handle) {
for (auto it : handle->sockets) {
if (swoole_event_del(it.second->socket) == SW_OK) {
event_count_--;
}
}
} else {
del_timer();
}
return;
}
if (selector.get() && handle) {
selector->active_handles.insert(handle);
}
if (defer_callback) {
return;
}
defer_callback = true;
swoole_event_defer(
[this](void *data) {
defer_callback = false;
if (co) {
co->resume();
}
},
nullptr);
}
} // namespace curl
} // namespace swoole
#endif