6095: Minor refactoring of `CServerBrowser` r=def- a=Robyt3

Some refactorings from teeworlds/teeworlds#3090 and more.

## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-12-05 22:45:22 +00:00 committed by GitHub
commit 4f1ced0ab9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 91 deletions

View file

@ -2884,8 +2884,7 @@ void CClient::Update()
} }
// update the server browser // update the server browser
m_ServerBrowser.Update(m_ResortServerBrowser); m_ServerBrowser.Update();
m_ResortServerBrowser = false;
// update editor/gameclient // update editor/gameclient
if(m_EditorActive) if(m_EditorActive)
@ -4031,7 +4030,7 @@ void CClient::UpdateAndSwap()
void CClient::ServerBrowserUpdate() void CClient::ServerBrowserUpdate()
{ {
m_ResortServerBrowser = true; m_ServerBrowser.RequestResort();
} }
void CClient::ConchainServerBrowserUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) void CClient::ConchainServerBrowserUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)

View file

@ -158,7 +158,6 @@ class CClient : public IClient, public CDemoPlayer::IListener
bool m_AutoCSVRecycle; bool m_AutoCSVRecycle;
bool m_EditorActive; bool m_EditorActive;
bool m_SoundInitFailed; bool m_SoundInitFailed;
bool m_ResortServerBrowser;
int m_aAckGameTick[NUM_DUMMIES]; int m_aAckGameTick[NUM_DUMMIES];
int m_aCurrentRecvTick[NUM_DUMMIES]; int m_aCurrentRecvTick[NUM_DUMMIES];

View file

