SHARE
TWEET

Untitled

a guest Mar 24th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef _WIN32
  2. #define ENABLED
  3. #endif
  4.  
  5. #ifdef ENABLED
  6.  
  7. #include "LCHttp.h"
  8. #include <chrono>
  9. #include <thread>
  10. #include "Zip.h"
  11. #include <mutex>
  12.  
  13. #include "curlinc/curl/curl.h"
  14.  
  15. CURL *LCHttp::curl = NULL;
  16. auto min_sleep = std::chrono::milliseconds(1);
  17. auto max_sleep = std::chrono::milliseconds(5000);
  18. auto current_sleep = min_sleep;
  19.  
  20. //lock is just to prevent closing until finished
  21. static std::mutex lock;
  22.  
  23. void LCHttp::delayReset()
  24. {
  25.     current_sleep = min_sleep;
  26. }
  27. void LCHttp::delayRetry()
  28. {
  29.  
  30.     std::this_thread::sleep_for(current_sleep);
  31.     current_sleep *= 2;
  32.  
  33.     current_sleep = std::min(current_sleep, max_sleep);
  34.  
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41. size_t LCHttp::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  42. {
  43.     size_t realsize = size * nmemb;
  44.     struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  45.  
  46.     byte *ptr = (byte*)realloc(mem->memory, mem->size + realsize + 1);
  47.     if (ptr == NULL) {
  48.         /* out of memory! */
  49.         printf("not enough memory (realloc returned NULL)\n");
  50.         return 0;
  51.     }
  52.  
  53.     mem->memory = ptr;
  54.     memcpy(&(mem->memory[mem->size]), contents, realsize);
  55.     mem->size += realsize;
  56.     mem->memory[mem->size] = 0;
  57.  
  58.     return realsize;
  59. }
  60.  
  61. void LCHttp::finish()
  62. {
  63.     lock.lock();
  64.     lock.unlock();
  65.     if (curl != NULL)
  66.     {
  67.         curl_easy_cleanup(curl);
  68.         curl = NULL;
  69.     }
  70. }
  71.  
  72.  
  73. void LCHttp::start()
  74. {
  75.     if (curl == NULL)
  76.     {
  77.         curl_global_init(CURL_GLOBAL_ALL);
  78.         curl = curl_easy_init();
  79.  
  80.     }
  81. }
  82. struct MemoryStruct LCHttp::get_response(std::string url)
  83. {
  84.  
  85.  
  86.     CURLcode res;
  87.     struct MemoryStruct chunk;
  88.  
  89.     chunk.memory = (byte*)malloc(8);  /* will be grown as needed by the realloc above */
  90.     chunk.size = 0;    /* no data at this point */
  91.  
  92.  
  93.  
  94.     if (curl) {
  95.  
  96.  
  97.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  98.         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  99.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  100.         curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  101.  
  102.         res = curl_easy_perform(curl);
  103.         /* Check for errors */
  104.         if (res != CURLE_OK)
  105.             fprintf(stderr, "curl_easy_perform() failed: %s\n",
  106.                 curl_easy_strerror(res));
  107.  
  108.  
  109.         // curl_easy_cleanup(curl);
  110.     }
  111.     return chunk;
  112. }
  113. std::string LCHttp::loadUrl;
  114.  
  115. struct MemoryStruct  LCHttp::get_http(long long offset, size_t size)
  116. {
  117.  
  118.     std::string url = loadUrl + "&offset=" + std::to_string(offset);
  119.     url = url + "&size=";
  120.     url = url + std::to_string(size);
  121.  
  122.     auto    chunk = get_response(url);
  123.  
  124.  
  125.     return chunk;
  126. }
  127.  
  128. std::vector<byte> LCHttp::getFromHttp(DataSource d)
  129. {
  130.     lock.lock();
  131.  
  132.      
  133.     std::vector<byte> empty;
  134.     {
  135.  
  136.         auto chunk = get_http(d.offset, d.compressed_size);
  137.  
  138.         if (d.compressed_size != d.size)
  139.         {
  140.  
  141.             if (chunk.size != d.compressed_size)
  142.             {
  143.                 delayRetry();
  144.                 lock.unlock();
  145.  
  146.                 return empty;
  147.             }
  148.  
  149.             std::vector<byte> decompressedData(d.size);
  150.  
  151.  
  152.             int length = Zip::UncompressData((const BYTE*)chunk.memory, d.compressed_size, &decompressedData[0], decompressedData.size());
  153.             //decompress data
  154.  
  155.             free(chunk.memory);
  156.  
  157.  
  158.             if (length != d.size)
  159.             {
  160.                 std::cout << " decompress size mismatch a " << length << " , " << d.size << "  c " << chunk.size << "  , " << d.compressed_size << "\n";
  161.                 delayRetry();
  162.                 lock.unlock();
  163.  
  164.                 return empty;
  165.             }
  166.  
  167.             delayReset();
  168.             lock.unlock();
  169.  
  170.             return decompressedData;
  171.         }
  172.         else
  173.         {
  174.             std::vector<byte> data(&chunk.memory[0], &chunk.memory[d.size]);
  175.             free(chunk.memory);
  176.  
  177.             if (chunk.size != d.size)
  178.             {
  179.                 std::cout << " size mismatch b" << chunk.size << " , " << d.size << "\n";
  180.                 delayRetry();
  181.                 lock.unlock();
  182.  
  183.                 return empty;
  184.             }
  185.             delayReset();
  186.             lock.unlock();
  187.  
  188.             return data;
  189.         }
  190.     }
  191. }
  192.  
  193.  
  194. #else
  195.  
  196. #include "LCHttp.h"
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. void LCHttp::delayReset()
  213. {
  214.  
  215. }
  216. void LCHttp::delayRetry()
  217. {
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225. size_t LCHttp::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  226. {
  227.  
  228.     return 0;
  229. }
  230.  
  231. void LCHttp::finish()
  232. {
  233.    
  234. }
  235.  
  236.  
  237. void LCHttp::start()
  238. {
  239.    
  240. }
  241. struct MemoryStruct LCHttp::get_response(std::string url)
  242. {
  243.  
  244.  
  245.    
  246.     struct MemoryStruct chunk;
  247.  
  248.     return chunk;
  249. }
  250.  
  251. struct MemoryStruct  LCHttp::get_http(long long offset, size_t size)
  252. {
  253.  
  254.     struct MemoryStruct chunk;
  255.  
  256.     return chunk;
  257. }
  258.  
  259. std::vector<byte> LCHttp::getFromHttp(DataSource d)
  260. {
  261.     return std::vector<byte>();
  262. }
  263.  
  264.  
  265. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top