某些句柄不会调用卷曲写回调

时间:2011-04-20 11:07:11

标签: curl callback

我只是按照http://curl.haxx.se/libcurl/c/multi-single.html中给出的curl multihandle示例进行了以下修改:

  1. 我在多手柄上添加了2个手柄,而不是一个。
  2. 我在两个句柄上都设置了CURLOPT_WRITEDATACURLOPT_WRITEFUNCTION选项。
  3. 响应应该是写回调函数,而不是stdout。但是当我运行程序时,对于一个curl句柄,响应被写入stdout,而另一个,写入回调函数被调用。如果您有兴趣,下面是我的完整计划。我不只是试图尝试这些例子,我在实际代码中遇到了类似的问题(只是在那里更糟糕,因为一些curl句柄响应既不会转到stdout也不会回调),所以这很重要。在我开枪之前请帮忙。墨盒不是空的。

    #include <stdio.h>
    #include <string>
    #include<iostream>
    
    /* somewhat unix-specific */ 
    #include <sys/time.h>
    #include <unistd.h>
    
    /* curl stuff */ 
    #include <curl/curl.h>
    using namespace std;
    
    size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) //no ordering of responses against requests maintained yet.
    {
      cout << "\nwrite_data called with non 0 data with userp " << userp;
      cout.flush();
      string* stored_response = (string*)userp;
      string chunk((char*)buffer,nmemb*size/sizeof(char));
      stored_response->append(chunk);
      return nmemb*size;
    }
    
    
    /*
     * Simply download a HTTP file.
     */ 
    
    int main(void)
    {
      CURL *http_handle;
      CURLM *multi_handle;
    
      int still_running; /* keep number of running handles */ 
    
      http_handle = curl_easy_init();
    
      string* response = new string("");
      cout << "\nresponse pointer for yahoo.com=" << response;
      cout.flush();
      curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, response);
      curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, write_data);
    
      /* set the options (I left out a few, you'll get the point anyway) */ 
      curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.yahoo.com/");
    
      /* init a multi stack */ 
      multi_handle = curl_multi_init();
    
      /* add the individual transfers */ 
      curl_multi_add_handle(multi_handle, http_handle);
      http_handle = curl_easy_init();
    
      response = new string("");
      cout << "\nresponse pointer for google.com=" << response;
      cout.flush();
      // curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, response);
      // curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, write_data);
    
      /* set the options (I left out a few, you'll get the point anyway) */
      curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.google.com/");
      curl_multi_add_handle(multi_handle, http_handle);
    
      /* we start some action by calling perform right away */ 
      int multi_perform_ret;
      do
      {
        multi_perform_ret = curl_multi_perform(multi_handle, &still_running);
      }
      while(multi_perform_ret == CURLM_CALL_MULTI_PERFORM); 
      while(still_running) {
        cout << "\nstill_running=" << still_running;
        cout.flush();
        struct timeval timeout;
        int rc; /* select() return code */ 
    
        fd_set fdread;
        fd_set fdwrite;
        fd_set fdexcep;
        int maxfd = -1;
    
        long curl_timeo = -1;
    
        FD_ZERO(&fdread);
        FD_ZERO(&fdwrite);
        FD_ZERO(&fdexcep);
    
        /* set a suitable timeout to play around with */ 
        /*
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
    
        curl_multi_timeout(multi_handle, &curl_timeo);
        if(curl_timeo >= 0) {
          timeout.tv_sec = curl_timeo / 1000;
            if(timeout.tv_sec > 1)
              timeout.tv_sec = 1;
            else
              timeout.tv_usec = (curl_timeo % 1000) * 1000;
        }
        */
    
        /* get file descriptors from the transfers */ 
        curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
    
        rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    
        switch(rc) {
        case -1:
          /* select error */ 
          still_running = 0;
          printf("select() returns error, this is badness\n");
          break;
        case 0:
        default:
          /* timeout or readable/writable sockets */ 
          do
          {
            multi_perform_ret = curl_multi_perform(multi_handle, &still_running);
          }
          while(multi_perform_ret == CURLM_CALL_MULTI_PERFORM);
          break;
        }
      }
    
      curl_multi_cleanup(multi_handle);
      curl_easy_cleanup(http_handle);
      return 0;
    }
    

1 个答案:

答案 0 :(得分:0)

我是个白痴。我已经注释掉了为第二个卷曲手柄设置回调。