Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Create Multi handle
- auto multi_handle = curl_multi_init();
- curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
- curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, 0);
- //Create easy handle and add it to multi
- auto easy_handle = curl_easy_init();
- curl_multi_add_handle(multi_handle, easy_handle);
- //Configure easy handle
- curl_easy_setopt(m_handle, CURLOPT_URL, "http://www.example.org");
- curl_easy_setopt(m_handle, CURLOPT_PRIVATE, some_handler_ptr);
- curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, curl_write_cb);
- curl_easy_setopt(m_handle, CURLOPT_TIMEOUT_MS, 12500);
- m_input_buffer->reserve(EXPECTED_BODY_SIZE); //std::unique_ptr<std::vector<char>>
- curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, m_input_buffer.get());
- //Process requests
- int running_handles;
- auto result = curl_multi_perform(multi_handle, &running_handles);
- while(running_handles > 0)
- {
- result = curl_multi_wait(multi_handle, nullptr, 0, m_timeout_milliseconds, nullptr);
- int left_performers;
- CURLMsg* performer = nullptr;
- while((performer = curl_multi_info_read(multi_handle, &left_performers)) != nullptr)
- {
- if(performer->msg == CURLMSG_DONE)
- {
- auto easy_handle = performer->easy_handle;
- //Here handler object is extracted from private data
- if(performer->data.result == CURLE_OK)
- {
- //extracted handler object is called here to process the receive buffer
- curl_multi_remove_handle(multi_handle, easy_handle);
- curl_easy_cleanup(easy_handle);
- }
- else
- {
- //Failed request needs to be retried
- curl_multi_remove_handle(multi_handle, easy_handle);
- curl_easy_cleanup(easy_handle);
- auto easy_handle = curl_easy_init();
- curl_multi_add_handle(multi_handle, easy_handle);
- curl_easy_setopt(m_handle, CURLOPT_URL, "http://www.example.org");
- curl_easy_setopt(m_handle, CURLOPT_PRIVATE, some_handler_ptr);
- curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, curl_write_cb);
- curl_easy_setopt(m_handle, CURLOPT_TIMEOUT_MS, 12500);
- m_input_buffer->reserve(EXPECTED_BODY_SIZE); //std::unique_ptr<std::vector<char>>
- curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, m_input_buffer.get());
- }
- }
- curl_multi_perform(multi_handle, &running_handles);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement