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

Method Update

src/engine/client/serverbrowser_http.cpp:361–414  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

359}
360
361void CServerBrowserHttp::Update()
362{
363 if(m_State == STATE_WANTREFRESH)
364 {
365 const char *pBestUrl;
366 if(m_pChooseMaster->GetBestUrl(&pBestUrl))
367 {
368 if(!m_pChooseMaster->IsRefreshing())
369 {
370 log_error("serverbrowser_http", "no working serverlist URL found");
371 m_State = STATE_NO_MASTER;
372 }
373 return;
374 }
375 m_pGetServers = HttpGet(pBestUrl);
376 // 10 seconds connection timeout, lower than 8KB/s for 10 seconds to fail.
377 m_pGetServers->Timeout(CTimeout{10000, 0, 8000, 10});
378 m_pHttp->Run(m_pGetServers);
379 m_State = STATE_REFRESHING;
380 }
381 else if(m_State == STATE_REFRESHING)
382 {
383 if(!m_pGetServers->Done())
384 {
385 return;
386 }
387 m_State = STATE_DONE;
388 std::shared_ptr<CHttpRequest> pGetServers = nullptr;
389 std::swap(m_pGetServers, pGetServers);
390
391 bool Success = true;
392 json_value *pJson = pGetServers->State() == EHttpState::DONE ? pGetServers->ResultJson() : nullptr;
393 Success = Success && pJson;
394 Success = Success && !Parse(pJson, &m_vServers);
395 json_value_free(pJson);
396 if(!Success)
397 {
398 log_error("serverbrowser_http", "failed getting serverlist, trying to find best URL");
399 m_pChooseMaster->Reset();
400 m_pChooseMaster->Refresh();
401 }
402 else
403 {
404 // Try to find new master if the current one returns
405 // results that are 5 minutes old.
406 int Age = SanitizeAge(pGetServers->ResultAgeSeconds());
407 if(Age > 300)
408 {
409 log_info("serverbrowser_http", "got stale serverlist, age=%ds, trying to find best URL", Age);
410 m_pChooseMaster->Refresh();
411 }
412 }
413 }
414}
415void CServerBrowserHttp::Refresh()
416{
417 if(m_State == STATE_WANTREFRESH || m_State == STATE_REFRESHING || m_State == STATE_NO_MASTER)

Callers

nothing calls this directly

Calls 13

HttpGetFunction · 0.85
json_value_freeFunction · 0.85
SanitizeAgeFunction · 0.85
TimeoutMethod · 0.80
ResultJsonMethod · 0.80
ResultAgeSecondsMethod · 0.80
GetBestUrlMethod · 0.45
IsRefreshingMethod · 0.45
RunMethod · 0.45
DoneMethod · 0.45
StateMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected