| 359 | } |
| 360 | |
| 361 | void 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 | } |
| 415 | void CServerBrowserHttp::Refresh() |
| 416 | { |
| 417 | if(m_State == STATE_WANTREFRESH || m_State == STATE_REFRESHING || m_State == STATE_NO_MASTER) |
nothing calls this directly
no test coverage detected