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
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
http://www.cocos2d-x.org
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
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:
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
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-03-27 18:26:14 +08:00
|
|
|
#ifndef __CCHTTPCLIENT_H__
|
|
|
|
#define __CCHTTPCLIENT_H__
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include "base/CCVector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "network/HttpRequest.h"
|
|
|
|
#include "network/HttpResponse.h"
|
2015-05-22 10:47:52 +08:00
|
|
|
#include "network/HttpCookie.h"
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
/**
|
2015-05-28 15:46:36 +08:00
|
|
|
* @addtogroup network
|
2012-08-07 18:28:07 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace network {
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2016-01-11 18:15:34 +08:00
|
|
|
/** Singleton that handles asynchronous http requests.
|
2015-03-19 16:45:53 +08:00
|
|
|
*
|
|
|
|
* Once the request completed, a callback will issued in main thread when it provided during make request.
|
|
|
|
*
|
|
|
|
* @lua NA
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2014-08-22 13:42:46 +08:00
|
|
|
class CC_DLL HttpClient
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2016-05-18 12:54:16 +08:00
|
|
|
/**
|
|
|
|
* The buffer size of _responseMessage
|
|
|
|
*/
|
|
|
|
static const int RESPONSE_BUFFER_SIZE = 256;
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* Get instance of HttpClient.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return the instance of HttpClient.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
static HttpClient *getInstance();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the instance of HttpClient.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2012-08-07 18:28:07 +08:00
|
|
|
static void destroyInstance();
|
2013-07-10 11:37:39 +08:00
|
|
|
|
2016-05-18 12:54:16 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Enable cookie support.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param cookieFile the filepath of cookie file.
|
|
|
|
*/
|
2013-07-10 15:46:01 +08:00
|
|
|
void enableCookies(const char* cookieFile);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
/**
|
|
|
|
* Get the cookie filename
|
2016-05-18 12:54:16 +08:00
|
|
|
*
|
2015-05-22 10:47:52 +08:00
|
|
|
* @return the cookie filename
|
|
|
|
*/
|
|
|
|
const std::string& getCookieFilename();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-09-13 23:28:11 +08:00
|
|
|
/**
|
|
|
|
* Set root certificate path for SSL verification.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param caFile a full path of root certificate.if it is empty, SSL verification is disabled.
|
2014-09-13 23:28:11 +08:00
|
|
|
*/
|
|
|
|
void setSSLVerification(const std::string& caFile);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
/**
|
2016-01-11 18:15:34 +08:00
|
|
|
* Get the ssl CA filename
|
2016-05-18 12:54:16 +08:00
|
|
|
*
|
2015-05-22 10:47:52 +08:00
|
|
|
* @return the ssl CA filename
|
|
|
|
*/
|
|
|
|
const std::string& getSSLVerification();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
|
|
|
* Add a get request to task queue
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2013-06-20 14:15:53 +08:00
|
|
|
* @param request a HttpRequest object, which includes url, response callback etc.
|
2012-09-10 23:17:16 +08:00
|
|
|
please make sure request->_requestData is clear before calling "send" here.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void send(HttpRequest* request);
|
2014-05-20 20:08:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Immediate send a request
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2014-05-20 20:08:26 +08:00
|
|
|
* @param request a HttpRequest object, which includes url, response callback etc.
|
2014-06-22 11:39:39 +08:00
|
|
|
please make sure request->_requestData is clear before calling "sendImmediate" here.
|
2014-05-20 20:08:26 +08:00
|
|
|
*/
|
2014-06-22 11:39:39 +08:00
|
|
|
void sendImmediate(HttpRequest* request);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set the timeout value for connecting.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param value the timeout value for connecting.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
void setTimeoutForConnect(int value);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the timeout value for connecting.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return int the timeout value for connecting.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
int getTimeoutForConnect();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set the timeout value for reading.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param value the timeout value for reading.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
void setTimeoutForRead(int value);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the timeout value for reading.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return int the timeout value for reading.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
int getTimeoutForRead();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpCookie* getCookie() const {return _cookie; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::mutex& getCookieFileMutex() {return _cookieFileMutex;}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::mutex& getSSLCaFileMutex() {return _sslCaFileMutex;}
|
2012-08-07 18:28:07 +08:00
|
|
|
private:
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient();
|
|
|
|
virtual ~HttpClient();
|
2016-05-18 12:54:16 +08:00
|
|
|
bool init();
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
|
|
|
* Init pthread mutex, semaphore, and create new thread for http requests
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-18 15:40:46 +08:00
|
|
|
bool lazyInitThreadSemaphore();
|
2013-12-10 15:08:20 +08:00
|
|
|
void networkThread();
|
2015-02-02 11:23:15 +08:00
|
|
|
void networkThreadAlone(HttpRequest* request, HttpResponse* response);
|
2012-08-07 18:28:07 +08:00
|
|
|
/** Poll function called from main thread to dispatch callbacks when http requests finished **/
|
2013-12-10 15:08:20 +08:00
|
|
|
void dispatchResponseCallbacks();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
void processResponse(HttpResponse* response, char* responseMessage);
|
|
|
|
void increaseThreadCount();
|
|
|
|
void decreaseThreadCountAndMayDeleteThis();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
private:
|
2015-05-22 10:47:52 +08:00
|
|
|
bool _isInited;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
int _timeoutForConnect;
|
2015-05-22 10:47:52 +08:00
|
|
|
std::mutex _timeoutForConnectMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
int _timeoutForRead;
|
2015-05-22 10:47:52 +08:00
|
|
|
std::mutex _timeoutForReadMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
int _threadCount;
|
|
|
|
std::mutex _threadCountMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
Scheduler* _scheduler;
|
|
|
|
std::mutex _schedulerMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
Vector<HttpRequest*> _requestQueue;
|
|
|
|
std::mutex _requestQueueMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
Vector<HttpResponse*> _responseQueue;
|
|
|
|
std::mutex _responseQueueMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::string _cookieFilename;
|
|
|
|
std::mutex _cookieFileMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::string _sslCaFilename;
|
|
|
|
std::mutex _sslCaFileMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpCookie* _cookie;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::condition_variable_any _sleepCondition;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
char _responseMessage[RESPONSE_BUFFER_SIZE];
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpRequest* _requestSentinel;
|
2012-08-07 18:28:07 +08:00
|
|
|
};
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
} // namespace network
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
#endif //__CCHTTPCLIENT_H__
|
2016-05-18 12:54:16 +08:00
|
|
|
|