From 2cdb5ce2159203945407729024a1c81889e4e1fc Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 8 Jan 2012 18:47:30 +0000 Subject: [PATCH] No longer use the server list md5, it caused only trouble, and the serverlist .z file is small enough to be downloaded each time (#95). --- src/gui/qt/startwindow/startwindowimpl.cpp | 12 --- src/net/clientstate.h | 28 ----- src/net/common/clientstate.cpp | 113 ++------------------- src/net/socket_msg.h | 16 ++- 4 files changed, 17 insertions(+), 152 deletions(-) diff --git a/src/gui/qt/startwindow/startwindowimpl.cpp b/src/gui/qt/startwindow/startwindowimpl.cpp index 70193ddc..0d580e68 100644 --- a/src/gui/qt/startwindow/startwindowimpl.cpp +++ b/src/gui/qt/startwindow/startwindowimpl.cpp @@ -734,18 +734,6 @@ void startWindowImpl::networkError(int errorID, int /*osErrorID*/) QMessageBox::Close); } break; - case ERR_SOCK_OPEN_MD5_FAILED: { - QMessageBox::warning(this, tr("Network Error"), - tr("Could not open the server list MD5 file.\nPlease make sure that the server list URL is correct."), - QMessageBox::Close); - } - break; - case ERR_SOCK_INVALID_SERVERLIST_MD5: { - QMessageBox::warning(this, tr("Network Error"), - tr("Synchronization of the PokerTH internet server list has failed.\nPlease make sure that the server list URL is correct."), - QMessageBox::Close); - } - break; case ERR_SOCK_INVALID_SERVERLIST_XML: { QMessageBox::warning(this, tr("Network Error"), tr("The PokerTH internet server list contains invalid data.\nIf you use a custom server list, please make sure its format is correct."), diff --git a/src/net/clientstate.h b/src/net/clientstate.h index 1f1ee81b..8b09d8fb 100644 --- a/src/net/clientstate.h +++ b/src/net/clientstate.h @@ -107,34 +107,6 @@ protected: ClientStateStartServerListDownload(); }; -// State: Synchronizing server list. -class ClientStateSynchronizingServerList : public ClientState -{ -public: - // Access the state singleton. - static ClientStateSynchronizingServerList &Instance(); - virtual ~ClientStateSynchronizingServerList(); - - virtual void Enter(boost::shared_ptr client); - virtual void Exit(boost::shared_ptr client); - - virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} - - void SetDownloadHelper(boost::shared_ptr helper); - -protected: - - // Protected constructor - this is a singleton. - ClientStateSynchronizingServerList(); - - // Poll for the completion of the download. - void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client); - -private: - - boost::shared_ptr m_downloadHelper; -}; - // State: Downloading the server list. class ClientStateDownloadingServerList : public ClientState { diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 8806cfa7..d45834e9 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -178,24 +178,20 @@ ClientStateStartServerListDownload::Enter(boost::shared_ptr client string serverListUrl(context.GetServerListUrl()); // Retrieve the file name from the URL. size_t pos = serverListUrl.find_last_of('/'); - if (serverListUrl.empty() || pos == string::npos || ++pos >= serverListUrl.length()) { + 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); if (exists(tmpServerListPath)) { - // Download and compare md5. - tmpServerListPath = tmpServerListPath.directory_string() + ".md5"; - boost::shared_ptr downloader(new DownloadHelper); - downloader->Init(serverListUrl + ".md5", tmpServerListPath.directory_string()); - ClientStateSynchronizingServerList::Instance().SetDownloadHelper(downloader); - client->SetState(ClientStateSynchronizingServerList::Instance()); - } else { - // Download server list. - boost::shared_ptr downloader(new DownloadHelper); - downloader->Init(serverListUrl, tmpServerListPath.directory_string()); - ClientStateDownloadingServerList::Instance().SetDownloadHelper(downloader); - client->SetState(ClientStateDownloadingServerList::Instance()); + // 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 @@ -206,95 +202,6 @@ ClientStateStartServerListDownload::Exit(boost::shared_ptr /*clien //----------------------------------------------------------------------------- -ClientStateSynchronizingServerList & -ClientStateSynchronizingServerList::Instance() -{ - static ClientStateSynchronizingServerList state; - return state; -} - -ClientStateSynchronizingServerList::ClientStateSynchronizingServerList() -{ -} - -ClientStateSynchronizingServerList::~ClientStateSynchronizingServerList() -{ -} - -void -ClientStateSynchronizingServerList::Enter(boost::shared_ptr client) -{ - client->GetStateTimer().expires_from_now( - boost::posix_time::milliseconds(CLIENT_WAIT_TIMEOUT_MSEC)); - client->GetStateTimer().async_wait( - boost::bind( - &ClientStateSynchronizingServerList::TimerLoop, this, boost::asio::placeholders::error, client)); -} - -void -ClientStateSynchronizingServerList::Exit(boost::shared_ptr client) -{ - client->GetStateTimer().cancel(); -} - -void -ClientStateSynchronizingServerList::SetDownloadHelper(boost::shared_ptr helper) -{ - m_downloadHelper = helper; -} - -void -ClientStateSynchronizingServerList::TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client) -{ - if (!ec && &client->GetState() == this) { - if (m_downloadHelper->Process()) { - m_downloadHelper.reset(); - ClientContext &context = client->GetContext(); - path md5ServerListPath(context.GetCacheDir()); - - // No more checking needed as this was done before. - md5ServerListPath /= context.GetServerListUrl().substr(context.GetServerListUrl().find_last_of('/') + 1) + ".md5"; - path serverListPath = change_extension(md5ServerListPath, ""); - // Compare the md5 sums. - string tmpMd5; - { - ifstream inFile(md5ServerListPath.directory_string().c_str(), ios_base::in); - if (inFile.fail()) - throw ClientException(__FILE__, __LINE__, ERR_SOCK_OPEN_MD5_FAILED, 0); - inFile >> tmpMd5; - } - MD5Buf downloadedMd5; - if (!downloadedMd5.FromString(tmpMd5)) - throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_MD5, 0); - MD5Buf currentMd5; - if (!CryptHelper::MD5Sum(serverListPath.directory_string(), currentMd5)) - throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_MD5, 0); - if (downloadedMd5 == currentMd5) { - // Server list is still current. - client->SetState(ClientStateReadingServerList::Instance()); - } else { - // Download new server list. - // Paranoia check before removing the file, we do not want to delete wrong files. - path tmpPath(serverListPath); - if (path(context.GetCacheDir()) == tmpPath.remove_leaf()) { - remove(serverListPath); - client->SetState(ClientStateStartServerListDownload::Instance()); - } else - throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_URL, 0); - } - } else { - // Download still in process. Delay. - client->GetStateTimer().expires_from_now( - boost::posix_time::milliseconds(CLIENT_WAIT_TIMEOUT_MSEC)); - client->GetStateTimer().async_wait( - boost::bind( - &ClientStateSynchronizingServerList::TimerLoop, this, boost::asio::placeholders::error, client)); - } - } -} - -//----------------------------------------------------------------------------- - ClientStateDownloadingServerList & ClientStateDownloadingServerList::Instance() { @@ -379,7 +286,7 @@ ClientStateReadingServerList::Enter(boost::shared_ptr client) // Unzip the file using zlib. try { ifstream inFile(zippedServerListPath.directory_string().c_str(), ios_base::in | ios_base::binary); - ofstream outFile(xmlServerListPath.directory_string().c_str(), ios_base::out); + ofstream outFile(xmlServerListPath.directory_string().c_str(), ios_base::out | ios_base::trunc); boost::iostreams::filtering_streambuf in; in.push(boost::iostreams::zlib_decompressor()); in.push(inFile); diff --git a/src/net/socket_msg.h b/src/net/socket_msg.h index 13cd583e..5982fe26 100644 --- a/src/net/socket_msg.h +++ b/src/net/socket_msg.h @@ -41,15 +41,13 @@ #define ERR_SOCK_INVALID_STATE 19 #define ERR_SOCK_INVALID_TYPE 20 #define ERR_SOCK_INVALID_SERVERLIST_URL 21 -#define ERR_SOCK_OPEN_MD5_FAILED 22 -#define ERR_SOCK_INVALID_SERVERLIST_MD5 23 -#define ERR_SOCK_INVALID_SERVERLIST_XML 24 -#define ERR_SOCK_UNZIP_FAILED 25 -#define ERR_SOCK_TRANSFER_INIT_FAILED 26 -#define ERR_SOCK_TRANSFER_OPEN_FAILED 27 -#define ERR_SOCK_TRANSFER_INVALID_URL 28 -#define ERR_SOCK_TRANSFER_SELECT_FAILED 29 -#define ERR_SOCK_TRANSFER_FAILED 30 +#define ERR_SOCK_INVALID_SERVERLIST_XML 22 +#define ERR_SOCK_UNZIP_FAILED 23 +#define ERR_SOCK_TRANSFER_INIT_FAILED 24 +#define ERR_SOCK_TRANSFER_OPEN_FAILED 25 +#define ERR_SOCK_TRANSFER_INVALID_URL 26 +#define ERR_SOCK_TRANSFER_SELECT_FAILED 27 +#define ERR_SOCK_TRANSFER_FAILED 28 // The following errors are game errors. #define ERR_NET_VERSION_NOT_SUPPORTED 101 #define ERR_NET_SERVER_MAINTENANCE 102