Refresh server browser immediately when updated ddnet info arrived

At the moment it only happens the next time you press refresh.

Before we had it so that it always reloaded once the ddnet info arrived,
causing a quick flickering and lots of packets having to be resent every
time someone pressed refresh, even if nothing changed.

The new approach combines the advantages of both without the
disadvantages. An even nicer way would be to compare the json objects,
so that news and version updates don't matter, but our json library
doesn't seem to support that.
This commit is contained in:
def 2020-09-09 17:49:29 +02:00
parent ddc4b74354
commit 279deffa86
2 changed files with 52 additions and 2 deletions

View file

@ -2381,11 +2381,60 @@ void CClient::ResetDDNetInfo()
}
}
bool CClient::IsDDNetInfoChanged()
{
IOHANDLE OldFile = m_pStorage->OpenFile(DDNET_INFO, IOFLAG_READ, IStorage::TYPE_SAVE);
if(!OldFile)
return true;
IOHANDLE NewFile = m_pStorage->OpenFile(m_aDDNetInfoTmp, IOFLAG_READ, IStorage::TYPE_SAVE);
if(NewFile)
{
char aOldData[4096];
char aNewData[4096];
unsigned OldBytes;
unsigned NewBytes;
do
{
OldBytes = io_read(OldFile, aOldData, sizeof(aOldData));
NewBytes = io_read(NewFile, aNewData, sizeof(aNewData));
if(OldBytes != NewBytes || mem_comp(aOldData, aNewData, OldBytes) != 0)
{
io_close(NewFile);
io_close(OldFile);
return true;
}
}
while(OldBytes > 0);
io_close(NewFile);
}
io_close(OldFile);
return false;
}
void CClient::FinishDDNetInfo()
{
ResetDDNetInfo();
m_pStorage->RenameFile(m_aDDNetInfoTmp, DDNET_INFO, IStorage::TYPE_SAVE);
LoadDDNetInfo();
if(IsDDNetInfoChanged())
{
m_pStorage->RenameFile(m_aDDNetInfoTmp, DDNET_INFO, IStorage::TYPE_SAVE);
LoadDDNetInfo();
if(g_Config.m_UiPage == CMenus::PAGE_DDNET)
m_ServerBrowser.Refresh(IServerBrowser::TYPE_DDNET);
else if(g_Config.m_UiPage == CMenus::PAGE_KOG)
m_ServerBrowser.Refresh(IServerBrowser::TYPE_KOG);
}
else
{
m_pStorage->RemoveFile(m_aDDNetInfoTmp, IStorage::TYPE_SAVE);
}
}
typedef std::tuple<int, int, int> Version;

View file

@ -343,6 +343,7 @@ public:
void RequestDDNetInfo();
void ResetDDNetInfo();
bool IsDDNetInfoChanged();
void FinishDDNetInfo();
void LoadDDNetInfo();