2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "KnownFile.h" // Needed for CKnownFile
31 #include "CFile.h" // Needed for CFile
33 #include "OtherStructs.h" // Needed for Gap_Struct
34 #include "DeadSourceList.h" // Needed for CDeadSourceList
44 //#define BUFFER_SIZE_LIMIT 500000 // Max bytes before forcing a flush
45 #define BUFFER_TIME_LIMIT 60000 // Max milliseconds before forcing a flush
47 #define STATES_COUNT 13
49 // Ok, eMule and aMule are building incompatible backup files because
50 // of the different name. aMule was using ".BAK" and eMule ".bak".
51 // This should fix it.
52 #define PARTMET_BAK_EXT wxT(".bak")
54 enum EPartFileFormat
{
63 struct PartFileBufferedData
65 byte
*data
; // Barry - This is the data to be written
66 uint64 start
; // Barry - This is the start offset of the data
67 uint64 end
; // Barry - This is the end offset of the data
68 Requested_Block_Struct
*block
; // Barry - This is the requested block that this data relates to
80 SFileRating(const wxString
&u
, const wxString
&f
, sint16 r
, const wxString
&c
);
81 SFileRating(const SFileRating
&fr
);
82 SFileRating(const CUpDownClient
&client
);
86 typedef std::list
<SFileRating
> FileRatingList
;
94 SourcenameItem(const wxString
&n
, long c
)
99 typedef std::list
<SourcenameItem
> SourcenameItemList
;
101 class CPartFile
: public CKnownFile
{
103 typedef std::list
<Gap_Struct
*> CGapPtrList
;
104 typedef std::list
<Requested_Block_Struct
*> CReqBlockPtrList
;
109 CPartFile(CEC_PartFile_Tag
*tag
);
111 virtual void SetFileName(const CPath
& filename
);
113 CPartFile(CSearchFile
* searchresult
); //used when downloading a new file
114 CPartFile(const CED2KFileLink
* fileLink
);
115 virtual ~CPartFile();
117 bool CreateFromFile(wxString
WXUNUSED(directory
), wxString
WXUNUSED(filename
), void* WXUNUSED(pvProgressParam
)) {return false;}// not supported in this class
118 void SetPartFileStatus(uint8 newstatus
);
119 virtual bool LoadFromFile(const CFileDataIO
* WXUNUSED(file
)) { return false; }
120 bool WriteToFile(CFileDataIO
* WXUNUSED(file
)) { return false; }
121 bool IsPartFile() const { return !(status
== PS_COMPLETE
); }
122 uint32
Process(uint32 reducedownload
, uint8 m_icounter
);
123 uint8
LoadPartFile(const CPath
& in_directory
, const CPath
& filename
, bool from_backup
= false, bool getsizeonly
= false);
124 bool SavePartFile(bool Initial
= false);
125 void PartFileHashFinished(CKnownFile
* result
);
126 bool HashSinglePart(uint16 partnumber
); // true = ok , false = corrupted
128 bool CheckShowItemInGivenCat(int inCategory
);
130 bool IsComplete(uint64 start
, uint64 end
);
132 void UpdateCompletedInfos();
134 bool GetNextRequestedBlock(CUpDownClient
* sender
,Requested_Block_Struct
** newblocks
,uint16
* count
);
135 void WritePartStatus(CMemFile
* file
);
136 void WriteCompleteSourcesCount(CMemFile
* file
);
137 static bool CanAddSource(uint32 userid
, uint16 port
, uint32 serverip
, uint16 serverport
, uint8
* pdebug_lowiddropped
= NULL
, bool ed2kID
= true);
138 void AddSources(CMemFile
& sources
, uint32 serverip
, uint16 serverport
, unsigned origin
, bool bWithObfuscationAndHash
);
140 uint8
GetStatus() const { return status
; }
141 uint8
GetStatus(bool /*ignorepause = false*/) const { return status
; }
143 uint8
GetStatus(bool ignorepause
= false) const;
145 virtual void UpdatePartsInfo();
146 const CPath
& GetPartMetFileName() const { return m_partmetfilename
; }
147 uint64
GetTransferred() const { return transferred
; }
148 const CPath
& GetFullName() const { return m_fullname
; }
149 float GetKBpsDown() const { return kBpsDown
; }
150 double GetPercentCompleted() const { return percentcompleted
; }
153 uint16
GetSourceCount() const { return (uint16
)m_SrcList
.size(); }
154 uint16
GetSrcA4AFCount() const { return (uint16
)m_A4AFsrclist
.size(); }
156 uint16 m_source_count
, m_a4af_source_count
;
157 uint16
GetSourceCount() const { return m_source_count
; }
158 uint16
GetSrcA4AFCount() const { return m_a4af_source_count
; }
160 uint16
GetTransferingSrcCount() const { return transferingsrc
; }
161 uint16
GetNotCurrentSourcesCount() const { return m_notCurrentSources
; };
162 void SetNotCurrentSourcesCount(uint16 new_count
) { m_notCurrentSources
= new_count
; };
163 uint16
GetValidSourcesCount() const { return m_validSources
; };
165 uint64
GetNeededSpace();
167 wxString
GetFeedback();
169 wxString
getPartfileStatus() const; //<<--9/21/02
170 sint32
getTimeRemaining() const; //<<--9/21/02
171 time_t lastseencomplete
;
172 int getPartfileStatusRang() const;
174 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
175 uint32
WriteToBuffer(uint32 transize
, byte
*data
, uint64 start
, uint64 end
, Requested_Block_Struct
*block
);
176 void FlushBuffer(bool forcewait
=false, bool bForceICH
= false, bool bNoAICH
= false);
178 // Barry - Is archive recovery in progress
179 volatile bool m_bRecoveringArchive
;
181 // Barry - Added to prevent list containing deleted blocks on shutdown
182 void RemoveAllRequestedBlocks(void);
184 void RemoveBlockFromList(uint64 start
,uint64 end
);
185 void RemoveAllSources(bool bTryToSwap
);
187 void StopFile(bool bCancel
= false);
188 void PauseFile(bool bInsufficient
= false);
191 virtual CPacket
* CreateSrcInfoPacket(const CUpDownClient
* forClient
, uint8 byRequestedVersion
, uint16 nRequestedOptions
);
192 void AddClientSources(CMemFile
* sources
, unsigned nSourceFrom
, uint8 uClientSXVersion
, bool bSourceExchange2
, const CUpDownClient
* pClient
= NULL
);
194 bool PreviewAvailable();
195 uint8
GetAvailablePartCount() const { return m_availablePartsCount
; }
196 uint32
GetLastAnsweredTime() const { return m_ClientSrcAnswered
; }
197 void SetLastAnsweredTime();
198 void SetLastAnsweredTimeTimeout();
199 uint64
GetLostDueToCorruption() const { return m_iLostDueToCorruption
; }
200 uint64
GetGainDueToCompression() const { return m_iGainDueToCompression
; }
201 uint32
TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH
; }
202 bool IsStopped() const { return this ? m_stopped
: true; }
203 bool IsPaused() const { return m_paused
; }
204 void UpdateFileRatingCommentAvail();
205 bool m_CommentUpdated
;
207 int GetCommonFilePenalty();
208 void UpdateDisplayedInfo(bool force
= false);
210 uint8
GetCategory() const { return m_category
; }
211 void SetCategory(uint8 cat
);
213 CFile m_hpartfile
; //permanent opened handle to avoid write conflicts
214 volatile bool m_bPreviewing
;
215 void SetDownPriority(uint8 newDownPriority
, bool bSave
= true, bool bRefresh
= true);
216 bool IsAutoDownPriority() const { return m_bAutoDownPriority
; }
217 void SetAutoDownPriority(bool flag
) { m_bAutoDownPriority
= flag
; }
218 void UpdateAutoDownPriority();
219 uint8
GetDownPriority() const { return m_iDownPriority
; }
220 void SetActive(bool bActive
);
221 uint32
GetDlActiveTime() const;
222 bool GetInsufficient() const { return m_insufficient
; }
224 void CompleteFileEnded(bool errorOccured
, const CPath
& newname
);
226 bool RemoveSource(CUpDownClient
* toremove
, bool updatewindow
= true, bool bDoStatsUpdate
= true);
228 void RequestAICHRecovery(uint16 nPart
);
229 void AICHRecoveryDataAvailable(uint16 nPart
);
232 * This function is used to update source-counts.
234 * @param oldState The old state of the client, or -1 to ignore.
235 * @param newState The new state of the client, or -1 to ignore.
237 * Call this function for a client belonging to this file, which has changed
238 * its state. The value -1 can be used to make the function ignore one of
241 * AddSource and DelSource takes care of calling this function when a source is
242 * removed, so there's no need to call this function when calling either of those.
244 void ClientStateChanged( int oldState
, int newState
);
246 bool AddSource( CUpDownClient
* client
);
247 bool DelSource( CUpDownClient
* client
);
250 * Updates the requency of avilable parts from with the data the client provides.
252 * @param client The clients whoose available parts should be considered.
253 * @param increment If true, the counts are incremented, otherwise they are decremented.
255 * This functions updates the frequency list of file-parts, using the clients
256 * parts-status. This function should be called by clients every time they update their
257 * parts-status, or when they are added or removed from the file.
259 void UpdatePartsFrequency( CUpDownClient
* client
, bool increment
);
261 ArrayOfUInts16 m_SrcpartFrequency
;
263 void SetShowSources( bool val
) { m_showSources
= val
; }
264 bool ShowSources() const { return m_showSources
; }
266 typedef std::set
<CUpDownClient
*> SourceSet
;
268 const SourceSet
& GetSourceList() const { return m_SrcList
; }
269 const SourceSet
& GetA4AFList() const { return m_A4AFsrclist
; }
271 const CGapPtrList
GetGapList() const { return m_gaplist
; }
272 const CReqBlockPtrList
GetRequestedBlockList() const { return m_requestedblocks_list
; }
276 * Adds a source to the list of dead sources.
278 * @param client The source to be recorded as dead for this file.
280 void AddDeadSource(const CUpDownClient
* client
);
283 * Checks if a source is recorded as being dead for this file.
285 * @param client The client to evaluate.
286 * @return True if dead, false otherwise.
288 * Sources that are dead are not to be considered valid
289 * sources and should not be added to the partfile.
291 bool IsDeadSource(const CUpDownClient
* client
);
294 uint16
GetMaxSources() const;
295 uint16
GetMaxSourcePerFileSoft() const;
296 uint16
GetMaxSourcePerFileUDP() const;
298 const FileRatingList
&GetRatingAndComments();
301 //! A local list of sources that are invalid for this file.
303 CDeadSourceList m_deadSources
;
306 uint16 m_notCurrentSources
;
310 uint32 m_validSources
;
312 void AddGap(uint64 start
, uint64 end
);
313 void FillGap(uint64 start
, uint64 end
);
314 bool GetNextEmptyBlockInPart(uint16 partnumber
,Requested_Block_Struct
* result
);
315 bool IsAlreadyRequested(uint64 start
, uint64 end
);
316 void CompleteFile(bool hashingdone
);
317 void CreatePartFile();
320 bool CheckFreeDiskSpace( uint32 neededSpace
= 0 );
322 bool IsCorruptedPart(uint16 partnumber
);
324 uint64
GetTotalGapSizeInPart(uint32 uPart
) const;
326 uint64
GetTotalGapSizeInRange(uint64 uRangeStart
, uint64 uRangeEnd
) const;
328 uint32 m_iLastPausePurge
;
330 uint16 m_anStates
[STATES_COUNT
];
331 uint16 transferingsrc
;
332 uint64 completedsize
;
335 uint64 m_iLostDueToCorruption
;
336 uint64 m_iGainDueToCompression
;
337 uint32 m_iTotalPacketsSavedDueToICH
;
340 CPath m_partmetfilename
;
344 uint8 m_iDownPriority
;
345 bool m_bAutoDownPriority
;
347 uint32 lastpurgetime
;
348 uint32 m_LastNoNeededCheck
;
349 CGapPtrList m_gaplist
;
350 CReqBlockPtrList m_requestedblocks_list
;
351 double percentcompleted
;
352 std::list
<uint16
> m_corrupted_list
;
353 uint8 m_availablePartsCount
;
354 uint32 m_ClientSrcAnswered
;
355 bool m_bPercentUpdated
;
357 void PerformFileComplete();
359 uint32 m_lastRefreshedDLDisplay
;
361 // Barry - Buffered data to be written
362 std::list
<PartFileBufferedData
*> m_BufferedData_list
;
364 uint32 m_nTotalBufferData
;
365 uint32 m_nLastBufferFlushTime
;
368 uint32 m_nDlActiveTime
;
373 SourceSet m_A4AFsrclist
;
374 bool m_hashsetneeded
;
375 uint32 m_lastsearchtime
;
376 bool m_localSrcReqQueued
;
378 FileRatingList m_FileRatingList
;
380 SourcenameItemList m_SourcenameItem_list
;
382 const SourcenameItemList
&GetSourcenameItemList() { return m_SourcenameItem_list
; }
383 void ClearSourcenameItemList() { m_SourcenameItem_list
.clear(); }
384 void AddSourcenameItemList(const wxString
&name
, long count
) { m_SourcenameItem_list
.push_back(SourcenameItem(name
, count
)); }
387 const FileRatingList
&GetFileRatingList() { return m_FileRatingList
; }
388 void ClearFileRatingList() { m_FileRatingList
.clear(); }
389 void AddFileRatingList(const wxString
& u
, const wxString
& f
, sint16 r
, const wxString
& c
) {
390 m_FileRatingList
.push_back(SFileRating(u
, f
, r
, c
)); }
392 bool IsHashSetNeeded() const { return m_hashsetneeded
; }
393 void SetHashSetNeeded(bool value
) { m_hashsetneeded
= value
; }
395 uint64
GetCompletedSize() const { return completedsize
; }
396 void SetCompletedSize(uint64 size
) { completedsize
= size
; }
398 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued
; }
399 void SetLocalSrcRequestQueued(bool value
) { m_localSrcReqQueued
= value
; }
401 void AddA4AFSource(CUpDownClient
* src
) { m_A4AFsrclist
.insert(src
); }
402 bool RemoveA4AFSource(CUpDownClient
* src
) { return (m_A4AFsrclist
.erase(src
) > 0); }
404 uint32
GetLastSearchTime() const { return m_lastsearchtime
; }
405 void SetLastSearchTime(uint32 time
) { m_lastsearchtime
= time
; }
408 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
410 void AddDownloadingSource(CUpDownClient
* client
);
412 void RemoveDownloadingSource(CUpDownClient
* client
);
413 void SetStatus(uint8 in
);
414 void StopPausedFile();
416 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
417 void SetA4AFAuto(bool in
) { m_is_A4AF_auto
= in
; }
418 bool IsA4AFAuto() const { return m_is_A4AF_auto
; }
420 // Kry -Sources seeds
421 void SaveSourceSeeds();
422 void LoadSourceSeeds();
424 // Dropping slow sources
426 CUpDownClient
* GetSlowerDownloadingClient(uint32 speed
, CUpDownClient
* caller
) ;
429 /* downloading sources list */
430 CClientPtrList m_downloadingSourcesList
;
433 uint32 m_LastSearchTimeKad
;
434 uint8 m_TotalSearchesKad
;
436 friend class CDownQueueRem
;
437 friend class CPartFileConvert
;
441 // File_checked_for_headers