2018-07-11 20:46:04 +00:00
|
|
|
#include "serverbrowser_http.h"
|
|
|
|
|
|
|
|
#include "http.h"
|
|
|
|
|
2021-05-13 00:46:28 +00:00
|
|
|
#include <engine/console.h>
|
2018-07-11 20:46:04 +00:00
|
|
|
#include <engine/engine.h>
|
|
|
|
#include <engine/external/json-parser/json.h>
|
|
|
|
#include <engine/serverbrowser.h>
|
2021-05-13 00:46:28 +00:00
|
|
|
#include <engine/shared/linereader.h>
|
2018-07-11 20:46:04 +00:00
|
|
|
#include <engine/shared/serverinfo.h>
|
2021-05-13 00:46:28 +00:00
|
|
|
#include <engine/storage.h>
|
2018-07-11 20:46:04 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class CChooseMaster
|
|
|
|
{
|
|
|
|
public:
|
2021-05-12 17:09:16 +00:00
|
|
|
typedef bool (*VALIDATOR)(json_value *pJson);
|
|
|
|
|
2018-07-11 20:46:04 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAX_URLS = 16,
|
|
|
|
};
|
2021-05-12 17:09:16 +00:00
|
|
|
CChooseMaster(IEngine *pEngine, VALIDATOR pfnValidator, const char **ppUrls, int NumUrls, int PreviousBestIndex);
|
2018-07-11 20:46:04 +00:00
|
|
|
virtual ~CChooseMaster() {}
|
|
|
|
|
2021-05-13 00:46:28 +00:00
|
|
|
bool GetBestUrl(const char **pBestUrl) const;
|
|
|
|
void Reset();
|
|
|
|
bool IsRefreshing() const { return m_pJob && m_pJob->Status() != IJob::STATE_DONE; }
|
2018-07-11 20:46:04 +00:00
|
|
|
void Refresh();
|
|
|
|
|
|
|
|
private:
|
2021-05-13 00:46:28 +00:00
|
|
|
int GetBestIndex() const;
|
|
|
|
|
2018-07-11 20:46:04 +00:00
|
|
|
class CData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::atomic_int m_BestIndex{-1};
|
|
|
|
// Constant after construction.
|
2021-05-12 17:09:16 +00:00
|
|
|
VALIDATOR m_pfnValidator;
|
2018-07-11 20:46:04 +00:00
|
|
|
int m_NumUrls;
|
|
|
|
char m_aaUrls[MAX_URLS][256];
|
|
|
|
};
|
|
|
|
class CJob : public IJob
|
|
|
|
{
|
|
|
|
std::shared_ptr<CData> m_pData;
|
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
public:
|
|
|
|
CJob(std::shared_ptr<CData> pData) :
|
2021-05-13 23:25:59 +00:00
|
|
|
m_pData(std::move(pData)) {}
|
2018-07-11 20:46:04 +00:00
|
|
|
virtual ~CJob() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
IEngine *m_pEngine;
|
|
|
|
int m_PreviousBestIndex;
|
|
|
|
std::shared_ptr<CData> m_pData;
|
2021-05-13 00:46:28 +00:00
|
|
|
std::shared_ptr<CJob> m_pJob;
|
2018-07-11 20:46:04 +00:00
|
|
|
};
|
|
|
|
|
2021-05-12 17:09:16 +00:00
|
|
|
CChooseMaster::CChooseMaster(IEngine *pEngine, VALIDATOR pfnValidator, const char **ppUrls, int NumUrls, int PreviousBestIndex) :
|
2018-07-11 20:46:04 +00:00
|
|
|
m_pEngine(pEngine),
|
|
|
|
m_PreviousBestIndex(PreviousBestIndex)
|
|
|
|
{
|
|
|
|
dbg_assert(NumUrls >= 0, "no master URLs");
|
|
|
|
dbg_assert(NumUrls <= MAX_URLS, "too many master URLs");
|
|
|
|
dbg_assert(PreviousBestIndex >= -1, "previous best index negative and not -1");
|
|
|
|
dbg_assert(PreviousBestIndex < NumUrls, "previous best index too high");
|
|
|
|
m_pData = std::make_shared<CData>();
|
2021-05-12 17:09:16 +00:00
|
|
|
m_pData->m_pfnValidator = pfnValidator;
|
2018-07-11 20:46:04 +00:00
|
|
|
m_pData->m_NumUrls = NumUrls;
|
|
|
|
for(int i = 0; i < m_pData->m_NumUrls; i++)
|
|
|
|
{
|
|
|
|
str_copy(m_pData->m_aaUrls[i], ppUrls[i], sizeof(m_pData->m_aaUrls[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CChooseMaster::GetBestIndex() const
|
|
|
|
{
|
|
|
|
int BestIndex = m_pData->m_BestIndex.load();
|
|
|
|
if(BestIndex >= 0)
|
|
|
|
{
|
|
|
|
return BestIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_PreviousBestIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:46:28 +00:00
|
|
|
bool CChooseMaster::GetBestUrl(const char **ppBestUrl) const
|
|
|
|
{
|
|
|
|
int Index = GetBestIndex();
|
|
|
|
if(Index < 0)
|
|
|
|
{
|
|
|
|
*ppBestUrl = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*ppBestUrl = m_pData->m_aaUrls[Index];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CChooseMaster::Reset()
|
2018-07-11 20:46:04 +00:00
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
m_PreviousBestIndex = -1;
|
|
|
|
m_pData->m_BestIndex.store(-1);
|
2018-07-11 20:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CChooseMaster::Refresh()
|
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
m_pEngine->AddJob(m_pJob = std::make_shared<CJob>(m_pData));
|
2018-07-11 20:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CChooseMaster::CJob::Run()
|
|
|
|
{
|
|
|
|
// Check masters in a random order.
|
2021-05-13 23:25:59 +00:00
|
|
|
int aRandomized[MAX_URLS] = {0};
|
2018-07-11 20:46:04 +00:00
|
|
|
for(int i = 0; i < m_pData->m_NumUrls; i++)
|
|
|
|
{
|
|
|
|
aRandomized[i] = i;
|
|
|
|
}
|
|
|
|
// https://en.wikipedia.org/w/index.php?title=Fisher%E2%80%93Yates_shuffle&oldid=1002922479#The_modern_algorithm
|
|
|
|
// The equivalent version.
|
|
|
|
for(int i = 0; i <= m_pData->m_NumUrls - 2; i++)
|
|
|
|
{
|
|
|
|
int j = i + secure_rand_below(m_pData->m_NumUrls - i);
|
|
|
|
std::swap(aRandomized[i], aRandomized[j]);
|
|
|
|
}
|
|
|
|
// Do a HEAD request to ensure that a connection is established and
|
|
|
|
// then do a GET request to check how fast we can get the server list.
|
|
|
|
//
|
|
|
|
// 10 seconds connection timeout, lower than 8KB/s for 10 seconds to
|
|
|
|
// fail.
|
|
|
|
CTimeout Timeout{10000, 8000, 10};
|
|
|
|
int aTimeMs[MAX_URLS];
|
|
|
|
for(int i = 0; i < m_pData->m_NumUrls; i++)
|
|
|
|
{
|
|
|
|
aTimeMs[i] = -1;
|
|
|
|
const char *pUrl = m_pData->m_aaUrls[aRandomized[i]];
|
2021-05-28 10:18:53 +00:00
|
|
|
CHead Head(pUrl, Timeout, false);
|
2018-07-11 20:46:04 +00:00
|
|
|
IEngine::RunJobBlocking(&Head);
|
|
|
|
if(Head.State() != HTTP_DONE)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int64 StartTime = time_get();
|
2021-05-28 10:18:53 +00:00
|
|
|
CGet Get(pUrl, Timeout, false);
|
2018-07-11 20:46:04 +00:00
|
|
|
IEngine::RunJobBlocking(&Get);
|
|
|
|
int Time = (time_get() - StartTime) * 1000 / time_freq();
|
|
|
|
if(Get.State() != HTTP_DONE)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-12 17:09:16 +00:00
|
|
|
json_value *pJson = Get.ResultJson();
|
|
|
|
if(!pJson)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool ParseFailure = m_pData->m_pfnValidator(pJson);
|
|
|
|
json_value_free(pJson);
|
|
|
|
if(ParseFailure)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-11 20:46:04 +00:00
|
|
|
dbg_msg("serverbrowse_http", "found master, url='%s' time=%dms", pUrl, Time);
|
|
|
|
aTimeMs[i] = Time;
|
|
|
|
}
|
|
|
|
// Determine index of the minimum time.
|
|
|
|
int BestIndex = -1;
|
|
|
|
int BestTime = 0;
|
|
|
|
for(int i = 0; i < m_pData->m_NumUrls; i++)
|
|
|
|
{
|
|
|
|
if(aTimeMs[i] < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(BestIndex == -1 || aTimeMs[i] < BestTime)
|
|
|
|
{
|
|
|
|
BestTime = aTimeMs[i];
|
|
|
|
BestIndex = aRandomized[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(BestIndex == -1)
|
|
|
|
{
|
|
|
|
dbg_msg("serverbrowse_http", "WARNING: no usable masters found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbg_msg("serverbrowse_http", "determined best master, url='%s' time=%dms", m_pData->m_aaUrls[BestIndex], BestTime);
|
|
|
|
m_pData->m_BestIndex.store(BestIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
class CServerBrowserHttp : public IServerBrowserHttp
|
|
|
|
{
|
|
|
|
public:
|
2021-05-13 00:46:28 +00:00
|
|
|
CServerBrowserHttp(IEngine *pEngine, IConsole *pConsole, const char **ppUrls, int NumUrls, int PreviousBestIndex);
|
2018-07-11 20:46:04 +00:00
|
|
|
virtual ~CServerBrowserHttp() {}
|
|
|
|
void Update();
|
2021-05-13 00:46:28 +00:00
|
|
|
bool IsRefreshing() { return m_State != STATE_DONE; }
|
2018-07-11 20:46:04 +00:00
|
|
|
void Refresh();
|
2021-05-13 00:46:28 +00:00
|
|
|
bool GetBestUrl(const char **pBestUrl) const { return m_pChooseMaster->GetBestUrl(pBestUrl); }
|
2018-07-11 20:46:04 +00:00
|
|
|
|
|
|
|
int NumServers() const
|
|
|
|
{
|
|
|
|
return m_aServers.size();
|
|
|
|
}
|
|
|
|
const NETADDR &ServerAddress(int Index) const
|
|
|
|
{
|
|
|
|
return m_aServers[Index].m_Addr;
|
|
|
|
}
|
|
|
|
void Server(int Index, NETADDR *pAddr, CServerInfo *pInfo) const
|
|
|
|
{
|
|
|
|
const CEntry &Entry = m_aServers[Index];
|
|
|
|
*pAddr = Entry.m_Addr;
|
|
|
|
*pInfo = Entry.m_Info;
|
|
|
|
}
|
|
|
|
int NumLegacyServers() const
|
|
|
|
{
|
|
|
|
return m_aLegacyServers.size();
|
|
|
|
}
|
|
|
|
const NETADDR &LegacyServer(int Index) const
|
|
|
|
{
|
|
|
|
return m_aLegacyServers[Index];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-05-13 00:46:28 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STATE_DONE,
|
|
|
|
STATE_WANTREFRESH,
|
|
|
|
STATE_REFRESHING,
|
|
|
|
};
|
|
|
|
|
2018-07-11 20:46:04 +00:00
|
|
|
class CEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NETADDR m_Addr;
|
|
|
|
CServerInfo m_Info;
|
|
|
|
};
|
2021-05-12 17:09:16 +00:00
|
|
|
|
|
|
|
static bool Validate(json_value *pJson);
|
|
|
|
static bool Parse(json_value *pJson, std::vector<CEntry> *paServers, std::vector<NETADDR> *paLegacyServers);
|
|
|
|
|
2018-07-11 20:46:04 +00:00
|
|
|
IEngine *m_pEngine;
|
2021-05-13 00:46:28 +00:00
|
|
|
IConsole *m_pConsole;
|
|
|
|
|
|
|
|
int m_State = STATE_DONE;
|
2018-07-11 20:46:04 +00:00
|
|
|
std::shared_ptr<CGet> m_pGetServers;
|
|
|
|
std::unique_ptr<CChooseMaster> m_pChooseMaster;
|
|
|
|
|
|
|
|
std::vector<CEntry> m_aServers;
|
|
|
|
std::vector<NETADDR> m_aLegacyServers;
|
|
|
|
};
|
|
|
|
|
2021-05-13 00:46:28 +00:00
|
|
|
CServerBrowserHttp::CServerBrowserHttp(IEngine *pEngine, IConsole *pConsole, const char **ppUrls, int NumUrls, int PreviousBestIndex) :
|
2018-07-11 20:46:04 +00:00
|
|
|
m_pEngine(pEngine),
|
2021-05-13 00:46:28 +00:00
|
|
|
m_pConsole(pConsole),
|
|
|
|
m_pChooseMaster(new CChooseMaster(pEngine, Validate, ppUrls, NumUrls, PreviousBestIndex))
|
2018-07-11 20:46:04 +00:00
|
|
|
{
|
|
|
|
m_pChooseMaster->Refresh();
|
|
|
|
}
|
|
|
|
void CServerBrowserHttp::Update()
|
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
if(m_State == STATE_WANTREFRESH)
|
2018-07-11 20:46:04 +00:00
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
const char *pBestUrl;
|
|
|
|
if(m_pChooseMaster->GetBestUrl(&pBestUrl))
|
|
|
|
{
|
|
|
|
if(!m_pChooseMaster->IsRefreshing())
|
|
|
|
{
|
|
|
|
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowse_http", "no working serverlist URL found");
|
|
|
|
m_State = STATE_DONE;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_pEngine->AddJob(m_pGetServers = std::make_shared<CGet>(pBestUrl, CTimeout{0, 0, 0}));
|
|
|
|
m_State = STATE_REFRESHING;
|
|
|
|
}
|
|
|
|
else if(m_State == STATE_REFRESHING)
|
|
|
|
{
|
|
|
|
if(m_pGetServers->State() == HTTP_QUEUED || m_pGetServers->State() == HTTP_RUNNING)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_State = STATE_DONE;
|
2021-05-12 17:09:16 +00:00
|
|
|
std::shared_ptr<CGet> pGetServers = nullptr;
|
|
|
|
std::swap(m_pGetServers, pGetServers);
|
|
|
|
|
2021-05-13 00:46:28 +00:00
|
|
|
bool Success = true;
|
2021-05-12 17:09:16 +00:00
|
|
|
json_value *pJson = pGetServers->ResultJson();
|
2021-05-13 00:46:28 +00:00
|
|
|
Success = Success && pJson;
|
|
|
|
Success = Success && !Parse(pJson, &m_aServers, &m_aLegacyServers);
|
2021-05-12 17:09:16 +00:00
|
|
|
json_value_free(pJson);
|
2021-05-13 00:46:28 +00:00
|
|
|
if(!Success)
|
2018-07-11 20:46:04 +00:00
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowse_http", "failed getting serverlist, trying to find best URL");
|
|
|
|
m_pChooseMaster->Reset();
|
|
|
|
m_pChooseMaster->Refresh();
|
2018-07-11 20:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CServerBrowserHttp::Refresh()
|
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
if(m_State == STATE_WANTREFRESH)
|
|
|
|
{
|
|
|
|
m_pChooseMaster->Refresh();
|
|
|
|
}
|
|
|
|
m_State = STATE_WANTREFRESH;
|
|
|
|
Update();
|
2018-07-11 20:46:04 +00:00
|
|
|
}
|
|
|
|
bool ServerbrowserParseUrl(NETADDR *pOut, const char *pUrl)
|
|
|
|
{
|
|
|
|
char aHost[128];
|
|
|
|
const char *pRest = str_startswith(pUrl, "tw-0.6+udp://");
|
|
|
|
if(!pRest)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int Length = str_length(pRest);
|
|
|
|
int Start = 0;
|
|
|
|
int End = Length;
|
|
|
|
for(int i = 0; i < Length; i++)
|
|
|
|
{
|
|
|
|
if(pRest[i] == '@')
|
|
|
|
{
|
|
|
|
if(Start != 0)
|
|
|
|
{
|
|
|
|
// Two at signs.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Start = i + 1;
|
|
|
|
}
|
|
|
|
else if(pRest[i] == '/' || pRest[i] == '?' || pRest[i] == '#')
|
|
|
|
{
|
|
|
|
End = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str_truncate(aHost, sizeof(aHost), pRest + Start, End - Start);
|
|
|
|
if(net_addr_from_str(pOut, aHost))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-12 17:09:16 +00:00
|
|
|
bool CServerBrowserHttp::Validate(json_value *pJson)
|
|
|
|
{
|
|
|
|
std::vector<CEntry> aServers;
|
|
|
|
std::vector<NETADDR> aLegacyServers;
|
|
|
|
return Parse(pJson, &aServers, &aLegacyServers);
|
|
|
|
}
|
|
|
|
bool CServerBrowserHttp::Parse(json_value *pJson, std::vector<CEntry> *paServers, std::vector<NETADDR> *paLegacyServers)
|
2018-07-11 20:46:04 +00:00
|
|
|
{
|
|
|
|
std::vector<CEntry> aServers;
|
|
|
|
std::vector<NETADDR> aLegacyServers;
|
|
|
|
|
|
|
|
const json_value &Json = *pJson;
|
|
|
|
const json_value &Servers = Json["servers"];
|
|
|
|
const json_value &LegacyServers = Json["servers_legacy"];
|
|
|
|
if(Servers.type != json_array || (LegacyServers.type != json_array && LegacyServers.type != json_none))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for(unsigned int i = 0; i < Servers.u.array.length; i++)
|
|
|
|
{
|
|
|
|
const json_value &Server = Servers[i];
|
|
|
|
const json_value &Addresses = Server["addresses"];
|
|
|
|
const json_value &Info = Server["info"];
|
|
|
|
const json_value &Location = Server["location"];
|
|
|
|
int ParsedLocation = CServerInfo::LOC_UNKNOWN;
|
|
|
|
CServerInfo2 ParsedInfo;
|
|
|
|
if(Addresses.type != json_array || (Location.type != json_string && Location.type != json_none))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(Location.type == json_string)
|
|
|
|
{
|
|
|
|
if(CServerInfo::ParseLocation(&ParsedLocation, Location))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(CServerInfo2::FromJson(&ParsedInfo, &Info))
|
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
//dbg_msg("dbg/serverbrowser", "skipped due to info, i=%d", i);
|
2018-07-11 20:46:04 +00:00
|
|
|
// Only skip the current server on parsing
|
|
|
|
// failure; the server info is "user input" by
|
|
|
|
// the game server and can be set to arbitrary
|
|
|
|
// values.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CServerInfo SetInfo = ParsedInfo;
|
|
|
|
SetInfo.m_Location = ParsedLocation;
|
|
|
|
for(unsigned int a = 0; a < Addresses.u.array.length; a++)
|
|
|
|
{
|
|
|
|
const json_value &Address = Addresses[a];
|
|
|
|
if(Address.type != json_string)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// TODO: Address address handling :P
|
|
|
|
NETADDR ParsedAddr;
|
|
|
|
if(ServerbrowserParseUrl(&ParsedAddr, Addresses[a]))
|
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
//dbg_msg("dbg/serverbrowser", "unknown address, i=%d a=%d", i, a);
|
2018-07-11 20:46:04 +00:00
|
|
|
// Skip unknown addresses.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
aServers.push_back({ParsedAddr, SetInfo});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(LegacyServers.type == json_array)
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < LegacyServers.u.array.length; i++)
|
|
|
|
{
|
|
|
|
const json_value &Address = LegacyServers[i];
|
|
|
|
NETADDR ParsedAddr;
|
|
|
|
if(Address.type != json_string || net_addr_from_str(&ParsedAddr, Address))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
aLegacyServers.push_back(ParsedAddr);
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 17:09:16 +00:00
|
|
|
*paServers = aServers;
|
|
|
|
*paLegacyServers = aLegacyServers;
|
2018-07-11 20:46:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-13 00:46:28 +00:00
|
|
|
|
|
|
|
static const char *DEFAULT_SERVERLIST_URLS[] = {
|
|
|
|
"https://master1.ddnet.tw/ddnet/15/servers.json",
|
|
|
|
"https://master2.ddnet.tw/ddnet/15/servers.json",
|
|
|
|
"https://master3.ddnet.tw/ddnet/15/servers.json",
|
|
|
|
"https://master4.ddnet.tw/ddnet/15/servers.json",
|
|
|
|
};
|
|
|
|
|
|
|
|
IServerBrowserHttp *CreateServerBrowserHttp(IEngine *pEngine, IConsole *pConsole, IStorage *pStorage, const char *pPreviousBestUrl)
|
2018-07-11 20:46:04 +00:00
|
|
|
{
|
2021-05-13 00:46:28 +00:00
|
|
|
char aaUrls[CChooseMaster::MAX_URLS][256];
|
2021-05-13 23:25:59 +00:00
|
|
|
const char *apUrls[CChooseMaster::MAX_URLS] = {0};
|
2021-05-13 00:46:28 +00:00
|
|
|
const char **ppUrls = apUrls;
|
|
|
|
int NumUrls = 0;
|
2021-05-27 19:45:12 +00:00
|
|
|
IOHANDLE File = pStorage->OpenFile("ddnet-serverlist-urls.cfg", IOFLAG_READ, IStorage::TYPE_ALL);
|
2021-05-13 00:46:28 +00:00
|
|
|
if(File)
|
|
|
|
{
|
|
|
|
CLineReader Lines;
|
|
|
|
Lines.Init(File);
|
|
|
|
while(NumUrls < CChooseMaster::MAX_URLS)
|
|
|
|
{
|
|
|
|
const char *pLine = Lines.Get();
|
|
|
|
if(!pLine)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str_copy(aaUrls[NumUrls], pLine, sizeof(aaUrls[NumUrls]));
|
|
|
|
apUrls[NumUrls] = aaUrls[NumUrls];
|
|
|
|
NumUrls += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(NumUrls == 0)
|
|
|
|
{
|
|
|
|
ppUrls = DEFAULT_SERVERLIST_URLS;
|
|
|
|
NumUrls = sizeof(DEFAULT_SERVERLIST_URLS) / sizeof(DEFAULT_SERVERLIST_URLS[0]);
|
|
|
|
}
|
|
|
|
int PreviousBestIndex = -1;
|
|
|
|
for(int i = 0; i < NumUrls; i++)
|
|
|
|
{
|
|
|
|
if(str_comp(ppUrls[i], pPreviousBestUrl) == 0)
|
|
|
|
{
|
|
|
|
PreviousBestIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new CServerBrowserHttp(pEngine, pConsole, ppUrls, NumUrls, PreviousBestIndex);
|
2018-07-11 20:46:04 +00:00
|
|
|
}
|