- remove LFS dependant parts which caused some real weirdance with curl_share
[m6w6/ext-http] / http_request_pool_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #define HTTP_WANT_EVENT
17 #include "php_http.h"
18
19 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20
21 #include "php_http_api.h"
22 #include "php_http_exception_object.h"
23 #include "php_http_persistent_handle_api.h"
24 #include "php_http_request_api.h"
25 #include "php_http_request_object.h"
26 #include "php_http_request_pool_api.h"
27 #include "php_http_requestpool_object.h"
28
29 #ifndef HTTP_DEBUG_REQPOOLS
30 # define HTTP_DEBUG_REQPOOLS 0
31 #endif
32
33 #ifdef HTTP_HAVE_EVENT
34 typedef struct _http_request_pool_event_t {
35 struct event evnt;
36 http_request_pool *pool;
37 } http_request_pool_event;
38
39 static void http_request_pool_timeout_callback(int socket, short action, void *event_data);
40 static void http_request_pool_event_callback(int socket, short action, void *event_data);
41 static int http_request_pool_socket_callback(CURL *easy, curl_socket_t s, int action, void *, void *);
42 static void http_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data);
43 #endif
44
45 static int http_request_pool_compare_handles(void *h1, void *h2);
46
47 PHP_MINIT_FUNCTION(http_request_pool)
48 {
49 if (SUCCESS != http_persistent_handle_provide("http_request_pool", curl_multi_init, (http_persistent_handle_dtor) curl_multi_cleanup, NULL)) {
50 return FAILURE;
51 }
52 return SUCCESS;
53 }
54
55 #ifdef HTTP_HAVE_EVENT
56 PHP_RINIT_FUNCTION(http_request_pool)
57 {
58 if (!HTTP_G->request.pool.event.base && !(HTTP_G->request.pool.event.base = event_init())) {
59 return FAILURE;
60 }
61
62 return SUCCESS;
63 }
64 #endif
65
66 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
67 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
68 {
69 zend_bool free_pool;
70
71 #if HTTP_DEBUG_REQPOOLS
72 fprintf(stderr, "Initializing request pool %p\n", pool);
73 #endif
74
75 if ((free_pool = (!pool))) {
76 pool = emalloc(sizeof(http_request_pool));
77 pool->ch = NULL;
78 }
79
80 if (SUCCESS != http_persistent_handle_acquire("http_request_pool", &pool->ch)) {
81 if (free_pool) {
82 efree(pool);
83 }
84 return NULL;
85 }
86
87 TSRMLS_SET_CTX(pool->tsrm_ls);
88
89 #if HTTP_HAVE_EVENT
90 pool->timeout = ecalloc(1, sizeof(struct event));
91 curl_multi_setopt(pool->ch, CURLMOPT_SOCKETDATA, pool);
92 curl_multi_setopt(pool->ch, CURLMOPT_SOCKETFUNCTION, http_request_pool_socket_callback);
93 curl_multi_setopt(pool->ch, CURLMOPT_TIMERDATA, pool);
94 curl_multi_setopt(pool->ch, CURLMOPT_TIMERFUNCTION, http_request_pool_timer_callback);
95 #endif
96
97 pool->unfinished = 0;
98 zend_llist_init(&pool->finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
99 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
100
101 #if HTTP_DEBUG_REQPOOLS
102 fprintf(stderr, "Initialized request pool %p\n", pool);
103 #endif
104
105 return pool;
106 }
107 /* }}} */
108
109 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
110 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request)
111 {
112 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
113 getObjectEx(http_request_object, req, request);
114
115 #if HTTP_DEBUG_REQPOOLS
116 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
117 #endif
118
119 if (req->pool) {
120 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is already member of %s HttpRequestPool", Z_OBJ_HANDLE_P(request), req->pool == pool ? "this" : "another");
121 } else if (SUCCESS != http_request_object_requesthandler(req, request)) {
122 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object(#%d) for attaching to the HttpRequestPool", Z_OBJ_HANDLE_P(request));
123 } else {
124 CURLMcode code = curl_multi_add_handle(pool->ch, req->request->ch);
125
126 if (CURLM_OK != code) {
127 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object(#%d) to the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), curl_multi_strerror(code));
128 } else {
129 req->pool = pool;
130
131 ZVAL_ADDREF(request);
132 zend_llist_add_element(&pool->handles, &request);
133 ++pool->unfinished;
134
135 #ifdef HTTP_HAVE_EVENT
136 if (pool->runsocket) {
137 while (CURLM_CALL_MULTI_PERFORM == curl_multi_socket_all(pool->ch, &pool->unfinished));
138 }
139 #endif
140 #if HTTP_DEBUG_REQPOOLS
141 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
142 #endif
143 return SUCCESS;
144 }
145 }
146 return FAILURE;
147 }
148 /* }}} */
149
150 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
151 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request)
152 {
153 CURLMcode code;
154 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
155 getObjectEx(http_request_object, req, request);
156
157 #if HTTP_DEBUG_REQPOOLS
158 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
159 #endif
160
161 if (!req->pool) {
162 /* not attached to any pool */
163 #if HTTP_DEBUG_REQPOOLS
164 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
165 #endif
166 } else if (req->pool != pool) {
167 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
168 } else if (req->request->_in_progress_cb) {
169 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "HttpRequest object(#%d) cannot be detached from the HttpRequestPool while executing the progress callback", Z_OBJ_HANDLE_P(request));
170 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
171 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object(#%d) from the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), curl_multi_strerror(code));
172 } else {
173 req->pool = NULL;
174 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
175 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
176
177 #if HTTP_DEBUG_REQPOOLS
178 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
179 #endif
180
181 return SUCCESS;
182 }
183 return FAILURE;
184 }
185 /* }}} */
186
187 /* {{{ void http_request_pool_apply(http_request_pool *, http_request_pool_apply_func) */
188 PHP_HTTP_API void _http_request_pool_apply(http_request_pool *pool, http_request_pool_apply_func cb)
189 {
190 int count = zend_llist_count(&pool->handles);
191
192 if (count) {
193 int i = 0;
194 zend_llist_position pos;
195 zval **handle, **handles = emalloc(count * sizeof(zval *));
196
197 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
198 handles[i++] = *handle;
199 }
200
201 /* should never happen */
202 if (i != count) {
203 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
204 count = i;
205 }
206
207 for (i = 0; i < count; ++i) {
208 if (cb(pool, handles[i])) {
209 break;
210 }
211 }
212 efree(handles);
213 }
214 }
215 /* }}} */
216
217 /* {{{ void http_request_pool_apply_with_arg(http_request_pool *, http_request_pool_apply_with_arg_func, void *) */
218 PHP_HTTP_API void _http_request_pool_apply_with_arg(http_request_pool *pool, http_request_pool_apply_with_arg_func cb, void *arg)
219 {
220 int count = zend_llist_count(&pool->handles);
221
222 if (count) {
223 int i = 0;
224 zend_llist_position pos;
225 zval **handle, **handles = emalloc(count * sizeof(zval *));
226
227 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
228 handles[i++] = *handle;
229 }
230
231 /* should never happen */
232 if (i != count) {
233 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
234 count = i;
235 }
236
237 for (i = 0; i < count; ++i) {
238 if (cb(pool, handles[i], arg)) {
239 break;
240 }
241 }
242 efree(handles);
243 }
244 }
245 /* }}} */
246
247 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
248 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool)
249 {
250 #if HTTP_DEBUG_REQPOOLS
251 fprintf(stderr, "Detaching %d requests from pool %p\n", zend_llist_count(&pool->handles), pool);
252 #endif
253 http_request_pool_apply(pool, _http_request_pool_detach);
254 }
255 /* }}} */
256
257 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
258 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool)
259 {
260 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
261
262 #if HTTP_DEBUG_REQPOOLS
263 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
264 #endif
265
266 #ifdef HTTP_HAVE_EVENT
267 if (pool->useevents) {
268 /* run socket action */
269 pool->runsocket = 1;
270 do {
271 while (CURLM_CALL_MULTI_PERFORM == curl_multi_socket_all(pool->ch, &pool->unfinished));
272 event_base_dispatch(HTTP_G->request.pool.event.base);
273 } while (pool->unfinished);
274 } else
275 #endif
276 {
277 while (http_request_pool_perform(pool)) {
278 if (SUCCESS != http_request_pool_select(pool)) {
279 #ifdef PHP_WIN32
280 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
281 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
282 #else
283 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
284 #endif
285 return FAILURE;
286 }
287 }
288 }
289
290 #if HTTP_DEBUG_REQPOOLS
291 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
292 #endif
293
294 return SUCCESS;
295 }
296 /* }}} */
297
298 /* {{{ void http_request_pool_dtor(http_request_pool *) */
299 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool)
300 {
301 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
302
303 #if HTTP_DEBUG_REQPOOLS
304 fprintf(stderr, "Destructing request pool %p\n", pool);
305 #endif
306
307 #if HTTP_HAVE_EVENT
308 efree(pool->timeout);
309 #endif
310
311 pool->unfinished = 0;
312 zend_llist_clean(&pool->finished);
313 zend_llist_clean(&pool->handles);
314 http_persistent_handle_release("http_request_pool", &pool->ch);
315 }
316 /* }}} */
317
318 #ifdef PHP_WIN32
319 # define SELECT_ERROR SOCKET_ERROR
320 #else
321 # define SELECT_ERROR -1
322 #endif
323
324 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
325 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
326 {
327 int MAX;
328 fd_set R, W, E;
329 struct timeval timeout;
330
331 #ifdef HTTP_HAVE_EVENT
332 if (pool->useevents) {
333 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
334 http_error(HE_WARNING, HTTP_E_RUNTIME, "not implemented; use HttpRequest callbacks");
335 return FAILURE;
336 }
337 #endif
338
339 http_request_pool_timeout(pool, &timeout);
340
341 FD_ZERO(&R);
342 FD_ZERO(&W);
343 FD_ZERO(&E);
344
345 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
346 if (MAX == -1) {
347 http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / HTTP_MCROSEC));
348 return SUCCESS;
349 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
350 return SUCCESS;
351 }
352 }
353 return FAILURE;
354 }
355 /* }}} */
356
357 /* {{{ int http_request_pool_perform(http_request_pool *) */
358 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
359 {
360 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
361
362 #ifdef HTTP_HAVE_EVENT
363 if (pool->useevents) {
364 http_error(HE_WARNING, HTTP_E_RUNTIME, "not implemented; use HttpRequest callbacks");
365 return FAILURE;
366 }
367 #endif
368
369 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
370
371 #if HTTP_DEBUG_REQPOOLS
372 fprintf(stderr, "%u unfinished requests of pool %p remaining\n", pool->unfinished, pool);
373 #endif
374
375 http_request_pool_responsehandler(pool);
376
377 return pool->unfinished;
378 }
379 /* }}} */
380
381 /* {{{ void http_request_pool_responsehandler(http_request_pool *) */
382 void _http_request_pool_responsehandler(http_request_pool *pool)
383 {
384 CURLMsg *msg;
385 int remaining = 0;
386 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
387
388 do {
389 msg = curl_multi_info_read(pool->ch, &remaining);
390 if (msg && CURLMSG_DONE == msg->msg) {
391 if (CURLE_OK != msg->data.result) {
392 http_request *r = NULL;
393 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
394 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
395 }
396 http_request_pool_apply_with_arg(pool, _http_request_pool_apply_responsehandler, msg->easy_handle);
397 }
398 } while (remaining);
399 }
400 /* }}} */
401
402 /* {{{ int http_request_pool_apply_responsehandler(http_request_pool *, zval *, void *) */
403 int _http_request_pool_apply_responsehandler(http_request_pool *pool, zval *req, void *ch)
404 {
405 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
406 getObjectEx(http_request_object, obj, req);
407
408 if ((!ch) || obj->request->ch == (CURL *) ch) {
409
410 #if HTTP_DEBUG_REQPOOLS
411 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req), obj, obj->pool);
412 #endif
413
414 ZVAL_ADDREF(req);
415 zend_llist_add_element(&obj->pool->finished, &req);
416 http_request_object_responsehandler(obj, req);
417 return 1;
418 }
419 return 0;
420 }
421 /* }}} */
422
423 /* {{{ struct timeval *_http_request_pool_timeout(http_request_pool *, struct timeval *) */
424 struct timeval *_http_request_pool_timeout(http_request_pool *pool, struct timeval *timeout)
425 {
426 #ifdef HAVE_CURL_MULTI_TIMEOUT
427 long max_tout = 1000;
428
429 if ((CURLM_OK == curl_multi_timeout(pool->ch, &max_tout)) && (max_tout != -1)) {
430 timeout->tv_sec = max_tout / 1000;
431 timeout->tv_usec = (max_tout % 1000) * 1000;
432 } else {
433 #endif
434 timeout->tv_sec = 1;
435 timeout->tv_usec = 0;
436 #ifdef HAVE_CURL_MULTI_TIMEOUT
437 }
438 #endif
439
440 #if HTTP_DEBUG_REQPOOLS
441 fprintf(stderr, "Calculating timeout (%lu, %lu) of pool %p\n", (ulong) timeout->tv_sec, (ulong) timeout->tv_usec, pool);
442 #endif
443
444 return timeout;
445 }
446 /* }}} */
447
448 /*#*/
449
450 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
451 static int http_request_pool_compare_handles(void *h1, void *h2)
452 {
453 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
454 }
455 /* }}} */
456
457 #ifdef HTTP_HAVE_EVENT
458 /* {{{ static void http_request_pool_timeout_callback(int, short, void *) */
459 static void http_request_pool_timeout_callback(int socket, short action, void *event_data)
460 {
461 http_request_pool *pool = event_data;
462
463 if (pool->useevents) {
464 CURLMcode rc;
465 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
466
467 #if HTTP_DEBUG_REQPOOLS
468 fprintf(stderr, "Timeout occurred of pool %p\n", pool);
469 #endif
470
471 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket(pool->ch, CURL_SOCKET_TIMEOUT, &pool->unfinished)));
472
473 if (CURLM_OK != rc) {
474 http_error(HE_WARNING, HTTP_E_SOCKET, curl_multi_strerror(rc));
475 }
476
477 http_request_pool_timer_callback(pool->ch, 1000, pool);
478 }
479 }
480 /* }}} */
481
482 /* {{{ static void http_request_pool_event_callback(int, short, void *) */
483 static void http_request_pool_event_callback(int socket, short action, void *event_data)
484 {
485 http_request_pool_event *ev = event_data;
486 http_request_pool *pool = ev->pool;
487
488 if (pool->useevents) {
489 CURLMcode rc = CURLE_OK;
490 TSRMLS_FETCH_FROM_CTX(ev->pool->tsrm_ls);
491
492 #if HTTP_DEBUG_REQPOOLS
493 {
494 static const char event_strings[][20] = {"NONE","TIMEOUT","READ","TIMEOUT|READ","WRITE","TIMEOUT|WRITE","READ|WRITE","TIMEOUT|READ|WRITE","SIGNAL"};
495 fprintf(stderr, "Event on socket %d (%s) event %p of pool %p\n", socket, event_strings[action], ev, pool);
496 }
497 #endif
498
499 /* don't use 'ev' below this loop as it might 've been freed in the socket callback */
500 do {
501 #ifdef HAVE_CURL_MULTI_SOCKET_ACTION
502 switch (action & (EV_READ|EV_WRITE)) {
503 case EV_READ:
504 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_IN, &pool->unfinished);
505 break;
506 case EV_WRITE:
507 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_OUT, &pool->unfinished);
508 break;
509 case EV_READ|EV_WRITE:
510 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_IN|CURL_CSELECT_OUT, &pool->unfinished);
511 break;
512 default:
513 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "Unknown event %d", (int) action);
514 return;
515 }
516 #else
517 rc = curl_multi_socket(pool->ch, socket, &pool->unfinished);
518 #endif
519 } while (CURLM_CALL_MULTI_PERFORM == rc);
520
521 if (CURLM_OK != rc) {
522 http_error(HE_WARNING, HTTP_E_SOCKET, curl_multi_strerror(rc));
523 }
524
525 http_request_pool_responsehandler(pool);
526
527 /* remove timeout if there are no transfers left */
528 if (!pool->unfinished && event_initialized(pool->timeout) && event_pending(pool->timeout, EV_TIMEOUT, NULL)) {
529 event_del(pool->timeout);
530 #if HTTP_DEBUG_REQPOOLS
531 fprintf(stderr, "Removed timeout of pool %p\n", pool);
532 #endif
533 }
534 }
535 }
536 /* }}} */
537
538 /* {{{ static int http_request_pool_socket_callback(CURL *, curl_socket_t, int, void *, void *) */
539 static int http_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
540 {
541 http_request_pool *pool = socket_data;
542
543 if (pool->useevents) {
544 int events = EV_PERSIST;
545 http_request_pool_event *ev = assign_data;
546 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
547
548 if (!ev) {
549 ev = ecalloc(1, sizeof(http_request_pool_event));
550 ev->pool = pool;
551 curl_multi_assign(pool->ch, sock, ev);
552 } else {
553 event_del(&ev->evnt);
554 }
555
556 #if HTTP_DEBUG_REQPOOLS
557 {
558 static const char action_strings[][8] = {"NONE", "IN", "OUT", "INOUT", "REMOVE"};
559 http_request *r;
560 curl_easy_getinfo(easy, CURLINFO_PRIVATE, &r);
561 fprintf(stderr, "Callback on socket %d (%s) event %p of pool %p (%s)\n", (int) sock, action_strings[action], ev, pool, r->url);
562 }
563 #endif
564
565 switch (action) {
566 case CURL_POLL_IN:
567 events |= EV_READ;
568 break;
569 case CURL_POLL_OUT:
570 events |= EV_WRITE;
571 break;
572 case CURL_POLL_INOUT:
573 events |= EV_READ|EV_WRITE;
574 break;
575
576 case CURL_POLL_REMOVE:
577 efree(ev);
578 case CURL_POLL_NONE:
579 return 0;
580
581 default:
582 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "Unknown socket action %d", action);
583 return -1;
584 }
585
586 event_set(&ev->evnt, sock, events, http_request_pool_event_callback, ev);
587 event_base_set(HTTP_G->request.pool.event.base, &ev->evnt);
588 event_add(&ev->evnt, NULL);
589 }
590
591 return 0;
592 }
593 /* }}} */
594
595 /* {{{ static void http_request_pool_timer_callback(CURLM *, long, void*) */
596 static void http_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
597 {
598 http_request_pool *pool = timer_data;
599
600 if (pool->useevents) {
601 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
602 struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
603
604 if (event_initialized(pool->timeout) && event_pending(pool->timeout, EV_TIMEOUT, NULL)) {
605 event_del(pool->timeout);
606 }
607
608 event_set(pool->timeout, -1, 0, http_request_pool_timeout_callback, pool);
609 event_base_set(HTTP_G->request.pool.event.base, pool->timeout);
610 event_add(pool->timeout, &timeout);
611
612 #if HTTP_DEBUG_REQPOOLS
613 fprintf(stderr, "Updating timeout (%lu, %lu) of pool %p\n", (ulong) timeout.tv_sec, (ulong) timeout.tv_usec, pool);
614 #endif
615 }
616 }
617 /* }}} */
618 #endif /* HTTP_HAVE_EVENT */
619
620 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
621
622
623 /*
624 * Local variables:
625 * tab-width: 4
626 * c-basic-offset: 4
627 * End:
628 * vim600: noet sw=4 ts=4 fdm=marker
629 * vim<600: noet sw=4 ts=4
630 */
631