diff options
Diffstat (limited to 'docs/examples/evhiperfifo.c')
-rw-r--r-- | docs/examples/evhiperfifo.c | 106 |
1 files changed, 55 insertions, 51 deletions
diff --git a/docs/examples/evhiperfifo.c b/docs/examples/evhiperfifo.c index ab4d00b30..6d7936b5c 100644 --- a/docs/examples/evhiperfifo.c +++ b/docs/examples/evhiperfifo.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -119,12 +119,12 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) { DPRINT("%s %li\n", __PRETTY_FUNCTION__, timeout_ms); ev_timer_stop(g->loop, &g->timer_event); - if (timeout_ms > 0) - { + if(timeout_ms > 0) { double t = timeout_ms / 1000; ev_timer_init(&g->timer_event, timer_cb, t, 0.); ev_timer_start(g->loop, &g->timer_event); - }else + } + else timer_cb(g->loop, &g->timer_event, 0); return 0; } @@ -132,20 +132,32 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) /* Die if we get a bad CURLMcode somewhere */ static void mcode_or_die(const char *where, CURLMcode code) { - if ( CURLM_OK != code ) - { + if(CURLM_OK != code) { const char *s; - switch ( code ) - { - case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break; - case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break; - case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break; - case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break; - case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break; - case CURLM_LAST: s="CURLM_LAST"; break; - default: s="CURLM_unknown"; + switch (code) { + case CURLM_BAD_HANDLE: + s="CURLM_BAD_HANDLE"; + break; + case CURLM_BAD_EASY_HANDLE: + s="CURLM_BAD_EASY_HANDLE"; + break; + case CURLM_OUT_OF_MEMORY: + s="CURLM_OUT_OF_MEMORY"; + break; + case CURLM_INTERNAL_ERROR: + s="CURLM_INTERNAL_ERROR"; + break; + case CURLM_UNKNOWN_OPTION: + s="CURLM_UNKNOWN_OPTION"; + break; + case CURLM_LAST: + s="CURLM_LAST"; break; - case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET"; + default: + s="CURLM_unknown"; + break; + case CURLM_BAD_SOCKET: + s="CURLM_BAD_SOCKET"; fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s); /* ignore this error */ return; @@ -168,8 +180,8 @@ static void check_multi_info(GlobalInfo *g) CURLcode res; fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running); - while ((msg = curl_multi_info_read(g->multi, &msgs_left))) { - if (msg->msg == CURLMSG_DONE) { + while((msg = curl_multi_info_read(g->multi, &msgs_left))) { + if(msg->msg == CURLMSG_DONE) { easy = msg->easy_handle; res = msg->data.result; curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); @@ -197,8 +209,7 @@ static void event_cb(EV_P_ struct ev_io *w, int revents) rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running); mcode_or_die("event_cb: curl_multi_socket_action", rc); check_multi_info(g); - if ( g->still_running <= 0 ) - { + if(g->still_running <= 0) { fprintf(MSG_OUT, "last transfer done, kill timeout\n"); ev_timer_stop(g->loop, &g->timer_event); } @@ -212,7 +223,8 @@ static void timer_cb(EV_P_ struct ev_timer *w, int revents) GlobalInfo *g = (GlobalInfo *)w->data; CURLMcode rc; - rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running); + rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, + &g->still_running); mcode_or_die("timer_cb: curl_multi_socket_action", rc); check_multi_info(g); } @@ -221,9 +233,8 @@ static void timer_cb(EV_P_ struct ev_timer *w, int revents) static void remsock(SockInfo *f, GlobalInfo *g) { printf("%s \n", __PRETTY_FUNCTION__); - if ( f ) - { - if ( f->evset ) + if(f) { + if(f->evset) ev_io_stop(g->loop, &f->ev); free(f); } @@ -241,7 +252,7 @@ static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g) f->sockfd = s; f->action = act; f->easy = e; - if ( f->evset ) + if(f->evset) ev_io_stop(g->loop, &f->ev); ev_io_init(&f->ev, event_cb, f->sockfd, kind); f->ev.data = g; @@ -273,18 +284,16 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) fprintf(MSG_OUT, "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); - if ( what == CURL_POLL_REMOVE ) - { + if(what == CURL_POLL_REMOVE) { fprintf(MSG_OUT, "\n"); remsock(fdp, g); - } else - { - if ( !fdp ) - { + } + else { + if(!fdp) { fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]); addsock(s, e, what, g); - } else - { + } + else { fprintf(MSG_OUT, "Changing action from %s to %s\n", whatstr[fdp->action], whatstr[what]); @@ -330,8 +339,7 @@ static void new_conn(char *url, GlobalInfo *g ) conn->error[0]='\0'; conn->easy = curl_easy_init(); - if ( !conn->easy ) - { + if(!conn->easy) { fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n"); exit(2); } @@ -366,16 +374,16 @@ static void fifo_cb(EV_P_ struct ev_io *w, int revents) int n=0; GlobalInfo *g = (GlobalInfo *)w->data; - do - { + do { s[0]='\0'; rv=fscanf(g->input, "%1023s%n", s, &n); s[n]='\0'; - if ( n && s[0] ) - { - new_conn(s,g); /* if we read a URL, go get it! */ - } else break; - } while ( rv != EOF ); + if(n && s[0]) { + new_conn(s, g); /* if we read a URL, go get it! */ + } + else + break; + } while(rv != EOF); } /* Create a named pipe and tell libevent to monitor it */ @@ -386,24 +394,20 @@ static int init_fifo (GlobalInfo *g) curl_socket_t sockfd; fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo); - if ( lstat (fifo, &st) == 0 ) - { - if ( (st.st_mode & S_IFMT) == S_IFREG ) - { + if(lstat (fifo, &st) == 0) { + if((st.st_mode & S_IFMT) == S_IFREG) { errno = EEXIST; perror("lstat"); exit (1); } } unlink(fifo); - if ( mkfifo (fifo, 0600) == -1 ) - { + if(mkfifo (fifo, 0600) == -1) { perror("mkfifo"); exit (1); } sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0); - if ( sockfd == -1 ) - { + if(sockfd == -1) { perror("open"); exit (1); } @@ -412,7 +416,7 @@ static int init_fifo (GlobalInfo *g) fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo); ev_io_init(&g->fifo_event, fifo_cb, sockfd, EV_READ); ev_io_start(g->loop, &g->fifo_event); - return(0); + return (0); } int main(int argc, char **argv) |