Download serverlist only once a day. Delete the current list if the connection to the server fails. #119
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
#define CLIENT_FINAL_STATE ClientStateFinal
|
#define CLIENT_FINAL_STATE ClientStateFinal
|
||||||
|
|
||||||
class ClientThread;
|
class ClientThread;
|
||||||
|
class ClientContext;
|
||||||
class ClientCallback;
|
class ClientCallback;
|
||||||
class Game;
|
class Game;
|
||||||
class NetPacket;
|
class NetPacket;
|
||||||
@@ -101,6 +102,8 @@ 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.
|
||||||
|
|||||||
@@ -174,6 +174,34 @@ void
|
|||||||
ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client)
|
ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client)
|
||||||
{
|
{
|
||||||
const ClientContext &context = client->GetContext();
|
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<DownloadHelper> downloader(new DownloadHelper);
|
||||||
|
downloader->Init(context.GetServerListUrl(), tmpServerListPath.directory_string());
|
||||||
|
ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader);
|
||||||
|
client->SetState(ClientStateDownloadingServerList::Instance());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ClientStateStartServerListDownload::Exit(boost::shared_ptr<ClientThread> /*client*/)
|
||||||
|
{
|
||||||
|
// Nothing to do.
|
||||||
|
}
|
||||||
|
|
||||||
|
string
|
||||||
|
ClientStateStartServerListDownload::GetCacheServerListFileName(const ClientContext &context)
|
||||||
|
{
|
||||||
path tmpServerListPath(context.GetCacheDir());
|
path tmpServerListPath(context.GetCacheDir());
|
||||||
string serverListUrl(context.GetServerListUrl());
|
string serverListUrl(context.GetServerListUrl());
|
||||||
// Retrieve the file name from the URL.
|
// Retrieve the file name from the URL.
|
||||||
@@ -182,22 +210,7 @@ ClientStateStartServerListDownload::Enter(boost::shared_ptr<ClientThread> client
|
|||||||
throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0);
|
throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0);
|
||||||
}
|
}
|
||||||
tmpServerListPath /= serverListUrl.substr(pos);
|
tmpServerListPath /= serverListUrl.substr(pos);
|
||||||
if (exists(tmpServerListPath)) {
|
return tmpServerListPath.directory_string();
|
||||||
// Always download the current server list.
|
|
||||||
// If a previous file exists, delete it.
|
|
||||||
remove(tmpServerListPath);
|
|
||||||
}
|
|
||||||
// Download server list.
|
|
||||||
boost::shared_ptr<DownloadHelper> downloader(new DownloadHelper);
|
|
||||||
downloader->Init(serverListUrl, tmpServerListPath.directory_string());
|
|
||||||
ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader);
|
|
||||||
client->SetState(ClientStateDownloadingServerList::Instance());
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ClientStateStartServerListDownload::Exit(boost::shared_ptr<ClientThread> /*client*/)
|
|
||||||
{
|
|
||||||
// Nothing to do.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@@ -493,8 +506,14 @@ ClientStateStartConnect::HandleConnect(const boost::system::error_code& ec, boos
|
|||||||
++m_remoteEndpointIterator,
|
++m_remoteEndpointIterator,
|
||||||
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user