DownloadEngine.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:4k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // DownloadEngine.cpp: implementation of the CDownloadEngine class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "testbt.h"
  6. #include "DownloadEngine.h"
  7. #include "FileDB.h"
  8. #include "Download.h"
  9. #include "CategoryBarWnd.h"
  10. #include "SetupRegister.h"
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char THIS_FILE[]=__FILE__;
  14. #define new DEBUG_NEW
  15. #endif
  16. //////////////////////////////////////////////////////////////////////
  17. // Construction/Destruction
  18. //////////////////////////////////////////////////////////////////////
  19. CDownloadEngine::CDownloadEngine()
  20. {
  21. m_pSetupRegister = 0;
  22. m_pCurrentCheckingItem = 0;
  23. m_pCategoryBarWnd = 0;
  24. }
  25. CDownloadEngine::~CDownloadEngine()
  26. {
  27. }
  28. bool CDownloadEngine::Create(CSetupRegister* pSetupRegister, CCategoryBarWnd* pCategoryBarWnd)
  29. {
  30. m_pSetupRegister = pSetupRegister;
  31. m_pCategoryBarWnd = pCategoryBarWnd;
  32. return true;
  33. }
  34. bool CDownloadEngine::StartDownload(CFileDBItem* pFileDBItem, HTREEITEM hCursorItem)
  35. {
  36. assert(!pFileDBItem->m_pDownload);
  37. pFileDBItem->m_pDownload = new CDownload();
  38. m_vTostartDownloads.push_back(CEngineItem(pFileDBItem, hCursorItem));
  39. if (m_pCurrentCheckingItem)
  40. return true;
  41. ExcuteDownload(0);
  42. return true;
  43. }
  44. //
  45. // long lparam: pointer to  the download which sends the message.
  46. //
  47. void CDownloadEngine::ExcuteDownload(long lParam)
  48. {
  49. assert(m_pSetupRegister);
  50. if (m_vTostartDownloads.size() <= 0)
  51. {
  52. m_pCurrentCheckingItem = 0;
  53. return;
  54. }
  55. // in case bad message, skip it.
  56. if (lParam != 0 && m_pCurrentCheckingItem !=0 )
  57. {
  58. if (m_pCurrentCheckingItem->m_pDownload != (CDownload*)lParam)
  59. {
  60. assert(false);
  61. return;
  62. }
  63. }
  64. HTREEITEM hTreeItem = m_vTostartDownloads[0].m_hTreeItem;
  65. CFileDBItem* pFileDBItem = m_vTostartDownloads[0].m_pFileDBItem;
  66. m_vTostartDownloads.erase(m_vTostartDownloads.begin());
  67. m_pCurrentCheckingItem = pFileDBItem;
  68. assert(pFileDBItem->m_pDownload);
  69. // pFileDBItem->m_pDownload = new CDownload();
  70. //
  71. // Run parameter setup.
  72. //
  73. if (!pFileDBItem->m_bSpecialRunParameter)
  74. {
  75. pFileDBItem->m_lUploadSpeedMode = m_pSetupRegister->m_lDownSpeed;
  76. pFileDBItem->m_lUploadSpeedMax = CSpeedParameters::m_lArrSpeedVal[m_pSetupRegister->m_lDownSpeed];
  77. pFileDBItem->m_lUploadPeerMax = CSpeedParameters::m_lArrPeerVal[m_pSetupRegister->m_lDownSpeed];
  78. pFileDBItem->m_lPeersMin = m_pSetupRegister->m_lPeersMin;
  79. pFileDBItem->m_lPeersMax = m_pSetupRegister->m_lPeersMax;
  80. pFileDBItem->m_lConnectErrorMax = m_pSetupRegister->m_lConnectErrorMax;
  81. pFileDBItem->m_lConnectIntval = m_pSetupRegister->m_lConnectIntval;
  82. pFileDBItem->m_lCompleteKeepSeedHour = m_pSetupRegister->m_lCompleteKeepSeedHour;
  83. }
  84. //
  85. // Set modified time.
  86. //
  87. time(&pFileDBItem->m_tModifiedTime);
  88. bool bCheckHash = true;
  89. if (pFileDBItem->m_fComplete >= 1 && pFileDBItem->m_vUnneededFileInx.empty())
  90. {
  91. bCheckHash = false;
  92. }
  93. if (!pFileDBItem->m_pDownload->DownloadEx(m_pSetupRegister, 
  94. formatDir(m_pCategoryBarWnd->GetDBSavePath())+ pFileDBItem->m_strTorrentFileName, 
  95. pFileDBItem->m_strFileName, bCheckHash, hTreeItem, 
  96. m_pCategoryBarWnd->GetSafeHwnd(), (long)pFileDBItem->m_pDownload, pFileDBItem))
  97. {
  98. pFileDBItem->m_bBad = true;
  99. m_pCategoryBarWnd->OnDownloadTerminated((long)hTreeItem, (long)pFileDBItem->m_pDownload);
  100. m_pCurrentCheckingItem = 0;
  101. ExcuteDownload(0);
  102. return;
  103. }
  104. }
  105. void CDownloadEngine::StopDownload(CFileDBItem* pFileDBItem)
  106. {
  107. for (int i=0; i<m_vTostartDownloads.size(); i++)
  108. {
  109. if (m_vTostartDownloads[i].m_pFileDBItem == pFileDBItem)
  110. {
  111. m_vTostartDownloads.erase(m_vTostartDownloads.begin() + i);
  112. break;
  113. }
  114. }
  115. if(m_pCurrentCheckingItem == pFileDBItem)
  116. {
  117. m_pCurrentCheckingItem = 0;
  118. ExcuteDownload(0);
  119. }
  120. }
  121. void CDownloadEngine::StopDownloadArray(const vector<CFileDBItem*>&  vpFileDBItems)
  122. {
  123. bool bExcuteCommand = false;
  124. for (int j=0; j<vpFileDBItems.size(); j++)
  125. {
  126. // erase item from vector TostartDownloads.
  127. for (int i=0; i<m_vTostartDownloads.size(); i++)
  128. {
  129. if (m_vTostartDownloads[i].m_pFileDBItem == vpFileDBItems[j])
  130. {
  131. m_vTostartDownloads.erase(m_vTostartDownloads.begin() + i);
  132. break;
  133. }
  134. }
  135. if (m_pCurrentCheckingItem == vpFileDBItems[j])
  136. bExcuteCommand = true;
  137. }
  138. if (bExcuteCommand)
  139. {
  140. m_pCurrentCheckingItem = 0;
  141. ExcuteDownload(0);
  142. }
  143. }