MCPcopy Create free account
hub / github.com/cocos/cocos-engine / networkThread

Method networkThread

native/cocos/network/HttpClient.cpp:83–133  ·  view source on GitHub ↗

Worker thread

Source from the content-addressed store, hash-verified

81
82// Worker thread
83void 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
136void HttpClient::networkThreadAlone(HttpRequest *request, HttpResponse *response) {

Callers

nothing calls this directly

Calls 8

HttpResponseClass · 0.85
lockMethod · 0.80
clearMethod · 0.65
emptyMethod · 0.45
waitMethod · 0.45
eraseMethod · 0.45
addRefMethod · 0.45

Tested by

no test coverage detected