| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include "HttpRequestCurlTask.h"
- #include "core/oxygine.h"
- #include "core/ThreadDispatcher.h"
- #include "SDL.h"
- #include "pthread.h"
- namespace oxygine
- {
- CURLM* multi_handle = 0;
- static pthread_t _thread;
- static ThreadDispatcher _messages;
- //ThreadMessages _main;
- static HttpRequestTask* createCurl()
- {
- return new HttpRequestTaskCURL;
- }
- const unsigned int ID_DONE = sysEventID('C', 'D', 'N');
- const unsigned int ID_PROGRESS = sysEventID('C', 'P', 'R');
- void mainThreadFunc(const ThreadDispatcher::message& msg)
- {
- switch (msg.msgid)
- {
- case ID_DONE:
- {
- CURL* easy = (CURL*)msg.arg1;
- HttpRequestTaskCURL* task = 0;
- curl_easy_getinfo(easy, CURLINFO_PRIVATE, &task);
- bool ok = (size_t)msg.arg2 == CURLE_OK;
- if (ok)
- {
- int response = 0;
- curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &response);
- task->_responseCode = response;
- //ok = response == 200;
- }
- #if 0
- const Uint8* data = SDL_GetKeyboardState(0);
- static bool fail = false;
- if (data[SDL_SCANCODE_N])
- fail = true;
- if (data[SDL_SCANCODE_M])
- fail = false;
- if (fail)
- ok = false;
- #endif
- if (ok)
- {
- task->onComplete();
- }
- else
- task->onError();
- task->releaseRef();
- } break;
- case ID_PROGRESS:
- {
- HttpRequestTaskCURL* task = (HttpRequestTaskCURL*)msg.cbData;
- task->dispatchProgress((int)(size_t)msg.arg2, (int)(size_t)msg.arg1);
- } break;
- }
- }
- void* thread(void*)
- {
- while (true)
- {
- _messages.wait();
- int still_running = -1;
- while (still_running)
- {
- static int i = 0;
- //log::messageln("upd---------%d - %d", getTimeMS(), ++i);
- ThreadDispatcher::peekMessage tmsg;
- if (_messages.peek(tmsg, true))
- {
- if (tmsg.msgid == 1)
- return 0;
- curl_multi_add_handle(multi_handle, (CURL*)tmsg.arg1);
- int q = 0;
- }
- curl_multi_perform(multi_handle, &still_running);
- if (still_running)
- {
- struct timeval timeout;
- fd_set fdread;
- fd_set fdwrite;
- fd_set fdexcep;
- int maxfd;
- FD_ZERO(&fdread);
- FD_ZERO(&fdwrite);
- FD_ZERO(&fdexcep);
- /* set a suitable timeout to play around with */
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- /* get file descriptors from the transfers */
- curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
- /* In a real-world program you OF COURSE check the return code of the
- function calls, *and* you make sure that maxfd is bigger than -1 so
- that the call to select() below makes sense! */
- int rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
- #if LIBCURL_VERSION_MINOR >= 44
- //todo: fix libcurl 30 silently
- if (rc == -1)
- return 0;
- #endif
- //while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running));
- }
- CURLMsg* msg = 0;
- int num;
- while ((msg = curl_multi_info_read(multi_handle, &num)))
- {
- if (msg->msg == CURLMSG_DONE)
- {
- curl_multi_remove_handle(multi_handle, msg->easy_handle);
- core::getMainThreadDispatcher().postCallback(ID_DONE, msg->easy_handle, (void*)msg->data.result, mainThreadFunc, 0);
- }
- }
- }
- }
- return 0;
- }
- void HttpRequestTask::init()
- {
- if (multi_handle)
- return;
- setCustomRequests(createCurl);
- multi_handle = curl_multi_init();
- pthread_create(&_thread, 0, thread, 0);
- }
- void HttpRequestTask::release()
- {
- _messages.post(1, 0, 0);
- pthread_join(_thread, 0);
- if (multi_handle)
- curl_multi_cleanup(multi_handle);
- multi_handle = 0;
- }
- size_t HttpRequestTaskCURL::cbXRefInfoFunction(void* userData, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
- {
- return ((HttpRequestTaskCURL*)userData)->_cbXRefInfoFunction(dltotal, dlnow);
- }
- size_t HttpRequestTaskCURL::_cbXRefInfoFunction(curl_off_t dltotal, curl_off_t dlnow)
- {
- core::getMainThreadDispatcher().postCallback(ID_PROGRESS, (void*)dltotal, (void*)dlnow, mainThreadFunc, this);
- return 0;
- }
- size_t HttpRequestTaskCURL::cbWriteFunction(char* d, size_t n, size_t l, void* userData)
- {
- return ((HttpRequestTaskCURL*)userData)->_cbWriteFunction(d, n, l);
- }
- size_t HttpRequestTaskCURL::_cbWriteFunction(char* d, size_t n, size_t l)
- {
- if (!_handle && !_fname.empty())
- {
- _handle = file::open(_fname, "wb");
- }
- size_t size = n * l;
- if (!_fname.empty())
- {
- file::write(_handle, d, (unsigned int)size);
- }
- else
- {
- _response.insert(_response.end(), d, d + size);
- }
- return size;
- }
- HttpRequestTaskCURL::HttpRequestTaskCURL() : _easy(0), _handle(0), _httpHeaders(0)
- {
- _easy = curl_easy_init();
- }
- HttpRequestTaskCURL::~HttpRequestTaskCURL()
- {
- if (_handle)
- file::close(_handle);
- _handle = 0;
- if (_easy)
- curl_easy_cleanup(_easy);
- _easy = 0;
- if (_httpHeaders)
- curl_slist_free_all(_httpHeaders);
- }
- void HttpRequestTaskCURL::_run()
- {
- curl_easy_setopt(_easy, CURLOPT_URL, _url.c_str());
- curl_easy_setopt(_easy, CURLOPT_PRIVATE, this);
- curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, HttpRequestTaskCURL::cbWriteFunction);
- curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this);
- #ifdef CURLOPT_XFERINFOFUNCTION
- curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, HttpRequestTaskCURL::cbXRefInfoFunction);
- curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this);
- #else
- #endif
- curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, true);
- curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0);
- curl_easy_setopt(_easy, CURLOPT_SSL_VERIFYPEER, false);
- if (!_postData.empty())
- {
- curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, &_postData.front());
- curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, _postData.size());
- }
- for (size_t i = 0; i < _headers.size(); ++i)
- _httpHeaders = curl_slist_append(_httpHeaders, (_headers[i].first + ": " + _headers[i].second).c_str());
- curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, _httpHeaders);
- addRef();
- _messages.post(0, _easy, 0);
- }
- void HttpRequestTaskCURL::_finalize(bool error)
- {
- if (_handle)
- {
- file::close(_handle);
- if (error)
- file::deleteFile(_fname);
- }
- _handle = 0;
- }
- }
|