From 58a0d3fb2e32efef0e687215e2795506d16c89e7 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sat, 4 Feb 2012 20:33:39 +0000 Subject: [PATCH] Download serverlist only once a day. Delete the current list if the connection to the server fails. #119 --- src/net/clientstate.h | 3 ++ src/net/common/clientstate.cpp | 53 +++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/net/clientstate.h b/src/net/clientstate.h index 8b09d8fb..d741b8c4 100644 --- a/src/net/clientstate.h +++ b/src/net/clientstate.h @@ -28,6 +28,7 @@ #define CLIENT_FINAL_STATE ClientStateFinal class ClientThread; +class ClientContext; class ClientCallback; class Game; class NetPacket; @@ -101,6 +102,8 @@ public: virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} + static std::string GetCacheServerListFileName(const ClientContext &context); + protected: // Protected constructor - this is a singleton. diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 2b60d5a0..9506ccea 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -174,6 +174,34 @@ void ClientStateStartServerListDownload::Enter(boost::shared_ptr client) { const ClientContext &context = client->GetContext(); + path tmpServerListPath(GetCacheServerListFileName(context)); + if (exists(tmpServerListPath) && (last_write_time(tmpServerListPath) + 86400 < time(NULL))) { + // Download the current server list once a day. + // If the previous file is older than one day, delete it. + remove(tmpServerListPath); + } + + if (exists(tmpServerListPath)) { + // Use the existing server list. + client->SetState(ClientStateReadingServerList::Instance()); + } else { + // Download the server list. + boost::shared_ptr downloader(new DownloadHelper); + downloader->Init(context.GetServerListUrl(), tmpServerListPath.directory_string()); + ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader); + client->SetState(ClientStateDownloadingServerList::Instance()); + } +} + +void +ClientStateStartServerListDownload::Exit(boost::shared_ptr /*client*/) +{ + // Nothing to do. +} + +string +ClientStateStartServerListDownload::GetCacheServerListFileName(const ClientContext &context) +{ path tmpServerListPath(context.GetCacheDir()); string serverListUrl(context.GetServerListUrl()); // Retrieve the file name from the URL. @@ -182,22 +210,7 @@ ClientStateStartServerListDownload::Enter(boost::shared_ptr client throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0); } tmpServerListPath /= serverListUrl.substr(pos); - if (exists(tmpServerListPath)) { - // Always download the current server list. - // If a previous file exists, delete it. - remove(tmpServerListPath); - } - // Download server list. - boost::shared_ptr downloader(new DownloadHelper); - downloader->Init(serverListUrl, tmpServerListPath.directory_string()); - ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader); - client->SetState(ClientStateDownloadingServerList::Instance()); -} - -void -ClientStateStartServerListDownload::Exit(boost::shared_ptr /*client*/) -{ - // Nothing to do. + return tmpServerListPath.directory_string(); } //----------------------------------------------------------------------------- @@ -493,8 +506,14 @@ ClientStateStartConnect::HandleConnect(const boost::system::error_code& ec, boos ++m_remoteEndpointIterator, client)); } else { - if (ec != boost::asio::error::operation_aborted) + if (ec != boost::asio::error::operation_aborted) { + // Delete the cached server list, as it may be outdated. + path tmpServerListPath(ClientStateStartServerListDownload::GetCacheServerListFileName(client->GetContext())); + if (exists(tmpServerListPath)) { + remove(tmpServerListPath); + } throw ClientException(__FILE__, __LINE__, ERR_SOCK_CONNECT_FAILED, ec.value()); + } } } }