~ubuntu-branches/ubuntu/saucy/curl/saucy-proposed

« back to all changes in this revision

Viewing changes to docs/examples/hiperfifo.c

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2013-04-18 12:55:09 UTC
  • mfrom: (1.3.2)
  • mto: (70.1.1 curl)
  • mto: This revision was merged to the branch mainline in revision 69.
  • Revision ID: package-import@ubuntu.com-20130418125509-qcx8ilk3om2p51ij
Tags: 7.30.0-1
* New upstream release
* Update upstream copyright years
* Drop patches merged upstream:
  - 08_NULL-pointer-dereference-on-close.patch
  - 09_CVE-213-1944.patch
  - 10_test1218-another-cookie-tailmatch-test.patch
* Update patches:
  - 03_keep_symbols_compat.patch
  - 90_gnutls.patch
  - 99_nss.patch
* Add libcurl4-doc package:
  - Move *.pdf and *.html files to the libcurl4-doc package
  - Add Suggests for -doc package to -dev packages
  - Move examples to the -doc package
* Add Build-Depends on python which is used by some tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
24
24
 
25
25
Written by Jeff Pohlmeyer
26
26
 
27
 
Requires libevent and a (POSIX?) system that has mkfifo().
 
27
Requires libevent version 2 and a (POSIX?) system that has mkfifo().
28
28
 
29
29
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
30
30
sample programs.
61
61
#include <unistd.h>
62
62
#include <sys/poll.h>
63
63
#include <curl/curl.h>
64
 
#include <event.h>
 
64
#include <event2/event.h>
65
65
#include <fcntl.h>
66
66
#include <sys/stat.h>
67
67
#include <errno.h>
71
71
 
72
72
 
73
73
/* Global information, common to all connections */
74
 
typedef struct _GlobalInfo {
75
 
  struct event fifo_event;
76
 
  struct event timer_event;
 
74
typedef struct _GlobalInfo
 
75
{
 
76
  struct event_base *evbase;
 
77
  struct event *fifo_event;
 
78
  struct event *timer_event;
77
79
  CURLM *multi;
78
80
  int still_running;
79
81
  FILE* input;
81
83
 
82
84
 
83
85
/* Information associated with a specific easy handle */
84
 
typedef struct _ConnInfo {
 
86
typedef struct _ConnInfo
 
87
{
85
88
  CURL *easy;
86
89
  char *url;
87
90
  GlobalInfo *global;
90
93
 
91
94
 
92
95
/* Information associated with a specific socket */
93
 
typedef struct _SockInfo {
 
96
typedef struct _SockInfo
 
97
{
94
98
  curl_socket_t sockfd;
95
99
  CURL *easy;
96
100
  int action;
97
101
  long timeout;
98
 
  struct event ev;
 
102
  struct event *ev;
99
103
  int evset;
100
104
  GlobalInfo *global;
101
105
} SockInfo;
111
115
  timeout.tv_sec = timeout_ms/1000;
112
116
  timeout.tv_usec = (timeout_ms%1000)*1000;
113
117
  fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
114
 
  evtimer_add(&g->timer_event, &timeout);
 
118
  evtimer_add(g->timer_event, &timeout);
115
119
  return 0;
116
120
}
117
121
 
186
190
  check_multi_info(g);
187
191
  if ( g->still_running <= 0 ) {
188
192
    fprintf(MSG_OUT, "last transfer done, kill timeout\n");
189
 
    if (evtimer_pending(&g->timer_event, NULL)) {
190
 
      evtimer_del(&g->timer_event);
 
193
    if (evtimer_pending(g->timer_event, NULL)) {
 
194
      evtimer_del(g->timer_event);
191
195
    }
192
196
  }
193
197
}
215
219
{
216
220
  if (f) {
217
221
    if (f->evset)
218
 
      event_del(&f->ev);
 
222
      event_free(f->ev);
219
223
    free(f);
220
224
  }
221
225
}
232
236
  f->action = act;
233
237
  f->easy = e;
234
238
  if (f->evset)
235
 
    event_del(&f->ev);
236
 
  event_set(&f->ev, f->sockfd, kind, event_cb, g);
237
 
  f->evset=1;
238
 
  event_add(&f->ev, NULL);
 
239
    event_free(f->ev);
 
240
  f->ev = event_new(g->evbase, f->sockfd, kind, event_cb, g);
 
241
  f->evset = 1;
 
242
  event_add(f->ev, NULL);
239
243
}
240
244
 
241
245
 
242
246
 
243
247
/* Initialize a new SockInfo structure */
244
 
static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g) {
 
248
static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
 
249
{
245
250
  SockInfo *fdp = calloc(sizeof(SockInfo), 1);
246
251
 
247
252
  fdp->global = g;
359
364
}
360
365
 
361
366
/* Create a named pipe and tell libevent to monitor it */
 
367
static const char *fifo = "hiper.fifo";
362
368
static int init_fifo (GlobalInfo *g)
363
369
{
364
370
  struct stat st;
365
 
  static const char *fifo = "hiper.fifo";
366
371
  curl_socket_t sockfd;
367
372
 
368
373
  fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
386
391
  g->input = fdopen(sockfd, "r");
387
392
 
388
393
  fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
389
 
  event_set(&g->fifo_event, sockfd, EV_READ | EV_PERSIST, fifo_cb, g);
390
 
  event_add(&g->fifo_event, NULL);
 
394
  g->fifo_event = event_new(g->evbase, sockfd, EV_READ|EV_PERSIST, fifo_cb, g);
 
395
  event_add(g->fifo_event, NULL);
391
396
  return (0);
392
397
}
393
398
 
 
399
static void clean_fifo(GlobalInfo *g)
 
400
{
 
401
    event_free(g->fifo_event);
 
402
    fclose(g->input);
 
403
    unlink(fifo);
 
404
}
 
405
 
394
406
int main(int argc, char **argv)
395
407
{
396
408
  GlobalInfo g;
398
410
  (void)argv;
399
411
 
400
412
  memset(&g, 0, sizeof(GlobalInfo));
401
 
  event_init();
 
413
  g.evbase = event_base_new();
402
414
  init_fifo(&g);
403
415
  g.multi = curl_multi_init();
404
 
  evtimer_set(&g.timer_event, timer_cb, &g);
 
416
  g.timer_event = evtimer_new(g.evbase, timer_cb, &g);
405
417
 
406
418
  /* setup the generic multi interface options we want */
407
419
  curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
412
424
  /* we don't call any curl_multi_socket*() function yet as we have no handles
413
425
     added! */
414
426
 
415
 
  event_dispatch();
 
427
  event_base_dispatch(g.evbase);
 
428
 
 
429
  /* this, of course, won't get called since only way to stop this program is
 
430
     via ctrl-C, but it is here to show how cleanup /would/ be done. */
 
431
  clean_fifo(&g);
 
432
  event_free(g.timer_event);
 
433
  event_base_free(g.evbase);
416
434
  curl_multi_cleanup(g.multi);
417
435
  return 0;
418
436
}