00001 // 00002 // DownloadWithTorrent.h 00003 // 00004 // Copyright (c) Shareaza Development Team, 2002-2005. 00005 // This file is part of SHAREAZA (www.shareaza.com) 00006 // 00007 // Shareaza is free software; you can redistribute it 00008 // and/or modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 of 00010 // the License, or (at your option) any later version. 00011 // 00012 // Shareaza is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with Shareaza; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 // 00021 00022 #if !defined(AFX_DOWNLOADWITHTORRENT_H__0F93FE22_BFCF_4B6E_8416_7C896432E65A__INCLUDED_) 00023 #define AFX_DOWNLOADWITHTORRENT_H__0F93FE22_BFCF_4B6E_8416_7C896432E65A__INCLUDED_ 00024 00025 #pragma once 00026 00027 #include "DownloadWithFile.h" 00028 #include "BTInfo.h" 00029 00030 class CDownloadTransferBT; 00031 class CUploadTransferBT; 00032 class CBTClient; 00033 class CBTPacket; 00034 00035 00036 class CDownloadWithTorrent : public CDownloadWithFile 00037 { 00038 // Construction 00039 public: 00040 CDownloadWithTorrent(); 00041 virtual ~CDownloadWithTorrent(); 00042 00043 // Attributes 00044 public: 00045 CBTInfo m_pTorrent; 00046 BOOL m_bTorrentRequested; 00047 BOOL m_bTorrentStarted; 00048 DWORD m_tTorrentTracker; 00049 QWORD m_nTorrentUploaded; 00050 QWORD m_nTorrentDownloaded; 00051 BOOL m_bTorrentEndgame; 00052 BOOL m_bTorrentTrackerError; 00053 CString m_sTorrentTrackerError; 00054 int m_nTorrentTrackerErrors; 00055 SHA1 m_pPeerID; 00056 CString m_sKey; 00057 int m_nStartTorrentDownloads; 00058 protected: 00059 BYTE* m_pTorrentBlock; 00060 DWORD m_nTorrentBlock; 00061 DWORD m_nTorrentSize; 00062 DWORD m_nTorrentSuccess; 00063 BOOL m_bSeeding; 00064 private: 00065 CPtrList m_pTorrentUploads; 00066 DWORD m_tTorrentChoke; 00067 DWORD m_tTorrentSources; 00068 00069 // Operations 00070 public: 00071 virtual void Serialize(CArchive& ar, int nVersion); 00072 BOOL SetTorrent(CBTInfo* pTorrent); 00073 void AddUpload(CUploadTransferBT* pUpload); 00074 void RemoveUpload(CUploadTransferBT* pUpload); 00075 void ChokeTorrent(DWORD tNow = 0); 00076 void OnTrackerEvent(BOOL bSuccess, LPCTSTR pszReason = NULL); 00077 virtual BOOL FindMoreSources(); 00078 BOOL SeedTorrent(LPCTSTR pszTarget); 00079 void CloseTorrent(); 00080 inline BOOL IsSeeding() const { return m_bSeeding; } 00081 float GetRatio() const; 00082 BOOL CheckTorrentRatio() const; 00083 BOOL UploadExists(in_addr* pIP) const; 00084 BOOL UploadExists(SHA1* pGUID) const; 00085 public: 00086 CDownloadTransferBT* CreateTorrentTransfer(CBTClient* pClient); 00087 CBTPacket* CreateBitfieldPacket(); 00088 protected: 00089 BOOL GenerateTorrentDownloadID(); //Generate Peer ID 00090 BOOL RunTorrent(DWORD tNow); 00091 void OnFinishedTorrentBlock(DWORD nBlock); 00092 void CloseTorrentUploads(); 00093 00094 inline TCHAR GenerateCharacter() const 00095 { 00096 switch (rand() % 3) 00097 { 00098 case 0 : return( 'a' + ( rand() % 26 ) ); 00099 case 1 : return( 'A' + ( rand() % 26 ) ); 00100 default: return( '0' + ( rand() % 10 ) ); 00101 } 00102 } 00103 00104 friend class CDownloadTransferBT; 00105 }; 00106 00107 enum 00108 { 00109 dtAlways, dtWhenRatio, dtWhenRequested, dtNever 00110 }; 00111 00112 #endif // !defined(AFX_DOWNLOADWITHTORRENT_H__0F93FE22_BFCF_4B6E_8416_7C896432E65A__INCLUDED_)