HttpRequestCurlTask.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "HttpRequestCurlTask.h"
  2. #include "core/oxygine.h"
  3. #include "core/ThreadDispatcher.h"
  4. #include "SDL.h"
  5. #include "pthread.h"
  6. namespace oxygine
  7. {
  8. CURLM* multi_handle = 0;
  9. static pthread_t _thread;
  10. static ThreadDispatcher _messages;
  11. //ThreadMessages _main;
  12. static HttpRequestTask* createCurl()
  13. {
  14. return new HttpRequestTaskCURL;
  15. }
  16. const unsigned int ID_DONE = sysEventID('C', 'D', 'N');
  17. const unsigned int ID_PROGRESS = sysEventID('C', 'P', 'R');
  18. void mainThreadFunc(const ThreadDispatcher::message& msg)
  19. {
  20. switch (msg.msgid)
  21. {
  22. case ID_DONE:
  23. {
  24. CURL* easy = (CURL*)msg.arg1;
  25. HttpRequestTaskCURL* task = 0;
  26. curl_easy_getinfo(easy, CURLINFO_PRIVATE, &task);
  27. bool ok = (size_t)msg.arg2 == CURLE_OK;
  28. if (ok)
  29. {
  30. int response = 0;
  31. curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &response);
  32. task->_responseCode = response;
  33. //ok = response == 200;
  34. }
  35. #if 0
  36. const Uint8* data = SDL_GetKeyboardState(0);
  37. static bool fail = false;
  38. if (data[SDL_SCANCODE_N])
  39. fail = true;
  40. if (data[SDL_SCANCODE_M])
  41. fail = false;
  42. if (fail)
  43. ok = false;
  44. #endif
  45. if (ok)
  46. {
  47. task->onComplete();
  48. }
  49. else
  50. task->onError();
  51. task->releaseRef();
  52. } break;
  53. case ID_PROGRESS:
  54. {
  55. HttpRequestTaskCURL* task = (HttpRequestTaskCURL*)msg.cbData;
  56. task->dispatchProgress((int)(size_t)msg.arg2, (int)(size_t)msg.arg1);
  57. } break;
  58. }
  59. }
  60. void* thread(void*)
  61. {
  62. while (true)
  63. {
  64. _messages.wait();
  65. int still_running = -1;
  66. while (still_running)
  67. {
  68. static int i = 0;
  69. //log::messageln("upd---------%d - %d", getTimeMS(), ++i);
  70. ThreadDispatcher::peekMessage tmsg;
  71. if (_messages.peek(tmsg, true))
  72. {
  73. if (tmsg.msgid == 1)
  74. return 0;
  75. curl_multi_add_handle(multi_handle, (CURL*)tmsg.arg1);
  76. int q = 0;
  77. }
  78. curl_multi_perform(multi_handle, &still_running);
  79. if (still_running)
  80. {
  81. struct timeval timeout;
  82. fd_set fdread;
  83. fd_set fdwrite;
  84. fd_set fdexcep;
  85. int maxfd;
  86. FD_ZERO(&fdread);
  87. FD_ZERO(&fdwrite);
  88. FD_ZERO(&fdexcep);
  89. /* set a suitable timeout to play around with */
  90. timeout.tv_sec = 1;
  91. timeout.tv_usec = 0;
  92. /* get file descriptors from the transfers */
  93. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  94. /* In a real-world program you OF COURSE check the return code of the
  95. function calls, *and* you make sure that maxfd is bigger than -1 so
  96. that the call to select() below makes sense! */
  97. int rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  98. #if LIBCURL_VERSION_MINOR >= 44
  99. //todo: fix libcurl 30 silently
  100. if (rc == -1)
  101. return 0;
  102. #endif
  103. //while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running));
  104. }
  105. CURLMsg* msg = 0;
  106. int num;
  107. while ((msg = curl_multi_info_read(multi_handle, &num)))
  108. {
  109. if (msg->msg == CURLMSG_DONE)
  110. {
  111. curl_multi_remove_handle(multi_handle, msg->easy_handle);
  112. core::getMainThreadDispatcher().postCallback(ID_DONE, msg->easy_handle, (void*)msg->data.result, mainThreadFunc, 0);
  113. }
  114. }
  115. }
  116. }
  117. return 0;
  118. }
  119. void HttpRequestTask::init()
  120. {
  121. if (multi_handle)
  122. return;
  123. setCustomRequests(createCurl);
  124. multi_handle = curl_multi_init();
  125. pthread_create(&_thread, 0, thread, 0);
  126. }
  127. void HttpRequestTask::release()
  128. {
  129. _messages.post(1, 0, 0);
  130. pthread_join(_thread, 0);
  131. if (multi_handle)
  132. curl_multi_cleanup(multi_handle);
  133. multi_handle = 0;
  134. }
  135. size_t HttpRequestTaskCURL::cbXRefInfoFunction(void* userData, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
  136. {
  137. return ((HttpRequestTaskCURL*)userData)->_cbXRefInfoFunction(dltotal, dlnow);
  138. }
  139. size_t HttpRequestTaskCURL::_cbXRefInfoFunction(curl_off_t dltotal, curl_off_t dlnow)
  140. {
  141. core::getMainThreadDispatcher().postCallback(ID_PROGRESS, (void*)dltotal, (void*)dlnow, mainThreadFunc, this);
  142. return 0;
  143. }
  144. size_t HttpRequestTaskCURL::cbWriteFunction(char* d, size_t n, size_t l, void* userData)
  145. {
  146. return ((HttpRequestTaskCURL*)userData)->_cbWriteFunction(d, n, l);
  147. }
  148. size_t HttpRequestTaskCURL::_cbWriteFunction(char* d, size_t n, size_t l)
  149. {
  150. if (!_handle && !_fname.empty())
  151. {
  152. _handle = file::open(_fname, "wb");
  153. }
  154. size_t size = n * l;
  155. if (!_fname.empty())
  156. {
  157. file::write(_handle, d, (unsigned int)size);
  158. }
  159. else
  160. {
  161. _response.insert(_response.end(), d, d + size);
  162. }
  163. return size;
  164. }
  165. HttpRequestTaskCURL::HttpRequestTaskCURL() : _easy(0), _handle(0), _httpHeaders(0)
  166. {
  167. _easy = curl_easy_init();
  168. }
  169. HttpRequestTaskCURL::~HttpRequestTaskCURL()
  170. {
  171. if (_handle)
  172. file::close(_handle);
  173. _handle = 0;
  174. if (_easy)
  175. curl_easy_cleanup(_easy);
  176. _easy = 0;
  177. if (_httpHeaders)
  178. curl_slist_free_all(_httpHeaders);
  179. }
  180. void HttpRequestTaskCURL::_run()
  181. {
  182. curl_easy_setopt(_easy, CURLOPT_URL, _url.c_str());
  183. curl_easy_setopt(_easy, CURLOPT_PRIVATE, this);
  184. curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, HttpRequestTaskCURL::cbWriteFunction);
  185. curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this);
  186. #ifdef CURLOPT_XFERINFOFUNCTION
  187. curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, HttpRequestTaskCURL::cbXRefInfoFunction);
  188. curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this);
  189. #else
  190. #endif
  191. curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, true);
  192. curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0);
  193. curl_easy_setopt(_easy, CURLOPT_SSL_VERIFYPEER, false);
  194. if (!_postData.empty())
  195. {
  196. curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, &_postData.front());
  197. curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, _postData.size());
  198. }
  199. for (size_t i = 0; i < _headers.size(); ++i)
  200. _httpHeaders = curl_slist_append(_httpHeaders, (_headers[i].first + ": " + _headers[i].second).c_str());
  201. curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, _httpHeaders);
  202. addRef();
  203. _messages.post(0, _easy, 0);
  204. }
  205. void HttpRequestTaskCURL::_finalize(bool error)
  206. {
  207. if (_handle)
  208. {
  209. file::close(_handle);
  210. if (error)
  211. file::deleteFile(_fname);
  212. }
  213. _handle = 0;
  214. }
  215. }