diff --git a/src/net/clientstate.h b/src/net/clientstate.h index d741b8c4..e6699c89 100644 --- a/src/net/clientstate.h +++ b/src/net/clientstate.h @@ -102,8 +102,6 @@ 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/clientthread.h b/src/net/clientthread.h index b6424cc5..fd5779c6 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -156,6 +156,7 @@ protected: const ClientContext &GetContext() const; ClientContext &GetContext(); + std::string GetCacheServerListFileName(); void CreateContextSession(); ClientState &GetState(); diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 9506ccea..a7d9b0cb 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -173,12 +173,14 @@ ClientStateStartServerListDownload::~ClientStateStartServerListDownload() 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))) { + path tmpServerListPath(client->GetCacheServerListFileName()); + if (exists(tmpServerListPath)) { // Download the current server list once a day. // If the previous file is older than one day, delete it. - remove(tmpServerListPath); + // Also delete the file if it is empty. + if (file_size(tmpServerListPath) == 0 || (last_write_time(tmpServerListPath) + 86400 < time(NULL))) { + remove(tmpServerListPath); + } } if (exists(tmpServerListPath)) { @@ -187,7 +189,7 @@ ClientStateStartServerListDownload::Enter(boost::shared_ptr client } else { // Download the server list. boost::shared_ptr downloader(new DownloadHelper); - downloader->Init(context.GetServerListUrl(), tmpServerListPath.directory_string()); + downloader->Init(client->GetContext().GetServerListUrl(), tmpServerListPath.directory_string()); ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader); client->SetState(ClientStateDownloadingServerList::Instance()); } @@ -199,20 +201,6 @@ ClientStateStartServerListDownload::Exit(boost::shared_ptr /*clien // Nothing to do. } -string -ClientStateStartServerListDownload::GetCacheServerListFileName(const ClientContext &context) -{ - path tmpServerListPath(context.GetCacheDir()); - string serverListUrl(context.GetServerListUrl()); - // Retrieve the file name from the URL. - size_t pos = serverListUrl.find_last_of('/'); - if (context.GetCacheDir().empty() || serverListUrl.empty() || pos == string::npos || ++pos >= serverListUrl.length()) { - throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0); - } - tmpServerListPath /= serverListUrl.substr(pos); - return tmpServerListPath.directory_string(); -} - //----------------------------------------------------------------------------- ClientStateDownloadingServerList & @@ -507,11 +495,6 @@ ClientStateStartConnect::HandleConnect(const boost::system::error_code& ec, boos client)); } else { 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()); } } diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 6ba5d33c..d6bfdcea 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -47,6 +48,7 @@ #define CLIENT_SEND_LOOP_MSEC 50 using namespace std; +using namespace boost::filesystem; using boost::asio::ip::tcp; ClientThread::ClientThread(GuiInterface &gui, AvatarManager &avatarManager, Log *myLog) @@ -542,6 +544,11 @@ ClientThread::Main() m_ioService->run(); // Will only be aborted asynchronously. } catch (const PokerTHException &e) { + // Delete the cached server list, as it may be outdated. + path tmpServerListPath(GetCacheServerListFileName()); + if (exists(tmpServerListPath)) { + remove(tmpServerListPath); + } GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode()); } // Close the socket. @@ -916,6 +923,20 @@ ClientThread::GetContext() return *m_context; } +string +ClientThread::GetCacheServerListFileName() +{ + path tmpServerListPath(GetContext().GetCacheDir()); + string serverListUrl(GetContext().GetServerListUrl()); + // Retrieve the file name from the URL. + size_t pos = serverListUrl.find_last_of('/'); + if (GetContext().GetCacheDir().empty() || serverListUrl.empty() || pos == string::npos || ++pos >= serverListUrl.length()) { + throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0); + } + tmpServerListPath /= serverListUrl.substr(pos); + return tmpServerListPath.directory_string(); +} + void ClientThread::CreateContextSession() {