2012-08-07 18:28:07 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2012 greathqy
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "HttpClient.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2013-06-26 10:08:02 +08:00
|
|
|
#include <thread>
|
2012-08-07 18:28:07 +08:00
|
|
|
#include <queue>
|
2014-01-17 13:49:14 +08:00
|
|
|
#include <condition_variable>
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCVector.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
#include "curl/curl.h"
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-07-10 11:37:39 +08:00
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_BEGIN
|
2013-10-15 18:00:03 +08:00
|
|
|
|
|
|
|
namespace network {
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-06-26 10:08:02 +08:00
|
|
|
static std::mutex s_requestQueueMutex;
|
|
|
|
static std::mutex s_responseQueueMutex;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-06-24 23:01:15 +08:00
|
|
|
static std::condition_variable_any s_SleepCondition;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
|
2012-08-09 17:04:55 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
typedef int int32_t;
|
|
|
|
#endif
|
|
|
|
|
2013-12-10 15:08:20 +08:00
|
|
|
static Vector<HttpRequest*>* s_requestQueue = nullptr;
|
|
|
|
static Vector<HttpResponse*>* s_responseQueue = nullptr;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-01-02 15:47:38 +08:00
|
|
|
static HttpClient *s_pHttpClient = nullptr; // pointer to singleton
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-12-10 15:08:20 +08:00
|
|
|
static char s_errorBuffer[CURL_ERROR_SIZE] = {0};
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
typedef size_t (*write_callback)(void *ptr, size_t size, size_t nmemb, void *stream);
|
|
|
|
|
2013-07-10 11:37:39 +08:00
|
|
|
static std::string s_cookieFilename = "";
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// Callback function used by libcurl for collect response data
|
2013-05-20 18:11:55 +08:00
|
|
|
static size_t writeData(void *ptr, size_t size, size_t nmemb, void *stream)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
std::vector<char> *recvBuffer = (std::vector<char>*)stream;
|
|
|
|
size_t sizes = size * nmemb;
|
2012-09-09 18:25:02 +08:00
|
|
|
|
2012-09-10 23:17:16 +08:00
|
|
|
// add data to the end of recvBuffer
|
|
|
|
// write data maybe called more than once in a single request
|
|
|
|
recvBuffer->insert(recvBuffer->end(), (char*)ptr, (char*)ptr+sizes);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
2013-05-13 22:57:07 +08:00
|
|
|
// Callback function used by libcurl for collect header data
|
2013-05-20 18:11:55 +08:00
|
|
|
static size_t writeHeaderData(void *ptr, size_t size, size_t nmemb, void *stream)
|
2013-05-13 22:57:07 +08:00
|
|
|
{
|
|
|
|
std::vector<char> *recvBuffer = (std::vector<char>*)stream;
|
|
|
|
size_t sizes = size * nmemb;
|
|
|
|
|
|
|
|
// add data to the end of recvBuffer
|
|
|
|
// write data maybe called more than once in a single request
|
|
|
|
recvBuffer->insert(recvBuffer->end(), (char*)ptr, (char*)ptr+sizes);
|
|
|
|
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-23 01:43:32 +08:00
|
|
|
static int processGetTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
|
|
|
static int processPostTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
|
|
|
static int processPutTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
|
|
|
static int processDeleteTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
2012-08-07 18:28:07 +08:00
|
|
|
// int processDownloadTask(HttpRequest *task, write_callback callback, void *stream, int32_t *errorCode);
|
2014-06-22 12:37:23 +08:00
|
|
|
static void processResponse(HttpResponse* response, char* errorBuffer);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-06-24 23:01:15 +08:00
|
|
|
static HttpRequest *s_requestSentinel = new HttpRequest;
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
// Worker thread
|
2013-12-10 15:08:20 +08:00
|
|
|
void HttpClient::networkThread()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-12-10 15:08:20 +08:00
|
|
|
auto scheduler = Director::getInstance()->getScheduler();
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
while (true)
|
|
|
|
{
|
2014-06-24 23:01:15 +08:00
|
|
|
HttpRequest *request;
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// step 1: send http request if the requestQueue isn't empty
|
2013-06-26 11:10:19 +08:00
|
|
|
{
|
2014-06-24 23:01:15 +08:00
|
|
|
std::lock_guard<std::mutex> lock(s_requestQueueMutex);
|
|
|
|
while (s_requestQueue->empty()) {
|
|
|
|
s_SleepCondition.wait(s_requestQueueMutex);
|
|
|
|
}
|
2013-12-10 15:08:20 +08:00
|
|
|
request = s_requestQueue->at(0);
|
2013-12-11 17:53:45 +08:00
|
|
|
s_requestQueue->erase(0);
|
2013-06-26 11:10:19 +08:00
|
|
|
}
|
2014-06-24 23:01:15 +08:00
|
|
|
|
|
|
|
if (request == s_requestSentinel) {
|
|
|
|
break;
|
2013-06-26 11:10:19 +08:00
|
|
|
}
|
2014-06-24 23:01:15 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// step 2: libcurl sync access
|
|
|
|
|
|
|
|
// Create a HttpResponse object, the default setting is http access failed
|
2014-08-28 07:31:57 +08:00
|
|
|
HttpResponse *response = new (std::nothrow) HttpResponse(request);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-06-22 12:37:23 +08:00
|
|
|
processResponse(response, s_errorBuffer);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-05-21 15:31:58 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// add response packet into queue
|
2014-05-21 15:31:58 +08:00
|
|
|
s_responseQueueMutex.lock();
|
|
|
|
s_responseQueue->pushBack(response);
|
|
|
|
s_responseQueueMutex.unlock();
|
|
|
|
|
2014-02-26 10:20:34 +08:00
|
|
|
if (nullptr != s_pHttpClient) {
|
|
|
|
scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
|
|
|
|
}
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup: if worker thread received quit signal, clean up un-completed request queue
|
2013-06-26 10:45:46 +08:00
|
|
|
s_requestQueueMutex.lock();
|
2013-12-10 15:08:20 +08:00
|
|
|
s_requestQueue->clear();
|
2013-06-26 10:45:46 +08:00
|
|
|
s_requestQueueMutex.unlock();
|
2013-06-26 10:08:02 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-12-10 15:08:20 +08:00
|
|
|
if (s_requestQueue != nullptr) {
|
|
|
|
delete s_requestQueue;
|
|
|
|
s_requestQueue = nullptr;
|
2014-05-21 15:31:58 +08:00
|
|
|
delete s_responseQueue;
|
|
|
|
s_responseQueue = nullptr;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:08:26 +08:00
|
|
|
// Worker thread
|
|
|
|
void HttpClient::networkThreadAlone(HttpRequest* request)
|
2014-05-21 15:31:58 +08:00
|
|
|
{
|
2014-05-21 10:44:07 +08:00
|
|
|
// Create a HttpResponse object, the default setting is http access failed
|
2014-08-28 07:31:57 +08:00
|
|
|
HttpResponse *response = new (std::nothrow) HttpResponse(request);
|
2014-05-23 01:43:32 +08:00
|
|
|
char errorBuffer[CURL_ERROR_SIZE] = { 0 };
|
2014-06-22 12:37:23 +08:00
|
|
|
processResponse(response, errorBuffer);
|
2014-05-20 20:08:26 +08:00
|
|
|
|
2014-05-21 15:31:58 +08:00
|
|
|
auto scheduler = Director::getInstance()->getScheduler();
|
2014-06-26 02:03:53 +08:00
|
|
|
scheduler->performFunctionInCocosThread([response, request]{
|
2014-05-21 15:31:58 +08:00
|
|
|
const ccHttpRequestCallback& callback = request->getCallback();
|
|
|
|
Ref* pTarget = request->getTarget();
|
|
|
|
SEL_HttpResponse pSelector = request->getSelector();
|
2014-05-21 10:44:07 +08:00
|
|
|
|
2014-05-21 15:31:58 +08:00
|
|
|
if (callback != nullptr)
|
|
|
|
{
|
|
|
|
callback(s_pHttpClient, response);
|
|
|
|
}
|
|
|
|
else if (pTarget && pSelector)
|
|
|
|
{
|
|
|
|
(pTarget->*pSelector)(s_pHttpClient, response);
|
|
|
|
}
|
|
|
|
response->release();
|
2014-06-22 00:08:02 +08:00
|
|
|
// do not release in other thread
|
|
|
|
request->release();
|
2014-05-21 15:31:58 +08:00
|
|
|
});
|
2014-05-20 20:08:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
//Configure curl's timeout property
|
2014-05-23 01:43:32 +08:00
|
|
|
static bool configureCURL(CURL *handle, char *errorBuffer)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
if (!handle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t code;
|
2014-05-23 01:43:32 +08:00
|
|
|
code = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);
|
2012-08-07 18:28:07 +08:00
|
|
|
if (code != CURLE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
code = curl_easy_setopt(handle, CURLOPT_TIMEOUT, HttpClient::getInstance()->getTimeoutForRead());
|
2012-08-07 18:28:07 +08:00
|
|
|
if (code != CURLE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
code = curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, HttpClient::getInstance()->getTimeoutForConnect());
|
2012-08-07 18:28:07 +08:00
|
|
|
if (code != CURLE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-13 22:57:07 +08:00
|
|
|
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
|
2013-11-19 00:19:57 +08:00
|
|
|
|
|
|
|
// FIXED #3224: The subthread of CCHttpClient interrupts main thread if timeout comes.
|
|
|
|
// Document is here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOSIGNAL
|
|
|
|
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
|
2013-05-13 22:57:07 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-21 20:57:31 +08:00
|
|
|
class CURLRaii
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-04-21 20:57:31 +08:00
|
|
|
/// Instance of CURL
|
2013-06-15 14:03:30 +08:00
|
|
|
CURL *_curl;
|
2013-04-21 20:57:31 +08:00
|
|
|
/// Keeps custom header data
|
2013-06-15 14:03:30 +08:00
|
|
|
curl_slist *_headers;
|
2013-04-21 20:57:31 +08:00
|
|
|
public:
|
|
|
|
CURLRaii()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _curl(curl_easy_init())
|
2014-01-02 15:47:38 +08:00
|
|
|
, _headers(nullptr)
|
2013-04-21 20:57:31 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~CURLRaii()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_curl)
|
|
|
|
curl_easy_cleanup(_curl);
|
2013-04-21 20:57:31 +08:00
|
|
|
/* free the linked list for header data */
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_headers)
|
|
|
|
curl_slist_free_all(_headers);
|
2013-04-21 20:57:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool setOption(CURLoption option, T data)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return CURLE_OK == curl_easy_setopt(_curl, option, data);
|
2013-04-21 20:57:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Inits CURL instance for common usage
|
|
|
|
* @param request Null not allowed
|
|
|
|
* @param callback Response write callback
|
|
|
|
* @param stream Response write stream
|
|
|
|
*/
|
2014-05-23 01:43:32 +08:00
|
|
|
bool init(HttpRequest *request, write_callback callback, void *stream, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
2013-04-21 20:57:31 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_curl)
|
2013-04-21 20:57:31 +08:00
|
|
|
return false;
|
2014-05-23 01:43:32 +08:00
|
|
|
if (!configureCURL(_curl, errorBuffer))
|
2013-04-21 20:57:31 +08:00
|
|
|
return false;
|
|
|
|
|
2012-09-22 22:52:04 +08:00
|
|
|
/* get custom header data (if set) */
|
2014-05-21 10:44:07 +08:00
|
|
|
std::vector<std::string> headers=request->getHeaders();
|
2013-04-21 20:57:31 +08:00
|
|
|
if(!headers.empty())
|
2012-09-20 23:47:54 +08:00
|
|
|
{
|
2013-04-21 20:57:31 +08:00
|
|
|
/* append custom headers one by one */
|
|
|
|
for (std::vector<std::string>::iterator it = headers.begin(); it != headers.end(); ++it)
|
2013-06-15 14:03:30 +08:00
|
|
|
_headers = curl_slist_append(_headers,it->c_str());
|
2013-04-21 20:57:31 +08:00
|
|
|
/* set custom headers for curl */
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!setOption(CURLOPT_HTTPHEADER, _headers))
|
2013-04-21 20:57:31 +08:00
|
|
|
return false;
|
2012-09-20 23:47:54 +08:00
|
|
|
}
|
2013-07-10 11:37:39 +08:00
|
|
|
if (!s_cookieFilename.empty()) {
|
|
|
|
if (!setOption(CURLOPT_COOKIEFILE, s_cookieFilename.c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!setOption(CURLOPT_COOKIEJAR, s_cookieFilename.c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 23:47:54 +08:00
|
|
|
|
2013-04-21 20:57:31 +08:00
|
|
|
return setOption(CURLOPT_URL, request->getUrl())
|
|
|
|
&& setOption(CURLOPT_WRITEFUNCTION, callback)
|
2013-05-13 22:57:07 +08:00
|
|
|
&& setOption(CURLOPT_WRITEDATA, stream)
|
|
|
|
&& setOption(CURLOPT_HEADERFUNCTION, headerCallback)
|
|
|
|
&& setOption(CURLOPT_HEADERDATA, headerStream);
|
|
|
|
|
2013-04-21 20:57:31 +08:00
|
|
|
}
|
2012-09-22 22:52:04 +08:00
|
|
|
|
2013-04-21 20:57:31 +08:00
|
|
|
/// @param responseCode Null not allowed
|
2013-09-17 18:21:20 +08:00
|
|
|
bool perform(long *responseCode)
|
2013-04-21 20:57:31 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (CURLE_OK != curl_easy_perform(_curl))
|
2013-04-21 20:57:31 +08:00
|
|
|
return false;
|
2013-06-15 14:03:30 +08:00
|
|
|
CURLcode code = curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, responseCode);
|
2014-04-09 15:04:42 +08:00
|
|
|
if (code != CURLE_OK || !(*responseCode >= 200 && *responseCode < 300)) {
|
2013-07-10 11:37:39 +08:00
|
|
|
CCLOGERROR("Curl curl_easy_getinfo failed: %s", curl_easy_strerror(code));
|
2013-04-21 20:57:31 +08:00
|
|
|
return false;
|
2013-07-10 11:37:39 +08:00
|
|
|
}
|
2013-05-13 22:57:07 +08:00
|
|
|
// Get some mor data.
|
|
|
|
|
2013-04-21 20:57:31 +08:00
|
|
|
return true;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2013-04-21 20:57:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//Process Get Request
|
2014-05-23 01:43:32 +08:00
|
|
|
static int processGetTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
2013-04-21 20:57:31 +08:00
|
|
|
{
|
|
|
|
CURLRaii curl;
|
2014-05-23 01:43:32 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
2013-04-21 20:57:31 +08:00
|
|
|
&& curl.setOption(CURLOPT_FOLLOWLOCATION, true)
|
|
|
|
&& curl.perform(responseCode);
|
|
|
|
return ok ? 0 : 1;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Process POST Request
|
2014-05-23 01:43:32 +08:00
|
|
|
static int processPostTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-04-21 20:57:31 +08:00
|
|
|
CURLRaii curl;
|
2014-05-23 01:43:32 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
2013-04-21 20:57:31 +08:00
|
|
|
&& curl.setOption(CURLOPT_POST, 1)
|
|
|
|
&& curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData())
|
|
|
|
&& curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize())
|
|
|
|
&& curl.perform(responseCode);
|
|
|
|
return ok ? 0 : 1;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2012-09-22 22:52:04 +08:00
|
|
|
|
2013-04-22 00:23:27 +08:00
|
|
|
//Process PUT Request
|
2014-05-23 01:43:32 +08:00
|
|
|
static int processPutTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
2013-04-22 00:23:27 +08:00
|
|
|
{
|
|
|
|
CURLRaii curl;
|
2014-05-23 01:43:32 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
2013-04-22 00:23:27 +08:00
|
|
|
&& curl.setOption(CURLOPT_CUSTOMREQUEST, "PUT")
|
|
|
|
&& curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData())
|
|
|
|
&& curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize())
|
|
|
|
&& curl.perform(responseCode);
|
|
|
|
return ok ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Process DELETE Request
|
2014-05-23 01:43:32 +08:00
|
|
|
static int processDeleteTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
2013-04-22 00:23:27 +08:00
|
|
|
{
|
|
|
|
CURLRaii curl;
|
2014-05-23 01:43:32 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
2013-04-22 00:23:27 +08:00
|
|
|
&& curl.setOption(CURLOPT_CUSTOMREQUEST, "DELETE")
|
|
|
|
&& curl.setOption(CURLOPT_FOLLOWLOCATION, true)
|
|
|
|
&& curl.perform(responseCode);
|
|
|
|
return ok ? 0 : 1;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-22 12:37:23 +08:00
|
|
|
|
|
|
|
// Process Response
|
|
|
|
static void processResponse(HttpResponse* response, char* errorBuffer)
|
|
|
|
{
|
|
|
|
auto request = response->getHttpRequest();
|
|
|
|
long responseCode = -1;
|
|
|
|
int retValue = 0;
|
|
|
|
|
|
|
|
// Process the request -> get response packet
|
|
|
|
switch (request->getRequestType())
|
|
|
|
{
|
|
|
|
case HttpRequest::Type::GET: // HTTP GET
|
|
|
|
retValue = processGetTask(request,
|
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader(),
|
|
|
|
errorBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HttpRequest::Type::POST: // HTTP POST
|
|
|
|
retValue = processPostTask(request,
|
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader(),
|
|
|
|
errorBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HttpRequest::Type::PUT:
|
|
|
|
retValue = processPutTask(request,
|
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader(),
|
|
|
|
errorBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HttpRequest::Type::DELETE:
|
|
|
|
retValue = processDeleteTask(request,
|
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader(),
|
|
|
|
errorBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CCASSERT(true, "CCHttpClient: unkown request type, only GET and POSt are supported");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write data to HttpResponse
|
|
|
|
response->setResponseCode(responseCode);
|
|
|
|
|
|
|
|
if (retValue != 0)
|
|
|
|
{
|
|
|
|
response->setSucceed(false);
|
|
|
|
response->setErrorBuffer(errorBuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response->setSucceed(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// HttpClient implementation
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient* HttpClient::getInstance()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2014-01-02 15:47:38 +08:00
|
|
|
if (s_pHttpClient == nullptr) {
|
2014-08-28 07:31:57 +08:00
|
|
|
s_pHttpClient = new (std::nothrow) HttpClient();
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_pHttpClient;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void HttpClient::destroyInstance()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2014-01-02 15:47:38 +08:00
|
|
|
CC_SAFE_DELETE(s_pHttpClient);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:37:39 +08:00
|
|
|
void HttpClient::enableCookies(const char* cookieFile) {
|
|
|
|
if (cookieFile) {
|
|
|
|
s_cookieFilename = std::string(cookieFile);
|
|
|
|
}
|
|
|
|
else {
|
2013-07-12 06:24:23 +08:00
|
|
|
s_cookieFilename = (FileUtils::getInstance()->getWritablePath() + "cookieFile.txt");
|
2013-07-10 11:37:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient::HttpClient()
|
2013-03-03 10:32:09 +08:00
|
|
|
: _timeoutForConnect(30)
|
|
|
|
, _timeoutForRead(60)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient::~HttpClient()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2014-01-02 15:47:38 +08:00
|
|
|
if (s_requestQueue != nullptr) {
|
2014-06-24 23:01:15 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(s_requestQueueMutex);
|
|
|
|
s_requestQueue->pushBack(s_requestSentinel);
|
|
|
|
}
|
2014-05-21 10:44:07 +08:00
|
|
|
s_SleepCondition.notify_one();
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2014-06-24 23:01:15 +08:00
|
|
|
|
2014-01-02 15:47:38 +08:00
|
|
|
s_pHttpClient = nullptr;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Lazy create semaphore & mutex & thread
|
2013-06-20 14:15:53 +08:00
|
|
|
bool HttpClient::lazyInitThreadSemphore()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2014-01-02 15:47:38 +08:00
|
|
|
if (s_requestQueue != nullptr) {
|
2012-08-07 18:28:07 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
s_requestQueue = new (std::nothrow) Vector<HttpRequest*>();
|
|
|
|
s_responseQueue = new (std::nothrow) Vector<HttpResponse*>();
|
2014-06-24 23:01:15 +08:00
|
|
|
|
2013-12-10 15:08:20 +08:00
|
|
|
auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
|
2013-06-26 10:08:02 +08:00
|
|
|
t.detach();
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add a get task to queue
|
2013-06-20 14:15:53 +08:00
|
|
|
void HttpClient::send(HttpRequest* request)
|
2014-05-21 15:31:58 +08:00
|
|
|
{
|
2012-08-07 18:28:07 +08:00
|
|
|
if (false == lazyInitThreadSemphore())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->retain();
|
|
|
|
|
2014-03-04 10:07:49 +08:00
|
|
|
if (nullptr != s_requestQueue) {
|
|
|
|
s_requestQueueMutex.lock();
|
|
|
|
s_requestQueue->pushBack(request);
|
|
|
|
s_requestQueueMutex.unlock();
|
|
|
|
|
|
|
|
// Notify thread start to work
|
|
|
|
s_SleepCondition.notify_one();
|
|
|
|
}
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-22 11:39:39 +08:00
|
|
|
void HttpClient::sendImmediate(HttpRequest* request)
|
2014-05-20 20:08:26 +08:00
|
|
|
{
|
2014-05-21 10:44:07 +08:00
|
|
|
if(!request)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->retain();
|
|
|
|
auto t = std::thread(&HttpClient::networkThreadAlone, this, request);
|
|
|
|
t.detach();
|
2014-05-20 20:08:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// Poll and notify main thread if responses exists in queue
|
2013-12-10 15:08:20 +08:00
|
|
|
void HttpClient::dispatchResponseCallbacks()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-07-24 06:20:22 +08:00
|
|
|
// log("CCHttpClient::dispatchResponseCallbacks is running");
|
2014-02-27 11:23:26 +08:00
|
|
|
//occurs when cocos thread fires but the network thread has already quited
|
|
|
|
if (nullptr == s_responseQueue) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-02 15:47:38 +08:00
|
|
|
HttpResponse* response = nullptr;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-06-26 10:45:46 +08:00
|
|
|
s_responseQueueMutex.lock();
|
2013-06-26 10:08:02 +08:00
|
|
|
|
2013-12-10 15:08:20 +08:00
|
|
|
if (!s_responseQueue->empty())
|
2013-06-26 10:45:46 +08:00
|
|
|
{
|
2013-12-10 15:08:20 +08:00
|
|
|
response = s_responseQueue->at(0);
|
2013-12-11 17:53:45 +08:00
|
|
|
s_responseQueue->erase(0);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 10:45:46 +08:00
|
|
|
s_responseQueueMutex.unlock();
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
if (response)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpRequest *request = response->getHttpRequest();
|
2014-04-13 05:06:04 +08:00
|
|
|
const ccHttpRequestCallback& callback = request->getCallback();
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* pTarget = request->getTarget();
|
2013-04-26 11:42:50 +08:00
|
|
|
SEL_HttpResponse pSelector = request->getSelector();
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-04-13 05:06:04 +08:00
|
|
|
if (callback != nullptr)
|
|
|
|
{
|
|
|
|
callback(this, response);
|
|
|
|
}
|
|
|
|
else if (pTarget && pSelector)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-04-26 11:42:50 +08:00
|
|
|
(pTarget->*pSelector)(this, response);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
response->release();
|
2014-06-22 12:37:23 +08:00
|
|
|
// do not release in other thread
|
|
|
|
request->release();
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
|