Download.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:24k
源码类别:
P2P编程
开发平台:
Visual C++
- // Download.cpp: implementation of the CDownload class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testBT.h"
- #include "Download.h"
- #include "val.h"
- #include "FileTorrent.h"
- #include "Storage.h"
- #include "StorageWrapper.h"
- #include "Rerequester.h"
- #include "RawServer.h"
- #include "Encrypter.h"
- #include "Connector.h"
- #include "PiecePicker.h"
- #include "RateMeasure.h"
- #include "Downloader.h"
- #include "Upload.h"
- #include "choker.h"
- #include "DownloaderFeedback.h"
- #include "SetupRegister.h"
- #include "FileDB.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- void SingleDownloadTest();
- CDownload::CDownload()
- {
- m_bCheckHash = true;
- m_hevUnPause = CreateEvent(0, true, true, 0);
- m_hThreadDone = CreateEvent(0, true, true, 0);
- m_hevDone = CreateEvent(0, true, false, 0);
- m_hCompleted = CreateEvent(0, true, false, 0);
- m_hThreadSafe = CreateEvent(0, true, false, 0);
- m_pdownloader = 0;
- m_pconncetorget = 0;
- m_pRequester = 0;
- m_pChoker = 0;
- m_pEncrypter = 0;
- GetSetupParameter();
- long ltime = 0;
- time(<ime);
- srand(ltime);
- m_bClosing = false;
- }
- CDownload::~CDownload()
- {
- Close();
- assert(IsEventSet(m_hThreadDone));
- CloseHandle(m_hevDone);
- CloseHandle(m_hCompleted);
- CloseHandle(m_hThreadDone);
- CloseHandle(m_hThreadSafe);
- }
- void CDownload::GetSetupParameter()
- {
- g_defaults["max_uploads"] = _variant_t(4L); //the maximum number of uploads to allow at once.
- g_defaults["keepalive_interval"] = _variant_t(120L); //number of seconds to pause between sending keepalives
- g_defaults["download_slice_size"] = _variant_t(pow(2, 14)); //How many bytes to query for per request.
- g_defaults["request_backlog"] = _variant_t(5L); //how many requests to keep in a single pipe at once.
- g_defaults["max_message_length"] = _variant_t(pow(2, 23)); //maximum length prefix encoding you'll accept over the wire - larger values get the connection dropped.
- g_defaults["ip"] = _variant_t(""); //ip to report you have to the tracker.
- g_defaults["minport"] = _variant_t(6881L); //minimum port to listen on, counts up if unavailable
- g_defaults["maxport"] = _variant_t(6999L); //maximum port to listen on
- g_defaults["responsefile"] = _variant_t(""); //file the server response was stored in, alternative to url
- g_defaults["url"] = _variant_t(""); //alternative to responsefile
- g_defaults["saveas"] = _variant_t(""); //local file name to save the file as, null indicates query user
- g_defaults["timeout"] = _variant_t(300L); //time to wait between closing sockets which nothing has been received on
- g_defaults["timeout_check_interval"] = _variant_t(60L); //time to wait between checking if any connections have timed out
- g_defaults["max_slice_length"] = _variant_t(pow(2, 17)); //maximum length slice to send to peers, larger requests are ignored
- g_defaults["max_rate_period"] = _variant_t(3L); //maximum amount of time to guess the current rate estimate represents
- g_defaults["bind"] = _variant_t(""); //ip to bind to locally
- g_defaults["upload_rate_fudge"] = _variant_t(1L); //time equivalent of writing to kernel-level TCP buffer, for rate adjustment
- g_defaults["display_interval"] = _variant_t(5L); //time between updates of displayed information
- g_defaults["rerequest_interval"] = _variant_t(5*60L); //time to wait between requesting more peers
- g_defaults["min_peers"] = _variant_t(100L); //minimum number of peers to not do rerequesting
- g_defaults["http_timeout"] = _variant_t(60L); //number of seconds to wait before assuming that an http connection has timed out
- g_defaults["max_initiate"] = _variant_t(60L); //number of peers at which to stop initiating new connections
- g_defaults["check_hashes"] = _variant_t(1L); //whether to check hashes on disk
- g_defaults["max_upload_rate"] = _variant_t(0L); //maximum kB/s to upload at, 0 means no limit
- g_defaults["alloc_pause"] = _variant_t(3L); //seconds to wait before displaying allocation feedback
- g_defaults["snub_time"] = _variant_t(60L); //seconds to wait for data to come in over a connection before assuming it's semi-permanently choked
- g_defaults["spew"] = _variant_t(0L); //whether to display diagnostic info to stdout
- g_defaults["AllocSpeed"] = _variant_t(2L); //whether to display diagnostic info to stdout
- g_defaults["AllocType"] = _variant_t(0L); //whether to display diagnostic info to stdout
- g_defaults["FileOpenMax"] = _variant_t(0L); //whether to display diagnostic info to stdout
- g_defaults["SuperSeed"] = _variant_t(0L); //whether to display diagnostic info to stdout
- m_lUploadPeerMax = 4L;
- m_lUploadSpeedMax = 0;
- m_lConnectErrorMax = 200;
- m_lConnectIntval = 10;
- m_bLockFile = true;
- m_bLockFileWhileReading = false;
- m_bDoubleCheck = false;
- m_bTripleCheck = false;
- }
- void CDownload::SetParameter(CSetupRegister* pSetupRegister)
- {
- assert(pSetupRegister);
- assert(pSetupRegister->m_lAllocSpeed > 0 && pSetupRegister->m_lAllocSpeed <= 10);
- assert(pSetupRegister->m_eAllocType >= CStorageWrapperEx::eAllocNormal &&
- pSetupRegister->m_eAllocType <= CStorageWrapperEx::eAllocBackGound);
- assert(pSetupRegister->m_lFileOpenMax > 0 && pSetupRegister->m_lFileOpenMax < 250);
- g_defaults["minport"] = pSetupRegister->m_lPortMin;
- g_defaults["maxport"] = pSetupRegister->m_lPortMax;
- g_defaults["max_initiate"] = pSetupRegister->m_lPeersMax;
- g_defaults["min_peers"] = pSetupRegister->m_lPeersMin;
- g_defaults["display_interval"] = (long)(pSetupRegister->m_lDisplayItval);
- g_defaults["AllocSpeed"] = pSetupRegister->m_lAllocSpeed;
- g_defaults["AllocType"] = (long)pSetupRegister->m_eAllocType;
- g_defaults["FileOpenMax"] = pSetupRegister->m_lFileOpenMax;
- g_defaults["SuperSeed"] = pSetupRegister->m_bSuperSeed;
- m_lConnectErrorMax = pSetupRegister->m_lConnectErrorMax;
- m_lConnectIntval = pSetupRegister->m_lConnectIntval;
- if (pSetupRegister->m_lSendServerIP != INADDR_NONE )
- {
- in_addr inaddress;
- inaddress.S_un.S_addr = pSetupRegister->m_lSendServerIP;
- g_defaults["ip"] = inet_ntoa(inaddress);
- }
- switch (pSetupRegister->m_eFileLock)
- {
- case CSetupRegister::eFileUnlock:
- {
- m_bLockFile = false;
- m_bLockFileWhileReading = false;
- }
- break;
- case CSetupRegister::eFileWriteLock:
- {
- m_bLockFile = true;
- m_bLockFileWhileReading = false;
- }
- break;
- case CSetupRegister::eFileLock:
- {
- m_bLockFile = true;
- m_bLockFileWhileReading = true;
- }
- break;
- }
- switch (pSetupRegister->m_eExtraCheck)
- {
- case CSetupRegister::eNoExtraCheck:
- {
- m_bDoubleCheck = false;
- m_bTripleCheck = false;
- }
- break;
- case CSetupRegister::eDoubleCheck:
- {
- m_bDoubleCheck = true;
- m_bTripleCheck = false;
- }
- break;
- case CSetupRegister::eTripleCheck:
- {
- m_bDoubleCheck = true;
- m_bTripleCheck = true;
- }
- break;
- }
- // m_bCompleteKeepSeed = pSetupRegister->m_bCompleteKeepSeed;
- /*
- if(pSetupRegister->m_lDownSpeed < CSpeedParameters::eSpeedModeAll && pSetupRegister->m_lDownSpeed >=0)
- {
- g_defaults["max_uploads"] = _variant_t(CSpeedParameters::m_lArrPeerVal[pSetupRegister->m_lDownSpeed]);
- g_defaults["max_upload_rate"] = _variant_t(CSpeedParameters::m_lArrSpeedVal[pSetupRegister->m_lDownSpeed]);
- }
- else
- assert(false);
- //*/
- }
- void CDownload::CloseDownload()
- {
- if (IsEventSet(m_hThreadDone))
- {
- assert(false);
- }
- m_bClosing = true;
- if (IsPause()) Pause(false);
- SetEvent(m_hevDone);
- return;
- }
- void CDownload::Close()
- {
- if (!IsEventSet(m_hThreadDone))
- {
- if (IsPause())
- Pause(false);
- SetEvent(m_hevDone);
- DWORD dwRet = WaitForSingleObject(m_hThreadDone, INFINITE);
- switch (dwRet)
- {
- case WAIT_OBJECT_0:
- break;
- case WAIT_TIMEOUT:
- break;
- }
- }
- }
- bool CDownload::CanSetRunParameters()
- {
- return IsEventSet(m_hThreadSafe);
- }
- void CDownload::SetUnNeededFilesInxs(const vector<CSize>& vUnNeededFileInxs)
- {
- bool bToPause = !IsPause();
- if (bToPause) Pause(true);
- _SetUnNeededFilesInxs(vUnNeededFileInxs);
- if (bToPause) Pause(false);
- }
- void CDownload::_SetUnNeededFilesInxs(const vector<CSize>& vUnNeededFileInxs)
- {
- if (!IsDownloading() || !CanSetRunParameters())
- {
- assert(false);
- return;
- }
- if (!IsEventSet(m_hThreadSafe))
- {
- Pause(false);
- return;
- }
- if (!m_pStorageWrapper)
- {
- assert(false);
- return;
- }
- m_pStorageWrapper->SetUnNeededFilesInxs(vUnNeededFileInxs);
- }
- void CDownload::SetRunParameters(long lMaxUploadSpeed, long lMaxUploads,
- long lCompleteKeepSeedHour, long lConnectErrorMax,
- long lConnectIntval,
- long minpeers, long maxpeers)
- {
- bool bToPause = !IsPause();
- if (bToPause) Pause(true);
- ASSERT(maxpeers > minpeers);
- _SetRunParameters(lMaxUploadSpeed, lMaxUploads, lCompleteKeepSeedHour, lConnectErrorMax, lConnectIntval, minpeers, maxpeers);
- if (bToPause) Pause(false);
- }
- void CDownload::_SetRunParameters(long lMaxUploadSpeed, long lMaxUploads,
- long lCompleteKeepSeedHour, long lConnectErrorMax,
- long lConnectIntval,
- long minpeers, long maxpeers)
- {
- assert(lMaxUploads > 0 && lMaxUploadSpeed >= 0);
- if (!IsDownloading() || !CanSetRunParameters())
- {
- assert(false);
- return;
- }
- if (!IsEventSet(m_hThreadSafe))
- {
- Pause(false);
- return;
- }
- if (m_pChoker && m_pconncetorget && m_pRequester && m_pEncrypter)
- {
- m_pChoker->SetMaxUploads(lMaxUploads);
- m_pconncetorget->SetMaxUploadRate(lMaxUploadSpeed * 1024);
- m_DownloaderFeedback.SetCompleteKeepSeedHour(lCompleteKeepSeedHour);
- m_pRequester->SetConnectErrorMax(lConnectErrorMax, lConnectIntval, minpeers, maxpeers);
- m_pEncrypter->SetMaxPeers(maxpeers);
- }
- else
- {
- assert(false);
- }
- }
- void CDownload::Pause(bool bPause)
- {
- if (bPause == IsPause())
- {
- assert(false);
- return;
- }
- if (bPause)
- {
- ResetEvent(m_hevUnPause);
- }
- else
- {
- SetEvent(m_hevUnPause);
- }
- }
- bool CDownload::IsPause()
- {
- return !IsEventSet(m_hevUnPause);
- }
- bool CDownload::IsDownloading()
- {
- return !IsEventSet(m_hThreadDone);
- }
- bool CDownload::IsBad()
- {
- return m_DownloaderFeedback.IsBad();
- }
- void CDownload::PausePeer(CAddrPort addrPort, bool bPause)
- {
- bool bToPause = !IsPause();
- if (bToPause) Pause(true);
- _PausePeer(addrPort, bPause);
- if (bToPause) Pause(false);
- }
- void CDownload::_PausePeer(CAddrPort addrPort, bool bPause)
- {
- if (!IsDownloading() || !m_pconncetorget || !CanSetRunParameters())
- {
- assert(false);
- return;
- }
- m_pconncetorget->PausePeer(addrPort, bPause);
- }
- void CDownload::ClosePeer(CAddrPort addrPort)
- {
- bool bToPause = !IsPause();
- if (bToPause) Pause(true);
- _ClosePeer(addrPort);
- if (bToPause) Pause(false);
- }
- void CDownload::_ClosePeer(CAddrPort addrPort)
- {
- if (!IsDownloading() || !m_pconncetorget || !CanSetRunParameters())
- {
- assert(false);
- return;
- }
- m_pconncetorget->ClosePeer(addrPort);
- }
- void CDownload::PartCompleted()
- {
- // SetEvent(m_hCompleted);
- m_pStorage->SetReadOnly();
- }
- void CDownload::Completed()
- {
- SetEvent(m_hCompleted);
- m_pStorage->SetReadOnly();
- if (m_pRequester)
- m_pRequester->AnnounceEx(REQ_COMPLETED);
- // todo:
- // send http request stoped.
- }
- void CDownload::data_flunked(long lAmount)
- {
- if (!m_pratemeasure)
- {
- ASSERT(FALSE);
- return;
- }
- m_pratemeasure->data_rejected(lAmount);
- TRACE("rn**********data_flunked****************rn");
- // todo:
- // ratemeasure.data_reject();
- }
- string CDownload::GetBadMsg()
- {
- return m_DownloaderFeedback.GetBadMsg();
- }
- /******************************************************************************
- parameter:
- lUploadSpeedMax unit is k.
- ******************************************************************************/
- bool CDownload::DownloadEx(CSetupRegister* pSetupRegister,
- string strTorrentFileName, string strSaveFileName, bool bCheckHash,
- HTREEITEM curItem, HWND hFeedbackwnd, long lParam, const CFileDBItem* const pFileDBItem)
- {
- if (strTorrentFileName.empty() || strSaveFileName.empty())
- {
- assert(false);
- return false;
- }
- assert(curItem && hFeedbackwnd && pSetupRegister);
- /////////////////////////////////////////////////////////////
- // pass parameter.
- SetParameter(pSetupRegister);
- m_vUnNeededFileInxs = pFileDBItem->m_vUnneededFileInx;
- m_lUploadPeerMax = pFileDBItem->m_lUploadPeerMax;
- m_lUploadSpeedMax = pFileDBItem->m_lUploadSpeedMax * 1024;
- m_lConnectErrorMax = pFileDBItem->m_lConnectErrorMax;
- m_lConnectIntval = pFileDBItem->m_lConnectIntval;
- g_defaults["max_initiate"] = pFileDBItem->m_lPeersMax;
- g_defaults["min_peers"] = pFileDBItem->m_lPeersMin;
- //
- // CDownloaderFeedback setdownload
- //
- m_DownloaderFeedback.SetDownload(this, curItem, hFeedbackwnd, pFileDBItem->m_lCompleteKeepSeedHour, lParam);
- /////////////////////////////////////////////////////////////
- // CFileTorrent
- m_bCheckHash = bCheckHash;
- m_strSaveFileName = strSaveFileName;
- m_strTorrentFileName = strTorrentFileName;
- try
- {
- if (!m_fileTorrernt.OpenFile(m_strTorrentFileName, m_strSaveFileName))
- {
- m_DownloaderFeedback.SetBadMsg(m_fileTorrernt.GetBadMsg(), false);
- return false;
- }
- if (!m_fileTorrernt.CreateDir(m_strSaveFileName))
- {
- assert(false);
- return false;
- }
- }
- catch (string e)
- {
- m_DownloaderFeedback.SetBadMsg(e, false);
- return false;
- }
- assert(IsEventSet(m_hThreadDone));
- ResetEvent(m_hThreadDone);
- ResetEvent(m_hCompleted);
- ResetEvent(m_hevDone);
- DWORD dwThreadId = 0;
- ::CreateThread(0, 0, DownloadProc, this, 0, &dwThreadId);
- TRACE("rndownload thread = (%x)rn", dwThreadId);
- return true;
- }
- ///////////////////////////////////////////////////////////
- // download thread
- ///////////////////////////////////////////////////////////
- DWORD WINAPI CDownload::DownloadProc(void *pParam)
- {
- CDownload* pdownload = (CDownload* )pParam;
- pdownload->DownloadProcEx();
- return 0;
- }
- void CDownload::DownloadProcEx()
- {
- // init socket.
- WORD wVersionRequested;
- WSADATA wsaData;
- wVersionRequested = MAKEWORD( 2, 2 );
- int err = WSAStartup( wVersionRequested, &wsaData );
- if ( err != 0 )
- return;
- // _CrtMemState s1, s2, s3;
- // _CrtMemCheckpoint( &s1 );
- Download();
- /*
- _CrtMemCheckpoint( &s2 );
- if ( _CrtMemDifference( &s3, &s1, &s2) )
- {
- _CrtMemDumpStatistics( &s3 );
- TRACE("rnendrn");
- }
- //*/
- WSACleanup();
- // if (!IsEventSet(m_hevDone))
- m_DownloaderFeedback.QuitDownload();
- SetEvent(m_hThreadDone);
- }
- void CDownload::Download()
- {
- try
- {
- //
- // CFileTorrent
- //
- CVal* pInfo = m_fileTorrernt.GetInfo();
- CVal* pResponse = m_fileTorrernt.GetResponse();
- if (!pResponse)
- {
- assert(false);
- return;
- }
- vector<CFileInfo>& vFiles = m_fileTorrernt.GetFileInfo();
- memstream& memMyid = m_fileTorrernt.GetMyId();
- memstream& memInfohash = m_fileTorrernt.GetInfohash();
- vector<char*>& vPieces = m_fileTorrernt.GetPieces();
- long lNumPieces = vPieces.size();
- //
- // CDownloaderFeedback setdownload
- //
- // m_DownloaderFeedback.SetDownload(this);
- CDownloader download;
- //
- // middle layer
- //
- // CChoker choker(g_defaults["max_uploads"], m_hCompleted);
- CChoker choker(m_lUploadPeerMax, m_hCompleted);
- m_pChoker = &choker;
- CConnector m_connector(m_lUploadSpeedMax);
- m_pconncetorget = &m_connector;
- CRerequester requester(m_lConnectErrorMax, m_lConnectIntval, g_defaults["min_peers"], g_defaults["max_initiate"]);
- assert(!m_pRequester);
- m_pRequester = &requester;
- CEncrypter encrypter(g_defaults["max_initiate"]);
- m_pEncrypter = &encrypter;
- //
- // CStorageWrapper
- //
- CStorage storage;
- m_pStorage = &storage;
- if (!storage.Create(&m_DownloaderFeedback, vFiles, m_hevDone, g_defaults["FileOpenMax"], m_bLockFile, m_bLockFileWhileReading))
- return;
- CStorageWrapper storageWrapper(m_vUnNeededFileInxs);
- m_pStorageWrapper = &storageWrapper;
- // Make sure user can manipulate run parameter.
- SetEvent(m_hThreadSafe);
- storageWrapper.Create(&m_DownloaderFeedback, m_hevDone, m_hevUnPause,
- &storage, &download, vPieces, m_bCheckHash, (long)g_defaults["download_slice_size"], (*pInfo)["piece length"]->lVal,
- (CStorageWrapperEx::eAllocType)((long)g_defaults["AllocType"]),
- g_defaults["AllocSpeed"], m_bDoubleCheck, m_bTripleCheck);
- // notify the window check completed.
- if (!IsEventSet(m_hevDone))
- m_DownloaderFeedback.StorageCheckComplete();
- CPiecePicker piecepicker(lNumPieces);
- CMeasure upmeasure(g_defaults["max_rate_period"], g_defaults["upload_rate_fudge"]);
- CMeasure downmeasure(g_defaults["max_rate_period"]);
- CRateMeasure ratemeasure(storageWrapper.get_amount_left(), &storageWrapper);
- m_pratemeasure = &ratemeasure;
- download.Create(&m_DownloaderFeedback, &storageWrapper, &piecepicker, g_defaults["request_backlog"],
- g_defaults["max_rate_period"], &downmeasure, lNumPieces,
- g_defaults["snub_time"], &ratemeasure);
- m_pdownloader = &download;
- CUploader uploader(&m_DownloaderFeedback, &storageWrapper, &choker, g_defaults["max_slice_length"],
- g_defaults["max_rate_period"], g_defaults["upload_rate_fudge"]);
- // CConnector m_connector(&download, &uploader, &choker,
- // lNumPieces, &storageWrapper, g_defaults["max_upload_rate"], &upmeasure);
- m_connector.Create(&download, &uploader, &choker,
- lNumPieces, &storageWrapper, &upmeasure);
- // m_connector.SetMaxUploadRate(5000);
- encrypter.Create(&m_DownloaderFeedback, &m_connector,
- memMyid, memInfohash, g_defaults["max_message_length"], g_defaults["keepalive_interval"]);
- // encrypter.start_connection("", 6881, memInfohash);
- // return;
- //
- // create CDownloaderFeedback
- //
- m_DownloaderFeedback.Create(&m_connector, g_defaults["display_interval"]);
- // throw string("test error handle");
- //
- // create raw server
- //
- CRawServer rawserver(m_hevDone, m_hevUnPause, &encrypter, &choker, &m_connector, &m_DownloaderFeedback, m_pStorageWrapper);
- long lport = rawserver.Bind(g_defaults["minport"], g_defaults["maxport"]);
- if (!lport)
- {
- m_DownloaderFeedback.errorFunc("Couldn't listen - ");
- return;
- }
- m_DownloaderFeedback.PutRunParameter(lport);
- //
- // CRerequester : Get infomation from track.
- //
- string strIp = (char*)((_bstr_t)g_defaults["ip"]);
- requester.Create(&m_DownloaderFeedback, &storageWrapper, &upmeasure
- ,&downmeasure, &m_connector, &encrypter,
- (*pResponse)["announce"]->pstrVal, lport, strIp, memMyid, memInfohash,
- g_defaults["rerequest_interval"],
- g_defaults["http_timeout"], m_hevDone);
- //
- // listen loop
- //
- rawserver.listen_forever(&requester);
- //
- // close.
- //
- storage.Close();
- requester.Close();
- // show end message.
- /*
- if (IsEventSet(m_hevDone))
- m_DownloaderFeedback.ShowSystemMessage("用户取消了该下载/上传", CSystemMsg::eCmd);
- else
- m_DownloaderFeedback.SetBadMsg("unknown reason terminate download");
- //*/
- ResetEvent(m_hThreadSafe);
- WaitForSingleObject(m_hevUnPause, INFINITE);
- }
- catch (string &e)
- {
- m_DownloaderFeedback.SetBadMsg(e);
- ResetEvent(m_hThreadSafe);
- WaitForSingleObject(m_hevUnPause, INFINITE);
- }
- }
- ///////////////////////////////////////////////////////////
- // download display
- ///////////////////////////////////////////////////////////
- void CDownload::GetFractions(vector<long>& vFractions)
- {
- m_DownloaderFeedback.GetFractions(vFractions);
- }
- void CDownload::GetRunParameter(long& lPort, long &lPeerMin, long &lPeerMax,
- long &lDisplayItval, long &lAllocType, long&lFileOpenMax)
- {
- m_DownloaderFeedback.GetRunParameter(lPort);
- lPeerMin = g_defaults["min_peers"];
- lPeerMax = g_defaults["max_initiate"];
- lDisplayItval = g_defaults["display_interval"];
- lAllocType = g_defaults["AllocType"];
- lFileOpenMax = g_defaults["FileOpenMax"];
- }
- CDownloaderFeedback& CDownload::GetFeedback()
- {
- return m_DownloaderFeedback;
- }
- void CDownload::GetSystemMessage(vector<CSystemMsg>& vMsg)
- {
- m_DownloaderFeedback.GetSystemMessage(vMsg);
- }
- string CDownload::GetSocketMessageAll()
- {
- if (!IsDownloading())
- {
- assert(false);
- return "not in loop";
- }
- return m_DownloaderFeedback.GetSocketMessageAll();
- }
- string CDownload::GetSocketMessage(long lAddr, short sPort)
- {
- assert(IsDownloading());
- return m_DownloaderFeedback.GetSocketMessage(lAddr, sPort);
- }
- vector<CConncetionData>& CDownload::GetConnections()
- {
- assert(IsDownloading());
- return m_DownloaderFeedback.GetData();
- }
- CStatics& CDownload::GetStatics()
- {
- assert(IsDownloading());
- return m_DownloaderFeedback.GetStatics();
- }
- string CDownload::getmessage()
- {
- if (!IsDownloading())
- {
- assert(false);
- return "not in loop";
- }
- vector<CConncetionData>& vConnectionDatas = m_DownloaderFeedback.GetData();
- string strRet;
- for (int i=0; i<vConnectionDatas.size(); i++)
- {
- CConncetionData& connectionData = vConnectionDatas[i];
- {
- in_addr inaddress;
- inaddress.S_un.S_addr = connectionData.m_lAddr;
- char szText[1024] = {0};
- sprintf(szText, "peer - ip:%s:%d - Local:%d - uprate:%d - downrate:%d - downcount:%d - upcount:%d - havePercent:%d",
- inet_ntoa(inaddress),
- ntohs(connectionData.m_sPort),
- connectionData.m_bLocallyInitiated,
- connectionData.m_lUpRate,
- connectionData.m_lDownRate,
- connectionData.m_lDownCount,
- connectionData.m_lUpCount,
- connectionData.m_fPeerHavePercent
- );
- strRet += szText;
- strRet += "rn";
- }
- }
- return strRet;
- /*
- char szText[100] = {0};
- string strRet;
- long lrate = 0;
- lrate = m_pdownloader->m_pDownMeasure->get_rate();
- sprintf(szText, "global download rate : (%d)", lrate);
- strRet += szText;
- strRet += "rn";
- lrate = m_pconncetorget->how_many_connections();
- sprintf(szText, "connections size : (%d)", lrate);
- strRet += szText;
- strRet += "rn";
- for (int i=0; i<m_pconncetorget->m_connections.size();i++)
- {
- string strIp = m_pconncetorget->m_connections[i]->GetIP();
- strRet += "ip : (" + strIp + ")";
- lrate = m_pconncetorget->m_connections[i]->m_pDownload->get_rate();
- sprintf(szText, "peer -- download rate : (%d)", lrate);
- strRet += szText;
- lrate = m_pconncetorget->m_connections[i]->m_pUpload->get_rate();
- sprintf(szText, " -- upload rate : (%d)", lrate);
- strRet += szText;
- long lHaveCount = m_pconncetorget->m_connections[i]->m_pDownload->GetHaveCountPercent();
- sprintf(szText, " -- have count: (%d)", lHaveCount);
- strRet += szText;
- strRet += "rn";
- }
- lrate = m_pdownloader->m_pRateMeasure->get_size_left();
- sprintf(szText, "size left: (%d)", lrate);
- strRet += szText;
- strRet += "rn";
- lrate = m_pdownloader->m_pRateMeasure->get_time_left();
- tm* tt = localtime(&lrate);
- if (tt)
- sprintf(szText, "time left: (%d:%d:%d:%d)", tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec);
- else
- sprintf(szText, "time left: no");
- strRet += szText;
- strRet += "rn";
- //*/
- return strRet;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- //
- // for test
- //
- /////////////////////////////////////////////////////////////////////////////////////////////
- void CDownload::test()
- {
- /*
- CVal* pRet = 0;
- CBdecode dec;
- char buf[] = "i1000e";
- pRet = dec.bdecode(buf, sizeof(buf)/sizeof(char));
- if (pRet)
- delete pRet;
- char buf1[] = "5:hello";
- pRet = dec.bdecode(buf1, sizeof(buf1)/sizeof(char));
- if (pRet)
- delete pRet;
- //*/
- // ShowMessage((*pRet).data());
- // CVal::test();
- // CStorage::test();
- /*
- long ltime = 0;
- time(<ime);
- srand(ltime);
- PiecePickerTest();
- //*/
- // RateMeasureTest();
- // SingleDownloadTest();
- /*
- char pbuf[262144] = {0};
- for (int i=0; i<262144; i++)
- {
- pbuf[i] = i;
- }
- for (i=0; i<3000; i++)
- {
- SHA1 sha;
- unsigned int shaRes[5] = {0};
- sha.Input(pbuf, 262144);
- sha.Result(shaRes);
- TRACE("%d, ", i/30);
- if (!(i %20))
- TRACE("rn");
- }
- //*/
- }