hiperfifo: updated to use current libevent API
Patch by: Myk Taylor
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
* | (__| |_| | _ <| |___
|
* | (__| |_| | _ <| |___
|
||||||
* \___|\___/|_| \_\_____|
|
* \___|\___/|_| \_\_____|
|
||||||
*
|
*
|
||||||
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||||
*
|
*
|
||||||
* This software is licensed as described in the file COPYING, which
|
* This software is licensed as described in the file COPYING, which
|
||||||
* you should have received as part of this distribution. The terms
|
* you should have received as part of this distribution. The terms
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
Written by Jeff Pohlmeyer
|
Written by Jeff Pohlmeyer
|
||||||
|
|
||||||
Requires libevent and a (POSIX?) system that has mkfifo().
|
Requires libevent version 2 and a (POSIX?) system that has mkfifo().
|
||||||
|
|
||||||
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
|
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
|
||||||
sample programs.
|
sample programs.
|
||||||
@@ -61,7 +61,7 @@ callback.
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <event.h>
|
#include <event2/event.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -71,9 +71,11 @@ callback.
|
|||||||
|
|
||||||
|
|
||||||
/* Global information, common to all connections */
|
/* Global information, common to all connections */
|
||||||
typedef struct _GlobalInfo {
|
typedef struct _GlobalInfo
|
||||||
struct event fifo_event;
|
{
|
||||||
struct event timer_event;
|
struct event_base *evbase;
|
||||||
|
struct event *fifo_event;
|
||||||
|
struct event *timer_event;
|
||||||
CURLM *multi;
|
CURLM *multi;
|
||||||
int still_running;
|
int still_running;
|
||||||
FILE* input;
|
FILE* input;
|
||||||
@@ -81,7 +83,8 @@ typedef struct _GlobalInfo {
|
|||||||
|
|
||||||
|
|
||||||
/* Information associated with a specific easy handle */
|
/* Information associated with a specific easy handle */
|
||||||
typedef struct _ConnInfo {
|
typedef struct _ConnInfo
|
||||||
|
{
|
||||||
CURL *easy;
|
CURL *easy;
|
||||||
char *url;
|
char *url;
|
||||||
GlobalInfo *global;
|
GlobalInfo *global;
|
||||||
@@ -90,12 +93,13 @@ typedef struct _ConnInfo {
|
|||||||
|
|
||||||
|
|
||||||
/* Information associated with a specific socket */
|
/* Information associated with a specific socket */
|
||||||
typedef struct _SockInfo {
|
typedef struct _SockInfo
|
||||||
|
{
|
||||||
curl_socket_t sockfd;
|
curl_socket_t sockfd;
|
||||||
CURL *easy;
|
CURL *easy;
|
||||||
int action;
|
int action;
|
||||||
long timeout;
|
long timeout;
|
||||||
struct event ev;
|
struct event *ev;
|
||||||
int evset;
|
int evset;
|
||||||
GlobalInfo *global;
|
GlobalInfo *global;
|
||||||
} SockInfo;
|
} SockInfo;
|
||||||
@@ -111,7 +115,7 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
|
|||||||
timeout.tv_sec = timeout_ms/1000;
|
timeout.tv_sec = timeout_ms/1000;
|
||||||
timeout.tv_usec = (timeout_ms%1000)*1000;
|
timeout.tv_usec = (timeout_ms%1000)*1000;
|
||||||
fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
|
fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
|
||||||
evtimer_add(&g->timer_event, &timeout);
|
evtimer_add(g->timer_event, &timeout);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,8 +190,8 @@ static void event_cb(int fd, short kind, void *userp)
|
|||||||
check_multi_info(g);
|
check_multi_info(g);
|
||||||
if ( g->still_running <= 0 ) {
|
if ( g->still_running <= 0 ) {
|
||||||
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
|
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
|
||||||
if (evtimer_pending(&g->timer_event, NULL)) {
|
if (evtimer_pending(g->timer_event, NULL)) {
|
||||||
evtimer_del(&g->timer_event);
|
evtimer_del(g->timer_event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,7 +219,7 @@ static void remsock(SockInfo *f)
|
|||||||
{
|
{
|
||||||
if (f) {
|
if (f) {
|
||||||
if (f->evset)
|
if (f->evset)
|
||||||
event_del(&f->ev);
|
event_free(f->ev);
|
||||||
free(f);
|
free(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -232,16 +236,17 @@ static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g)
|
|||||||
f->action = act;
|
f->action = act;
|
||||||
f->easy = e;
|
f->easy = e;
|
||||||
if (f->evset)
|
if (f->evset)
|
||||||
event_del(&f->ev);
|
event_free(f->ev);
|
||||||
event_set(&f->ev, f->sockfd, kind, event_cb, g);
|
f->ev = event_new(g->evbase, f->sockfd, kind, event_cb, g);
|
||||||
f->evset=1;
|
f->evset = 1;
|
||||||
event_add(&f->ev, NULL);
|
event_add(f->ev, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Initialize a new SockInfo structure */
|
/* Initialize a new SockInfo structure */
|
||||||
static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g) {
|
static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
|
||||||
|
{
|
||||||
SockInfo *fdp = calloc(sizeof(SockInfo), 1);
|
SockInfo *fdp = calloc(sizeof(SockInfo), 1);
|
||||||
|
|
||||||
fdp->global = g;
|
fdp->global = g;
|
||||||
@@ -359,10 +364,10 @@ static void fifo_cb(int fd, short event, void *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Create a named pipe and tell libevent to monitor it */
|
/* Create a named pipe and tell libevent to monitor it */
|
||||||
|
static const char *fifo = "hiper.fifo";
|
||||||
static int init_fifo (GlobalInfo *g)
|
static int init_fifo (GlobalInfo *g)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
static const char *fifo = "hiper.fifo";
|
|
||||||
curl_socket_t sockfd;
|
curl_socket_t sockfd;
|
||||||
|
|
||||||
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
|
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
|
||||||
@@ -386,11 +391,18 @@ static int init_fifo (GlobalInfo *g)
|
|||||||
g->input = fdopen(sockfd, "r");
|
g->input = fdopen(sockfd, "r");
|
||||||
|
|
||||||
fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
|
fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
|
||||||
event_set(&g->fifo_event, sockfd, EV_READ | EV_PERSIST, fifo_cb, g);
|
g->fifo_event = event_new(g->evbase, sockfd, EV_READ|EV_PERSIST, fifo_cb, g);
|
||||||
event_add(&g->fifo_event, NULL);
|
event_add(g->fifo_event, NULL);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void clean_fifo(GlobalInfo *g)
|
||||||
|
{
|
||||||
|
event_free(g->fifo_event);
|
||||||
|
fclose(g->input);
|
||||||
|
unlink(fifo);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
GlobalInfo g;
|
GlobalInfo g;
|
||||||
@@ -398,10 +410,10 @@ int main(int argc, char **argv)
|
|||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
memset(&g, 0, sizeof(GlobalInfo));
|
memset(&g, 0, sizeof(GlobalInfo));
|
||||||
event_init();
|
g.evbase = event_base_new();
|
||||||
init_fifo(&g);
|
init_fifo(&g);
|
||||||
g.multi = curl_multi_init();
|
g.multi = curl_multi_init();
|
||||||
evtimer_set(&g.timer_event, timer_cb, &g);
|
g.timer_event = evtimer_new(g.evbase, timer_cb, &g);
|
||||||
|
|
||||||
/* setup the generic multi interface options we want */
|
/* setup the generic multi interface options we want */
|
||||||
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
|
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
|
||||||
@@ -412,7 +424,13 @@ int main(int argc, char **argv)
|
|||||||
/* we don't call any curl_multi_socket*() function yet as we have no handles
|
/* we don't call any curl_multi_socket*() function yet as we have no handles
|
||||||
added! */
|
added! */
|
||||||
|
|
||||||
event_dispatch();
|
event_base_dispatch(g.evbase);
|
||||||
|
|
||||||
|
/* this, of course, won't get called since only way to stop this program is
|
||||||
|
via ctrl-C, but it is here to show how cleanup /would/ be done. */
|
||||||
|
clean_fifo(&g);
|
||||||
|
event_free(g.timer_event);
|
||||||
|
event_base_free(g.evbase);
|
||||||
curl_multi_cleanup(g.multi);
|
curl_multi_cleanup(g.multi);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user