You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
6.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
23 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "curlRequestsManager.h"
  2. #define LOG(X) m_pLog->Log("CURL_RQM", this, X)
  3. #define LOG_ERROR(X) \
  4. { \
  5. char _tBfrErrMSG2w34[1024]; \
  6. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  7. m_pLog->Log("CURL_RQM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  8. }
  9. // Constructor implementation
  10. curlRequestsManager::curlRequestsManager() :
  11. m_mPoolProtector()
  12. {
  13. m_pLog = basicLogCollection::getInstance();
  14. curl_global_init(CURL_GLOBAL_ALL);
  15. m_pCURLM = curl_multi_init();
  16. if (m_pCURLM)
  17. {
  18. init();
  19. }
  20. else
  21. {
  22. LOG_ERROR("Curl Muti Handle Cannot be created!!");
  23. };
  24. }
  25. curlRequestsManager::~curlRequestsManager()
  26. {
  27. waitThreadEnd(true);
  28. curl_multi_cleanup(m_pCURLM);
  29. }
  30. curlRequestsManager * curlRequestsManager::_instance = 0;
  31. /**
  32. * Returns the single instance of the object.
  33. */
  34. curlRequestsManager * curlRequestsManager::getInstance()
  35. {
  36. // check if the instance has been created yet
  37. if (0 == _instance)
  38. {
  39. // if not, then create it
  40. _instance = new curlRequestsManager();
  41. }
  42. // return the single instance
  43. return _instance;
  44. }
  45. void curlRequestsManager::m_fThreadProc(void)
  46. {
  47. CURLMsg * mMsg = NULL; /* for picking up messages with the transfer status */
  48. int nMsgsLeft = 0; /* how many messages are left */
  49. struct timeval mTimeOut;
  50. int RetVal; /* select() return code */
  51. CURLMcode mRetCode; /* curl_multi_fdset() return code */
  52. fd_set mReadFD;
  53. fd_set mWriteFD;
  54. fd_set mExcepFD;
  55. int mMaxFD = -1;
  56. long mCURLTimeOut;
  57. int mStillRunning;
  58. bool bProceedThread = false;
  59. #ifdef DEBUG
  60. LOG("Starting Finished Requests Reaper");
  61. #endif
  62. pthread_setname_np( pthread_self(), "cURL Manager" );
  63. // BugCurl Multi cannot starty if MR = empty ....
  64. m_bThreadStatus = TH_WORKING;
  65. while ((m_bThreadStatus < TH_ENDED) && ! bProceedThread)
  66. {
  67. SLEEP(10);
  68. m_mPoolProtector.lock();
  69. if (m_vRequestPool.size())
  70. {
  71. bProceedThread = true;
  72. }
  73. m_mPoolProtector.unlock();
  74. };
  75. #ifdef DEBUG
  76. LOG("First Req Enqueued");
  77. #endif
  78. while (m_bThreadStatus < TH_ENDED)
  79. {
  80. /* we start some action by calling perform right away */
  81. curl_multi_perform(m_pCURLM, &mStillRunning);
  82. do
  83. {
  84. curl_multi_timeout(m_pCURLM, &mCURLTimeOut);
  85. if (mCURLTimeOut < 0)
  86. {
  87. mCURLTimeOut = 1000;
  88. }
  89. mTimeOut.tv_sec = mCURLTimeOut / 1000;
  90. mTimeOut.tv_usec = (mCURLTimeOut % 1000) * 1000;
  91. FD_ZERO(&mReadFD);
  92. FD_ZERO(&mWriteFD);
  93. FD_ZERO(&mExcepFD);
  94. /* get file descriptors from the transfers */
  95. mRetCode = curl_multi_fdset(m_pCURLM, &mReadFD, &mWriteFD, &mExcepFD, &mMaxFD);
  96. if (mRetCode != CURLM_OK)
  97. {
  98. char tT[256];
  99. sprintf(tT, "curl_multi_fdset() failed, code %d!", mRetCode);
  100. LOG_ERROR(tT);
  101. break;
  102. };
  103. if (mMaxFD == -1)
  104. {
  105. SLEEP(10);
  106. RetVal = 0;
  107. break;
  108. }
  109. else
  110. {
  111. RetVal = select(mMaxFD + 1, &mReadFD, &mWriteFD, &mExcepFD, &mTimeOut);
  112. };
  113. switch (RetVal)
  114. {
  115. case -1:
  116. /* select error */
  117. break;
  118. case 0:
  119. default:
  120. /* timeout or readable/writable sockets */
  121. curl_multi_perform(m_pCURLM, &mStillRunning);
  122. break;
  123. };
  124. /* See how the transfers went */
  125. while ((mMsg = curl_multi_info_read(m_pCURLM, &nMsgsLeft)) && (m_bThreadStatus < TH_ENDED))
  126. {
  127. if (mMsg->msg == CURLMSG_DONE)
  128. {
  129. CURLcode rC = mMsg->data.result;
  130. #ifdef DEBUG_EXTRA
  131. char tB[256];
  132. sprintf(tB, "Request Done with Code: %d", rC);
  133. LOG(tB);
  134. #endif
  135. /* Find out which handle this message is about */
  136. m_mPoolProtector.lock();
  137. for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end();)
  138. {
  139. if ((*index)->getEH() == mMsg->easy_handle)
  140. {
  141. mRetCode = curl_multi_remove_handle(m_pCURLM, (*index)->getEH());
  142. if (rC == CURLE_OK)
  143. {
  144. (*index)->issue();
  145. };
  146. // CURLE_OPERATION_TIMEDOUT
  147. delete(*index);
  148. m_vRequestPool.erase(index);
  149. break;
  150. }
  151. else
  152. {
  153. index++;
  154. };
  155. };
  156. m_mPoolProtector.unlock();
  157. };
  158. };
  159. }
  160. while (mStillRunning && (m_bThreadStatus < TH_ENDED)); /* if there are still transfers, loop! */
  161. /* call curl_multi_perform or curl_multi_socket_action first, then loop
  162. through and check if there are any transfers that have completed */
  163. /* See how the transfers went */
  164. /*
  165. while( ( mMsg = curl_multi_info_read( m_pCURLM, &nMsgsLeft ) ) && ( m_bThreadStatus < TH_ENDED ) ){
  166. char tB[129];
  167. printf( tB, "[%d] Msgs Left", nMsgsLeft);
  168. LOG( tB );
  169. if( mMsg->msg == CURLMSG_DONE ){
  170. // Find out which handle this message is about
  171. LOG("REQ -> Done!");
  172. m_mPoolProtector.lock();
  173. for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end(); index++){
  174. if ((*index)->getEH() == mMsg->easy_handle){
  175. mRetCode = curl_multi_remove_handle( m_pCURLM, (*index)->getEH());
  176. (*index)->issue();
  177. delete( (*index) );
  178. m_vRequestPool.erase( index );
  179. break;
  180. };
  181. };
  182. m_mPoolProtector.unlock();
  183. };
  184. };
  185. */
  186. SLEEP(10);
  187. };
  188. #ifdef DEBUG
  189. LOG("Ending Finished Requests Reaper");
  190. #endif
  191. }
  192. bool curlRequestsManager::accomplish(curlRequest * RC)
  193. {
  194. if (RC)
  195. {
  196. #ifdef DEBUG_EXTRA
  197. LOG("New Request");
  198. #endif
  199. CURLMcode RetCode = curl_multi_add_handle(m_pCURLM, RC->getEH());
  200. if (RetCode == CURLM_OK)
  201. {
  202. m_mPoolProtector.lock();
  203. m_vRequestPool.push_back(RC);
  204. m_mPoolProtector.unlock();
  205. #ifdef DEBUG_EXTRA
  206. LOG("Added OK");
  207. #endif
  208. return true;
  209. }
  210. else
  211. {
  212. LOG_ERROR(curl_multi_strerror(RetCode));
  213. };
  214. // The manager owns the request: on success it is deleted when completed,
  215. // on failure it must be deleted here (no caller uses it afterwards).
  216. delete (RC);
  217. };
  218. return false;
  219. }
  220. bool curlRequestsManager::get(CSTR URL, CSTR contentType)
  221. {
  222. curlRequest * tRC;
  223. tRC = new curlRequest(URL, contentType);
  224. if (tRC != NULL)
  225. {
  226. #ifdef DEBUG_EXTRA
  227. LOG("Adding GET RQ");
  228. #endif
  229. return accomplish(tRC);
  230. };
  231. return false;
  232. }
  233. bool curlRequestsManager::post(CSTR URL, CSTR postData, CSTR contentType)
  234. {
  235. curlRequest * tRC;
  236. tRC = new curlRequest(URL, contentType);
  237. if (tRC != NULL)
  238. {
  239. #ifdef DEBUG_EXTRA
  240. LOG("Adding POST RQ");
  241. #endif
  242. tRC->setPOST(postData, strlen(postData));
  243. return accomplish(tRC);
  244. };
  245. return false;
  246. }
  247. #undef LOG
  248. #undef LOG_ERROR