Advertisement
carmelo12341

Untitled

Jul 16th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. prepareForDownload();
  2.     // This is the previous method, it works, but it is slow
  3.     /*string nextUrl = galleryInfo->firstPicURL();
  4.     for (int i = 0; i < galleryInfo->getAmountOfPics(); ++i) {
  5.         Image * image = new Image(nextUrl, this->downloadDir);
  6.         nextUrl = image->getNextURL();
  7.         image->saveToDisk();
  8.         delete(image);
  9.     }*/
  10.     // Apparently, curlpp doesn't implement curl_multi_wait
  11.     // Create a vector that contains a ofstream and a curl_easy_handle
  12.     CURLM* multi = curl_multi_init();
  13.     vector<std::pair<CURL *, std::ofstream* > > handles;
  14.     string nextUrl = galleryInfo->firstPicURL();
  15.     for (int i = 0; i < 5; i++) { // Dis is just a test, 5 for now
  16.         // Create required variables
  17.         CURL* handle = curl_easy_init();
  18.         Image* image = new Image(nextUrl, downloadDir);
  19.         std::ofstream* of = new std::ofstream(downloadDir + image->getFilename(), std::ios::binary);
  20.         // Set requred options...
  21.         curl_easy_setopt(handle, CURLOPT_URL, image->getSourceURL().c_str());
  22.         curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, [=](const char* p, std::size_t size, std::size_t nmemb) {
  23.             of->write(p, size*nmemb);
  24.             return size * nmemb;
  25.         });
  26.         handles.push_back(std::make_pair(handle, of));
  27.         nextUrl = image->getNextURL();
  28.         delete(image);
  29.     }
  30.     // Now push all the handles to the multi handle
  31.     for (auto it = handles.begin(); it != handles.end(); ++it) {
  32.         curl_multi_add_handle(multi, it->first);
  33.     }
  34.     // now start the process
  35.     int stillRunning;
  36.     do {
  37.         int numfds;
  38.         int repeats = 0;
  39.         CURLMcode mc;
  40.  
  41.         mc = curl_multi_perform(multi, &stillRunning);
  42.         if (mc == CURLM_OK) {
  43.             mc = curl_multi_wait(multi, NULL, 0, 1000, &numfds);
  44.         }
  45.         if (mc != CURLM_OK) {
  46.             std::cerr << "Multi failed, code " << mc;
  47.         }
  48.         if (!numfds) {
  49.             repeats++;
  50.             if (repeats > 1) {
  51.                 std::this_thread::sleep_for(std::chrono::milliseconds(100));
  52.             }
  53.         } else {
  54.             repeats = 0;
  55.         }
  56.     } while (stillRunning);
  57.     // Now remove the handles...
  58.     for (auto it = handles.begin(); it != handles.end(); ++it) {
  59.         curl_multi_remove_handle(multi, it->first);
  60.     }
  61.     // Now delet everything
  62.     for (auto it = handles.begin(); it != handles.end(); ++it) {
  63.         delete(it->second);
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement