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-2008 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
<Requested_Block_Struct
*> CReqBlockPtrList
;
107 CPartFile(CEC_PartFile_Tag
*tag
);
109 virtual void SetFileName(const CPath
& filename
);
111 CPartFile(CSearchFile
* searchresult
); //used when downloading a new file
112 CPartFile(const CED2KFileLink
* fileLink
);
113 virtual ~CPartFile();
115 bool CreateFromFile(wxString
WXUNUSED(directory
), wxString
WXUNUSED(filename
), void* WXUNUSED(pvProgressParam
)) {return false;}// not supported in this class
116 void SetPartFileStatus(uint8 newstatus
);
117 virtual bool LoadFromFile(const CFileDataIO
* WXUNUSED(file
)) { return false; }
118 bool WriteToFile(CFileDataIO
* WXUNUSED(file
)) { return false; }
119 bool IsPartFile() const { return !(status
== PS_COMPLETE
); }
120 uint32
Process(uint32 reducedownload
, uint8 m_icounter
);
121 uint8
LoadPartFile(const CPath
& in_directory
, const CPath
& filename
, bool from_backup
= false, bool getsizeonly
= false);
122 bool SavePartFile(bool Initial
= false);
123 void PartFileHashFinished(CKnownFile
* result
);
124 bool HashSinglePart(uint16 partnumber
); // true = ok , false = corrupted
126 bool CheckShowItemInGivenCat(int inCategory
);
128 bool IsComplete(uint64 start
, uint64 end
);
130 void UpdateCompletedInfos();
132 bool GetNextRequestedBlock(CUpDownClient
* sender
, std::vector
<Requested_Block_Struct
*>& toadd
, uint16
& count
);
133 void WritePartStatus(CMemFile
* file
);
134 void WriteCompleteSourcesCount(CMemFile
* file
);
135 static bool CanAddSource(uint32 userid
, uint16 port
, uint32 serverip
, uint16 serverport
, uint8
* pdebug_lowiddropped
= NULL
, bool ed2kID
= true);
136 void AddSources(CMemFile
& sources
, uint32 serverip
, uint16 serverport
, unsigned origin
, bool bWithObfuscationAndHash
);
138 uint8
GetStatus() const { return status
; }
139 uint8
GetStatus(bool /*ignorepause = false*/) const { return status
; }
141 uint8
GetStatus(bool ignorepause
= false) const;
143 virtual void UpdatePartsInfo();
144 const CPath
& GetPartMetFileName() const { return m_partmetfilename
; }
145 uint64
GetTransferred() const { return transferred
; }
146 const CPath
& GetFullName() const { return m_fullname
; }
147 float GetKBpsDown() const { return kBpsDown
; }
148 double GetPercentCompleted() const { return percentcompleted
; }
151 uint16
GetSourceCount() const { return (uint16
)m_SrcList
.size(); }
152 uint16
GetSrcA4AFCount() const { return (uint16
)m_A4AFsrclist
.size(); }
154 uint16 m_source_count
, m_a4af_source_count
;
155 uint16
GetSourceCount() const { return m_source_count
; }
156 uint16
GetSrcA4AFCount() const { return m_a4af_source_count
; }
158 uint16
GetTransferingSrcCount() const { return transferingsrc
; }
159 uint16
GetNotCurrentSourcesCount() const { return m_notCurrentSources
; };
160 void SetNotCurrentSourcesCount(uint16 new_count
) { m_notCurrentSources
= new_count
; };
161 uint16
GetValidSourcesCount() const { return m_validSources
; };
163 uint64
GetNeededSpace();
165 virtual wxString
GetFeedback() const;
167 wxString
getPartfileStatus() const; //<<--9/21/02
168 sint32
getTimeRemaining() const; //<<--9/21/02
169 time_t lastseencomplete
;
170 int getPartfileStatusRang() const;
172 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
173 uint32
WriteToBuffer(uint32 transize
, byte
*data
, uint64 start
, uint64 end
, Requested_Block_Struct
*block
);
174 void FlushBuffer(bool fromAICHRecoveryDataAvailable
= false);
176 // Barry - Is archive recovery in progress
177 volatile bool m_bRecoveringArchive
;
179 // Barry - Added to prevent list containing deleted blocks on shutdown
180 void RemoveAllRequestedBlocks(void);
182 void RemoveBlockFromList(uint64 start
,uint64 end
);
183 void RemoveAllSources(bool bTryToSwap
);
185 void StopFile(bool bCancel
= false);
186 void PauseFile(bool bInsufficient
= false);
189 virtual CPacket
* CreateSrcInfoPacket(const CUpDownClient
* forClient
, uint8 byRequestedVersion
, uint16 nRequestedOptions
);
190 void AddClientSources(CMemFile
* sources
, unsigned nSourceFrom
, uint8 uClientSXVersion
, bool bSourceExchange2
, const CUpDownClient
* pClient
= NULL
);
192 bool PreviewAvailable();
193 uint16
GetAvailablePartCount() const { return m_availablePartsCount
; }
194 uint32
GetLastAnsweredTime() const { return m_ClientSrcAnswered
; }
195 void SetLastAnsweredTime();
196 void SetLastAnsweredTimeTimeout();
197 uint64
GetLostDueToCorruption() const { return m_iLostDueToCorruption
; }
198 uint64
GetGainDueToCompression() const { return m_iGainDueToCompression
; }
199 uint32
TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH
; }
200 bool IsStopped() const { return this ? m_stopped
: true; }
201 bool IsPaused() const { return m_paused
; }
202 void UpdateFileRatingCommentAvail();
203 bool m_CommentUpdated
;
205 int GetCommonFilePenalty();
206 void UpdateDisplayedInfo(bool force
= false);
208 uint8
GetCategory() const { return m_category
; }
209 void SetCategory(uint8 cat
);
211 volatile bool m_bPreviewing
;
212 void SetDownPriority(uint8 newDownPriority
, bool bSave
= true, bool bRefresh
= true);
213 bool IsAutoDownPriority() const { return m_bAutoDownPriority
; }
214 void SetAutoDownPriority(bool flag
) { m_bAutoDownPriority
= flag
; }
215 void UpdateAutoDownPriority();
216 uint8
GetDownPriority() const { return m_iDownPriority
; }
217 void SetActive(bool bActive
);
218 uint32
GetDlActiveTime() const;
219 bool GetInsufficient() const { return m_insufficient
; }
221 void CompleteFileEnded(bool errorOccured
, const CPath
& newname
);
223 bool RemoveSource(CUpDownClient
* toremove
, bool updatewindow
= true, bool bDoStatsUpdate
= true);
225 void RequestAICHRecovery(uint16 nPart
);
226 void AICHRecoveryDataAvailable(uint16 nPart
);
229 * This function is used to update source-counts.
231 * @param oldState The old state of the client, or -1 to ignore.
232 * @param newState The new state of the client, or -1 to ignore.
234 * Call this function for a client belonging to this file, which has changed
235 * its state. The value -1 can be used to make the function ignore one of
238 * AddSource and DelSource takes care of calling this function when a source is
239 * removed, so there's no need to call this function when calling either of those.
241 void ClientStateChanged( int oldState
, int newState
);
243 bool AddSource( CUpDownClient
* client
);
244 bool DelSource( CUpDownClient
* client
);
247 * Updates the requency of avilable parts from with the data the client provides.
249 * @param client The clients whoose available parts should be considered.
250 * @param increment If true, the counts are incremented, otherwise they are decremented.
252 * This functions updates the frequency list of file-parts, using the clients
253 * parts-status. This function should be called by clients every time they update their
254 * parts-status, or when they are added or removed from the file.
256 void UpdatePartsFrequency( CUpDownClient
* client
, bool increment
);
258 ArrayOfUInts16 m_SrcpartFrequency
;
260 void SetShowSources( bool val
) { m_showSources
= val
; }
261 bool ShowSources() const { return m_showSources
; }
263 typedef std::set
<CUpDownClient
*> SourceSet
;
265 const SourceSet
& GetSourceList() const { return m_SrcList
; }
266 const SourceSet
& GetA4AFList() const { return m_A4AFsrclist
; }
268 const CReqBlockPtrList
GetRequestedBlockList() const { return m_requestedblocks_list
; }
270 // LEGACY - to be removed when possible
273 void Init(const CGapList
* gaplist
) { m_gaplist
= gaplist
; }
274 class const_iterator
{
275 CGapList::const_iterator m_it
;
279 const_iterator(const CGapList::const_iterator
& it
) { m_it
= it
; };
280 bool operator != (const const_iterator
& it
) { return m_it
!= it
.m_it
; }
281 const_iterator
& operator ++ () { ++ m_it
; return *this; }
282 Gap_Struct
* operator * () {
283 m_gap
.start
= m_it
.start();
284 m_gap
.end
= m_it
.end();
288 const_iterator
begin() const { return const_iterator(m_gaplist
->begin()); }
289 const_iterator
end() const { return const_iterator(m_gaplist
->end()); }
290 bool empty() const { return m_gaplist
->IsComplete(); }
291 uint32
size() const { return m_gaplist
->size(); }
294 const CGapList
* m_gaplist
;
296 // this function must stay, but return the m_gaplist instead
297 const CGapPtrList
& GetGapList() const { return m_gapptrlist
; }
299 const CGapList
& GetNewGapList() const { return m_gaplist
; }
301 CGapPtrList m_gapptrlist
;
306 * Adds a source to the list of dead sources.
308 * @param client The source to be recorded as dead for this file.
310 void AddDeadSource(const CUpDownClient
* client
);
313 * Checks if a source is recorded as being dead for this file.
315 * @param client The client to evaluate.
316 * @return True if dead, false otherwise.
318 * Sources that are dead are not to be considered valid
319 * sources and should not be added to the partfile.
321 bool IsDeadSource(const CUpDownClient
* client
);
324 uint16
GetMaxSources() const;
325 uint16
GetMaxSourcePerFileSoft() const;
326 uint16
GetMaxSourcePerFileUDP() const;
328 const FileRatingList
&GetRatingAndComments();
330 void AllocationFinished();
332 CFile m_hpartfile
; //permanent opened handle to avoid write conflicts
333 //! A local list of sources that are invalid for this file.
335 CDeadSourceList m_deadSources
;
338 uint16 m_notCurrentSources
;
342 uint32 m_validSources
;
344 void AddGap(uint64 start
, uint64 end
);
345 void AddGap(uint16 part
);
346 void FillGap(uint64 start
, uint64 end
);
347 void FillGap(uint16 part
);
348 bool GetNextEmptyBlockInPart(uint16 partnumber
,Requested_Block_Struct
* result
);
349 bool IsAlreadyRequested(uint64 start
, uint64 end
);
350 void CompleteFile(bool hashingdone
);
351 void CreatePartFile();
354 bool CheckFreeDiskSpace( uint64 neededSpace
= 0 );
356 bool IsCorruptedPart(uint16 partnumber
);
358 uint32 m_iLastPausePurge
;
360 uint16 m_anStates
[STATES_COUNT
];
361 uint16 transferingsrc
;
362 uint64 completedsize
;
365 uint64 m_iLostDueToCorruption
;
366 uint64 m_iGainDueToCompression
;
367 uint32 m_iTotalPacketsSavedDueToICH
;
369 CPath m_fullname
; // path/name of the met file
370 CPath m_partmetfilename
; // name of the met file
371 CPath m_PartPath
; // path/name of the partfile
375 uint8 m_iDownPriority
;
376 bool m_bAutoDownPriority
;
378 uint32 lastpurgetime
;
379 uint32 m_LastNoNeededCheck
;
381 CReqBlockPtrList m_requestedblocks_list
;
382 double percentcompleted
;
383 std::list
<uint16
> m_corrupted_list
;
384 uint16 m_availablePartsCount
;
385 uint32 m_ClientSrcAnswered
;
386 bool m_bPercentUpdated
;
388 void PerformFileComplete();
390 uint32 m_lastRefreshedDLDisplay
;
392 // Barry - Buffered data to be written
393 std::list
<PartFileBufferedData
*> m_BufferedData_list
;
395 uint32 m_nTotalBufferData
;
396 uint32 m_nLastBufferFlushTime
;
399 uint32 m_nDlActiveTime
;
404 SourceSet m_A4AFsrclist
;
405 bool m_hashsetneeded
;
406 uint32 m_lastsearchtime
;
407 bool m_localSrcReqQueued
;
409 FileRatingList m_FileRatingList
;
411 SourcenameItemList m_SourcenameItem_list
;
413 const SourcenameItemList
&GetSourcenameItemList() { return m_SourcenameItem_list
; }
414 void ClearSourcenameItemList() { m_SourcenameItem_list
.clear(); }
415 void AddSourcenameItemList(const wxString
&name
, long count
) { m_SourcenameItem_list
.push_back(SourcenameItem(name
, count
)); }
418 const FileRatingList
&GetFileRatingList() { return m_FileRatingList
; }
419 void ClearFileRatingList() { m_FileRatingList
.clear(); }
420 void AddFileRatingList(const wxString
& u
, const wxString
& f
, sint16 r
, const wxString
& c
) {
421 m_FileRatingList
.push_back(SFileRating(u
, f
, r
, c
)); }
423 bool IsHashSetNeeded() const { return m_hashsetneeded
; }
424 void SetHashSetNeeded(bool value
) { m_hashsetneeded
= value
; }
426 uint64
GetCompletedSize() const { return completedsize
; }
427 void SetCompletedSize(uint64 size
) { completedsize
= size
; }
429 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued
; }
430 void SetLocalSrcRequestQueued(bool value
) { m_localSrcReqQueued
= value
; }
432 void AddA4AFSource(CUpDownClient
* src
) { m_A4AFsrclist
.insert(src
); }
433 bool RemoveA4AFSource(CUpDownClient
* src
) { return (m_A4AFsrclist
.erase(src
) > 0); }
435 uint32
GetLastSearchTime() const { return m_lastsearchtime
; }
436 void SetLastSearchTime(uint32 time
) { m_lastsearchtime
= time
; }
438 // size of a certain part, last is different, all others are PARTSIZE
439 uint32
GetPartSize(uint16 part
) const;
441 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
443 void AddDownloadingSource(CUpDownClient
* client
);
445 void RemoveDownloadingSource(CUpDownClient
* client
);
446 void SetStatus(uint8 in
);
447 void StopPausedFile();
449 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
450 void SetA4AFAuto(bool in
) { m_is_A4AF_auto
= in
; }
451 bool IsA4AFAuto() const { return m_is_A4AF_auto
; }
453 // Kry -Sources seeds
454 void SaveSourceSeeds();
455 void LoadSourceSeeds();
457 // Dropping slow sources
458 CUpDownClient
* GetSlowerDownloadingClient(uint32 speed
, CUpDownClient
* caller
);
460 // Read data for sharing
461 bool ReadData(uint64 offset
, byte
* adr
, uint32 toread
);
464 /* downloading sources list */
465 CClientPtrList m_downloadingSourcesList
;
468 uint32 m_LastSearchTimeKad
;
469 uint8 m_TotalSearchesKad
;
471 friend class CDownQueueRem
;
472 friend class CPartFileConvert
;
476 // File_checked_for_headers