~ubuntu-branches/ubuntu/saucy/curl/saucy-201307251546

« back to all changes in this revision

Viewing changes to docs/examples/hiperfifo.c

  • Committer: Bazaar Package Importer
  • Author(s): Ramakrishnan Muthukrishnan
  • Date: 2010-10-18 11:13:17 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20101018111317-9rkas34ecwtq0upn
Tags: upstream-7.21.2
ImportĀ upstreamĀ versionĀ 7.21.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
  struct event fifo_event;
63
63
  struct event timer_event;
64
64
  CURLM *multi;
65
 
  int prev_running;
66
65
  int still_running;
67
66
  FILE* input;
68
67
} GlobalInfo;
110
109
    const char *s;
111
110
    switch (code) {
112
111
      case     CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break;
113
 
      case     CURLM_OK:                 s="CURLM_OK";                 break;
114
112
      case     CURLM_BAD_HANDLE:         s="CURLM_BAD_HANDLE";         break;
115
113
      case     CURLM_BAD_EASY_HANDLE:    s="CURLM_BAD_EASY_HANDLE";    break;
116
114
      case     CURLM_OUT_OF_MEMORY:      s="CURLM_OUT_OF_MEMORY";      break;
132
130
 
133
131
 
134
132
/* Check for completed transfers, and remove their easy handles */
135
 
static void check_run_count(GlobalInfo *g)
 
133
static void check_multi_info(GlobalInfo *g)
136
134
{
137
 
  if (g->prev_running > g->still_running) {
138
 
    char *eff_url=NULL;
139
 
    CURLMsg *msg;
140
 
    int msgs_left;
141
 
    ConnInfo *conn=NULL;
142
 
    CURL*easy;
143
 
    CURLcode res;
 
135
  char *eff_url;
 
136
  CURLMsg *msg;
 
137
  int msgs_left;
 
138
  ConnInfo *conn;
 
139
  CURL *easy;
 
140
  CURLcode res;
144
141
 
145
 
    fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
146
 
    /*
147
 
      I am still uncertain whether it is safe to remove an easy handle
148
 
      from inside the curl_multi_info_read loop, so here I will search
149
 
      for completed transfers in the inner "while" loop, and then remove
150
 
      them in the outer "do-while" loop...
151
 
   */
152
 
    do {
153
 
      easy=NULL;
154
 
      while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
155
 
        if (msg->msg == CURLMSG_DONE) {
156
 
          easy=msg->easy_handle;
157
 
          res=msg->data.result;
158
 
          break;
159
 
        }
160
 
      }
161
 
      if (easy) {
162
 
          curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
163
 
          curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
164
 
          fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
165
 
          curl_multi_remove_handle(g->multi, easy);
166
 
          free(conn->url);
167
 
          curl_easy_cleanup(easy);
168
 
          free(conn);
169
 
      }
170
 
    } while ( easy );
 
142
  fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
 
143
  while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
 
144
    if (msg->msg == CURLMSG_DONE) {
 
145
      easy = msg->easy_handle;
 
146
      res = msg->data.result;
 
147
      curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
 
148
      curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
 
149
      fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
 
150
      curl_multi_remove_handle(g->multi, easy);
 
151
      free(conn->url);
 
152
      curl_easy_cleanup(easy);
 
153
      free(conn);
 
154
    }
171
155
  }
172
 
  g->prev_running = g->still_running;
173
156
}
174
157
 
175
158
 
181
164
  CURLMcode rc;
182
165
 
183
166
  int action =
184
 
    (kind&EV_READ?CURL_CSELECT_IN:0)|
185
 
    (kind&EV_WRITE?CURL_CSELECT_OUT:0);
186
 
 
187
 
  do {
188
 
    rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
189
 
  } while (rc == CURLM_CALL_MULTI_PERFORM);
190
 
 
 
167
    (kind & EV_READ ? CURL_CSELECT_IN : 0) |
 
168
    (kind & EV_WRITE ? CURL_CSELECT_OUT : 0);
 
169
 
 
170
  rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
191
171
  mcode_or_die("event_cb: curl_multi_socket_action", rc);
192
172
 
193
 
  check_run_count(g);
 
173
  check_multi_info(g);
194
174
  if ( g->still_running <= 0 ) {
195
175
    fprintf(MSG_OUT, "last transfer done, kill timeout\n");
196
176
    if (evtimer_pending(&g->timer_event, NULL)) {
209
189
  (void)fd;
210
190
  (void)kind;
211
191
 
212
 
  do {
213
 
    rc = curl_multi_socket_action(g->multi,
 
192
  rc = curl_multi_socket_action(g->multi,
214
193
                                  CURL_SOCKET_TIMEOUT, 0, &g->still_running);
215
 
  } while (rc == CURLM_CALL_MULTI_PERFORM);
216
194
  mcode_or_die("timer_cb: curl_multi_socket_action", rc);
217
 
  check_run_count(g);
 
195
  check_multi_info(g);
218
196
}
219
197
 
220
198
 
340
318
  curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
341
319
  fprintf(MSG_OUT,
342
320
          "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
343
 
  rc =curl_multi_add_handle(g->multi, conn->easy);
 
321
  rc = curl_multi_add_handle(g->multi, conn->easy);
344
322
  mcode_or_die("new_conn: curl_multi_add_handle", rc);
345
323
 
346
324
  /* note that the add_handle() will set a time-out to trigger very soon so