Advertisement
Guest User

Untitled

a guest
Sep 27th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. //Create Multi handle
  2. auto multi_handle = curl_multi_init();
  3. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  4. curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, 0);
  5.  
  6. //Create easy handle and add it to multi
  7. auto easy_handle = curl_easy_init();
  8. curl_multi_add_handle(multi_handle, easy_handle);
  9.  
  10. //Configure easy handle
  11. curl_easy_setopt(m_handle, CURLOPT_URL, "http://www.example.org");
  12. curl_easy_setopt(m_handle, CURLOPT_PRIVATE, some_handler_ptr);
  13. curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, curl_write_cb);
  14. curl_easy_setopt(m_handle, CURLOPT_TIMEOUT_MS, 12500);
  15. m_input_buffer->reserve(EXPECTED_BODY_SIZE); //std::unique_ptr<std::vector<char>>
  16. curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, m_input_buffer.get());
  17.  
  18. //Process requests
  19. int running_handles;
  20. auto result = curl_multi_perform(multi_handle, &running_handles);
  21. while(running_handles > 0)
  22. {
  23.     result = curl_multi_wait(multi_handle, nullptr, 0, m_timeout_milliseconds, nullptr);
  24.     int left_performers;
  25.     CURLMsg* performer = nullptr;
  26.     while((performer = curl_multi_info_read(multi_handle, &left_performers)) != nullptr)
  27.     {
  28.         if(performer->msg == CURLMSG_DONE)
  29.         {
  30.             auto easy_handle = performer->easy_handle;
  31.             //Here handler object is extracted from private data
  32.             if(performer->data.result == CURLE_OK)
  33.             {
  34.                 //extracted handler object is called here to process the receive buffer
  35.                 curl_multi_remove_handle(multi_handle, easy_handle);
  36.                 curl_easy_cleanup(easy_handle);
  37.             }
  38.             else
  39.             {
  40.                 //Failed request needs to be retried
  41.                 curl_multi_remove_handle(multi_handle, easy_handle);
  42.                 curl_easy_cleanup(easy_handle);
  43.                 auto easy_handle = curl_easy_init();
  44.                 curl_multi_add_handle(multi_handle, easy_handle);
  45.                 curl_easy_setopt(m_handle, CURLOPT_URL, "http://www.example.org");
  46.                 curl_easy_setopt(m_handle, CURLOPT_PRIVATE, some_handler_ptr);
  47.                 curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, curl_write_cb);
  48.                 curl_easy_setopt(m_handle, CURLOPT_TIMEOUT_MS, 12500);
  49.                 m_input_buffer->reserve(EXPECTED_BODY_SIZE); //std::unique_ptr<std::vector<char>>
  50.                 curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, m_input_buffer.get());
  51.             }
  52.         }
  53.         curl_multi_perform(multi_handle, &running_handles);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement