Skip to content

Commit

Permalink
refactor epoll implementions
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Jan 12, 2013
1 parent 0f4cbbd commit 16fbd37
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ if test "$PHP_YAR" != "no"; then
PHP_EVAL_LIBLINE($CURL_LIBS, YAR_SHARED_LIBADD)
PHP_ADD_LIBRARY_WITH_PATH(curl, $CURL_DIR/$PHP_LIBDIR, YAR_SHARED_LIBADD)

dnl AC_YAR_EPOLL()
AC_YAR_EPOLL()

PHP_NEW_EXTENSION(yar, yar.c yar_server.c yar_client.c yar_request.c yar_response.c yar_exception.c yar_packager.c yar_protocol.c packagers/php.c packagers/json.c packagers/msgpack.c yar_transport.c transports/curl.c transports/socket.c, $ext_shared)
if test "$PHP_MSGPACK" != "no"; then
Expand Down
45 changes: 30 additions & 15 deletions transports/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ typedef struct _yar_curl_multi_data_t {
typedef struct _yar_curl_multi_gdata {
int epfd;
CURLM *multi;
uint still_running;
} yar_curl_multi_gdata;

typedef struct _yar_curl_multi_sockinfo {
Expand All @@ -82,32 +81,37 @@ typedef struct _yar_curl_multi_sockinfo {
} yar_curl_multi_sockinfo;

static int php_yar_sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) /* {{{ */ {
struct epoll_event ev;
yar_curl_multi_gdata *g = (yar_curl_multi_gdata *) cbp;
yar_curl_multi_sockinfo *fdp = (yar_curl_multi_sockinfo *) sockp;
const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };

ev.data.fd = s;
ev.events = 0; /* EPOLLET does not work normally with libcurl */
/* fprintf(stderr, "socket callback: s=%d e=%p what=%s\n", s, e, whatstr[what]); */
if (what == CURL_POLL_REMOVE) {
struct epoll_event ev;
if (fdp) {
efree(fdp);
}
--g->still_running;
ev.data.fd = s;
ev.events = EPOLLIN;
epoll_ctl(g->epfd, EPOLL_CTL_DEL, s, &ev);
} else {
if (what == CURL_POLL_IN) {
ev.events |= EPOLLIN;
} else if (what == CURL_POLL_OUT) {
ev.events |= EPOLLOUT;
} else if (what == CURL_POLL_INOUT) {
ev.events |= EPOLLIN | EPOLLOUT;
}
if (!fdp) {
struct epoll_event ev;
fdp = ecalloc(1, sizeof(yar_curl_multi_sockinfo));
fdp->fd = s;
fdp->cp = e;
fdp = ecalloc(1, sizeof(yar_curl_multi_sockinfo));
fdp->fd = s;
fdp->cp = e;

ev.data.fd = s;
ev.events = EPOLLIN;
epoll_ctl(g->epfd, EPOLL_CTL_ADD, s, &ev);
curl_multi_assign(g->multi, s, fdp);
}
} else {
epoll_ctl(g->epfd, EPOLL_CTL_MOD, s, &ev);
}
}
return 0;
} /* }}} */
Expand Down Expand Up @@ -638,7 +642,6 @@ int php_yar_curl_multi_exec(yar_transport_multi_interface_t *self, yar_concurren

epfd = epoll_create(YAR_EPOLL_MAX_SIZE);
g.epfd = epfd;
g.still_running = 0;
#endif

multi = (yar_curl_multi_data_t *)self->data;
Expand Down Expand Up @@ -671,8 +674,20 @@ int php_yar_curl_multi_exec(yar_transport_multi_interface_t *self, yar_concurren
nfds = epoll_wait(epfd, events, 16, 500);
/* fprintf(stderr, "ready %ld sockets\n", nfds); */
for (i=0; i<nfds; i++) {
if(events[i].events & EPOLLIN) {
while (CURLM_CALL_MULTI_PERFORM == curl_multi_socket_all(multi->cm, &running_count));
if (events[i].events & EPOLLIN) {
# if LIBCURL_VERSION_NUM >= 0x071000
curl_multi_socket_action(multi->cm, CURL_CSELECT_IN, events[i].data.fd, &running_count);
# else
curl_multi_socket(multi->cm, events[i].data.fd, &running_count);
# endif
}

if (events[i].events & EPOLLOUT) {
# if LIBCURL_VERSION_NUM >= 0x071000
curl_multi_socket_action(multi->cm, CURL_CSELECT_OUT, events[i].data.fd, &running_count);
# else
curl_multi_socket(multi->cm, events[i].data.fd, &running_count);
# endif
}
}
#else
Expand Down

0 comments on commit 16fbd37

Please sign in to comment.