2012-08-07 18:28:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 greathqy
|
|
|
|
|
|
|
|
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"
|
|
|
|
// #include "platform/CCThread.h"
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "curl/curl.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
static pthread_t s_networkThread;
|
|
|
|
static pthread_mutex_t s_requestQueueMutex;
|
|
|
|
static pthread_mutex_t s_responseQueueMutex;
|
|
|
|
|
2013-04-27 23:44:33 +08:00
|
|
|
static pthread_mutex_t s_SleepMutex;
|
|
|
|
static pthread_cond_t s_SleepCondition;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-04-27 23:44:33 +08:00
|
|
|
static unsigned long s_asyncRequestCount = 0;
|
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
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
static bool need_quit = false;
|
|
|
|
|
|
|
|
static CCArray* s_requestQueue = NULL;
|
|
|
|
static CCArray* s_responseQueue = NULL;
|
|
|
|
|
|
|
|
static CCHttpClient *s_pHttpClient = NULL; // pointer to singleton
|
|
|
|
|
|
|
|
static char s_errorBuffer[CURL_ERROR_SIZE];
|
|
|
|
|
|
|
|
typedef size_t (*write_callback)(void *ptr, size_t size, size_t nmemb, void *stream);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-20 18:11:55 +08:00
|
|
|
static int processGetTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *errorCode, write_callback headerCallback, void *headerStream);
|
|
|
|
static int processPostTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *errorCode, write_callback headerCallback, void *headerStream);
|
|
|
|
static int processPutTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *errorCode, write_callback headerCallback, void *headerStream);
|
|
|
|
static int processDeleteTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *errorCode, write_callback headerCallback, void *headerStream);
|
2012-08-07 18:28:07 +08:00
|
|
|
// int processDownloadTask(HttpRequest *task, write_callback callback, void *stream, int32_t *errorCode);
|
|
|
|
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
// Worker thread
|
2012-08-07 18:28:07 +08:00
|
|
|
static void* networkThread(void *data)
|
|
|
|
{
|
2012-08-09 10:45:11 +08:00
|
|
|
CCHttpRequest *request = NULL;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (need_quit)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// step 1: send http request if the requestQueue isn't empty
|
|
|
|
request = NULL;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&s_requestQueueMutex); //Get request task from queue
|
|
|
|
if (0 != s_requestQueue->count())
|
|
|
|
{
|
2012-08-09 10:45:11 +08:00
|
|
|
request = dynamic_cast<CCHttpRequest*>(s_requestQueue->objectAtIndex(0));
|
|
|
|
s_requestQueue->removeObjectAtIndex(0);
|
|
|
|
// request's refcount = 1 here
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&s_requestQueueMutex);
|
|
|
|
|
|
|
|
if (NULL == request)
|
|
|
|
{
|
2013-04-27 23:44:33 +08:00
|
|
|
// Wait for http request tasks from main thread
|
|
|
|
pthread_cond_wait(&s_SleepCondition, &s_SleepMutex);
|
2012-08-07 18:28:07 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// step 2: libcurl sync access
|
|
|
|
|
|
|
|
// Create a HttpResponse object, the default setting is http access failed
|
2012-08-09 10:45:11 +08:00
|
|
|
CCHttpResponse *response = new CCHttpResponse(request);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2012-08-09 10:45:11 +08:00
|
|
|
// request's refcount = 2 here, it's retained by HttpRespose constructor
|
|
|
|
request->release();
|
|
|
|
// ok, refcount = 1 now, only HttpResponse hold it.
|
|
|
|
|
2013-05-07 15:37:25 +08:00
|
|
|
int32_t responseCode = -1;
|
2012-08-07 18:28:07 +08:00
|
|
|
int retValue = 0;
|
|
|
|
|
|
|
|
// Process the request -> get response packet
|
|
|
|
switch (request->getRequestType())
|
|
|
|
{
|
2012-08-09 10:45:11 +08:00
|
|
|
case CCHttpRequest::kHttpGet: // HTTP GET
|
2013-05-13 22:57:07 +08:00
|
|
|
retValue = processGetTask(request,
|
2012-08-07 18:28:07 +08:00
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
2013-05-13 22:57:07 +08:00
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader());
|
2012-08-07 18:28:07 +08:00
|
|
|
break;
|
|
|
|
|
2012-08-09 10:45:11 +08:00
|
|
|
case CCHttpRequest::kHttpPost: // HTTP POST
|
2013-05-13 22:57:07 +08:00
|
|
|
retValue = processPostTask(request,
|
2012-08-07 18:28:07 +08:00
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
2013-05-13 22:57:07 +08:00
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader());
|
2012-08-07 18:28:07 +08:00
|
|
|
break;
|
2013-04-22 00:23:27 +08:00
|
|
|
|
|
|
|
case CCHttpRequest::kHttpPut:
|
|
|
|
retValue = processPutTask(request,
|
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
2013-05-13 22:57:07 +08:00
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader());
|
2013-04-22 00:23:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CCHttpRequest::kHttpDelete:
|
|
|
|
retValue = processDeleteTask(request,
|
|
|
|
writeData,
|
|
|
|
response->getResponseData(),
|
2013-05-13 22:57:07 +08:00
|
|
|
&responseCode,
|
|
|
|
writeHeaderData,
|
|
|
|
response->getResponseHeader());
|
2013-04-22 00:23:27 +08:00
|
|
|
break;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
CCAssert(true, "CCHttpClient: unkown request type, only GET and POSt are supported");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
// write data to HttpResponse
|
2012-08-07 18:28:07 +08:00
|
|
|
response->setResponseCode(responseCode);
|
|
|
|
|
|
|
|
if (retValue != 0)
|
|
|
|
{
|
|
|
|
response->setSucceed(false);
|
|
|
|
response->setErrorBuffer(s_errorBuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response->setSucceed(true);
|
|
|
|
}
|
2012-08-09 10:45:11 +08:00
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// add response packet into queue
|
|
|
|
pthread_mutex_lock(&s_responseQueueMutex);
|
|
|
|
s_responseQueue->addObject(response);
|
|
|
|
pthread_mutex_unlock(&s_responseQueueMutex);
|
|
|
|
|
|
|
|
// resume dispatcher selector
|
|
|
|
CCDirector::sharedDirector()->getScheduler()->resumeTarget(CCHttpClient::getInstance());
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup: if worker thread received quit signal, clean up un-completed request queue
|
|
|
|
pthread_mutex_lock(&s_requestQueueMutex);
|
|
|
|
s_requestQueue->removeAllObjects();
|
|
|
|
pthread_mutex_unlock(&s_requestQueueMutex);
|
2012-08-09 10:45:11 +08:00
|
|
|
s_asyncRequestCount -= s_requestQueue->count();
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-04-27 23:44:33 +08:00
|
|
|
if (s_requestQueue != NULL) {
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
pthread_mutex_destroy(&s_requestQueueMutex);
|
|
|
|
pthread_mutex_destroy(&s_responseQueueMutex);
|
|
|
|
|
2013-04-27 23:44:33 +08:00
|
|
|
pthread_mutex_destroy(&s_SleepMutex);
|
|
|
|
pthread_cond_destroy(&s_SleepCondition);
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
s_requestQueue->release();
|
2013-04-27 23:44:33 +08:00
|
|
|
s_requestQueue = NULL;
|
2012-08-07 18:28:07 +08:00
|
|
|
s_responseQueue->release();
|
2013-04-27 23:44:33 +08:00
|
|
|
s_responseQueue = NULL;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Configure curl's timeout property
|
2013-05-20 18:11:55 +08:00
|
|
|
static bool configureCURL(CURL *handle)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
if (!handle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t code;
|
|
|
|
code = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, s_errorBuffer);
|
|
|
|
if (code != CURLE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
code = curl_easy_setopt(handle, CURLOPT_TIMEOUT, CCHttpClient::getInstance()->getTimeoutForRead());
|
|
|
|
if (code != CURLE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
code = curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, CCHttpClient::getInstance()->getTimeoutForConnect());
|
|
|
|
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);
|
|
|
|
|
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())
|
|
|
|
, _headers(NULL)
|
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
|
|
|
|
*/
|
2013-05-13 22:57:07 +08:00
|
|
|
bool init(CCHttpRequest *request, write_callback callback, void *stream, write_callback headerCallback, void *headerStream)
|
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;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!configureCURL(_curl))
|
2013-04-21 20:57:31 +08:00
|
|
|
return false;
|
|
|
|
|
2012-09-22 22:52:04 +08:00
|
|
|
/* get custom header data (if set) */
|
|
|
|
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-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
|
|
|
|
bool perform(int *responseCode)
|
|
|
|
{
|
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);
|
2013-04-21 20:57:31 +08:00
|
|
|
if (code != CURLE_OK || *responseCode != 200)
|
|
|
|
return false;
|
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
|
2013-05-20 18:11:55 +08:00
|
|
|
static int processGetTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *responseCode, write_callback headerCallback, void *headerStream)
|
2013-04-21 20:57:31 +08:00
|
|
|
{
|
|
|
|
CURLRaii curl;
|
2013-05-13 22:57:07 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
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
|
2013-05-20 18:11:55 +08:00
|
|
|
static int processPostTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *responseCode, write_callback headerCallback, void *headerStream)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-04-21 20:57:31 +08:00
|
|
|
CURLRaii curl;
|
2013-05-13 22:57:07 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
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
|
2013-05-20 18:11:55 +08:00
|
|
|
static int processPutTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *responseCode, write_callback headerCallback, void *headerStream)
|
2013-04-22 00:23:27 +08:00
|
|
|
{
|
|
|
|
CURLRaii curl;
|
2013-05-13 22:57:07 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
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
|
2013-05-20 18:11:55 +08:00
|
|
|
static int processDeleteTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *responseCode, write_callback headerCallback, void *headerStream)
|
2013-04-22 00:23:27 +08:00
|
|
|
{
|
|
|
|
CURLRaii curl;
|
2013-05-13 22:57:07 +08:00
|
|
|
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// HttpClient implementation
|
|
|
|
CCHttpClient* CCHttpClient::getInstance()
|
|
|
|
{
|
|
|
|
if (s_pHttpClient == NULL) {
|
|
|
|
s_pHttpClient = new CCHttpClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_pHttpClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCHttpClient::destroyInstance()
|
|
|
|
{
|
2013-01-04 07:13:17 +08:00
|
|
|
CCAssert(s_pHttpClient, "");
|
2012-10-24 07:14:40 +08:00
|
|
|
CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CCHttpClient::dispatchResponseCallbacks), s_pHttpClient);
|
|
|
|
s_pHttpClient->release();
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCHttpClient::CCHttpClient()
|
2013-03-03 10:32:09 +08:00
|
|
|
: _timeoutForConnect(30)
|
|
|
|
, _timeoutForRead(60)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(
|
|
|
|
schedule_selector(CCHttpClient::dispatchResponseCallbacks), this, 0, false);
|
|
|
|
CCDirector::sharedDirector()->getScheduler()->pauseTarget(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCHttpClient::~CCHttpClient()
|
|
|
|
{
|
|
|
|
need_quit = true;
|
|
|
|
|
2013-04-27 23:44:33 +08:00
|
|
|
if (s_requestQueue != NULL) {
|
|
|
|
pthread_cond_signal(&s_SleepCondition);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2012-10-24 07:14:40 +08:00
|
|
|
s_pHttpClient = NULL;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Lazy create semaphore & mutex & thread
|
|
|
|
bool CCHttpClient::lazyInitThreadSemphore()
|
|
|
|
{
|
2013-04-27 23:44:33 +08:00
|
|
|
if (s_requestQueue != NULL) {
|
2012-08-07 18:28:07 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
s_requestQueue = new CCArray();
|
|
|
|
s_requestQueue->init();
|
|
|
|
|
|
|
|
s_responseQueue = new CCArray();
|
|
|
|
s_responseQueue->init();
|
|
|
|
|
|
|
|
pthread_mutex_init(&s_requestQueueMutex, NULL);
|
|
|
|
pthread_mutex_init(&s_responseQueueMutex, NULL);
|
|
|
|
|
2013-04-27 23:44:33 +08:00
|
|
|
pthread_mutex_init(&s_SleepMutex, NULL);
|
|
|
|
pthread_cond_init(&s_SleepCondition, NULL);
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
pthread_create(&s_networkThread, NULL, networkThread, NULL);
|
|
|
|
pthread_detach(s_networkThread);
|
|
|
|
|
|
|
|
need_quit = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add a get task to queue
|
2012-08-09 10:45:11 +08:00
|
|
|
void CCHttpClient::send(CCHttpRequest* request)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
if (false == lazyInitThreadSemphore())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
++s_asyncRequestCount;
|
|
|
|
|
|
|
|
request->retain();
|
|
|
|
|
|
|
|
pthread_mutex_lock(&s_requestQueueMutex);
|
|
|
|
s_requestQueue->addObject(request);
|
|
|
|
pthread_mutex_unlock(&s_requestQueueMutex);
|
|
|
|
|
|
|
|
// Notify thread start to work
|
2013-04-27 23:44:33 +08:00
|
|
|
pthread_cond_signal(&s_SleepCondition);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Poll and notify main thread if responses exists in queue
|
|
|
|
void CCHttpClient::dispatchResponseCallbacks(float delta)
|
|
|
|
{
|
2012-08-09 10:45:11 +08:00
|
|
|
// CCLog("CCHttpClient::dispatchResponseCallbacks is running");
|
|
|
|
|
|
|
|
CCHttpResponse* response = NULL;
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(&s_responseQueueMutex);
|
|
|
|
if (s_responseQueue->count())
|
|
|
|
{
|
2012-08-09 10:45:11 +08:00
|
|
|
response = dynamic_cast<CCHttpResponse*>(s_responseQueue->objectAtIndex(0));
|
2012-08-07 18:28:07 +08:00
|
|
|
s_responseQueue->removeObjectAtIndex(0);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&s_responseQueueMutex);
|
|
|
|
|
|
|
|
if (response)
|
|
|
|
{
|
|
|
|
--s_asyncRequestCount;
|
|
|
|
|
2012-08-09 10:45:11 +08:00
|
|
|
CCHttpRequest *request = response->getHttpRequest();
|
2012-08-07 18:28:07 +08:00
|
|
|
CCObject *pTarget = request->getTarget();
|
2013-04-26 11:42:50 +08:00
|
|
|
SEL_HttpResponse pSelector = request->getSelector();
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
if (pTarget && pSelector)
|
|
|
|
{
|
2013-04-26 11:42:50 +08:00
|
|
|
(pTarget->*pSelector)(this, response);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
response->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == s_asyncRequestCount)
|
|
|
|
{
|
|
|
|
CCDirector::sharedDirector()->getScheduler()->pauseTarget(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|
|
|
|
|
|
|
|
|