HttpClient.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /****************************************************************************
  2. Copyright (c) 2012 greathqy
  3. Copyright (c) 2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CCHTTPCLIENT_H__
  24. #define __CCHTTPCLIENT_H__
  25. #include <thread>
  26. #include <condition_variable>
  27. #include "base/CCVector.h"
  28. #include "base/CCScheduler.h"
  29. #include "network/HttpRequest.h"
  30. #include "network/HttpResponse.h"
  31. #include "network/HttpCookie.h"
  32. /**
  33. * @addtogroup network
  34. * @{
  35. */
  36. NS_CC_BEGIN
  37. namespace network {
  38. /** Singleton that handles asynchronous http requests.
  39. *
  40. * Once the request completed, a callback will issued in main thread when it provided during make request.
  41. *
  42. * @lua NA
  43. */
  44. class CC_DLL HttpClient
  45. {
  46. public:
  47. /**
  48. * The buffer size of _responseMessage
  49. */
  50. static const int RESPONSE_BUFFER_SIZE = 256;
  51. /**
  52. * Get instance of HttpClient.
  53. *
  54. * @return the instance of HttpClient.
  55. */
  56. static HttpClient *getInstance();
  57. /**
  58. * Release the instance of HttpClient.
  59. */
  60. static void destroyInstance();
  61. /**
  62. * Enable cookie support.
  63. *
  64. * @param cookieFile the filepath of cookie file.
  65. */
  66. void enableCookies(const char* cookieFile);
  67. /**
  68. * Get the cookie filename
  69. *
  70. * @return the cookie filename
  71. */
  72. const std::string& getCookieFilename();
  73. /**
  74. * Set root certificate path for SSL verification.
  75. *
  76. * @param caFile a full path of root certificate.if it is empty, SSL verification is disabled.
  77. */
  78. void setSSLVerification(const std::string& caFile);
  79. /**
  80. * Get the ssl CA filename
  81. *
  82. * @return the ssl CA filename
  83. */
  84. const std::string& getSSLVerification();
  85. /**
  86. * Add a get request to task queue
  87. *
  88. * @param request a HttpRequest object, which includes url, response callback etc.
  89. please make sure request->_requestData is clear before calling "send" here.
  90. */
  91. void send(HttpRequest* request);
  92. /**
  93. * Immediate send a request
  94. *
  95. * @param request a HttpRequest object, which includes url, response callback etc.
  96. please make sure request->_requestData is clear before calling "sendImmediate" here.
  97. */
  98. void sendImmediate(HttpRequest* request);
  99. /**
  100. * Set the timeout value for connecting.
  101. *
  102. * @param value the timeout value for connecting.
  103. */
  104. void setTimeoutForConnect(int value);
  105. /**
  106. * Get the timeout value for connecting.
  107. *
  108. * @return int the timeout value for connecting.
  109. */
  110. int getTimeoutForConnect();
  111. /**
  112. * Set the timeout value for reading.
  113. *
  114. * @param value the timeout value for reading.
  115. */
  116. void setTimeoutForRead(int value);
  117. /**
  118. * Get the timeout value for reading.
  119. *
  120. * @return int the timeout value for reading.
  121. */
  122. int getTimeoutForRead();
  123. HttpCookie* getCookie() const {return _cookie; }
  124. std::mutex& getCookieFileMutex() {return _cookieFileMutex;}
  125. std::mutex& getSSLCaFileMutex() {return _sslCaFileMutex;}
  126. private:
  127. HttpClient();
  128. virtual ~HttpClient();
  129. bool init();
  130. /**
  131. * Init pthread mutex, semaphore, and create new thread for http requests
  132. * @return bool
  133. */
  134. bool lazyInitThreadSemaphore();
  135. void networkThread();
  136. void networkThreadAlone(HttpRequest* request, HttpResponse* response);
  137. /** Poll function called from main thread to dispatch callbacks when http requests finished **/
  138. void dispatchResponseCallbacks();
  139. void processResponse(HttpResponse* response, char* responseMessage);
  140. void increaseThreadCount();
  141. void decreaseThreadCountAndMayDeleteThis();
  142. private:
  143. bool _isInited;
  144. int _timeoutForConnect;
  145. std::mutex _timeoutForConnectMutex;
  146. int _timeoutForRead;
  147. std::mutex _timeoutForReadMutex;
  148. int _threadCount;
  149. std::mutex _threadCountMutex;
  150. Scheduler* _scheduler;
  151. std::mutex _schedulerMutex;
  152. Vector<HttpRequest*> _requestQueue;
  153. std::mutex _requestQueueMutex;
  154. Vector<HttpResponse*> _responseQueue;
  155. std::mutex _responseQueueMutex;
  156. std::string _cookieFilename;
  157. std::mutex _cookieFileMutex;
  158. std::string _sslCaFilename;
  159. std::mutex _sslCaFileMutex;
  160. HttpCookie* _cookie;
  161. std::condition_variable_any _sleepCondition;
  162. char _responseMessage[RESPONSE_BUFFER_SIZE];
  163. HttpRequest* _requestSentinel;
  164. };
  165. } // namespace network
  166. NS_CC_END
  167. // end group
  168. /// @}
  169. #endif //__CCHTTPCLIENT_H__