MCPcopy Create free account
hub / github.com/ddnet/ddnet / Run

Method Run

src/engine/client/serverbrowser_http.cpp:189–303  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

187}
188
189void CChooseMaster::CJob::Run()
190{
191 // Check masters in a random order.
192 int aRandomized[MAX_URLS] = {0};
193 for(int i = 0; i < m_pData->m_NumUrls; i++)
194 {
195 aRandomized[i] = i;
196 }
197 // https://en.wikipedia.org/w/index.php?title=Fisher%E2%80%93Yates_shuffle&oldid=1002922479#The_modern_algorithm
198 // The equivalent version.
199 for(int i = 0; i <= m_pData->m_NumUrls - 2; i++)
200 {
201 int j = i + secure_rand_below(m_pData->m_NumUrls - i);
202 std::swap(aRandomized[i], aRandomized[j]);
203 }
204 // Do a HEAD request to ensure that a connection is established and
205 // then do a GET request to check how fast we can get the server list.
206 //
207 // 10 seconds connection timeout, lower than 8KB/s for 10 seconds to
208 // fail.
209 CTimeout Timeout{10000, 0, 8000, 10};
210 int aTimeMs[MAX_URLS];
211 int aAgeS[MAX_URLS];
212 for(int i = 0; i < m_pData->m_NumUrls; i++)
213 {
214 aTimeMs[i] = -1;
215 aAgeS[i] = SanitizeAge({});
216 const char *pUrl = m_pData->m_aaUrls[aRandomized[i]];
217 std::shared_ptr<CHttpRequest> pHead = HttpHead(pUrl);
218 pHead->Timeout(Timeout);
219 pHead->LogProgress(HTTPLOG::FAILURE);
220 {
221 const CLockScope LockScope(m_Lock);
222 m_pHead = pHead;
223 }
224
225 m_pParent->m_pHttp->Run(pHead);
226 pHead->Wait();
227 if(pHead->State() == EHttpState::ABORTED || State() == IJob::STATE_ABORTED)
228 {
229 log_debug("serverbrowser_http", "master chooser aborted");
230 return;
231 }
232 if(pHead->State() != EHttpState::DONE)
233 {
234 continue;
235 }
236
237 auto StartTime = time_get_nanoseconds();
238 std::shared_ptr<CHttpRequest> pGet = HttpGet(pUrl);
239 pGet->Timeout(Timeout);
240 pGet->LogProgress(HTTPLOG::FAILURE);
241 {
242 const CLockScope LockScope(m_Lock);
243 m_pGet = pGet;
244 }
245
246 m_pParent->m_pHttp->Run(pGet);

Callers 1

UpdateMethod · 0.45

Calls 13

secure_rand_belowFunction · 0.85
SanitizeAgeFunction · 0.85
HttpHeadFunction · 0.85
time_get_nanosecondsFunction · 0.85
HttpGetFunction · 0.85
json_value_freeFunction · 0.85
ClassifyAgeFunction · 0.85
TimeoutMethod · 0.80
LogProgressMethod · 0.80
ResultJsonMethod · 0.80
ResultAgeSecondsMethod · 0.80
WaitMethod · 0.45

Tested by

no test coverage detected