Worker thread
| 81 | |
| 82 | // Worker thread |
| 83 | void HttpClient::networkThread() { |
| 84 | increaseThreadCount(); |
| 85 | |
| 86 | while (true) { |
| 87 | HttpRequest *request; |
| 88 | |
| 89 | // step 1: send http request if the requestQueue isn't empty |
| 90 | { |
| 91 | std::lock_guard<std::mutex> lock(_requestQueueMutex); |
| 92 | while (_requestQueue.empty()) { |
| 93 | _sleepCondition.wait(_requestQueueMutex); |
| 94 | } |
| 95 | request = _requestQueue.at(0); |
| 96 | _requestQueue.erase(0); |
| 97 | } |
| 98 | |
| 99 | if (request == _requestSentinel) { |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | // step 2: libcurl sync access |
| 104 | |
| 105 | // Create a HttpResponse object, the default setting is http access failed |
| 106 | HttpResponse *response = ccnew HttpResponse(request); |
| 107 | response->addRef(); // NOTE: RefCounted object's reference count is changed to 0 now. so needs to addRef after ccnew. |
| 108 | |
| 109 | processResponse(response, _responseMessage); |
| 110 | |
| 111 | // add response packet into queue |
| 112 | _responseQueueMutex.lock(); |
| 113 | _responseQueue.pushBack(response); |
| 114 | _responseQueueMutex.unlock(); |
| 115 | |
| 116 | _schedulerMutex.lock(); |
| 117 | if (auto sche = _scheduler.lock()) { |
| 118 | sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); |
| 119 | } |
| 120 | _schedulerMutex.unlock(); |
| 121 | } |
| 122 | |
| 123 | // cleanup: if worker thread received quit signal, clean up un-completed request queue |
| 124 | _requestQueueMutex.lock(); |
| 125 | _requestQueue.clear(); |
| 126 | _requestQueueMutex.unlock(); |
| 127 | |
| 128 | _responseQueueMutex.lock(); |
| 129 | _responseQueue.clear(); |
| 130 | _responseQueueMutex.unlock(); |
| 131 | |
| 132 | decreaseThreadCountAndMayDeleteThis(); |
| 133 | } |
| 134 | |
| 135 | // Worker thread |
| 136 | void HttpClient::networkThreadAlone(HttpRequest *request, HttpResponse *response) { |
nothing calls this directly
no test coverage detected