From 8eeb0b550cc6e4a372f802a2f850f192d11f04ac Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 1 Jan 2011 03:12:28 +0000 Subject: [PATCH] - more ftp tileset bugfixes --- .../menu/menu_state_connected_game.cpp | 8 +- .../menu/menu_state_connected_game.h | 2 +- .../include/platform/posix/miniftpclient.h | 7 +- .../sources/feathery_ftp/ftpTargetWin32.c | 2 +- .../sources/platform/posix/miniftpclient.cpp | 113 ++++++++++++------ .../sources/platform/posix/miniftpserver.cpp | 2 +- 6 files changed, 92 insertions(+), 42 deletions(-) diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 2d631454..7ffdd59c 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -1674,10 +1674,10 @@ void MenuStateConnectedGame::showFTPMessageBox(const string &text, const string } } -void MenuStateConnectedGame::FTPClient_CallbackEvent(string itemName, FTP_Client_ResultType result) { +void MenuStateConnectedGame::FTPClient_CallbackEvent(string itemName, FTP_Client_CallbackType type, FTP_Client_ResultType result) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(ftpMissingDataType == ftpmsg_MissingMap) { + if(type == ftp_cct_Map) { getMissingMapFromFTPServerInProgress = false; printf("Got FTP Callback for [%s] result = %d\n",itemName.c_str(),result); @@ -1696,7 +1696,7 @@ void MenuStateConnectedGame::FTPClient_CallbackEvent(string itemName, FTP_Client clientInterface->sendTextMessage(szMsg,-1, true); } } - else if(ftpMissingDataType == ftpmsg_MissingTileset) { + else if(type == ftp_cct_Tileset) { getMissingTilesetFromFTPServerInProgress = false; printf("Got FTP Callback for [%s] result = %d\n",itemName.c_str(),result); @@ -1708,6 +1708,8 @@ void MenuStateConnectedGame::FTPClient_CallbackEvent(string itemName, FTP_Client char szMsg[1024]=""; sprintf(szMsg,"Player: %s SUCCESSFULLY downloaded the tileset: %s",getHumanPlayerName().c_str(),gameSettings->getTileset().c_str()); clientInterface->sendTextMessage(szMsg,-1, true); + + findDirs(Config::getInstance().getPathListForType(ptTilesets), tileSets); } else { char szMsg[1024]=""; diff --git a/source/glest_game/menu/menu_state_connected_game.h b/source/glest_game/menu/menu_state_connected_game.h index 0f62e334..32e4aaf0 100644 --- a/source/glest_game/menu/menu_state_connected_game.h +++ b/source/glest_game/menu/menu_state_connected_game.h @@ -181,7 +181,7 @@ private: void showMessageBox(const string &text, const string &header, bool toggle); void showFTPMessageBox(const string &text, const string &header, bool toggle); - virtual void FTPClient_CallbackEvent(string itemName, FTP_Client_ResultType result); + virtual void FTPClient_CallbackEvent(string itemName, FTP_Client_CallbackType type, FTP_Client_ResultType result); }; }}//end namespace diff --git a/source/shared_lib/include/platform/posix/miniftpclient.h b/source/shared_lib/include/platform/posix/miniftpclient.h index a201f14d..a1651931 100644 --- a/source/shared_lib/include/platform/posix/miniftpclient.h +++ b/source/shared_lib/include/platform/posix/miniftpclient.h @@ -32,9 +32,14 @@ enum FTP_Client_ResultType { ftp_crt_ABORTED = 2 }; +enum FTP_Client_CallbackType { + ftp_cct_Map = 0, + ftp_cct_Tileset = 1 +}; + class FTPClientCallbackInterface { public: - virtual void FTPClient_CallbackEvent(string mapFilename, FTP_Client_ResultType result) = 0; + virtual void FTPClient_CallbackEvent(string itemName, FTP_Client_CallbackType type, FTP_Client_ResultType result) = 0; }; class FTPClientThread : public BaseThread diff --git a/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c b/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c index 8520eb2f..f9d14648 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c +++ b/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c @@ -408,7 +408,7 @@ int ftpSelect(int poll) if(poll) { - struct timeval t = {0}; + struct timeval t = {250}; return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t); } else diff --git a/source/shared_lib/sources/platform/posix/miniftpclient.cpp b/source/shared_lib/sources/platform/posix/miniftpclient.cpp index 77b571a7..608f732b 100644 --- a/source/shared_lib/sources/platform/posix/miniftpclient.cpp +++ b/source/shared_lib/sources/platform/posix/miniftpclient.cpp @@ -33,6 +33,7 @@ namespace Shared { namespace PlatformCommon { struct FtpFile { const char *filename; + const char *filepath; FILE *stream; FTPClientThread *ftpServer; }; @@ -40,6 +41,14 @@ struct FtpFile { static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { struct FtpFile *out=(struct FtpFile *)stream; + string fullFilePath = ""; + if(out != NULL && out->filepath != NULL) { + fullFilePath = out->filepath; + } + if(out != NULL && out->filename != NULL) { + fullFilePath += out->filename; + } + // Abort file xfer and delete partial file if(out && out->ftpServer && out->ftpServer->getQuitStatus() == true) { if(out->stream) { @@ -47,18 +56,18 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { out->stream = NULL; } - unlink(out->filename); + unlink(fullFilePath.c_str()); return -1; } if(out && out->stream == NULL) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread opening file for writing [%s]\n",out->filename); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread opening file for writing [%s]\n",fullFilePath.c_str()); /* open file for writing */ - out->stream = fopen(out->filename, "wb"); + out->stream = fopen(fullFilePath.c_str(), "wb"); if(out->stream == NULL) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread FAILED to open file for writing [%s]\n",out->filename); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str()); return -1; /* failure, can't open file to write */ } @@ -69,33 +78,47 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { static long file_is_comming(struct curl_fileinfo *finfo,void *data,int remains) { - printf("%3d %40s %10luB ", remains, finfo->filename,(unsigned long)finfo->size); - - switch(finfo->filetype) { - case CURLFILETYPE_DIRECTORY: - printf(" DIR\n"); - break; - case CURLFILETYPE_FILE: - printf("FILE "); - break; - default: - printf("OTHER\n"); - break; - } - - if(finfo->filetype == CURLFILETYPE_FILE) { - // do not transfer files >= 50B - //if(finfo->size > 50) { - // printf("SKIPPED\n"); - // return CURL_CHUNK_BGN_FUNC_SKIP; - //} - struct FtpFile *out=(struct FtpFile *)data; - out->stream = fopen(finfo->filename, "w"); - if(!out->stream) { - return CURL_CHUNK_BGN_FUNC_FAIL; + + string rootFilePath = ""; + string fullFilePath = ""; + if(out != NULL && out->filepath != NULL) { + rootFilePath = out->filepath; + } + if(out != NULL && out->filename != NULL) { + fullFilePath = rootFilePath + finfo->filename; + } + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n===> FTP Client thread file_is_comming: remains: [%3d] filename: [%40s] size: [%10luB] ", remains, finfo->filename,(unsigned long)finfo->size); + + switch(finfo->filetype) { + case CURLFILETYPE_DIRECTORY: + printf("DIR (creating [%s%s])\n",rootFilePath.c_str(),finfo->filename); + rootFilePath += finfo->filename; + createDirectoryPaths(rootFilePath.c_str()); + break; + case CURLFILETYPE_FILE: + printf("FILE "); + break; + default: + printf("OTHER\n"); + break; + } + + if(finfo->filetype == CURLFILETYPE_FILE) { + // do not transfer files >= 50B + //if(finfo->size > 50) { + // printf("SKIPPED\n"); + // return CURL_CHUNK_BGN_FUNC_SKIP; + //} + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" opening file [%s] ", fullFilePath.c_str()); + + out->stream = fopen(fullFilePath.c_str(), "wb"); + if(!out->stream) { + return CURL_CHUNK_BGN_FUNC_FAIL; + } } - } return CURL_CHUNK_BGN_FUNC_OK; } @@ -104,7 +127,7 @@ static long file_is_downloaded(void *data) { struct FtpFile *out=(struct FtpFile *)data; if(out->stream) { - printf("DOWNLOADED\n"); + printf("DOWNLOAD COMPLETE!\n"); fclose(out->stream); out->stream = 0x0; @@ -156,6 +179,7 @@ FTP_Client_ResultType FTPClientThread::getMapFromServer(string mapFileName, stri struct FtpFile ftpfile = { destFile.c_str(), /* name to store the file as if succesful */ NULL, + NULL, this }; @@ -211,7 +235,7 @@ void FTPClientThread::getMapFromServer(string mapFileName) { } if(this->pCBObject != NULL) { - this->pCBObject->FTPClient_CallbackEvent(mapFileName,result); + this->pCBObject->FTPClient_CallbackEvent(mapFileName,ftp_cct_Map,result); } } @@ -236,7 +260,7 @@ void FTPClientThread::getTilesetFromServer(string tileSetName) { } if(this->pCBObject != NULL) { - this->pCBObject->FTPClient_CallbackEvent(tileSetName,result); + this->pCBObject->FTPClient_CallbackEvent(tileSetName,ftp_cct_Tileset,result); } } @@ -247,6 +271,21 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName, string destFile = this->tilesetsPath.second; + // Root folder for the tileset + string destRootFolder = ""; + if(tileSetNameSubfolder == "") { + destRootFolder = this->tilesetsPath.second; + if(EndsWith(destRootFolder,"/") == false && EndsWith(destRootFolder,"\\") == false) { + destRootFolder += "/"; + } + destRootFolder += tileSetName; + if(EndsWith(destRootFolder,"/") == false && EndsWith(destRootFolder,"\\") == false) { + destRootFolder += "/"; + } + + createDirectoryPaths(destRootFolder); + } + if(EndsWith(destFile,"/") == false && EndsWith(destFile,"\\") == false) { destFile += "/"; } @@ -266,7 +305,8 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName, if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread about to try to RETR into [%s]\n",destFile.c_str()); struct FtpFile ftpfile = { - destFile.c_str(), /* name to store the file as if succesful */ + destFile.c_str(), // name to store the file as if succesful + destFile.c_str(), NULL, this }; @@ -315,6 +355,9 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName, if(CURLE_OK != res) { // we failed fprintf(stderr, "curl told us %d\n", res); + if(destRootFolder != "") { + unlink(destRootFolder.c_str()); + } } else { result = ftp_crt_SUCCESS; @@ -333,8 +376,8 @@ FTP_Client_ResultType FTPClientThread::getTilesetFromServer(string tileSetName, requireMoreFolders = true; } else if(tileSetNameSubfolder == "textures") { - tileSetNameSubfolder = "textures"; - requireMoreFolders = true; + tileSetNameSubfolder = ""; + requireMoreFolders = false; } if(requireMoreFolders == true) { diff --git a/source/shared_lib/sources/platform/posix/miniftpserver.cpp b/source/shared_lib/sources/platform/posix/miniftpserver.cpp index 9c5f9dfb..8650b676 100644 --- a/source/shared_lib/sources/platform/posix/miniftpserver.cpp +++ b/source/shared_lib/sources/platform/posix/miniftpserver.cpp @@ -114,7 +114,7 @@ void FTPServerThread::execute() { ftpStart(portNumber); while(this->getQuitStatus() == false) { ftpExecute(); - sleep(25); + //sleep(25); } ftpShutdown();