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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCHTTPREQUEST_H__
|
|
|
|
#define __CCHTTPREQUEST_H__
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "network/HttpRequest.h"
|
|
|
|
#include "network/HttpResponse.h"
|
|
|
|
#include "network/HttpClient.h"
|
2012-08-07 18:28:07 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Network
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief Singleton that handles asynchrounous http requests
|
|
|
|
* Once the request completed, a callback will issued in main thread when it provided during make request
|
|
|
|
*/
|
2014-08-22 13:42:46 +08:00
|
|
|
class CC_DLL HttpClient
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Return the shared instance **/
|
2013-06-20 14:15:53 +08:00
|
|
|
static HttpClient *getInstance();
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
/** Relase the shared instance **/
|
|
|
|
static void destroyInstance();
|
2013-07-10 11:37:39 +08:00
|
|
|
|
|
|
|
/** Enable cookie support. **/
|
2013-07-10 15:46:01 +08:00
|
|
|
void enableCookies(const char* cookieFile);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a get request to task queue
|
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
|
|
|
|
* @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);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the connect timeout
|
2013-08-01 21:40:13 +08:00
|
|
|
* @param value The desired timeout.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
|
|
|
inline void setTimeoutForConnect(int value) {_timeoutForConnect = value;};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get connect timeout
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
inline int getTimeoutForConnect() {return _timeoutForConnect;}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the download timeout
|
2012-11-25 23:16:42 +08:00
|
|
|
* @param value
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
|
|
|
inline void setTimeoutForRead(int value) {_timeoutForRead = value;};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get download timeout
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
inline int getTimeoutForRead() {return _timeoutForRead;};
|
|
|
|
|
|
|
|
private:
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient();
|
|
|
|
virtual ~HttpClient();
|
2012-08-07 18:28:07 +08:00
|
|
|
bool init(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init pthread mutex, semaphore, and create new thread for http requests
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
bool lazyInitThreadSemphore();
|
2013-12-10 15:08:20 +08:00
|
|
|
void networkThread();
|
2014-05-20 20:08:26 +08:00
|
|
|
void networkThreadAlone(HttpRequest* request);
|
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();
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
int _timeoutForConnect;
|
|
|
|
int _timeoutForRead;
|
|
|
|
};
|
|
|
|
|
|
|
|
// end of Network group
|
|
|
|
/// @}
|
|
|
|
|
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
|
|
|
#endif //__CCHTTPREQUEST_H__
|