2014-12-31 10:15:14 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 greathqy
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2014-12-31 10:15:14 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "network/HttpClient.h"
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/android/jni/JniHelper.h"
|
|
|
|
|
2015-08-04 11:30:30 +08:00
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace network {
|
|
|
|
|
|
|
|
typedef std::vector<std::string> HttpRequestHeaders;
|
|
|
|
typedef HttpRequestHeaders::iterator HttpRequestHeadersIter;
|
|
|
|
typedef std::vector<std::string> HttpCookies;
|
|
|
|
typedef HttpCookies::iterator HttpCookiesIter;
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
static HttpClient* _httpClient = nullptr; // pointer to singleton
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
struct CookiesInfo
|
|
|
|
{
|
|
|
|
std::string domain;
|
|
|
|
bool tailmatch;
|
|
|
|
std::string path;
|
|
|
|
bool secure;
|
|
|
|
std::string key;
|
|
|
|
std::string value;
|
|
|
|
std::string expires;
|
|
|
|
};
|
|
|
|
|
|
|
|
//static size_t writeData(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
static size_t writeData(void* buffer, size_t sizes, HttpResponse* response)
|
|
|
|
{
|
|
|
|
std::vector<char> * recvBuffer = (std::vector<char>*)response->getResponseData();
|
|
|
|
recvBuffer->clear();
|
|
|
|
recvBuffer->insert(recvBuffer->end(), (char*)buffer, ((char*)buffer) + sizes);
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static size_t writeHeaderData(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
size_t writeHeaderData(void* buffer, size_t sizes,HttpResponse* response)
|
|
|
|
{
|
|
|
|
std::vector<char> * recvBuffer = (std::vector<char>*) response->getResponseHeader();
|
|
|
|
recvBuffer->clear();
|
|
|
|
recvBuffer->insert(recvBuffer->end(), (char*)buffer, (char*)buffer + sizes);
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
class HttpURLConnection
|
|
|
|
{
|
|
|
|
public:
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpURLConnection(HttpClient* httpClient)
|
2017-02-04 09:46:32 +08:00
|
|
|
:_client(httpClient)
|
|
|
|
,_httpURLConnection(nullptr)
|
2015-05-22 10:47:52 +08:00
|
|
|
,_requestmethod("")
|
|
|
|
,_responseCookies("")
|
|
|
|
,_cookieFileName("")
|
|
|
|
,_contentLength(0)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~HttpURLConnection()
|
|
|
|
{
|
2015-06-04 11:54:53 +08:00
|
|
|
if(_httpURLConnection != nullptr)
|
|
|
|
{
|
|
|
|
JniHelper::getEnv()->DeleteGlobalRef(_httpURLConnection);
|
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setRequestMethod(const char* method)
|
|
|
|
{
|
|
|
|
_requestmethod = method;
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"setRequestMethod",
|
|
|
|
"(Ljava/net/HttpURLConnection;Ljava/lang/String;)V"))
|
|
|
|
{
|
|
|
|
jstring jstr = methodInfo.env->NewStringUTF(_requestmethod.c_str());
|
|
|
|
methodInfo.env->CallStaticVoidMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, jstr);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstr);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(HttpRequest* request)
|
|
|
|
{
|
|
|
|
createHttpURLConnection(request->getUrl());
|
|
|
|
if(!configure())
|
2015-05-22 10:47:52 +08:00
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return false;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
/* get custom header data (if set) */
|
|
|
|
HttpRequestHeaders headers=request->getHeaders();
|
|
|
|
if(!headers.empty())
|
|
|
|
{
|
|
|
|
/* append custom headers one by one */
|
2016-10-12 01:59:44 +08:00
|
|
|
for (auto& header : headers)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2016-10-12 01:59:44 +08:00
|
|
|
int len = header.length();
|
|
|
|
int pos = header.find(':');
|
2014-12-31 10:15:14 +08:00
|
|
|
if (-1 == pos || pos >= len)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-12 01:59:44 +08:00
|
|
|
std::string str1 = header.substr(0, pos);
|
|
|
|
std::string str2 = header.substr(pos + 1, len - pos - 1);
|
2014-12-31 10:15:14 +08:00
|
|
|
addRequestHeader(str1.c_str(), str2.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addCookiesForRequestHeader();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int connect()
|
|
|
|
{
|
|
|
|
int suc = 0;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"connect",
|
|
|
|
"(Ljava/net/HttpURLConnection;)I"))
|
|
|
|
{
|
|
|
|
suc = methodInfo.env->CallStaticIntMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return suc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disconnect()
|
|
|
|
{
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"disconnect",
|
|
|
|
"(Ljava/net/HttpURLConnection;)V"))
|
|
|
|
{
|
|
|
|
methodInfo.env->CallStaticVoidMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int getResponseCode()
|
|
|
|
{
|
|
|
|
int responseCode = 0;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseCode",
|
|
|
|
"(Ljava/net/HttpURLConnection;)I"))
|
|
|
|
{
|
|
|
|
responseCode = methodInfo.env->CallStaticIntMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getResponseMessage()
|
|
|
|
{
|
|
|
|
char* message = nullptr;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseMessage",
|
|
|
|
"(Ljava/net/HttpURLConnection;)Ljava/lang/String;"))
|
|
|
|
{
|
|
|
|
jobject jObj = methodInfo.env->CallStaticObjectMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection);
|
|
|
|
message = getBufferFromJString((jstring)jObj, methodInfo.env);
|
|
|
|
if (nullptr != jObj)
|
|
|
|
{
|
|
|
|
methodInfo.env->DeleteLocalRef(jObj);
|
|
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendRequest(HttpRequest* request)
|
|
|
|
{
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"sendRequest",
|
|
|
|
"(Ljava/net/HttpURLConnection;[B)V"))
|
|
|
|
{
|
|
|
|
|
|
|
|
jbyteArray bytearray;
|
|
|
|
ssize_t dataSize = request->getRequestDataSize();
|
|
|
|
bytearray = methodInfo.env->NewByteArray(dataSize);
|
|
|
|
methodInfo.env->SetByteArrayRegion(bytearray, 0, dataSize, (const jbyte*)request->getRequestData());
|
|
|
|
methodInfo.env->CallStaticVoidMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, bytearray);
|
|
|
|
methodInfo.env->DeleteLocalRef(bytearray);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t saveResponseCookies(const char* responseCookies, size_t count)
|
|
|
|
{
|
|
|
|
if (nullptr == responseCookies || strlen(responseCookies) == 0 || count == 0)
|
2015-05-22 10:47:52 +08:00
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return 0;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
if (_cookieFileName.empty())
|
|
|
|
{
|
|
|
|
_cookieFileName = FileUtils::getInstance()->getWritablePath() + "cookieFile.txt";
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fp = fopen(_cookieFileName.c_str(), "w");
|
|
|
|
if (nullptr == fp)
|
|
|
|
{
|
|
|
|
CCLOG("can't create or open response cookie files");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite(responseCookies, sizeof(char), count, fp);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getResponseHeaders()
|
|
|
|
{
|
|
|
|
char* headers = nullptr;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseHeaders",
|
|
|
|
"(Ljava/net/HttpURLConnection;)Ljava/lang/String;"))
|
|
|
|
{
|
|
|
|
jobject jObj = methodInfo.env->CallStaticObjectMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection);
|
|
|
|
headers = getBufferFromJString((jstring)jObj, methodInfo.env);
|
|
|
|
if (nullptr != jObj) {
|
|
|
|
methodInfo.env->DeleteLocalRef(jObj);
|
|
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getResponseContent(HttpResponse* response)
|
|
|
|
{
|
|
|
|
if (nullptr == response)
|
2015-05-22 10:47:52 +08:00
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return nullptr;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
char* content = nullptr;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseContent",
|
|
|
|
"(Ljava/net/HttpURLConnection;)[B"))
|
|
|
|
{
|
|
|
|
jobject jObj = methodInfo.env->CallStaticObjectMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection);
|
|
|
|
|
|
|
|
_contentLength = getCStrFromJByteArray((jbyteArray)jObj, methodInfo.env, &content);
|
|
|
|
if (nullptr != jObj)
|
|
|
|
{
|
|
|
|
methodInfo.env->DeleteLocalRef(jObj);
|
|
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getResponseHeaderByKey(const char* key)
|
|
|
|
{
|
|
|
|
char* value = nullptr;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseHeaderByKey",
|
|
|
|
"(Ljava/net/HttpURLConnection;Ljava/lang/String;)Ljava/lang/String;"))
|
|
|
|
{
|
|
|
|
jstring jstrKey = methodInfo.env->NewStringUTF(key);
|
|
|
|
jobject jObj = methodInfo.env->CallStaticObjectMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, jstrKey);
|
|
|
|
value = getBufferFromJString((jstring)jObj, methodInfo.env);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrKey);
|
|
|
|
if (nullptr != jObj) {
|
|
|
|
methodInfo.env->DeleteLocalRef(jObj);
|
|
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getResponseHeaderByKeyInt(const char* key)
|
|
|
|
{
|
|
|
|
int contentLength = 0;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseHeaderByKeyInt",
|
|
|
|
"(Ljava/net/HttpURLConnection;Ljava/lang/String;)I"))
|
|
|
|
{
|
|
|
|
jstring jstrKey = methodInfo.env->NewStringUTF(key);
|
|
|
|
contentLength = methodInfo.env->CallStaticIntMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, jstrKey);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrKey);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getResponseHeaderByIdx(int idx)
|
|
|
|
{
|
|
|
|
char* header = nullptr;
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"getResponseHeaderByIdx",
|
|
|
|
"(Ljava/net/HttpURLConnection;I)Ljava/lang/String;"))
|
|
|
|
{
|
|
|
|
jobject jObj = methodInfo.env->CallStaticObjectMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, idx);
|
|
|
|
header = getBufferFromJString((jstring)jObj, methodInfo.env);
|
|
|
|
if (nullptr != jObj) {
|
|
|
|
methodInfo.env->DeleteLocalRef(jObj);
|
|
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2016-05-14 14:53:26 +08:00
|
|
|
const std::string& getCookieFileName() const
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
return _cookieFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCookieFileName(std::string& filename)
|
|
|
|
{
|
|
|
|
_cookieFileName = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getContentLength()
|
|
|
|
{
|
|
|
|
return _contentLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-17 12:17:56 +08:00
|
|
|
void createHttpURLConnection(const std::string& url)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"createHttpURLConnection",
|
|
|
|
"(Ljava/lang/String;)Ljava/net/HttpURLConnection;"))
|
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
jstring jurl = methodInfo.env->NewStringUTF(url.c_str());
|
|
|
|
jobject jObj = methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID, jurl);
|
|
|
|
_httpURLConnection = methodInfo.env->NewGlobalRef(jObj);
|
|
|
|
methodInfo.env->DeleteLocalRef(jurl);
|
2015-04-26 19:52:09 +08:00
|
|
|
methodInfo.env->DeleteLocalRef(jObj);
|
2014-12-31 10:15:14 +08:00
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addRequestHeader(const char* key, const char* value)
|
|
|
|
{
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"addRequestHeader",
|
|
|
|
"(Ljava/net/HttpURLConnection;Ljava/lang/String;Ljava/lang/String;)V"))
|
|
|
|
{
|
|
|
|
jstring jstrKey = methodInfo.env->NewStringUTF(key);
|
|
|
|
jstring jstrVal = methodInfo.env->NewStringUTF(value);
|
|
|
|
methodInfo.env->CallStaticVoidMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, jstrKey, jstrVal);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrKey);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrVal);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addCookiesForRequestHeader()
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if(_client->getCookieFilename().empty())
|
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
_cookieFileName = FileUtils::getInstance()->fullPathForFilename(_client->getCookieFilename());
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::string cookiesInfo = FileUtils::getInstance()->getStringFromFile(_cookieFileName);
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
if (cookiesInfo.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
HttpCookies cookiesVec;
|
|
|
|
cookiesVec.clear();
|
|
|
|
|
|
|
|
std::stringstream stream(cookiesInfo);
|
|
|
|
std::string item;
|
|
|
|
while (std::getline(stream, item, '\n'))
|
|
|
|
{
|
|
|
|
cookiesVec.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cookiesVec.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<CookiesInfo> cookiesInfoVec;
|
|
|
|
cookiesInfoVec.clear();
|
|
|
|
|
2016-10-12 01:59:44 +08:00
|
|
|
for (auto& cookies : cookiesVec)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
if (cookies.find("#HttpOnly_") != std::string::npos)
|
|
|
|
{
|
|
|
|
cookies = cookies.substr(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cookies.at(0) == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CookiesInfo co;
|
|
|
|
std::stringstream streamInfo(cookies);
|
|
|
|
std::string item;
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
|
|
|
|
while (std::getline(streamInfo, item, '\t'))
|
|
|
|
{
|
|
|
|
elems.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
co.domain = elems[0];
|
|
|
|
if (co.domain.at(0) == '.')
|
|
|
|
{
|
|
|
|
co.domain = co.domain.substr(1);
|
|
|
|
}
|
|
|
|
co.tailmatch = strcmp("TRUE", elems.at(1).c_str())?true: false;
|
|
|
|
co.path = elems.at(2);
|
|
|
|
co.secure = strcmp("TRUE", elems.at(3).c_str())?true: false;
|
|
|
|
co.expires = elems.at(4);
|
|
|
|
co.key = elems.at(5);
|
|
|
|
co.value = elems.at(6);
|
|
|
|
cookiesInfoVec.push_back(co);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string sendCookiesInfo = "";
|
|
|
|
int cookiesCount = 0;
|
2016-10-12 01:59:44 +08:00
|
|
|
for (auto& cookieInfo : cookiesInfoVec)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2016-10-12 01:59:44 +08:00
|
|
|
if (_url.find(cookieInfo.domain) != std::string::npos)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2016-10-12 01:59:44 +08:00
|
|
|
std::string keyValue = cookieInfo.key;
|
2014-12-31 10:15:14 +08:00
|
|
|
keyValue.append("=");
|
2016-10-12 01:59:44 +08:00
|
|
|
keyValue.append(cookieInfo.value);
|
2014-12-31 10:15:14 +08:00
|
|
|
if (cookiesCount != 0)
|
|
|
|
sendCookiesInfo.append(";");
|
|
|
|
|
|
|
|
sendCookiesInfo.append(keyValue);
|
|
|
|
}
|
|
|
|
cookiesCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set Cookie
|
|
|
|
addRequestHeader("Cookie",sendCookiesInfo.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void setReadAndConnectTimeout(int readMiliseconds, int connectMiliseconds)
|
|
|
|
{
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"setReadAndConnectTimeout",
|
|
|
|
"(Ljava/net/HttpURLConnection;II)V"))
|
|
|
|
{
|
|
|
|
methodInfo.env->CallStaticVoidMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, readMiliseconds, connectMiliseconds);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setVerifySSL()
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if(_client->getSSLVerification().empty())
|
2014-12-31 10:15:14 +08:00
|
|
|
return;
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(_client->getSSLVerification());
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxHttpURLConnection",
|
|
|
|
"setVerifySSL",
|
|
|
|
"(Ljava/net/HttpURLConnection;Ljava/lang/String;)V"))
|
|
|
|
{
|
|
|
|
jstring jstrfullpath = methodInfo.env->NewStringUTF(fullpath.c_str());
|
|
|
|
methodInfo.env->CallStaticVoidMethod(
|
|
|
|
methodInfo.classID, methodInfo.methodID, _httpURLConnection, jstrfullpath);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrfullpath);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool configure()
|
|
|
|
{
|
|
|
|
if(nullptr == _httpURLConnection)
|
2015-05-22 10:47:52 +08:00
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return false;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if(nullptr == _client)
|
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return false;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
setReadAndConnectTimeout(_client->getTimeoutForRead() * 1000, _client->getTimeoutForConnect() * 1000);
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
setVerifySSL();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getBufferFromJString(jstring jstr, JNIEnv* env)
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if (nullptr == jstr)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-05 16:10:14 +08:00
|
|
|
std::string strValue = cocos2d::StringUtils::getStringUTFCharsJNI(env, jstr);
|
2017-02-09 10:57:01 +08:00
|
|
|
return strdup(strValue.c_str());
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
int getCStrFromJByteArray(jbyteArray jba, JNIEnv* env, char** ppData)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if (nullptr == jba)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
*ppData = nullptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-09 10:57:01 +08:00
|
|
|
int len = env->GetArrayLength(jba);
|
|
|
|
char* str = (char*)malloc(sizeof(char)*len);
|
2014-12-31 10:15:14 +08:00
|
|
|
env->GetByteArrayRegion(jba, 0, len, (jbyte*)str);
|
|
|
|
|
|
|
|
*ppData = str;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-05-14 14:53:26 +08:00
|
|
|
const std::string& getCookieString() const
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
return _responseCookies;
|
|
|
|
}
|
|
|
|
private:
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpClient* _client;
|
2014-12-31 10:15:14 +08:00
|
|
|
jobject _httpURLConnection;
|
|
|
|
std::string _requestmethod;
|
|
|
|
std::string _responseCookies;
|
|
|
|
std::string _cookieFileName;
|
|
|
|
std::string _url;
|
|
|
|
int _contentLength;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Process Response
|
2015-05-22 10:47:52 +08:00
|
|
|
void HttpClient::processResponse(HttpResponse* response, char* responseMessage)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
auto request = response->getHttpRequest();
|
|
|
|
HttpRequest::Type requestType = request->getRequestType();
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if (HttpRequest::Type::GET != requestType &&
|
|
|
|
HttpRequest::Type::POST != requestType &&
|
2014-12-31 10:15:14 +08:00
|
|
|
HttpRequest::Type::PUT != requestType &&
|
|
|
|
HttpRequest::Type::DELETE != requestType)
|
|
|
|
{
|
2015-02-24 01:39:50 +08:00
|
|
|
CCASSERT(true, "CCHttpClient: unknown request type, only GET、POST、PUT、DELETE are supported");
|
2014-12-31 10:15:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long responseCode = -1;
|
|
|
|
int retValue = 0;
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpURLConnection urlConnection(this);
|
2014-12-31 10:15:14 +08:00
|
|
|
if(!urlConnection.init(request))
|
|
|
|
{
|
|
|
|
response->setSucceed(false);
|
|
|
|
response->setErrorBuffer("HttpURLConnetcion init failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (requestType)
|
|
|
|
{
|
|
|
|
case HttpRequest::Type::GET:
|
|
|
|
urlConnection.setRequestMethod("GET");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HttpRequest::Type::POST:
|
|
|
|
urlConnection.setRequestMethod("POST");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HttpRequest::Type::PUT:
|
|
|
|
urlConnection.setRequestMethod("PUT");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HttpRequest::Type::DELETE:
|
|
|
|
urlConnection.setRequestMethod("DELETE");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int suc = urlConnection.connect();
|
|
|
|
if (0 != suc)
|
|
|
|
{
|
|
|
|
response->setSucceed(false);
|
|
|
|
response->setErrorBuffer("connect failed");
|
2015-01-28 13:49:19 +08:00
|
|
|
response->setResponseCode(responseCode);
|
2014-12-31 10:15:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HttpRequest::Type::POST == requestType ||
|
|
|
|
HttpRequest::Type::PUT == requestType)
|
|
|
|
{
|
|
|
|
urlConnection.sendRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
responseCode = urlConnection.getResponseCode();
|
|
|
|
|
2017-07-13 09:53:54 +08:00
|
|
|
if (0 == responseCode)
|
|
|
|
{
|
|
|
|
response->setSucceed(false);
|
|
|
|
response->setErrorBuffer("connect failed");
|
|
|
|
response->setResponseCode(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
char* headers = urlConnection.getResponseHeaders();
|
|
|
|
if (nullptr != headers)
|
|
|
|
{
|
|
|
|
writeHeaderData(headers, strlen(headers), response);
|
|
|
|
}
|
|
|
|
free(headers);
|
|
|
|
|
|
|
|
//get and save cookies
|
|
|
|
char* cookiesInfo = urlConnection.getResponseHeaderByKey("set-cookie");
|
|
|
|
if (nullptr != cookiesInfo)
|
|
|
|
{
|
|
|
|
urlConnection.saveResponseCookies(cookiesInfo, strlen(cookiesInfo));
|
|
|
|
}
|
|
|
|
free(cookiesInfo);
|
|
|
|
|
|
|
|
//content len
|
|
|
|
int contentLength = urlConnection.getResponseHeaderByKeyInt("Content-Length");
|
|
|
|
char* contentInfo = urlConnection.getResponseContent(response);
|
2015-05-22 10:47:52 +08:00
|
|
|
if (nullptr != contentInfo)
|
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
std::vector<char> * recvBuffer = (std::vector<char>*)response->getResponseData();
|
|
|
|
recvBuffer->clear();
|
|
|
|
recvBuffer->insert(recvBuffer->begin(), (char*)contentInfo, ((char*)contentInfo) + urlConnection.getContentLength());
|
|
|
|
}
|
|
|
|
free(contentInfo);
|
|
|
|
|
2015-08-04 16:24:08 +08:00
|
|
|
char *messageInfo = urlConnection.getResponseMessage();
|
2017-01-19 09:32:58 +08:00
|
|
|
if (messageInfo)
|
|
|
|
{
|
|
|
|
strcpy(responseMessage, messageInfo);
|
|
|
|
free(messageInfo);
|
|
|
|
}
|
2015-08-04 16:24:08 +08:00
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
urlConnection.disconnect();
|
|
|
|
|
|
|
|
// write data to HttpResponse
|
|
|
|
response->setResponseCode(responseCode);
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if (responseCode == -1)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
response->setSucceed(false);
|
2015-05-22 10:47:52 +08:00
|
|
|
response->setErrorBuffer(responseMessage);
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response->setSucceed(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Worker thread
|
|
|
|
void HttpClient::networkThread()
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
increaseThreadCount();
|
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
HttpRequest *request;
|
|
|
|
|
|
|
|
// step 1: send http request if the requestQueue isn't empty
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
std::lock_guard<std::mutex> lock(_requestQueueMutex);
|
|
|
|
while (_requestQueue.empty()) {
|
|
|
|
_sleepCondition.wait(_requestQueueMutex);
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
2015-05-22 10:47:52 +08:00
|
|
|
request = _requestQueue.at(0);
|
|
|
|
_requestQueue.erase(0);
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if (request == _requestSentinel) {
|
2014-12-31 10:15:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a HttpResponse object, the default setting is http access failed
|
|
|
|
HttpResponse *response = new (std::nothrow) HttpResponse(request);
|
2015-05-22 10:47:52 +08:00
|
|
|
processResponse(response, _responseMessage);
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
// add response packet into queue
|
2015-05-22 10:47:52 +08:00
|
|
|
_responseQueueMutex.lock();
|
|
|
|
_responseQueue.pushBack(response);
|
|
|
|
_responseQueueMutex.unlock();
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
_schedulerMutex.lock();
|
|
|
|
if (nullptr != _scheduler)
|
|
|
|
{
|
|
|
|
_scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
2015-05-22 10:47:52 +08:00
|
|
|
_schedulerMutex.unlock();
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup: if worker thread received quit signal, clean up un-completed request queue
|
2015-05-22 10:47:52 +08:00
|
|
|
_requestQueueMutex.lock();
|
|
|
|
_requestQueue.clear();
|
|
|
|
_requestQueueMutex.unlock();
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
_responseQueueMutex.lock();
|
|
|
|
_responseQueue.clear();
|
|
|
|
_responseQueueMutex.unlock();
|
|
|
|
|
|
|
|
decreaseThreadCountAndMayDeleteThis();
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Worker thread
|
2015-02-02 11:23:15 +08:00
|
|
|
void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
increaseThreadCount();
|
|
|
|
|
|
|
|
char responseMessage[RESPONSE_BUFFER_SIZE] = { 0 };
|
2014-12-31 10:15:14 +08:00
|
|
|
processResponse(response, responseMessage);
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
_schedulerMutex.lock();
|
|
|
|
if (_scheduler != nullptr)
|
|
|
|
{
|
|
|
|
_scheduler->performFunctionInCocosThread([this, response, request]{
|
|
|
|
const ccHttpRequestCallback& callback = request->getCallback();
|
|
|
|
Ref* pTarget = request->getTarget();
|
|
|
|
SEL_HttpResponse pSelector = request->getSelector();
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if (callback != nullptr)
|
|
|
|
{
|
|
|
|
callback(this, response);
|
|
|
|
}
|
|
|
|
else if (pTarget && pSelector)
|
|
|
|
{
|
|
|
|
(pTarget->*pSelector)(this, response);
|
|
|
|
}
|
|
|
|
response->release();
|
|
|
|
// do not release in other thread
|
|
|
|
request->release();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_schedulerMutex.unlock();
|
|
|
|
decreaseThreadCountAndMayDeleteThis();
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HttpClient implementation
|
|
|
|
HttpClient* HttpClient::getInstance()
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if (_httpClient == nullptr)
|
|
|
|
{
|
|
|
|
_httpClient = new (std::nothrow) HttpClient();
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
return _httpClient;
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClient::destroyInstance()
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if (_httpClient == nullptr)
|
|
|
|
{
|
|
|
|
CCLOG("HttpClient singleton is nullptr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLOG("HttpClient::destroyInstance ...");
|
|
|
|
|
|
|
|
auto thiz = _httpClient;
|
|
|
|
_httpClient = nullptr;
|
|
|
|
|
|
|
|
thiz->_scheduler->unscheduleAllForTarget(thiz);
|
|
|
|
|
|
|
|
thiz->_schedulerMutex.lock();
|
|
|
|
thiz->_scheduler = nullptr;
|
|
|
|
thiz->_schedulerMutex.unlock();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(thiz->_requestQueueMutex);
|
|
|
|
thiz->_requestQueue.pushBack(thiz->_requestSentinel);
|
|
|
|
}
|
|
|
|
thiz->_sleepCondition.notify_one();
|
|
|
|
|
|
|
|
thiz->decreaseThreadCountAndMayDeleteThis();
|
|
|
|
CCLOG("HttpClient::destroyInstance() finished!");
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
void HttpClient::enableCookies(const char* cookieFile)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_cookieFileMutex);
|
|
|
|
if (cookieFile)
|
|
|
|
{
|
|
|
|
_cookieFilename = std::string(cookieFile);
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
2015-05-22 10:47:52 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_cookieFilename = (FileUtils::getInstance()->getWritablePath() + "cookieFile.txt");
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClient::setSSLVerification(const std::string& caFile)
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
std::lock_guard<std::mutex> lock(_sslCaFileMutex);
|
|
|
|
_sslCaFilename = caFile;
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpClient::HttpClient()
|
2017-02-04 09:46:32 +08:00
|
|
|
: _isInited(false)
|
|
|
|
, _timeoutForConnect(30)
|
2015-01-19 09:50:42 +08:00
|
|
|
, _timeoutForRead(60)
|
2015-05-22 10:47:52 +08:00
|
|
|
, _threadCount(0)
|
|
|
|
, _cookie(nullptr)
|
2017-02-04 09:46:32 +08:00
|
|
|
, _requestSentinel(new HttpRequest())
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
CCLOG("In the constructor of HttpClient!");
|
|
|
|
increaseThreadCount();
|
|
|
|
_scheduler = Director::getInstance()->getScheduler();
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpClient::~HttpClient()
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
CCLOG("In the destructor of HttpClient!");
|
2016-05-25 13:54:13 +08:00
|
|
|
CC_SAFE_RELEASE(_requestSentinel);
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Lazy create semaphore & mutex & thread
|
2017-01-18 15:40:46 +08:00
|
|
|
bool HttpClient::lazyInitThreadSemaphore()
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if (_isInited)
|
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
return true;
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-31 10:15:14 +08:00
|
|
|
auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
|
|
|
|
t.detach();
|
2015-05-22 10:47:52 +08:00
|
|
|
_isInited = true;
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add a get task to queue
|
|
|
|
void HttpClient::send(HttpRequest* request)
|
|
|
|
{
|
2017-01-18 15:40:46 +08:00
|
|
|
if (!lazyInitThreadSemaphore())
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if (nullptr == request)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->retain();
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
_requestQueueMutex.lock();
|
|
|
|
_requestQueue.pushBack(request);
|
|
|
|
_requestQueueMutex.unlock();
|
|
|
|
|
|
|
|
// Notify thread start to work
|
|
|
|
_sleepCondition.notify_one();
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClient::sendImmediate(HttpRequest* request)
|
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
if(nullptr == request)
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->retain();
|
2015-02-02 11:23:15 +08:00
|
|
|
// Create a HttpResponse object, the default setting is http access failed
|
|
|
|
HttpResponse *response = new (std::nothrow) HttpResponse(request);
|
|
|
|
|
|
|
|
auto t = std::thread(&HttpClient::networkThreadAlone, this, request, response);
|
2014-12-31 10:15:14 +08:00
|
|
|
t.detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll and notify main thread if responses exists in queue
|
|
|
|
void HttpClient::dispatchResponseCallbacks()
|
|
|
|
{
|
|
|
|
// log("CCHttpClient::dispatchResponseCallbacks is running");
|
|
|
|
//occurs when cocos thread fires but the network thread has already quited
|
|
|
|
HttpResponse* response = nullptr;
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
_responseQueueMutex.lock();
|
2014-12-31 10:15:14 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
if (!_responseQueue.empty())
|
2014-12-31 10:15:14 +08:00
|
|
|
{
|
2015-05-22 10:47:52 +08:00
|
|
|
response = _responseQueue.at(0);
|
|
|
|
_responseQueue.erase(0);
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
2015-05-22 10:47:52 +08:00
|
|
|
|
|
|
|
_responseQueueMutex.unlock();
|
2014-12-31 10:15:14 +08:00
|
|
|
|
|
|
|
if (response)
|
|
|
|
{
|
|
|
|
HttpRequest *request = response->getHttpRequest();
|
|
|
|
const ccHttpRequestCallback& callback = request->getCallback();
|
|
|
|
Ref* pTarget = request->getTarget();
|
|
|
|
SEL_HttpResponse pSelector = request->getSelector();
|
|
|
|
|
|
|
|
if (callback != nullptr)
|
|
|
|
{
|
|
|
|
callback(this, response);
|
|
|
|
}
|
|
|
|
else if (pTarget && pSelector)
|
|
|
|
{
|
|
|
|
(pTarget->*pSelector)(this, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
response->release();
|
|
|
|
// do not release in other thread
|
|
|
|
request->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
void HttpClient::increaseThreadCount()
|
|
|
|
{
|
|
|
|
_threadCountMutex.lock();
|
|
|
|
++_threadCount;
|
|
|
|
_threadCountMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClient::decreaseThreadCountAndMayDeleteThis()
|
|
|
|
{
|
|
|
|
bool needDeleteThis = false;
|
|
|
|
_threadCountMutex.lock();
|
|
|
|
--_threadCount;
|
|
|
|
if (0 == _threadCount)
|
|
|
|
{
|
|
|
|
needDeleteThis = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_threadCountMutex.unlock();
|
|
|
|
if (needDeleteThis)
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return _sslCaFilename;
|
|
|
|
}
|
|
|
|
|
2014-12-31 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|