StorageWrapper.h
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:6k
源码类别:
P2P编程
开发平台:
Visual C++
- // StorageWrapperEx.h: interface for the CStorageWrapperEx class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_STORAGEWRAPPEREX_H__52F46C21_CE25_4B36_B301_3C55E459E680__INCLUDED_)
- #define AFX_STORAGEWRAPPEREX_H__52F46C21_CE25_4B36_B301_3C55E459E680__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- class CDownloaderFeedback;
- class CStorageEx;
- class CDownloader;
- class CStorageWrapperEx
- {
- class CHashItem
- {
- public:
- CHashItem(const char* const pBuf)
- {
- memcpy(szBuf, pBuf, 20);
- }
- CHashItem()
- {
- memset(szBuf, 0, 20);
- }
- CHashItem& operator = (const char* const pother)
- {
- memcpy(szBuf, pother, 20);
- return *this;
- }
- CHashItem& operator = (const CHashItem& other)
- {
- memcpy(szBuf, other.szBuf, 20);
- return *this;
- }
- bool operator == (const unsigned char other [20]) const
- {
- return memcmp(szBuf, other, 20) == 0;
- }
- bool operator != (const unsigned char other [20]) const
- {
- return memcmp(szBuf, other, 20) != 0;
- }
- bool operator == (const CHashItem& other) const
- {
- return memcmp(szBuf, other.szBuf, 20) == 0;
- }
- bool operator < (const CHashItem& other) const
- {
- return memcmp(szBuf, other.szBuf, 20) < 0;
- }
- char szBuf[20];
- };
- class size
- {
- public :
- size(long _x, long _y)
- {
- x = _x;
- y = _y;
- }
- bool operator == (const size& other) const
- {
- return (x == other.x && y == other.y);
- }
- long x;
- long y;
- };
- public:
- //
- // Unneeded files.
- //
- // Called by gui.
- void SetUnNeededFilesInxs(const vector<CSize>& vUnNeededFileInxs = vector<CSize>());
- float GetPartCompletedPercent();
- BLONG get_total_legth_part();
- BLONG get_amount_left_part();
- BLONG get_amount_left_part_include_temp();
- CStorageWrapperEx( );
- virtual ~CStorageWrapperEx();
- enum eAllocType {eAllocNormal, eAllocSparse, eAllocPreAllocate, eAllocBackGound};
- bool Create(CDownloaderFeedback* pMain,
- HANDLE hevDone, HANDLE hevUnPause, CStorageEx* pStorageEx, CDownloader* pDownloader, vector<char*>& vPieces, bool check_hashes,
- long request_size, long piece_length,
- eAllocType eAllocType = eAllocNormal, long lAllocateRate = 1, bool m_bDoubleCheck = false, bool m_bTripleCheck = false) throw (string);
- bool get_piece(long index, char* pPieceBuf, long begin, long length);
- void piece_came_in(long index, char *pPieceBuf, long begin, long length);
- void new_request(long index, long& begin, long& length);
- void request_lost(long index, long begin, long length);
- bool do_I_have(long index);
- vector<bool>& get_have_list();
- BLONG get_total_legth();
- bool do_I_have_anything();
- bool is_everything_pending();
- void _make_inactive(long index);
- bool do_I_have_requests(long index);
- BLONG get_amount_left();
- void OnIdle();
- private:
- bool _MakeOffsetToTruePlace(const long index, const long n);
- bool _FindPlaceForNewComeIndex(long index, bool bFirst);
- bool _do_I_have_requests(long index);
- bool _get_piece(long index, char* pPieceBuf, long begin, long length);
- bool _piece_came_in(long index, char *pPieceBuf, long begin, long length);
- bool CheckAfterMoveing(int iIndex, char* pszBuf);
- bool _check_single(long index, bool check);
- bool CheckFiles();
- bool bgalloc();
- bool _bgalloc();
- bool _doalloc();
- long _piecelen(int iPiece);
- void markgot(long iPiece,long lPos);
- bool _waspre(int iIndex);
- void ShowDetailsTest();
- CDownloaderFeedback* m_pMain;
- HANDLE m_hevDone;
- HANDLE m_hevUnPause;
- CStorageEx* m_pStorage;
- CDownloader* m_pDownloader;
- long m_request_size;
- long m_piece_length;
- BLONG m_lTotalLength;
- BLONG m_lAmountLeft;
- vector<size> m_vNone;
- vector<long> m_vNumActive;
- vector<vector<size> > m_vInactiveRequests;
- vector<CHashItem> m_vHashs;
- vector<bool> m_vHave;
- BLONG m_lAmountInactive;
- bool m_bCheckHashes;
- bool m_bDoubleCheck;
- bool m_bTripleCheck;
- bool m_bBgallocEnabled;
- bool m_bBgallocActive;
- eAllocType m_eAllocType;
- vector<bool> m_vWasChecked;
- map<long, long > m_mPlaces;
- vector<long> m_vHoles;
- vector<bool> m_vStatActive;
- vector<bool> m_vStatNew;
- vector<bool> m_vDirty;
- long m_lStatNumflunked;
- long m_lStatMumdownloaded;
- long m_lStatNumfound;
- time_t m_tLast;
- time_t m_tTimeSpan;
- //
- // switch Unneedfiles.
- //
- bool _NormalizeUneedRange(BLONG & lbegPos, BLONG & lendPos);
- bool _ReadIndexData(char *pBuf, const BLONG lPos, const BLONG lAmount, bool bFlushFirst = false);
- bool _WriteIndexData(char *pBuf, const BLONG lPos, const BLONG lAmount );
- void _FormatRangeHoles();
- void _MakeUnNeededIndexs();
- bool _GetUnNeededFilesInxs(vector<CSize>& vUnNeededFileInxs);
- bool IsUnNeededIndex(long lIndex);
- // CSize _GetFileOffset(const long index);
- // long _GetMinHolesIndex(const long index);
- long _GetRequestSize(long index);
- long m_lPartAmountInactive;
- BLONG m_lPartTotalLength;
- BLONG m_lPartAmountLeft;
- bool m_bModified;
- vector<CSize> m_vUnNeededFileInxs;
- vector<CSize> m_vUnNeededIndexs;
- vector<long> m_vRangeHoles;
- vector<long> m_vFileOffset;
- vector<long> m_vOffsetTrueIndex;
- vector<bool> m_vFileOffsetPreAllocated;
- map<unsigned long , unsigned long> m_mPartAmountIncome;
- CCriticalSection m_criticalSection;
- };
- class CStorageWrapper : public CStorageWrapperEx
- {
- public:
- CStorageWrapper (const vector<CSize>& vUnNeededFileInxs = vector<CSize>());
- };
- #endif // !defined(AFX_STORAGEWRAPPEREX_H__52F46C21_CE25_4B36_B301_3C55E459E680__INCLUDED_)