DownloadEngine.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- // DownloadEngine.cpp: implementation of the CDownloadEngine class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "DownloadEngine.h"
- #include "FileDB.h"
- #include "Download.h"
- #include "CategoryBarWnd.h"
- #include "SetupRegister.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDownloadEngine::CDownloadEngine()
- {
- m_pSetupRegister = 0;
- m_pCurrentCheckingItem = 0;
- m_pCategoryBarWnd = 0;
- }
- CDownloadEngine::~CDownloadEngine()
- {
- }
- bool CDownloadEngine::Create(CSetupRegister* pSetupRegister, CCategoryBarWnd* pCategoryBarWnd)
- {
- m_pSetupRegister = pSetupRegister;
- m_pCategoryBarWnd = pCategoryBarWnd;
- return true;
- }
- bool CDownloadEngine::StartDownload(CFileDBItem* pFileDBItem, HTREEITEM hCursorItem)
- {
- assert(!pFileDBItem->m_pDownload);
- pFileDBItem->m_pDownload = new CDownload();
- m_vTostartDownloads.push_back(CEngineItem(pFileDBItem, hCursorItem));
- if (m_pCurrentCheckingItem)
- return true;
- ExcuteDownload(0);
- return true;
- }
- //
- // long lparam: pointer to the download which sends the message.
- //
- void CDownloadEngine::ExcuteDownload(long lParam)
- {
- assert(m_pSetupRegister);
- if (m_vTostartDownloads.size() <= 0)
- {
- m_pCurrentCheckingItem = 0;
- return;
- }
- // in case bad message, skip it.
- if (lParam != 0 && m_pCurrentCheckingItem !=0 )
- {
- if (m_pCurrentCheckingItem->m_pDownload != (CDownload*)lParam)
- {
- assert(false);
- return;
- }
- }
- HTREEITEM hTreeItem = m_vTostartDownloads[0].m_hTreeItem;
- CFileDBItem* pFileDBItem = m_vTostartDownloads[0].m_pFileDBItem;
- m_vTostartDownloads.erase(m_vTostartDownloads.begin());
- m_pCurrentCheckingItem = pFileDBItem;
- assert(pFileDBItem->m_pDownload);
- // pFileDBItem->m_pDownload = new CDownload();
- //
- // Run parameter setup.
- //
- if (!pFileDBItem->m_bSpecialRunParameter)
- {
- pFileDBItem->m_lUploadSpeedMode = m_pSetupRegister->m_lDownSpeed;
- pFileDBItem->m_lUploadSpeedMax = CSpeedParameters::m_lArrSpeedVal[m_pSetupRegister->m_lDownSpeed];
- pFileDBItem->m_lUploadPeerMax = CSpeedParameters::m_lArrPeerVal[m_pSetupRegister->m_lDownSpeed];
- pFileDBItem->m_lPeersMin = m_pSetupRegister->m_lPeersMin;
- pFileDBItem->m_lPeersMax = m_pSetupRegister->m_lPeersMax;
- pFileDBItem->m_lConnectErrorMax = m_pSetupRegister->m_lConnectErrorMax;
- pFileDBItem->m_lConnectIntval = m_pSetupRegister->m_lConnectIntval;
- pFileDBItem->m_lCompleteKeepSeedHour = m_pSetupRegister->m_lCompleteKeepSeedHour;
- }
- //
- // Set modified time.
- //
- time(&pFileDBItem->m_tModifiedTime);
- bool bCheckHash = true;
- if (pFileDBItem->m_fComplete >= 1 && pFileDBItem->m_vUnneededFileInx.empty())
- {
- bCheckHash = false;
- }
- if (!pFileDBItem->m_pDownload->DownloadEx(m_pSetupRegister,
- formatDir(m_pCategoryBarWnd->GetDBSavePath())+ pFileDBItem->m_strTorrentFileName,
- pFileDBItem->m_strFileName, bCheckHash, hTreeItem,
- m_pCategoryBarWnd->GetSafeHwnd(), (long)pFileDBItem->m_pDownload, pFileDBItem))
- {
- pFileDBItem->m_bBad = true;
- m_pCategoryBarWnd->OnDownloadTerminated((long)hTreeItem, (long)pFileDBItem->m_pDownload);
- m_pCurrentCheckingItem = 0;
- ExcuteDownload(0);
- return;
- }
- }
- void CDownloadEngine::StopDownload(CFileDBItem* pFileDBItem)
- {
- for (int i=0; i<m_vTostartDownloads.size(); i++)
- {
- if (m_vTostartDownloads[i].m_pFileDBItem == pFileDBItem)
- {
- m_vTostartDownloads.erase(m_vTostartDownloads.begin() + i);
- break;
- }
- }
- if(m_pCurrentCheckingItem == pFileDBItem)
- {
- m_pCurrentCheckingItem = 0;
- ExcuteDownload(0);
- }
- }
- void CDownloadEngine::StopDownloadArray(const vector<CFileDBItem*>& vpFileDBItems)
- {
- bool bExcuteCommand = false;
- for (int j=0; j<vpFileDBItems.size(); j++)
- {
- // erase item from vector TostartDownloads.
- for (int i=0; i<m_vTostartDownloads.size(); i++)
- {
- if (m_vTostartDownloads[i].m_pFileDBItem == vpFileDBItems[j])
- {
- m_vTostartDownloads.erase(m_vTostartDownloads.begin() + i);
- break;
- }
- }
- if (m_pCurrentCheckingItem == vpFileDBItems[j])
- bExcuteCommand = true;
- }
- if (bExcuteCommand)
- {
- m_pCurrentCheckingItem = 0;
- ExcuteDownload(0);
- }
- }