Downloader.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:3k
源码类别:
P2P编程
开发平台:
Visual C++
- // Downloader.cpp: implementation of the CDownloader class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "Downloader.h"
- /*
- #include "Storage.h"
- #include "PiecePicker.h"
- #include "Measure.h"
- #include "RateMeasure.h"
- //*/
- #include "SingleDownload.h"
- #include "StorageWrapper.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDownloader::CDownloader()
- {
- m_bCreate = false;
- }
- bool CDownloader::Create(CDownloaderFeedback* pDownloaderFeedback, CStorageWrapper* pstrorage, CPiecePicker* pPicker, long lBacklog,
- long lMaxRatePeriod, CMeasure* pDownMeasure, long lNumPieces, long lSnubTime,
- CRateMeasure* pRateMeasure)
- {
- m_pDownloaderFeedback = pDownloaderFeedback;
- m_pstrorage = pstrorage;
- m_pPicker = pPicker;
- m_lBacklog = lBacklog;
- m_lMaxRatePeriod = lMaxRatePeriod;
- m_pDownMeasure = pDownMeasure;
- m_lNumPieces = lNumPieces;
- m_lSnubTime = lSnubTime;
- m_pRateMeasure = pRateMeasure;
- m_bEndgame = false;
- m_bCreate = true;
- return true;
- }
- CDownloader::~CDownloader()
- {
- // for (int i=0; i<m_vDownloads.size(); i++)
- // delete m_vDownloads[i];
- }
- CSingleDownload* CDownloader::make_download(CConnection* pConnection)
- {
- CSingleDownload* pnew = new CSingleDownload(m_pDownloaderFeedback, this, pConnection);
- m_vDownloads.push_back(pnew);
- if (m_bEndgame)
- {
- pnew->make_endgame(m_vRequests);
- pnew->mak_endgameflush();
- }
- return pnew;
- }
- void CDownloader::make_endgame()
- {
- assert(!m_bEndgame);
- m_bEndgame = true;
- for (int i=0; i<m_vDownloads.size(); i++)
- {
- m_vDownloads[i]->make_endgame(m_vRequests);
- }
- for (i=0; i<m_vDownloads.size(); i++)
- {
- m_vDownloads[i]->mak_endgameflush();
- TRACE("rnm_vRequests : (index, begin, length){");
- for (int j=0; j<m_vRequests.size(); j++)
- {
- char szText[100] = {0};
- sprintf(szText, "(%d, %d, %d)", m_vRequests[j].m_index, m_vRequests[j].m_begin, m_vRequests[j].m_length);
- TRACE(szText);
- }
- TRACE("}rn");
- }
- }
- /////////////////////////////////////////////////////////////////
- // called by storageWrapper, when files needed setup change.
- //
- bool CDownloader::is_endgame()
- {
- return m_bEndgame;
- }
- void CDownloader::cancel_endgame()
- {
- assert(m_bEndgame);
- m_bEndgame = false;
- for (int i=0; i<m_vRequests.size();i++)
- {
- CRequestsItem& item = m_vRequests[i];
- m_pstrorage->request_lost(item.m_index, item.m_begin, item.m_length);
- }
- m_vRequests.clear();
- for (i=0; i<m_vDownloads.size(); i++)
- {
- m_vDownloads[i]->cancel_endgame();
- }
- }
- void CDownloader::OnNeedFilesChanged()
- {
- for (int i=0; i<m_vDownloads.size(); i++)
- {
- m_vDownloads[i]->download_more();
- }
- }
- bool CDownloader::IsCreate()
- {
- return m_bCreate;
- }