Cleaning up serverlist caching.
This commit is contained in:
@@ -102,8 +102,6 @@ public:
|
||||
|
||||
virtual void HandlePacket(boost::shared_ptr<ClientThread> /*client*/, boost::shared_ptr<NetPacket> /*tmpPacket*/) {}
|
||||
|
||||
static std::string GetCacheServerListFileName(const ClientContext &context);
|
||||
|
||||
protected:
|
||||
|
||||
// Protected constructor - this is a singleton.
|
||||
|
||||
@@ -156,6 +156,7 @@ protected:
|
||||
|
||||
const ClientContext &GetContext() const;
|
||||
ClientContext &GetContext();
|
||||
std::string GetCacheServerListFileName();
|
||||
void CreateContextSession();
|
||||
|
||||
ClientState &GetState();
|
||||
|
||||
@@ -173,12 +173,14 @@ ClientStateStartServerListDownload::~ClientStateStartServerListDownload()
|
||||
void
|
||||
ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> 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<ClientThread> client
|
||||
} else {
|
||||
// Download the server list.
|
||||
boost::shared_ptr<DownloadHelper> 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<ClientThread> /*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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <qttoolsinterface.h>
|
||||
|
||||
#include <boost/lambda/lambda.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user