DownloadManager.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: DownloadManager.cpp //////////////////////////////////////////////////////
  24. // Generals download manager code
  25. // Author: Matthew D. Campbell, July 2002
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "GameClient/GameText.h"
  28. #include "GameNetwork/DownloadManager.h"
  29. DownloadManager *TheDownloadManager;
  30. DownloadManager::DownloadManager()
  31. {
  32. m_download = NEW CDownload(this);
  33. m_wasError = m_sawEnd = false;
  34. //Added By Sadullah Nader
  35. //Initializations missing and needed
  36. m_queuedDownloads.clear();
  37. //
  38. m_statusString = TheGameText->fetch("FTP:StatusIdle");
  39. // ----- Initialize Winsock -----
  40. m_winsockInit = true;
  41. WORD verReq = MAKEWORD(2, 2);
  42. WSADATA wsadata;
  43. int err = WSAStartup(verReq, &wsadata);
  44. if (err != 0)
  45. {
  46. m_winsockInit = false;
  47. }
  48. else
  49. {
  50. if ((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) !=2))
  51. {
  52. WSACleanup();
  53. m_winsockInit = false;
  54. }
  55. }
  56. }
  57. DownloadManager::~DownloadManager()
  58. {
  59. delete m_download;
  60. if (m_winsockInit)
  61. {
  62. WSACleanup();
  63. m_winsockInit = false;
  64. }
  65. }
  66. void DownloadManager::init( void )
  67. {
  68. }
  69. void DownloadManager::reset( void )
  70. {
  71. }
  72. HRESULT DownloadManager::update( void )
  73. {
  74. return m_download->PumpMessages();
  75. }
  76. HRESULT DownloadManager::downloadFile( AsciiString server, AsciiString username, AsciiString password, AsciiString file, AsciiString localfile, AsciiString regkey, Bool tryResume )
  77. {
  78. return m_download->DownloadFile( server.str(), username.str(), password.str(), file.str(), localfile.str(), regkey.str(), tryResume );
  79. }
  80. void DownloadManager::queueFileForDownload( AsciiString server, AsciiString username, AsciiString password, AsciiString file, AsciiString localfile, AsciiString regkey, Bool tryResume )
  81. {
  82. QueuedDownload q;
  83. q.file = file;
  84. q.localFile = localfile;
  85. q.password = password;
  86. q.regKey = regkey;
  87. q.server = server;
  88. q.tryResume = tryResume;
  89. q.userName = username;
  90. m_queuedDownloads.push_back(q);
  91. }
  92. HRESULT DownloadManager::downloadNextQueuedFile( void )
  93. {
  94. QueuedDownload q;
  95. std::list<QueuedDownload>::iterator it = m_queuedDownloads.begin();
  96. if (it != m_queuedDownloads.end())
  97. {
  98. q = *it;
  99. m_queuedDownloads.pop_front();
  100. m_wasError = m_sawEnd = false;
  101. return downloadFile( q.server, q.userName, q.password, q.file, q.localFile, q.regKey, q.tryResume );
  102. }
  103. else
  104. {
  105. DEBUG_CRASH(("Starting non-existent download!"));
  106. return S_OK;
  107. }
  108. }
  109. AsciiString DownloadManager::getLastLocalFile( void )
  110. {
  111. char buf[256] = "";
  112. m_download->GetLastLocalFile(buf, 256);
  113. return buf;
  114. }
  115. HRESULT DownloadManager::OnError( Int error )
  116. {
  117. m_wasError = true;
  118. AsciiString s = "FTP:UnknownError";
  119. switch (error)
  120. {
  121. case DOWNLOADEVENT_NOSUCHSERVER:
  122. s = "FTP:NoSuchServer";
  123. break;
  124. case DOWNLOADEVENT_COULDNOTCONNECT:
  125. s = "FTP:CouldNotConnect";
  126. break;
  127. case DOWNLOADEVENT_LOGINFAILED:
  128. s = "FTP:LoginFailed";
  129. break;
  130. case DOWNLOADEVENT_NOSUCHFILE:
  131. s = "FTP:NoSuchFile";
  132. break;
  133. case DOWNLOADEVENT_LOCALFILEOPENFAILED:
  134. s = "FTP:LocalFileOpenFailed";
  135. break;
  136. case DOWNLOADEVENT_TCPERROR:
  137. s = "FTP:TCPError";
  138. break;
  139. case DOWNLOADEVENT_DISCONNECTERROR:
  140. s = "FTP:DisconnectError";
  141. break;
  142. }
  143. m_errorString = TheGameText->fetch(s);
  144. DEBUG_LOG(("DownloadManager::OnError(): %s(%d)\n", s.str(), error));
  145. return S_OK;
  146. }
  147. HRESULT DownloadManager::OnEnd()
  148. {
  149. m_sawEnd = true;
  150. DEBUG_LOG(("DownloadManager::OnEnd()\n"));
  151. return S_OK;
  152. }
  153. HRESULT DownloadManager::OnQueryResume()
  154. {
  155. DEBUG_LOG(("DownloadManager::OnQueryResume()\n"));
  156. //return DOWNLOADEVENT_DONOTRESUME;
  157. return DOWNLOADEVENT_RESUME;
  158. }
  159. HRESULT DownloadManager::OnProgressUpdate( Int bytesread, Int totalsize, Int timetaken, Int timeleft )
  160. {
  161. DEBUG_LOG(("DownloadManager::OnProgressUpdate(): %d/%d %d/%d\n", bytesread, totalsize, timetaken, timeleft));
  162. return S_OK;
  163. }
  164. HRESULT DownloadManager::OnStatusUpdate( Int status )
  165. {
  166. AsciiString s = "FTP:StatusNone";
  167. switch (status)
  168. {
  169. case DOWNLOADSTATUS_CONNECTING:
  170. s = "FTP:StatusConnecting";
  171. break;
  172. case DOWNLOADSTATUS_LOGGINGIN:
  173. s = "FTP:StatusLoggingIn";
  174. break;
  175. case DOWNLOADSTATUS_FINDINGFILE:
  176. s = "FTP:StatusFindingFile";
  177. break;
  178. case DOWNLOADSTATUS_QUERYINGRESUME:
  179. s = "FTP:StatusQueryingResume";
  180. break;
  181. case DOWNLOADSTATUS_DOWNLOADING:
  182. s = "FTP:StatusDownloading";
  183. break;
  184. case DOWNLOADSTATUS_DISCONNECTING:
  185. s = "FTP:StatusDisconnecting";
  186. break;
  187. case DOWNLOADSTATUS_FINISHING:
  188. s = "FTP:StatusFinishing";
  189. break;
  190. case DOWNLOADSTATUS_DONE:
  191. s = "FTP:StatusDone";
  192. break;
  193. }
  194. m_statusString = TheGameText->fetch(s);
  195. DEBUG_LOG(("DownloadManager::OnStatusUpdate(): %s(%d)\n", s.str(), status));
  196. return S_OK;
  197. }