@ -34,43 +34,43 @@
#include <game/client/components/menus.h> // PAGE_DDNET #include <game/client/components/menus.h> // PAGE_DDNET
class SortWrap class CSortWrap
{ {
typedef bool (CServerBrowser::*SortFunc)(int, int) const; typedef bool (CServerBrowser::*SortFunc)(int, int) const;
SortFunc m_pfnSort; SortFunc m_pfnSort;
CServerBrowser *m_pThis; CServerBrowser *m_pThis;
public: public:
SortWrap(CServerBrowser *pServer, SortFunc Func) : CSortWrap(CServerBrowser *pServer, SortFunc Func) :
m_pfnSort(Func), m_pThis(pServer) {} m_pfnSort(Func), m_pThis(pServer) {}
bool operator()(int a, int b) { return (g_Config.m_BrSortOrder ? (m_pThis->*m_pfnSort)(b, a) : (m_pThis->*m_pfnSort)(a, b)); } bool operator()(int a, int b) { return (g_Config.m_BrSortOrder ? (m_pThis->*m_pfnSort)(b, a) : (m_pThis->*m_pfnSort)(a, b)); }
}; };
CServerBrowser::CServerBrowser() CServerBrowser::CServerBrowser()
{ {
m_ppServerlist = 0; m_ppServerlist = nullptr;
m_pSortedServerlist = 0; m_pSortedServerlist = nullptr;
m_pFirstReqServer = 0; // request list m_pFirstReqServer = nullptr; // request list
m_pLastReqServer = 0; m_pLastReqServer = nullptr;
m_NumRequests = 0; m_NumRequests = 0;
m_NeedResort = false;
m_NumSortedServers = 0; m_NumSortedServers = 0;
m_NumSortedServersCapacity = 0; m_NumSortedServersCapacity = 0;
m_NumServers = 0; m_NumServers = 0;
m_NumServerCapacity = 0; m_NumServerCapacity = 0;
m_Sorthash = 0; m_Sorthash = 0;
m_aFilterString[0] = 0; m_aFilterString[0] = '\0';
m_aFilterGametypeString[0] = 0; m_aFilterGametypeString[0] = '\0';
m_ServerlistType = 0; m_ServerlistType = 0;
m_BroadcastTime = 0; m_BroadcastTime = 0;
secure_random_fill(m_aTokenSeed, sizeof(m_aTokenSeed)); secure_random_fill(m_aTokenSeed, sizeof(m_aTokenSeed));
m_pDDNetInfo = 0; m_pDDNetInfo = nullptr;
m_SortOnNextUpdate = false;
} }
CServerBrowser::~CServerBrowser() CServerBrowser::~CServerBrowser()
@ -158,7 +158,7 @@ void CServerBrowser::Con_LeakIpAddress(IConsole::IResult *pResult, void *pUserDa
pThis->QueueRequest(pChosen); pThis->QueueRequest(pChosen);
char aAddr[NETADDR_MAXSTRSIZE]; char aAddr[NETADDR_MAXSTRSIZE];
net_addr_str(&pChosen->m_Info.m_aAddresses[0], aAddr, sizeof(aAddr), true); net_addr_str(&pChosen->m_Info.m_aAddresses[0], aAddr, sizeof(aAddr), true);
dbg_msg("serverbrowse/dbg", "queuing ping request for %s", aAddr); dbg_msg("serverbrowser", "queuing ping request for %s", aAddr);
} }
if(i < (int)vSortedServers.size() && New) if(i < (int)vSortedServers.size() && New)
{ {
@ -171,7 +171,7 @@ void CServerBrowser::Con_LeakIpAddress(IConsole::IResult *pResult, void *pUserDa
const CServerInfo *CServerBrowser::SortedGet(int Index) const const CServerInfo *CServerBrowser::SortedGet(int Index) const
{ {
if(Index < 0 || Index >= m_NumSortedServers) if(Index < 0 || Index >= m_NumSortedServers)
return 0; return nullptr;
return &m_ppServerlist[m_pSortedServerlist[Index]]->m_Info; return &m_ppServerlist[m_pSortedServerlist[Index]]->m_Info;
} }
@ -254,7 +254,6 @@ bool CServerBrowser::SortCompareNumPlayersAndPing(int Index1, int Index2) const
void CServerBrowser::Filter() void CServerBrowser::Filter()
{ {
int i = 0, p = 0;
m_NumSortedServers = 0; m_NumSortedServers = 0;
// allocate the sorted list // allocate the sorted list
@ -266,35 +265,35 @@ void CServerBrowser::Filter()
} }
// filter the servers // filter the servers
for(i = 0; i < m_NumServers; i++) for(int i = 0; i < m_NumServers; i++)
{ {
int Filtered = 0; bool Filtered = false;
if(g_Config.m_BrFilterEmpty && m_ppServerlist[i]->m_Info.m_NumFilteredPlayers == 0) if(g_Config.m_BrFilterEmpty && m_ppServerlist[i]->m_Info.m_NumFilteredPlayers == 0)
Filtered = 1; Filtered = true;
else if(g_Config.m_BrFilterFull && Players(m_ppServerlist[i]->m_Info) == Max(m_ppServerlist[i]->m_Info)) else if(g_Config.m_BrFilterFull && Players(m_ppServerlist[i]->m_Info) == Max(m_ppServerlist[i]->m_Info))
Filtered = 1; Filtered = true;
else if(g_Config.m_BrFilterPw && m_ppServerlist[i]->m_Info.m_Flags & SERVER_FLAG_PASSWORD) else if(g_Config.m_BrFilterPw && m_ppServerlist[i]->m_Info.m_Flags & SERVER_FLAG_PASSWORD)
Filtered = 1; Filtered = true;
else if(g_Config.m_BrFilterServerAddress[0] && !str_find_nocase(m_ppServerlist[i]->m_Info.m_aAddress, g_Config.m_BrFilterServerAddress)) else if(g_Config.m_BrFilterServerAddress[0] && !str_find_nocase(m_ppServerlist[i]->m_Info.m_aAddress, g_Config.m_BrFilterServerAddress))
Filtered = 1; Filtered = true;
else if(g_Config.m_BrFilterGametypeStrict && g_Config.m_BrFilterGametype[0] && str_comp_nocase(m_ppServerlist[i]->m_Info.m_aGameType, g_Config.m_BrFilterGametype)) else if(g_Config.m_BrFilterGametypeStrict && g_Config.m_BrFilterGametype[0] && str_comp_nocase(m_ppServerlist[i]->m_Info.m_aGameType, g_Config.m_BrFilterGametype))
Filtered = 1; Filtered = true;
else if(!g_Config.m_BrFilterGametypeStrict && g_Config.m_BrFilterGametype[0] && !str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aGameType, g_Config.m_BrFilterGametype)) else if(!g_Config.m_BrFilterGametypeStrict && g_Config.m_BrFilterGametype[0] && !str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aGameType, g_Config.m_BrFilterGametype))
Filtered = 1; Filtered = true;
else if(g_Config.m_BrFilterUnfinishedMap && m_ppServerlist[i]->m_Info.m_HasRank == 1) else if(g_Config.m_BrFilterUnfinishedMap && m_ppServerlist[i]->m_Info.m_HasRank == 1)
Filtered = 1; Filtered = true;
else else
{ {
if(g_Config.m_BrFilterCountry) if(g_Config.m_BrFilterCountry)
{ {
Filtered = 1; Filtered = true;
// match against player country // match against player country
for(p = 0; p < minimum(m_ppServerlist[i]->m_Info.m_NumClients, (int)MAX_CLIENTS); p++) for(int p = 0; p < minimum(m_ppServerlist[i]->m_Info.m_NumClients, (int)MAX_CLIENTS); p++)
{ {
if(m_ppServerlist[i]->m_Info.m_aClients[p].m_Country == g_Config.m_BrFilterCountryIndex) if(m_ppServerlist[i]->m_Info.m_aClients[p].m_Country == g_Config.m_BrFilterCountryIndex)
{ {
Filtered = 0; Filtered = false;
break; break;
} }
} }
@ -302,8 +301,6 @@ void CServerBrowser::Filter()
if(!Filtered && g_Config.m_BrFilterString[0] != '\0') if(!Filtered && g_Config.m_BrFilterString[0] != '\0')
{ {
int MatchFound = 0;
m_ppServerlist[i]->m_Info.m_QuickSearchHit = 0; m_ppServerlist[i]->m_Info.m_QuickSearchHit = 0;
const char *pStr = g_Config.m_BrFilterString; const char *pStr = g_Config.m_BrFilterString;
@ -318,17 +315,15 @@ void CServerBrowser::Filter()
// match against server name // match against server name
if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aName, aFilterStr)) if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aName, aFilterStr))
{ {
MatchFound = 1;
m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_SERVERNAME; m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_SERVERNAME;
} }
// match against players // match against players
for(p = 0; p < minimum(m_ppServerlist[i]->m_Info.m_NumClients, (int)MAX_CLIENTS); p++) for(int p = 0; p < minimum(m_ppServerlist[i]->m_Info.m_NumClients, (int)MAX_CLIENTS); p++)
{ {
if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aClients[p].m_aName, aFilterStr) || if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aClients[p].m_aName, aFilterStr) ||
str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aClients[p].m_aClan, aFilterStr)) str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aClients[p].m_aClan, aFilterStr))
{ {
MatchFound = 1;
m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_PLAYER; m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_PLAYER;
break; break;
} }
@ -337,13 +332,12 @@ void CServerBrowser::Filter()
// match against map // match against map
if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aMap, aFilterStr)) if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aMap, aFilterStr))
{ {
MatchFound = 1;
m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_MAPNAME; m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_MAPNAME;
} }
} }
if(!MatchFound) if(!m_ppServerlist[i]->m_Info.m_QuickSearchHit)
Filtered = 1; Filtered = true;
} }
if(!Filtered && g_Config.m_BrExcludeString[0] != '\0') if(!Filtered && g_Config.m_BrExcludeString[0] != '\0')
@ -360,32 +354,32 @@ void CServerBrowser::Filter()
// match against server name // match against server name
if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aName, aExcludeStr)) if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aName, aExcludeStr))
{ {
Filtered = 1; Filtered = true;
break; break;
} }
// match against map // match against map
if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aMap, aExcludeStr)) if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aMap, aExcludeStr))
{ {
Filtered = 1; Filtered = true;
break; break;
} }
// match against gametype // match against gametype
if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aGameType, aExcludeStr)) if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aGameType, aExcludeStr))
{ {
Filtered = 1; Filtered = true;
break; break;
} }
} }
} }
} }
if(Filtered == 0) if(!Filtered)
{ {
// check for friend // check for friend
m_ppServerlist[i]->m_Info.m_FriendState = IFriends::FRIEND_NO; m_ppServerlist[i]->m_Info.m_FriendState = IFriends::FRIEND_NO;
for(p = 0; p < minimum(m_ppServerlist[i]->m_Info.m_NumClients, (int)MAX_CLIENTS); p++) for(int p = 0; p < minimum(m_ppServerlist[i]->m_Info.m_NumClients, (int)MAX_CLIENTS); p++)
{ {
m_ppServerlist[i]->m_Info.m_aClients[p].m_FriendState = m_pFriends->GetFriendState(m_ppServerlist[i]->m_Info.m_aClients[p].m_aName, m_ppServerlist[i]->m_Info.m_aClients[p].m_FriendState = m_pFriends->GetFriendState(m_ppServerlist[i]->m_Info.m_aClients[p].m_aName,
m_ppServerlist[i]->m_Info.m_aClients[p].m_aClan); m_ppServerlist[i]->m_Info.m_aClients[p].m_aClan);
@ -429,10 +423,8 @@ void SetFilteredPlayers(const CServerInfo &Item)
void CServerBrowser::Sort() void CServerBrowser::Sort()
{ {
int i;
// fill m_NumFilteredPlayers // fill m_NumFilteredPlayers
for(i = 0; i < m_NumServers; i++) for(int i = 0; i < m_NumServers; i++)
{ {
SetFilteredPlayers(m_ppServerlist[i]->m_Info); SetFilteredPlayers(m_ppServerlist[i]->m_Info);
} }
@ -442,17 +434,17 @@ void CServerBrowser::Sort()
// sort // sort
if(g_Config.m_BrSortOrder == 2 && (g_Config.m_BrSort == IServerBrowser::SORT_NUMPLAYERS || g_Config.m_BrSort == IServerBrowser::SORT_PING)) if(g_Config.m_BrSortOrder == 2 && (g_Config.m_BrSort == IServerBrowser::SORT_NUMPLAYERS || g_Config.m_BrSort == IServerBrowser::SORT_PING))
std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, SortWrap(this, &CServerBrowser::SortCompareNumPlayersAndPing)); std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, CSortWrap(this, &CServerBrowser::SortCompareNumPlayersAndPing));
else if(g_Config.m_BrSort == IServerBrowser::SORT_NAME) else if(g_Config.m_BrSort == IServerBrowser::SORT_NAME)
std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, SortWrap(this, &CServerBrowser::SortCompareName)); std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, CSortWrap(this, &CServerBrowser::SortCompareName));
else if(g_Config.m_BrSort == IServerBrowser::SORT_PING) else if(g_Config.m_BrSort == IServerBrowser::SORT_PING)
std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, SortWrap(this, &CServerBrowser::SortComparePing)); std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, CSortWrap(this, &CServerBrowser::SortComparePing));
else if(g_Config.m_BrSort == IServerBrowser::SORT_MAP) else if(g_Config.m_BrSort == IServerBrowser::SORT_MAP)
std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, SortWrap(this, &CServerBrowser::SortCompareMap)); std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, CSortWrap(this, &CServerBrowser::SortCompareMap));
else if(g_Config.m_BrSort == IServerBrowser::SORT_NUMPLAYERS) else if(g_Config.m_BrSort == IServerBrowser::SORT_NUMPLAYERS)
std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, SortWrap(this, &CServerBrowser::SortCompareNumPlayers)); std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, CSortWrap(this, &CServerBrowser::SortCompareNumPlayers));
else if(g_Config.m_BrSort == IServerBrowser::SORT_GAMETYPE) else if(g_Config.m_BrSort == IServerBrowser::SORT_GAMETYPE)
std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, SortWrap(this, &CServerBrowser::SortCompareGametype)); std::stable_sort(m_pSortedServerlist, m_pSortedServerlist + m_NumSortedServers, CSortWrap(this, &CServerBrowser::SortCompareGametype));
str_copy(m_aFilterGametypeString, g_Config.m_BrFilterGametype); str_copy(m_aFilterGametypeString, g_Config.m_BrFilterGametype);
str_copy(m_aFilterString, g_Config.m_BrFilterString); str_copy(m_aFilterString, g_Config.m_BrFilterString);
@ -473,8 +465,8 @@ void CServerBrowser::RemoveRequest(CServerEntry *pEntry)
else else
m_pLastReqServer = pEntry->m_pPrevReq; m_pLastReqServer = pEntry->m_pPrevReq;
pEntry->m_pPrevReq = 0; pEntry->m_pPrevReq = nullptr;
pEntry->m_pNextReq = 0; pEntry->m_pNextReq = nullptr;
m_NumRequests--; m_NumRequests--;
} }
} }
@ -498,7 +490,7 @@ void CServerBrowser::QueueRequest(CServerEntry *pEntry)
else else
m_pFirstReqServer = pEntry; m_pFirstReqServer = pEntry;
m_pLastReqServer = pEntry; m_pLastReqServer = pEntry;
pEntry->m_pNextReq = 0; pEntry->m_pNextReq = nullptr;
m_NumRequests++; m_NumRequests++;
} }
@ -513,7 +505,7 @@ void ServerBrowserFormatAddresses(char *pBuffer, int BufferSize, NETADDR *pAddrs
return; return;
} }
pBuffer[0] = ','; pBuffer[0] = ',';
pBuffer[1] = 0; pBuffer[1] = '\0';
pBuffer += 1; pBuffer += 1;
BufferSize -= 1; BufferSize -= 1;
} }
@ -607,10 +599,8 @@ void CServerBrowser::SetLatency(NETADDR Addr, int Latency)
CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int NumAddrs) CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int NumAddrs)
{ {
CServerEntry *pEntry = 0;
// create new pEntry // create new pEntry
pEntry = (CServerEntry *)m_ServerlistHeap.Allocate(sizeof(CServerEntry)); CServerEntry *pEntry = (CServerEntry *)m_ServerlistHeap.Allocate(sizeof(CServerEntry));
mem_zero(pEntry, sizeof(CServerEntry)); mem_zero(pEntry, sizeof(CServerEntry));
// set the info // set the info
@ -628,11 +618,11 @@ CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int Num
// check if it's an official server // check if it's an official server
bool Official = false; bool Official = false;
for(int i = 0; !Official && i < (int)std::size(m_aNetworks); i++) for(const auto &Network : m_aNetworks)
{ {
for(int j = 0; !Official && j < m_aNetworks[i].m_NumCountries; j++) for(int j = 0; !Official && j < Network.m_NumCountries; j++)
{ {
CNetworkCountry *pCntr = &m_aNetworks[i].m_aCountries[j]; const CNetworkCountry *pCntr = &Network.m_aCountries[j];
for(int k = 0; !Official && k < pCntr->m_NumServers; k++) for(int k = 0; !Official && k < pCntr->m_NumServers; k++)
{ {
for(int l = 0; !Official && l < NumAddrs; l++) for(int l = 0; !Official && l < NumAddrs; l++)
@ -644,6 +634,8 @@ CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int Num
} }
} }
} }
if(Official)
break;
} }
pEntry->m_Info.m_Official = Official; pEntry->m_Info.m_Official = Official;
@ -737,14 +729,13 @@ void CServerBrowser::OnServerInfoUpdate(const NETADDR &Addr, int Token, const CS
{ {
char aAddr[NETADDR_MAXSTRSIZE]; char aAddr[NETADDR_MAXSTRSIZE];
net_addr_str(&Addr, aAddr, sizeof(aAddr), true); net_addr_str(&Addr, aAddr, sizeof(aAddr), true);
dbg_msg("serverbrowse/dbg", "received ping response from %s", aAddr); dbg_msg("serverbrowser", "received ping response from %s", aAddr);
SetLatency(Addr, Latency); SetLatency(Addr, Latency);
} }
pEntry->m_RequestTime = -1; // Request has been answered pEntry->m_RequestTime = -1; // Request has been answered
} }
RemoveRequest(pEntry); RemoveRequest(pEntry);
RequestResort();
m_SortOnNextUpdate = true;
} }
void CServerBrowser::Refresh(int Type) void CServerBrowser::Refresh(int Type)
@ -761,7 +752,6 @@ void CServerBrowser::Refresh(int Type)
{ {
unsigned char aBuffer[sizeof(SERVERBROWSE_GETINFO) + 1]; unsigned char aBuffer[sizeof(SERVERBROWSE_GETINFO) + 1];
CNetChunk Packet; CNetChunk Packet;
int i;
/* do the broadcast version */ /* do the broadcast version */
Packet.m_ClientID = -1; Packet.m_ClientID = -1;
@ -781,14 +771,14 @@ void CServerBrowser::Refresh(int Type)
m_BroadcastTime = time_get(); m_BroadcastTime = time_get();
for(i = 8303; i <= 8310; i++) for(int i = 8303; i <= 8310; i++)
{ {
Packet.m_Address.port = i; Packet.m_Address.port = i;
m_pNetClient->Send(&Packet); m_pNetClient->Send(&Packet);
} }
if(g_Config.m_Debug) if(g_Config.m_Debug)
m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "client_srvbrowse", "broadcasting for servers"); m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "serverbrowser", "broadcasting for servers");
} }
else if(Type == IServerBrowser::TYPE_FAVORITES || Type == IServerBrowser::TYPE_INTERNET || Type == IServerBrowser::TYPE_DDNET || Type == IServerBrowser::TYPE_KOG) else if(Type == IServerBrowser::TYPE_FAVORITES || Type == IServerBrowser::TYPE_INTERNET || Type == IServerBrowser::TYPE_DDNET || Type == IServerBrowser::TYPE_KOG)
{ {
@ -807,16 +797,13 @@ void CServerBrowser::Refresh(int Type)
void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry, int *pBasicToken, int *pToken, bool RandomToken) const void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry, int *pBasicToken, int *pToken, bool RandomToken) const
{ {
unsigned char aBuffer[sizeof(SERVERBROWSE_GETINFO) + 1];
CNetChunk Packet;
if(g_Config.m_Debug) if(g_Config.m_Debug)
{ {
char aAddrStr[NETADDR_MAXSTRSIZE]; char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(&Addr, aAddrStr, sizeof(aAddrStr), true); net_addr_str(&Addr, aAddrStr, sizeof(aAddrStr), true);
char aBuf[256]; char aBuf[256];
str_format(aBuf, sizeof(aBuf), "requesting server info from %s", aAddrStr); str_format(aBuf, sizeof(aBuf), "requesting server info from %s", aAddrStr);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "client_srvbrowse", aBuf); m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "serverbrowser", aBuf);
} }
int Token = GenerateToken(Addr); int Token = GenerateToken(Addr);
@ -838,9 +825,11 @@ void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry, int
*pBasicToken = GetBasicToken(Token); *pBasicToken = GetBasicToken(Token);
} }
unsigned char aBuffer[sizeof(SERVERBROWSE_GETINFO) + 1];
mem_copy(aBuffer, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)); mem_copy(aBuffer, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO));
aBuffer[sizeof(SERVERBROWSE_GETINFO)] = GetBasicToken(Token); aBuffer[sizeof(SERVERBROWSE_GETINFO)] = GetBasicToken(Token);
CNetChunk Packet;
Packet.m_ClientID = -1; Packet.m_ClientID = -1;
Packet.m_Address = Addr; Packet.m_Address = Addr;
Packet.m_Flags = NETSENDFLAG_CONNLESS | NETSENDFLAG_EXTENDED; Packet.m_Flags = NETSENDFLAG_CONNLESS | NETSENDFLAG_EXTENDED;
@ -884,7 +873,7 @@ void CServerBrowser::UpdateFromHttp()
{ {
char aBuf[64]; char aBuf[64];
str_format(aBuf, sizeof(aBuf), "cannot parse br_location: '%s'", g_Config.m_BrLocation); str_format(aBuf, sizeof(aBuf), "cannot parse br_location: '%s'", g_Config.m_BrLocation);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowse", aBuf); m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowser", aBuf);
} }
} }
@ -916,7 +905,7 @@ void CServerBrowser::UpdateFromHttp()
pExcludeTypes = g_Config.m_BrFilterExcludeTypesKoG; pExcludeTypes = g_Config.m_BrFilterExcludeTypesKoG;
break; break;
default: default:
dbg_assert(0, "invalid network"); dbg_assert(false, "invalid network");
return; return;
} }
// remove unknown elements of exclude list // remove unknown elements of exclude list
@ -1021,7 +1010,7 @@ void CServerBrowser::UpdateFromHttp()
} }
} }
m_SortOnNextUpdate = true; RequestResort();
} }
void CServerBrowser::CleanUp() void CServerBrowser::CleanUp()
@ -1031,13 +1020,13 @@ void CServerBrowser::CleanUp()
m_NumServers = 0; m_NumServers = 0;
m_NumSortedServers = 0; m_NumSortedServers = 0;
m_ByAddr.clear(); m_ByAddr.clear();
m_pFirstReqServer = 0; m_pFirstReqServer = nullptr;
m_pLastReqServer = 0; m_pLastReqServer = nullptr;
m_NumRequests = 0; m_NumRequests = 0;
m_CurrentMaxRequests = g_Config.m_BrMaxRequests; m_CurrentMaxRequests = g_Config.m_BrMaxRequests;
} }
void CServerBrowser::Update(bool ForceResort) void CServerBrowser::Update()
{ {
int64_t Timeout = time_freq(); int64_t Timeout = time_freq();
int64_t Now = time_get(); int64_t Now = time_get();
@ -1116,7 +1105,7 @@ void CServerBrowser::Update(bool ForceResort)
} }
// check if we need to resort // check if we need to resort
if(m_Sorthash != SortHash() || ForceResort || m_SortOnNextUpdate) if(m_Sorthash != SortHash() || m_NeedResort)
{ {
for(int i = 0; i < m_NumServers; i++) for(int i = 0; i < m_NumServers; i++)
{ {
@ -1125,7 +1114,7 @@ void CServerBrowser::Update(bool ForceResort)
pInfo->m_FavoriteAllowPing = m_pFavorites->IsPingAllowed(pInfo->m_aAddresses, pInfo->m_NumAddresses); pInfo->m_FavoriteAllowPing = m_pFavorites->IsPingAllowed(pInfo->m_aAddresses, pInfo->m_NumAddresses);
} }
Sort(); Sort();
m_SortOnNextUpdate = false; m_NeedResort = false;
} }
} }
@ -1158,7 +1147,7 @@ void CServerBrowser::LoadDDNetServers()
if(pSrv->type != json_object || pTypes->type != json_object || pName->type != json_string || pFlagID->type != json_integer) if(pSrv->type != json_object || pTypes->type != json_object || pName->type != json_string || pFlagID->type != json_integer)
{ {
dbg_msg("client_srvbrowse", "invalid attributes"); dbg_msg("serverbrowser", "invalid attributes");
continue; continue;
} }
@ -1178,7 +1167,7 @@ void CServerBrowser::LoadDDNetServers()
if(pAddrs->type != json_array) if(pAddrs->type != json_array)
{ {
dbg_msg("client_srvbrowse", "invalid attributes"); dbg_msg("serverbrowser", "invalid attributes");
continue; continue;
} }
@ -1204,7 +1193,7 @@ void CServerBrowser::LoadDDNetServers()
const json_value *pAddr = json_array_get(pAddrs, g); const json_value *pAddr = json_array_get(pAddrs, g);
if(pAddr->type != json_string) if(pAddr->type != json_string)
{ {
dbg_msg("client_srvbrowse", "invalid attributes"); dbg_msg("serverbrowser", "invalid attributes");
continue; continue;
} }
const char *pStr = json_string_get(pAddr); const char *pStr = json_string_get(pAddr);
@ -1287,7 +1276,7 @@ void CServerBrowser::LoadDDNetInfoJson()
if(m_pDDNetInfo && m_pDDNetInfo->type != json_object) if(m_pDDNetInfo && m_pDDNetInfo->type != json_object)
{ {
json_value_free(m_pDDNetInfo); json_value_free(m_pDDNetInfo);
m_pDDNetInfo = 0; m_pDDNetInfo = nullptr;
} }
m_OwnLocation = CServerInfo::LOC_UNKNOWN; m_OwnLocation = CServerInfo::LOC_UNKNOWN;
@ -1298,7 +1287,7 @@ void CServerBrowser::LoadDDNetInfoJson()
{ {
char aBuf[64]; char aBuf[64];
str_format(aBuf, sizeof(aBuf), "cannot parse location from info.json: '%s'", (const char *)Location); str_format(aBuf, sizeof(aBuf), "cannot parse location from info.json: '%s'", (const char *)Location);
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowse", aBuf); m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "serverbrowser", aBuf);
} }
} }
} }
@ -1348,7 +1337,7 @@ const json_value *CServerBrowser::LoadDDNetInfo()
bool CServerBrowser::IsRefreshing() const bool CServerBrowser::IsRefreshing() const
{ {
return m_pFirstReqServer != 0; return m_pFirstReqServer != nullptr;
} }
bool CServerBrowser::IsGettingServerlist() const bool CServerBrowser::IsGettingServerlist() const

