From 518210dfe1a7d10de51fb207197cd32d8baa294a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:14:14 +0100 Subject: [PATCH 1/9] Move variable declarations closer to usage --- src/engine/client/serverbrowser.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index aadff40dd..7a7baa45f 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -254,7 +254,6 @@ bool CServerBrowser::SortCompareNumPlayersAndPing(int Index1, int Index2) const void CServerBrowser::Filter() { - int i = 0, p = 0; m_NumSortedServers = 0; // allocate the sorted list @@ -266,7 +265,7 @@ void CServerBrowser::Filter() } // filter the servers - for(i = 0; i < m_NumServers; i++) + for(int i = 0; i < m_NumServers; i++) { int Filtered = 0; @@ -290,7 +289,7 @@ void CServerBrowser::Filter() { Filtered = 1; // 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) { @@ -323,7 +322,7 @@ void CServerBrowser::Filter() } // 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) || str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aClients[p].m_aClan, aFilterStr)) @@ -385,7 +384,7 @@ void CServerBrowser::Filter() { // check for friend 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_aClan); @@ -429,10 +428,8 @@ void SetFilteredPlayers(const CServerInfo &Item) void CServerBrowser::Sort() { - int i; - // fill m_NumFilteredPlayers - for(i = 0; i < m_NumServers; i++) + for(int i = 0; i < m_NumServers; i++) { SetFilteredPlayers(m_ppServerlist[i]->m_Info); } @@ -607,10 +604,8 @@ void CServerBrowser::SetLatency(NETADDR Addr, int Latency) CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int NumAddrs) { - CServerEntry *pEntry = 0; - // create new pEntry - pEntry = (CServerEntry *)m_ServerlistHeap.Allocate(sizeof(CServerEntry)); + CServerEntry *pEntry = (CServerEntry *)m_ServerlistHeap.Allocate(sizeof(CServerEntry)); mem_zero(pEntry, sizeof(CServerEntry)); // set the info @@ -761,7 +756,6 @@ void CServerBrowser::Refresh(int Type) { unsigned char aBuffer[sizeof(SERVERBROWSE_GETINFO) + 1]; CNetChunk Packet; - int i; /* do the broadcast version */ Packet.m_ClientID = -1; @@ -781,7 +775,7 @@ void CServerBrowser::Refresh(int Type) m_BroadcastTime = time_get(); - for(i = 8303; i <= 8310; i++) + for(int i = 8303; i <= 8310; i++) { Packet.m_Address.port = i; m_pNetClient->Send(&Packet); @@ -807,9 +801,6 @@ void CServerBrowser::Refresh(int Type) 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) { char aAddrStr[NETADDR_MAXSTRSIZE]; @@ -838,9 +829,11 @@ void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry, int *pBasicToken = GetBasicToken(Token); } + unsigned char aBuffer[sizeof(SERVERBROWSE_GETINFO) + 1]; mem_copy(aBuffer, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)); aBuffer[sizeof(SERVERBROWSE_GETINFO)] = GetBasicToken(Token); + CNetChunk Packet; Packet.m_ClientID = -1; Packet.m_Address = Addr; Packet.m_Flags = NETSENDFLAG_CONNLESS | NETSENDFLAG_EXTENDED; From c72861fb6dcc64938af13a77d966df9fff231fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:16:01 +0100 Subject: [PATCH 2/9] Rename class `SortWrap` to `CSortWrap` --- src/engine/client/serverbrowser.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 7a7baa45f..ad9ed78ac 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -34,14 +34,14 @@ #include // PAGE_DDNET -class SortWrap +class CSortWrap { typedef bool (CServerBrowser::*SortFunc)(int, int) const; SortFunc m_pfnSort; CServerBrowser *m_pThis; public: - SortWrap(CServerBrowser *pServer, SortFunc Func) : + CSortWrap(CServerBrowser *pServer, SortFunc Func) : 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)); } }; @@ -439,17 +439,17 @@ void CServerBrowser::Sort() // sort 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) - 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) - 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) - 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) - 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) - 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_aFilterString, g_Config.m_BrFilterString); From 1051a935a048f8d5605136fc0a3132fff6dc2348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:26:56 +0100 Subject: [PATCH 3/9] Use `nullptr`, `'\0'` and `false` instead of `0` --- src/engine/client/serverbrowser.cpp | 34 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index ad9ed78ac..837caa6b5 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -48,11 +48,11 @@ public: CServerBrowser::CServerBrowser() { - m_ppServerlist = 0; - m_pSortedServerlist = 0; + m_ppServerlist = nullptr; + m_pSortedServerlist = nullptr; - m_pFirstReqServer = 0; // request list - m_pLastReqServer = 0; + m_pFirstReqServer = nullptr; // request list + m_pLastReqServer = nullptr; m_NumRequests = 0; m_NumSortedServers = 0; @@ -61,14 +61,14 @@ CServerBrowser::CServerBrowser() m_NumServerCapacity = 0; m_Sorthash = 0; - m_aFilterString[0] = 0; - m_aFilterGametypeString[0] = 0; + m_aFilterString[0] = '\0'; + m_aFilterGametypeString[0] = '\0'; m_ServerlistType = 0; m_BroadcastTime = 0; secure_random_fill(m_aTokenSeed, sizeof(m_aTokenSeed)); - m_pDDNetInfo = 0; + m_pDDNetInfo = nullptr; m_SortOnNextUpdate = false; } @@ -171,7 +171,7 @@ void CServerBrowser::Con_LeakIpAddress(IConsole::IResult *pResult, void *pUserDa const CServerInfo *CServerBrowser::SortedGet(int Index) const { if(Index < 0 || Index >= m_NumSortedServers) - return 0; + return nullptr; return &m_ppServerlist[m_pSortedServerlist[Index]]->m_Info; } @@ -470,8 +470,8 @@ void CServerBrowser::RemoveRequest(CServerEntry *pEntry) else m_pLastReqServer = pEntry->m_pPrevReq; - pEntry->m_pPrevReq = 0; - pEntry->m_pNextReq = 0; + pEntry->m_pPrevReq = nullptr; + pEntry->m_pNextReq = nullptr; m_NumRequests--; } } @@ -495,7 +495,7 @@ void CServerBrowser::QueueRequest(CServerEntry *pEntry) else m_pFirstReqServer = pEntry; m_pLastReqServer = pEntry; - pEntry->m_pNextReq = 0; + pEntry->m_pNextReq = nullptr; m_NumRequests++; } @@ -510,7 +510,7 @@ void ServerBrowserFormatAddresses(char *pBuffer, int BufferSize, NETADDR *pAddrs return; } pBuffer[0] = ','; - pBuffer[1] = 0; + pBuffer[1] = '\0'; pBuffer += 1; BufferSize -= 1; } @@ -909,7 +909,7 @@ void CServerBrowser::UpdateFromHttp() pExcludeTypes = g_Config.m_BrFilterExcludeTypesKoG; break; default: - dbg_assert(0, "invalid network"); + dbg_assert(false, "invalid network"); return; } // remove unknown elements of exclude list @@ -1024,8 +1024,8 @@ void CServerBrowser::CleanUp() m_NumServers = 0; m_NumSortedServers = 0; m_ByAddr.clear(); - m_pFirstReqServer = 0; - m_pLastReqServer = 0; + m_pFirstReqServer = nullptr; + m_pLastReqServer = nullptr; m_NumRequests = 0; m_CurrentMaxRequests = g_Config.m_BrMaxRequests; } @@ -1280,7 +1280,7 @@ void CServerBrowser::LoadDDNetInfoJson() if(m_pDDNetInfo && m_pDDNetInfo->type != json_object) { json_value_free(m_pDDNetInfo); - m_pDDNetInfo = 0; + m_pDDNetInfo = nullptr; } m_OwnLocation = CServerInfo::LOC_UNKNOWN; @@ -1341,7 +1341,7 @@ const json_value *CServerBrowser::LoadDDNetInfo() bool CServerBrowser::IsRefreshing() const { - return m_pFirstReqServer != 0; + return m_pFirstReqServer != nullptr; } bool CServerBrowser::IsGettingServerlist() const From cbf08b002542e4bdd3667b58e20a8d626058e726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:30:09 +0100 Subject: [PATCH 4/9] Change type of `Filtered` from `int` to `bool` --- src/engine/client/serverbrowser.cpp | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 837caa6b5..b455cd4f7 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -267,33 +267,33 @@ void CServerBrowser::Filter() // filter the servers 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) - Filtered = 1; + Filtered = true; 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) - 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)) - 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)) - 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)) - Filtered = 1; + Filtered = true; else if(g_Config.m_BrFilterUnfinishedMap && m_ppServerlist[i]->m_Info.m_HasRank == 1) - Filtered = 1; + Filtered = true; else { if(g_Config.m_BrFilterCountry) { - Filtered = 1; + Filtered = true; // match against player country 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) { - Filtered = 0; + Filtered = false; break; } } @@ -342,7 +342,7 @@ void CServerBrowser::Filter() } if(!MatchFound) - Filtered = 1; + Filtered = true; } if(!Filtered && g_Config.m_BrExcludeString[0] != '\0') @@ -359,28 +359,28 @@ void CServerBrowser::Filter() // match against server name if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aName, aExcludeStr)) { - Filtered = 1; + Filtered = true; break; } // match against map if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aMap, aExcludeStr)) { - Filtered = 1; + Filtered = true; break; } // match against gametype if(str_utf8_find_nocase(m_ppServerlist[i]->m_Info.m_aGameType, aExcludeStr)) { - Filtered = 1; + Filtered = true; break; } } } } - if(Filtered == 0) + if(!Filtered) { // check for friend m_ppServerlist[i]->m_Info.m_FriendState = IFriends::FRIEND_NO; From 0c8aac6f0500e02a2893ea2f1022016c8f3a18a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:39:22 +0100 Subject: [PATCH 5/9] Remove unnecessary `MatchFound` variable This variable is always `1` when `m_ppServerlist[i]->m_Info.m_QuickSearchHit` is not `0`. --- src/engine/client/serverbrowser.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index b455cd4f7..344e18fba 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -301,8 +301,6 @@ void CServerBrowser::Filter() if(!Filtered && g_Config.m_BrFilterString[0] != '\0') { - int MatchFound = 0; - m_ppServerlist[i]->m_Info.m_QuickSearchHit = 0; const char *pStr = g_Config.m_BrFilterString; @@ -317,7 +315,6 @@ void CServerBrowser::Filter() // match against server name 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; } @@ -327,7 +324,6 @@ void CServerBrowser::Filter() 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)) { - MatchFound = 1; m_ppServerlist[i]->m_Info.m_QuickSearchHit |= IServerBrowser::QUICK_PLAYER; break; } @@ -336,12 +332,11 @@ void CServerBrowser::Filter() // match against map 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; } } - if(!MatchFound) + if(!m_ppServerlist[i]->m_Info.m_QuickSearchHit) Filtered = true; } From a6e0e37c4a05df353fb4fdee7f2ab1261d2f4bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:43:04 +0100 Subject: [PATCH 6/9] Use consistent system name `serverbrowser` for log messages --- src/engine/client/serverbrowser.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 344e18fba..0d35a7a2d 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -158,7 +158,7 @@ void CServerBrowser::Con_LeakIpAddress(IConsole::IResult *pResult, void *pUserDa pThis->QueueRequest(pChosen); char aAddr[NETADDR_MAXSTRSIZE]; 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) { @@ -727,7 +727,7 @@ void CServerBrowser::OnServerInfoUpdate(const NETADDR &Addr, int Token, const CS { char aAddr[NETADDR_MAXSTRSIZE]; 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); } pEntry->m_RequestTime = -1; // Request has been answered @@ -777,7 +777,7 @@ void CServerBrowser::Refresh(int Type) } 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) { @@ -802,7 +802,7 @@ void CServerBrowser::RequestImpl(const NETADDR &Addr, CServerEntry *pEntry, int net_addr_str(&Addr, aAddrStr, sizeof(aAddrStr), true); char aBuf[256]; 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); @@ -872,7 +872,7 @@ void CServerBrowser::UpdateFromHttp() { char aBuf[64]; 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); } } @@ -1146,7 +1146,7 @@ void CServerBrowser::LoadDDNetServers() 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; } @@ -1166,7 +1166,7 @@ void CServerBrowser::LoadDDNetServers() if(pAddrs->type != json_array) { - dbg_msg("client_srvbrowse", "invalid attributes"); + dbg_msg("serverbrowser", "invalid attributes"); continue; } @@ -1192,7 +1192,7 @@ void CServerBrowser::LoadDDNetServers() const json_value *pAddr = json_array_get(pAddrs, g); if(pAddr->type != json_string) { - dbg_msg("client_srvbrowse", "invalid attributes"); + dbg_msg("serverbrowser", "invalid attributes"); continue; } const char *pStr = json_string_get(pAddr); @@ -1286,7 +1286,7 @@ void CServerBrowser::LoadDDNetInfoJson() { char aBuf[64]; 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); } } } From d0635246c84879c7fb7e00fa7dfcb587316316fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 22:49:57 +0100 Subject: [PATCH 7/9] Remove unused `m_NeedRefresh` variable --- src/engine/client/serverbrowser.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/engine/client/serverbrowser.h b/src/engine/client/serverbrowser.h index a21d34474..a35c86eac 100644 --- a/src/engine/client/serverbrowser.h +++ b/src/engine/client/serverbrowser.h @@ -166,8 +166,6 @@ private: // used instead of g_Config.br_max_requests to get more servers int m_CurrentMaxRequests; - int m_NeedRefresh; - int m_NumSortedServers; int m_NumSortedServersCapacity; int m_NumServers; From 6d8fad775d85c3ddc1976809f7ae3adfa6ebaf11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 23:00:35 +0100 Subject: [PATCH 8/9] Encapsulate `m_ResortServerBrowser` inside `CServerBrowser` The variable `m_ResortServerBrowser` was only used in the client to pass it to the server browser. For better separation of concerns, this variable is moved inside `CServerBrowser` and the function `RequestResort` should be called to set it to `true`. The existing variable `m_SortOnNextUpdate` is replaced with this, as it served the same purpose already. The variable name from upstream is used to reduce conflicts. --- src/engine/client/client.cpp | 5 ++--- src/engine/client/client.h | 1 - src/engine/client/serverbrowser.cpp | 15 +++++++-------- src/engine/client/serverbrowser.h | 7 ++++--- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 23aeb30ff..b071b4535 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -2884,8 +2884,7 @@ void CClient::Update() } // update the server browser - m_ServerBrowser.Update(m_ResortServerBrowser); - m_ResortServerBrowser = false; + m_ServerBrowser.Update(); // update editor/gameclient if(m_EditorActive) @@ -4031,7 +4030,7 @@ void CClient::UpdateAndSwap() void CClient::ServerBrowserUpdate() { - m_ResortServerBrowser = true; + m_ServerBrowser.RequestResort(); } void CClient::ConchainServerBrowserUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 4f65b21e7..0f5159638 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -158,7 +158,6 @@ class CClient : public IClient, public CDemoPlayer::IListener bool m_AutoCSVRecycle; bool m_EditorActive; bool m_SoundInitFailed; - bool m_ResortServerBrowser; int m_aAckGameTick[NUM_DUMMIES]; int m_aCurrentRecvTick[NUM_DUMMIES]; diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 0d35a7a2d..92b6e4095 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -55,6 +55,8 @@ CServerBrowser::CServerBrowser() m_pLastReqServer = nullptr; m_NumRequests = 0; + m_NeedResort = false; + m_NumSortedServers = 0; m_NumSortedServersCapacity = 0; m_NumServers = 0; @@ -69,8 +71,6 @@ CServerBrowser::CServerBrowser() secure_random_fill(m_aTokenSeed, sizeof(m_aTokenSeed)); m_pDDNetInfo = nullptr; - - m_SortOnNextUpdate = false; } CServerBrowser::~CServerBrowser() @@ -733,8 +733,7 @@ void CServerBrowser::OnServerInfoUpdate(const NETADDR &Addr, int Token, const CS pEntry->m_RequestTime = -1; // Request has been answered } RemoveRequest(pEntry); - - m_SortOnNextUpdate = true; + RequestResort(); } void CServerBrowser::Refresh(int Type) @@ -1009,7 +1008,7 @@ void CServerBrowser::UpdateFromHttp() } } - m_SortOnNextUpdate = true; + RequestResort(); } void CServerBrowser::CleanUp() @@ -1025,7 +1024,7 @@ void CServerBrowser::CleanUp() m_CurrentMaxRequests = g_Config.m_BrMaxRequests; } -void CServerBrowser::Update(bool ForceResort) +void CServerBrowser::Update() { int64_t Timeout = time_freq(); int64_t Now = time_get(); @@ -1104,7 +1103,7 @@ void CServerBrowser::Update(bool ForceResort) } // 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++) { @@ -1113,7 +1112,7 @@ void CServerBrowser::Update(bool ForceResort) pInfo->m_FavoriteAllowPing = m_pFavorites->IsPingAllowed(pInfo->m_aAddresses, pInfo->m_NumAddresses); } Sort(); - m_SortOnNextUpdate = false; + m_NeedResort = false; } } diff --git a/src/engine/client/serverbrowser.h b/src/engine/client/serverbrowser.h index a35c86eac..73b290765 100644 --- a/src/engine/client/serverbrowser.h +++ b/src/engine/client/serverbrowser.h @@ -83,6 +83,7 @@ public: bool IsRefreshing() const override; bool IsGettingServerlist() const override; int LoadingProgression() const override; + void RequestResort() { m_NeedResort = true; } int NumServers() const override { return m_NumServers; } @@ -120,7 +121,7 @@ public: void TypeFilterClean(int Network) override; // - void Update(bool ForceResort); + void Update(); void OnServerInfoUpdate(const NETADDR &Addr, int Token, const CServerInfo *pInfo); void SetHttpInfo(const CServerInfo *pInfo); void RequestCurrentServer(const NETADDR &Addr) const; @@ -163,6 +164,8 @@ private: CServerEntry *m_pLastReqServer; int m_NumRequests; + bool m_NeedResort; + // used instead of g_Config.br_max_requests to get more servers int m_CurrentMaxRequests; @@ -179,8 +182,6 @@ private: int64_t m_BroadcastTime; unsigned char m_aTokenSeed[16]; - bool m_SortOnNextUpdate; - int GenerateToken(const NETADDR &Addr) const; static int GetBasicToken(int Token); static int GetExtraToken(int Token); From 57d5c5a3d5107a3897a269cb11d5172303679f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 5 Dec 2022 23:06:51 +0100 Subject: [PATCH 9/9] Use for-each loop instead of for-loop --- src/engine/client/serverbrowser.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index 92b6e4095..b447bc8f0 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -618,11 +618,11 @@ CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int Num // check if it's an official server 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 l = 0; !Official && l < NumAddrs; l++) @@ -634,6 +634,8 @@ CServerBrowser::CServerEntry *CServerBrowser::Add(const NETADDR *pAddrs, int Num } } } + if(Official) + break; } pEntry->m_Info.m_Official = Official;