Merge branch 'v1.6' of git://github.com/cocos-creator/cocos2d-x-lite into jsb2.0

This commit is contained in:
James Chen
2017-09-07 18:32:24 +08:00
32 changed files with 3420 additions and 417 deletions
+36 -35
View File
@@ -1,20 +1,20 @@
/****************************************************************************
Copyright (c) 2012 greathqy
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2013-2017 Chukong Technologies Inc.
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
@@ -48,11 +48,11 @@ static size_t writeData(void *ptr, size_t size, size_t nmemb, void *stream)
{
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;
}
@@ -61,11 +61,11 @@ static size_t writeHeaderData(void *ptr, size_t size, size_t nmemb, void *stream
{
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;
}
@@ -101,7 +101,7 @@ void HttpClient::networkThread()
}
// step 2: libcurl sync access
// Create a HttpResponse object, the default setting is http access failed
HttpResponse *response = new (std::nothrow) HttpResponse(request);
@@ -120,7 +120,7 @@ void HttpClient::networkThread()
}
_schedulerMutex.unlock();
}
// cleanup: if worker thread received quit signal, clean up un-completed request queue
_requestQueueMutex.lock();
_requestQueue.clear();
@@ -167,7 +167,7 @@ static bool configureCURL(HttpClient* client, CURL* handle, char* errorBuffer)
if (!handle) {
return false;
}
int32_t code;
code = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);
if (code != CURLE_OK) {
@@ -191,9 +191,9 @@ static bool configureCURL(HttpClient* client, CURL* handle, char* errorBuffer)
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(handle, CURLOPT_CAINFO, sslCaFilename.c_str());
}
// 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
// Document is here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOSIGNAL
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "");
@@ -247,8 +247,8 @@ public:
if(!headers.empty())
{
/* append custom headers one by one */
for (std::vector<std::string>::iterator it = headers.begin(); it != headers.end(); ++it)
_headers = curl_slist_append(_headers,it->c_str());
for (auto& header : headers)
_headers = curl_slist_append(_headers,header.c_str());
/* set custom headers for curl */
if (!setOption(CURLOPT_HTTPHEADER, _headers))
return false;
@@ -268,7 +268,7 @@ public:
&& setOption(CURLOPT_WRITEDATA, stream)
&& setOption(CURLOPT_HEADERFUNCTION, headerCallback)
&& setOption(CURLOPT_HEADERDATA, headerStream);
}
/// @param responseCode Null not allowed
@@ -282,7 +282,7 @@ public:
return false;
}
// Get some mor data.
return true;
}
};
@@ -339,7 +339,7 @@ HttpClient* HttpClient::getInstance()
{
_httpClient = new (std::nothrow) HttpClient();
}
return _httpClient;
}
@@ -382,7 +382,7 @@ void HttpClient::enableCookies(const char* cookieFile)
_cookieFilename = (FileUtils::getInstance()->getWritablePath() + "cookieFile.txt");
}
}
void HttpClient::setSSLVerification(const std::string& caFile)
{
std::lock_guard<std::mutex> lock(_sslCaFileMutex);
@@ -390,12 +390,12 @@ void HttpClient::setSSLVerification(const std::string& caFile)
}
HttpClient::HttpClient()
: _timeoutForConnect(30)
: _isInited(false)
, _timeoutForConnect(30)
, _timeoutForRead(60)
, _isInited(false)
, _threadCount(0)
, _requestSentinel(new HttpRequest())
, _cookie(nullptr)
, _requestSentinel(new HttpRequest())
{
CCLOG("In the constructor of HttpClient!");
memset(_responseMessage, 0, RESPONSE_BUFFER_SIZE * sizeof(char));
@@ -410,7 +410,7 @@ HttpClient::~HttpClient()
}
//Lazy create semaphore & mutex & thread
bool HttpClient::lazyInitThreadSemphore()
bool HttpClient::lazyInitThreadSemaphore()
{
if (_isInited)
{
@@ -422,23 +422,23 @@ bool HttpClient::lazyInitThreadSemphore()
t.detach();
_isInited = true;
}
return true;
}
//Add a get task to queue
void HttpClient::send(HttpRequest* request)
{
if (false == lazyInitThreadSemphore())
if (false == lazyInitThreadSemaphore())
{
return;
}
if (!request)
{
return;
}
request->retain();
_requestQueueMutex.lock();
@@ -478,7 +478,7 @@ void HttpClient::dispatchResponseCallbacks()
_responseQueue.erase(0);
}
_responseQueueMutex.unlock();
if (response)
{
HttpRequest *request = response->getHttpRequest();
@@ -546,7 +546,7 @@ void HttpClient::processResponse(HttpResponse* response, char* responseMessage)
break;
default:
CCASSERT(false , "CCHttpClient: unknown request type, only GET,POST,PUT or DELETE is supported");
CCASSERT(false, "CCHttpClient: unknown request type, only GET, POST, PUT or DELETE is supported");
break;
}
@@ -592,31 +592,31 @@ void HttpClient::setTimeoutForConnect(int value)
std::lock_guard<std::mutex> lock(_timeoutForConnectMutex);
_timeoutForConnect = value;
}
int HttpClient::getTimeoutForConnect()
{
std::lock_guard<std::mutex> lock(_timeoutForConnectMutex);
return _timeoutForConnect;
}
void HttpClient::setTimeoutForRead(int value)
{
std::lock_guard<std::mutex> lock(_timeoutForReadMutex);
_timeoutForRead = value;
}
int HttpClient::getTimeoutForRead()
{
std::lock_guard<std::mutex> lock(_timeoutForReadMutex);
return _timeoutForRead;
}
const std::string& HttpClient::getCookieFilename()
{
std::lock_guard<std::mutex> lock(_cookieFileMutex);
return _cookieFilename;
}
const std::string& HttpClient::getSSLVerification()
{
std::lock_guard<std::mutex> lock(_sslCaFileMutex);
@@ -627,3 +627,4 @@ const std::string& HttpClient::getSSLVerification()
NS_CC_END