View file

@ -83,6 +83,7 @@ public:
bool IsRefreshing() const override; bool IsRefreshing() const override;
bool IsGettingServerlist() const override; bool IsGettingServerlist() const override;
int LoadingProgression() const override; int LoadingProgression() const override;
void RequestResort() { m_NeedResort = true; }
int NumServers() const override { return m_NumServers; } int NumServers() const override { return m_NumServers; }
@ -120,7 +121,7 @@ public:
void TypeFilterClean(int Network) override; void TypeFilterClean(int Network) override;
// //
void Update(bool ForceResort); void Update();
void OnServerInfoUpdate(const NETADDR &Addr, int Token, const CServerInfo *pInfo); void OnServerInfoUpdate(const NETADDR &Addr, int Token, const CServerInfo *pInfo);
void SetHttpInfo(const CServerInfo *pInfo); void SetHttpInfo(const CServerInfo *pInfo);
void RequestCurrentServer(const NETADDR &Addr) const; void RequestCurrentServer(const NETADDR &Addr) const;
@ -163,11 +164,11 @@ private:
CServerEntry *m_pLastReqServer; CServerEntry *m_pLastReqServer;
int m_NumRequests; int m_NumRequests;
bool m_NeedResort;
// used instead of g_Config.br_max_requests to get more servers // used instead of g_Config.br_max_requests to get more servers
int m_CurrentMaxRequests; int m_CurrentMaxRequests;
int m_NeedRefresh;
int m_NumSortedServers; int m_NumSortedServers;
int m_NumSortedServersCapacity; int m_NumSortedServersCapacity;
int m_NumServers; int m_NumServers;
@ -181,8 +182,6 @@ private:
int64_t m_BroadcastTime; int64_t m_BroadcastTime;
unsigned char m_aTokenSeed[16]; unsigned char m_aTokenSeed[16];
bool m_SortOnNextUpdate;
int GenerateToken(const NETADDR &Addr) const; int GenerateToken(const NETADDR &Addr) const;
static int GetBasicToken(int Token); static int GetBasicToken(int Token);
static int GetExtraToken(int Token); static int GetExtraToken(int Token);