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*/) {}
|
virtual void HandlePacket(boost::shared_ptr<ClientThread> /*client*/, boost::shared_ptr<NetPacket> /*tmpPacket*/) {}
|
||||||
|
|
||||||
static std::string GetCacheServerListFileName(const ClientContext &context);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
// Protected constructor - this is a singleton.
|
// Protected constructor - this is a singleton.
|
||||||
|
|||||||
@@ -156,6 +156,7 @@ protected:
|
|||||||
|
|
||||||
const ClientContext &GetContext() const;
|
const ClientContext &GetContext() const;
|
||||||
ClientContext &GetContext();
|
ClientContext &GetContext();
|
||||||
|
std::string GetCacheServerListFileName();
|
||||||
void CreateContextSession();
|
void CreateContextSession();
|
||||||
|
|
||||||
ClientState &GetState();
|
ClientState &GetState();
|
||||||
|
|||||||
@@ -173,12 +173,14 @@ ClientStateStartServerListDownload::~ClientStateStartServerListDownload()
|
|||||||
void
|
void
|
||||||
ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client)
|
ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client)
|
||||||
{
|
{
|
||||||
const ClientContext &context = client->GetContext();
|
path tmpServerListPath(client->GetCacheServerListFileName());
|
||||||
path tmpServerListPath(GetCacheServerListFileName(context));
|
if (exists(tmpServerListPath)) {
|
||||||
if (exists(tmpServerListPath) && (last_write_time(tmpServerListPath) + 86400 < time(NULL))) {
|
|
||||||
// Download the current server list once a day.
|
// Download the current server list once a day.
|
||||||
// If the previous file is older than one day, delete it.
|
// 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)) {
|
if (exists(tmpServerListPath)) {
|
||||||
@@ -187,7 +189,7 @@ ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client
|
|||||||
} else {
|
} else {
|
||||||
// Download the server list.
|
// Download the server list.
|
||||||
boost::shared_ptr<DownloadHelper> downloader(new DownloadHelper);
|
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);
|
ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader);
|
||||||
client->SetState(ClientStateDownloadingServerList::Instance());
|
client->SetState(ClientStateDownloadingServerList::Instance());
|
||||||
}
|
}
|
||||||
@@ -199,20 +201,6 @@ ClientStateStartServerListDownload::Exit(boost::shared_ptr<ClientThread> /*clien
|
|||||||
// Nothing to do.
|
// 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 &
|
ClientStateDownloadingServerList &
|
||||||
@@ -507,11 +495,6 @@ ClientStateStartConnect::HandleConnect(const boost::system::error_code& ec, boos
|
|||||||
client));
|
client));
|
||||||
} else {
|
} 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());
|
throw ClientException(__FILE__, __LINE__, ERR_SOCK_CONNECT_FAILED, ec.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@
|
|||||||
#include <qttoolsinterface.h>
|
#include <qttoolsinterface.h>
|
||||||
|
|
||||||
#include <boost/lambda/lambda.hpp>
|
#include <boost/lambda/lambda.hpp>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -47,6 +48,7 @@
|
|||||||
#define CLIENT_SEND_LOOP_MSEC 50
|
#define CLIENT_SEND_LOOP_MSEC 50
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace boost::filesystem;
|
||||||
using boost::asio::ip::tcp;
|
using boost::asio::ip::tcp;
|
||||||
|
|
||||||
ClientThread::ClientThread(GuiInterface &gui, AvatarManager &avatarManager, Log *myLog)
|
ClientThread::ClientThread(GuiInterface &gui, AvatarManager &avatarManager, Log *myLog)
|
||||||
@@ -542,6 +544,11 @@ ClientThread::Main()
|
|||||||
m_ioService->run(); // Will only be aborted asynchronously.
|
m_ioService->run(); // Will only be aborted asynchronously.
|
||||||
|
|
||||||
} catch (const PokerTHException &e) {
|
} 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());
|
GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
|
||||||
}
|
}
|
||||||
// Close the socket.
|
// Close the socket.
|
||||||
@@ -916,6 +923,20 @@ ClientThread::GetContext()
|
|||||||
return *m_context;
|
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
|
void
|
||||||
ClientThread::CreateContextSession()
|
ClientThread::CreateContextSession()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user