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
26 #include "DownloadQueue.h" // Interface declarations
28 #include <protocol/Protocols.h>
29 #include <protocol/kad/Constants.h>
30 #include <common/Macros.h>
31 #include <common/MenuIDs.h>
32 #include <common/Constants.h>
36 #include "Server.h" // Needed for CServer
37 #include "Packet.h" // Needed for CPacket
38 #include "MemFile.h" // Needed for CMemFile
39 #include "ClientList.h" // Needed for CClientList
40 #include "updownclient.h" // Needed for CUpDownClient
41 #include "ServerList.h" // Needed for CServerList
42 #include "ServerConnect.h" // Needed for CServerConnect
43 #include "ED2KLink.h" // Needed for CED2KFileLink
44 #include "SearchList.h" // Needed for CSearchFile
45 #include "SharedFileList.h" // Needed for CSharedFileList
46 #include "PartFile.h" // Needed for CPartFile
47 #include "Preferences.h" // Needed for thePrefs
48 #include "amule.h" // Needed for theApp
49 #include "AsyncDNS.h" // Needed for CAsyncDNS
50 #include "Statistics.h" // Needed for theStats
52 #include <common/Format.h> // Needed for CFormat
54 #include <common/FileFunctions.h> // Needed for CDirIterator
55 #include "GuiEvents.h" // Needed for Notify_*
56 #include "UserEvents.h"
57 #include "MagnetURI.h" // Needed for CMagnetED2KConverter
58 #include "ScopedPtr.h" // Needed for CScopedPtr
59 #include "PlatformSpecific.h" // Needed for CanFSHandleLargeFiles
61 #include "kademlia/kademlia/Kademlia.h"
63 #include <string> // Do_not_auto_remove (mingw-gcc-3.4.5)
66 // Max. file IDs per UDP packet
67 // ----------------------------
68 // 576 - 30 bytes of header (28 for UDP, 2 for "E3 9A" edonkey proto) = 546 bytes
72 #define MAX_FILES_PER_UDP_PACKET 31 // 2+16*31 = 498 ... is still less than 512 bytes!!
73 #define MAX_REQUESTS_PER_SERVER 35
76 CDownloadQueue::CDownloadQueue()
77 // Needs to be recursive that that is can own an observer assigned to itself
78 : m_mutex( wxMUTEX_RECURSIVE
)
83 m_lastudpsearchtime
= 0;
84 m_lastudpstattime
= 0;
86 m_nLastED2KLinkCheck
= 0;
87 m_dwNextTCPSrcReq
= 0;
88 m_cRequestsSentToServer
= 0;
90 SetLastKademliaFileRequest();
94 CDownloadQueue::~CDownloadQueue()
96 if ( !m_filelist
.empty() ) {
97 for ( unsigned int i
= 0; i
< m_filelist
.size(); i
++ ) {
98 AddLogLineNS(CFormat(_("Saving PartFile %u of %u")) % (i
+ 1) % m_filelist
.size());
101 AddLogLineNS(_("All PartFiles Saved."));
106 void CDownloadQueue::LoadMetFiles(const CPath
& path
)
108 AddLogLineNS(CFormat(_("Loading temp files from %s.")) % path
.GetPrintable());
110 std::vector
<CPath
> files
;
112 // Locate part-files to be loaded
113 CDirIterator
TempDir(path
);
114 CPath fileName
= TempDir
.GetFirstFile(CDirIterator::File
, wxT("*.part.met"));
115 while (fileName
.IsOk()) {
116 files
.push_back(path
.JoinPaths(fileName
));
118 fileName
= TempDir
.GetNextFile();
121 // Loading in order makes it easier to figure which
122 // file is broken in case of crashes, or the like.
123 std::sort(files
.begin(), files
.end());
126 for ( size_t i
= 0; i
< files
.size(); i
++ ) {
127 AddLogLineNS(CFormat(_("Loading PartFile %u of %u")) % (i
+ 1) % files
.size());
128 fileName
= files
[i
].GetFullName();
129 CPartFile
*toadd
= new CPartFile();
130 bool result
= toadd
->LoadPartFile(path
, fileName
) != 0;
133 result
= toadd
->LoadPartFile(path
, fileName
, true) != 0;
135 if (result
&& !IsFileExisting(toadd
->GetFileHash())) {
137 wxMutexLocker
lock(m_mutex
);
138 m_filelist
.push_back(toadd
);
140 NotifyObservers(EventType(EventType::INSERTED
, toadd
));
141 Notify_DownloadCtrlAddFile(toadd
);
145 msg
<< CFormat(wxT("WARNING: Duplicate partfile with hash '%s' found, skipping: %s"))
146 % toadd
->GetFileHash().Encode() % fileName
;
148 // If result is false, then reading of both the primary and the backup .met failed
150 _("ERROR: Failed to load backup file. Search http://forum.amule.org for .part.met recovery solutions."));
151 msg
<< CFormat(wxT("ERROR: Failed to load PartFile '%s'")) % fileName
;
155 // Delete the partfile object in the end.
159 AddLogLineNS(_("All PartFiles Loaded."));
161 if ( GetFileCount() == 0 ) {
162 AddLogLineM(false, _("No part files found"));
164 AddLogLineM(false, wxString::Format(wxPLURAL("Found %u part file", "Found %u part files", GetFileCount()), GetFileCount()) );
167 CheckDiskspace( path
);
172 uint16
CDownloadQueue::GetFileCount() const
174 wxMutexLocker
lock( m_mutex
);
176 return m_filelist
.size();
180 CServer
* CDownloadQueue::GetUDPServer() const
182 wxMutexLocker
lock( m_mutex
);
188 void CDownloadQueue::SetUDPServer( CServer
* server
)
190 wxMutexLocker
lock( m_mutex
);
192 m_udpserver
= server
;
196 void CDownloadQueue::SaveSourceSeeds()
198 for ( uint16 i
= 0; i
< GetFileCount(); i
++ ) {
199 GetFileByIndex( i
)->SaveSourceSeeds();
204 void CDownloadQueue::LoadSourceSeeds()
206 for ( uint16 i
= 0; i
< GetFileCount(); i
++ ) {
207 GetFileByIndex( i
)->LoadSourceSeeds();
212 void CDownloadQueue::AddSearchToDownload(CSearchFile
* toadd
, uint8 category
)
214 if ( IsFileExisting(toadd
->GetFileHash()) ) {
218 if (toadd
->GetFileSize() > OLD_MAX_FILE_SIZE
) {
219 if (!PlatformSpecific::CanFSHandleLargeFiles(thePrefs::GetTempDir())) {
220 AddLogLineM(true, _("Filesystem for Temp directory cannot handle large files."));
222 } else if (!PlatformSpecific::CanFSHandleLargeFiles(theApp
->glob_prefs
->GetCatPath(category
))) {
223 AddLogLineM(true, _("Filesystem for Incoming directory cannot handle large files."));
228 CPartFile
* newfile
= NULL
;
230 newfile
= new CPartFile(toadd
);
231 } catch (const CInvalidPacket
& WXUNUSED(e
)) {
232 AddDebugLogLineM(true, logDownloadQueue
, wxT("Search-result contained invalid tags, could not add"));
235 if ( newfile
&& newfile
->GetStatus() != PS_ERROR
) {
236 AddDownload( newfile
, thePrefs::AddNewFilesPaused(), category
);
237 // Add any possible sources
238 if (toadd
->GetClientID() && toadd
->GetClientPort()) {
239 CMemFile
sources(1+4+2);
240 sources
.WriteUInt8(1);
241 sources
.WriteUInt32(toadd
->GetClientID());
242 sources
.WriteUInt16(toadd
->GetClientPort());
244 newfile
->AddSources(sources
, toadd
->GetClientServerIP(), toadd
->GetClientServerPort(), SF_SEARCH_RESULT
, false);
246 for (std::list
<CSearchFile::ClientStruct
>::const_iterator it
= toadd
->GetClients().begin(); it
!= toadd
->GetClients().end(); ++it
) {
247 CMemFile
sources(1+4+2);
248 sources
.WriteUInt8(1);
249 sources
.WriteUInt32(it
->m_ip
);
250 sources
.WriteUInt16(it
->m_port
);
252 newfile
->AddSources(sources
, it
->m_serverIP
, it
->m_serverPort
, SF_SEARCH_RESULT
, false);
262 void operator()(CPartFile
* file
) {
263 // Check if we should filter out other categories
264 if ((m_category
!= -1) && (file
->GetCategory() != m_category
)) {
266 } else if (file
->GetStatus() != PS_PAUSED
) {
270 if (!m_result
|| (file
->GetDownPriority() > m_result
->GetDownPriority())) {
275 //! The category to look for, or -1 if any category is good
277 //! If any acceptable files are found, this variable store their pointer
282 void CDownloadQueue::StartNextFile(CPartFile
* oldfile
)
284 if ( thePrefs::StartNextFile() ) {
285 SFindBestPF visitor
= { -1, NULL
};
288 wxMutexLocker
lock(m_mutex
);
290 if (thePrefs::StartNextFileSame()) {
291 // Get a download in the same category
292 visitor
.m_category
= oldfile
->GetCategory();
294 visitor
= std::for_each(m_filelist
.begin(), m_filelist
.end(), visitor
);
297 if (visitor
.m_result
== NULL
) {
298 // Get a download, regardless of category
299 visitor
.m_category
= -1;
301 visitor
= std::for_each(m_filelist
.begin(), m_filelist
.end(), visitor
);
305 if (visitor
.m_result
) {
306 visitor
.m_result
->ResumeFile();
312 void CDownloadQueue::AddDownload(CPartFile
* file
, bool paused
, uint8 category
)
314 wxCHECK_RET(!IsFileExisting(file
->GetFileHash()), wxT("Adding duplicate part-file"));
316 if (file
->GetStatus(true) == PS_ALLOCATING
) {
318 } else if (paused
&& GetFileCount()) {
323 wxMutexLocker
lock(m_mutex
);
324 m_filelist
.push_back( file
);
328 NotifyObservers( EventType( EventType::INSERTED
, file
) );
329 if (category
< theApp
->glob_prefs
->GetCatCount()) {
330 file
->SetCategory(category
);
332 AddDebugLogLineM( false, logDownloadQueue
, wxT("Tried to add download into invalid category.") );
334 Notify_DownloadCtrlAddFile( file
);
335 theApp
->searchlist
->UpdateSearchFileByHash(file
->GetFileHash()); // Update file in the search dialog if it's still open
336 AddLogLineM(true, CFormat(_("Downloading %s")) % file
->GetFileName() );
340 bool CDownloadQueue::IsFileExisting( const CMD4Hash
& fileid
) const
342 if (CKnownFile
* file
= theApp
->sharedfiles
->GetFileByID(fileid
)) {
343 if (file
->IsPartFile()) {
344 AddLogLineM(true, CFormat( _("You are already trying to download the file '%s'") ) % file
->GetFileName());
346 // Check if the file exists, since otherwise the user is forced to
347 // manually reload the shares to download a file again.
348 CPath fullpath
= file
->GetFilePath().JoinPaths(file
->GetFileName());
349 if (!fullpath
.FileExists()) {
350 // The file is no longer available, unshare it
351 theApp
->sharedfiles
->RemoveFile(file
);
356 AddLogLineM(true, CFormat( _("You already have the file '%s'") ) % file
->GetFileName());
360 } else if ((file
= GetFileByID(fileid
))) {
361 AddLogLineM(true, CFormat( _("You are already trying to download the file %s") ) % file
->GetFileName());
369 void CDownloadQueue::Process()
371 // send src requests to local server
372 ProcessLocalRequests();
375 wxMutexLocker
lock(m_mutex
);
377 uint32 downspeed
= 0;
378 if (thePrefs::GetMaxDownload() != UNLIMITED
&& m_datarate
> 1500) {
379 downspeed
= (((uint32
)thePrefs::GetMaxDownload())*1024*100)/(m_datarate
+1);
380 if (downspeed
< 50) {
382 } else if (downspeed
> 200) {
389 uint32 cur_datarate
= 0;
390 uint32 cur_udcounter
= m_udcounter
;
392 for ( uint16 i
= 0; i
< m_filelist
.size(); i
++ ) {
393 CPartFile
* file
= m_filelist
[i
];
395 CMutexUnlocker
unlocker(m_mutex
);
397 if ( file
->GetStatus() == PS_READY
|| file
->GetStatus() == PS_EMPTY
){
398 cur_datarate
+= file
->Process( downspeed
, cur_udcounter
);
400 //This will make sure we don't keep old sources to paused and stoped files..
401 file
->StopPausedFile();
405 m_datarate
+= cur_datarate
;
408 if (m_udcounter
== 5) {
409 if (theApp
->serverconnect
->IsUDPSocketAvailable()) {
410 if( (::GetTickCount() - m_lastudpstattime
) > UDPSERVERSTATTIME
) {
411 m_lastudpstattime
= ::GetTickCount();
413 CMutexUnlocker
unlocker(m_mutex
);
414 theApp
->serverlist
->ServerStats();
419 if (m_udcounter
== 10) {
421 if (theApp
->serverconnect
->IsUDPSocketAvailable()) {
422 if ( (::GetTickCount() - m_lastudpsearchtime
) > UDPSERVERREASKTIME
) {
428 if ( (::GetTickCount() - m_lastsorttime
) > 10000 ) {
433 // Check if any paused files can be resumed
435 CheckDiskspace(thePrefs::GetTempDir());
439 // Check for new links once per second.
440 if ((::GetTickCount() - m_nLastED2KLinkCheck
) >= 1000) {
441 theApp
->AddLinksFromFile();
442 m_nLastED2KLinkCheck
= ::GetTickCount();
447 CPartFile
* CDownloadQueue::GetFileByID(const CMD4Hash
& filehash
) const
449 wxMutexLocker
lock( m_mutex
);
451 for ( uint16 i
= 0; i
< m_filelist
.size(); ++i
) {
452 if ( filehash
== m_filelist
[i
]->GetFileHash()) {
453 return m_filelist
[ i
];
461 CPartFile
* CDownloadQueue::GetFileByIndex(unsigned int index
) const
463 wxMutexLocker
lock( m_mutex
);
465 if ( index
< m_filelist
.size() ) {
466 return m_filelist
[ index
];
474 bool CDownloadQueue::IsPartFile(const CKnownFile
* file
) const
476 wxMutexLocker
lock(m_mutex
);
478 for (uint16 i
= 0; i
< m_filelist
.size(); ++i
) {
479 if (file
== m_filelist
[i
]) {
488 void CDownloadQueue::OnConnectionState(bool bConnected
)
490 wxMutexLocker
lock(m_mutex
);
492 for (uint16 i
= 0; i
< m_filelist
.size(); ++i
) {
493 if ( m_filelist
[i
]->GetStatus() == PS_READY
||
494 m_filelist
[i
]->GetStatus() == PS_EMPTY
) {
495 m_filelist
[i
]->SetActive(bConnected
);
501 void CDownloadQueue::CheckAndAddSource(CPartFile
* sender
, CUpDownClient
* source
)
503 // if we block loopbacks at this point it should prevent us from connecting to ourself
504 if ( source
->HasValidHash() ) {
505 if ( source
->GetUserHash() == thePrefs::GetUserHash() ) {
506 AddDebugLogLineM( false, logDownloadQueue
, wxT("Tried to add source with matching hash to your own.") );
507 source
->Safe_Delete();
512 if (sender
->IsStopped()) {
513 source
->Safe_Delete();
517 // Filter sources which are known to be dead/useless
518 if ( theApp
->clientlist
->IsDeadSource( source
) || sender
->IsDeadSource(source
) ) {
519 source
->Safe_Delete();
523 // Filter sources which are incompatible with our encryption setting (one requires it, and the other one doesn't supports it)
524 if ( (source
->RequiresCryptLayer() && (!thePrefs::IsClientCryptLayerSupported() || !source
->HasValidHash())) || (thePrefs::IsClientCryptLayerRequired() && (!source
->SupportsCryptLayer() || !source
->HasValidHash()))) {
525 source
->Safe_Delete();
529 // Find all clients with the same hash
530 if ( source
->HasValidHash() ) {
531 CClientList::SourceList found
= theApp
->clientlist
->GetClientsByHash( source
->GetUserHash() );
533 CClientList::SourceList::iterator it
= found
.begin();
534 for ( ; it
!= found
.end(); it
++ ) {
535 CKnownFile
* file
= (*it
)->GetRequestFile();
537 // Only check files on the download-queue
539 // Is the found source queued for something else?
540 if ( file
!= sender
) {
541 // Try to add a request for the other file
542 if ( (*it
)->AddRequestForAnotherFile(sender
)) {
543 // Add it to downloadlistctrl
544 Notify_DownloadCtrlAddSource(sender
, *it
, A4AF_SOURCE
);
548 source
->Safe_Delete();
556 // Our new source is real new but maybe it is already uploading to us?
557 // If yes the known client will be attached to the var "source" and the old
558 // source-client will be deleted. However, if the request file of the known
559 // source is NULL, then we have to treat it almost like a new source and if
560 // it isn't NULL and not "sender", then we shouldn't move it, but rather add
561 // a request for the new file.
562 ESourceFrom nSourceFrom
= source
->GetSourceFrom();
563 if ( theApp
->clientlist
->AttachToAlreadyKnown(&source
, 0) ) {
564 // Already queued for another file?
565 if ( source
->GetRequestFile() ) {
566 // If we're already queued for the right file, then there's nothing to do
567 if ( sender
!= source
->GetRequestFile() ) {
568 // Add the new file to the request list
569 source
->AddRequestForAnotherFile( sender
);
572 // Source was known, but reqfile NULL.
573 source
->SetRequestFile( sender
);
574 source
->SetSourceFrom(nSourceFrom
);
575 sender
->AddSource( source
);
576 if ( source
->GetFileRating() || !source
->GetFileComment().IsEmpty() ) {
577 sender
->UpdateFileRatingCommentAvail();
580 Notify_DownloadCtrlAddSource(sender
, source
, UNAVAILABLE_SOURCE
);
583 // Unknown client, add it to the clients list
584 source
->SetRequestFile( sender
);
586 theApp
->clientlist
->AddClient(source
);
588 sender
->AddSource( source
);
589 if ( source
->GetFileRating() || !source
->GetFileComment().IsEmpty() ) {
590 sender
->UpdateFileRatingCommentAvail();
593 Notify_DownloadCtrlAddSource(sender
, source
, UNAVAILABLE_SOURCE
);
598 void CDownloadQueue::CheckAndAddKnownSource(CPartFile
* sender
,CUpDownClient
* source
)
602 if (sender
->IsStopped()) {
606 // Filter sources which are known to be dead/useless
607 if ( sender
->IsDeadSource(source
) ) {
611 // "Filter LAN IPs" -- this may be needed here in case we are connected to the internet and are also connected
612 // to a LAN and some client from within the LAN connected to us. Though this situation may be supported in future
613 // by adding that client to the source list and filtering that client's LAN IP when sending sources to
614 // a client within the internet.
616 // "IPfilter" is not needed here, because that "known" client was already IPfiltered when receiving OP_HELLO.
617 if (!source
->HasLowID()) {
618 uint32 nClientIP
= wxUINT32_SWAP_ALWAYS(source
->GetUserIDHybrid());
619 if (!IsGoodIP(nClientIP
, thePrefs::FilterLanIPs())) { // check for 0-IP, localhost and LAN addresses
620 AddDebugLogLineM(false, logIPFilter
, wxT("Ignored already known source with IP=%s") + Uint32toStringIP(nClientIP
));
625 // Filter sources which are incompatible with our encryption setting (one requires it, and the other one doesn't supports it)
626 if ( (source
->RequiresCryptLayer() && (!thePrefs::IsClientCryptLayerSupported() || !source
->HasValidHash())) || (thePrefs::IsClientCryptLayerRequired() && (!source
->SupportsCryptLayer() || !source
->HasValidHash())))
628 source
->Safe_Delete();
632 CPartFile
* file
= source
->GetRequestFile();
634 // Check if the file is already queued for something else
636 if ( file
!= sender
) {
637 if ( source
->AddRequestForAnotherFile( sender
) ) {
638 Notify_DownloadCtrlAddSource( sender
, source
, A4AF_SOURCE
);
642 source
->SetRequestFile( sender
);
644 if ( source
->GetFileRating() || !source
->GetFileComment().IsEmpty() ) {
645 sender
->UpdateFileRatingCommentAvail();
648 source
->SetSourceFrom(SF_PASSIVE
);
649 sender
->AddSource( source
);
650 Notify_DownloadCtrlAddSource( sender
, source
, UNAVAILABLE_SOURCE
);
655 bool CDownloadQueue::RemoveSource(CUpDownClient
* toremove
, bool WXUNUSED(updatewindow
), bool bDoStatsUpdate
)
657 bool removed
= false;
658 toremove
->DeleteAllFileRequests();
660 for ( uint16 i
= 0; i
< GetFileCount(); i
++ ) {
661 CPartFile
* cur_file
= GetFileByIndex( i
);
663 // Remove from source-list
664 if ( cur_file
->DelSource( toremove
) ) {
665 cur_file
->RemoveDownloadingSource(toremove
);
667 if ( bDoStatsUpdate
) {
668 cur_file
->UpdatePartsInfo();
672 // Remove from A4AF-list
673 cur_file
->RemoveA4AFSource( toremove
);
677 if ( !toremove
->GetFileComment().IsEmpty() || toremove
->GetFileRating()>0) {
678 toremove
->GetRequestFile()->UpdateFileRatingCommentAvail();
681 toremove
->SetRequestFile( NULL
);
682 toremove
->SetDownloadState(DS_NONE
);
684 // Remove from downloadlist widget
685 Notify_DownloadCtrlRemoveSource(toremove
, (CPartFile
*)NULL
);
686 toremove
->ResetFileStatusInfo();
692 void CDownloadQueue::RemoveFile(CPartFile
* file
)
694 RemoveLocalServerRequest( file
);
696 NotifyObservers( EventType( EventType::REMOVED
, file
) );
698 wxMutexLocker
lock( m_mutex
);
700 EraseValue( m_filelist
, file
);
704 CUpDownClient
* CDownloadQueue::GetDownloadClientByIP_UDP(uint32 dwIP
, uint16 nUDPPort
) const
706 wxMutexLocker
lock( m_mutex
);
708 for ( unsigned int i
= 0; i
< m_filelist
.size(); i
++ ) {
709 const CPartFile::SourceSet
& set
= m_filelist
[i
]->GetSourceList();
711 for ( CPartFile::SourceSet::const_iterator it
= set
.begin(); it
!= set
.end(); it
++ ) {
712 if ( (*it
)->GetIP() == dwIP
&& (*it
)->GetUDPPort() == nUDPPort
) {
722 * Checks if the specified server is the one we are connected to.
724 bool IsConnectedServer(const CServer
* server
)
726 if (server
&& theApp
->serverconnect
->GetCurrentServer()) {
727 wxString srvAddr
= theApp
->serverconnect
->GetCurrentServer()->GetAddress();
728 uint16 srvPort
= theApp
->serverconnect
->GetCurrentServer()->GetPort();
730 return server
->GetAddress() == srvAddr
&& server
->GetPort() == srvPort
;
737 bool CDownloadQueue::SendNextUDPPacket()
739 if ( m_filelist
.empty() || !theApp
->serverconnect
->IsUDPSocketAvailable() || !theApp
->IsConnectedED2K()) {
743 // Start monitoring the server and the files list
744 if ( !m_queueServers
.IsActive() ) {
745 AddObserver( &m_queueFiles
);
747 theApp
->serverlist
->AddObserver( &m_queueServers
);
751 bool packetSent
= false;
752 while ( !packetSent
) {
753 // Get max files ids per packet for current server
754 int filesAllowed
= GetMaxFilesPerUDPServerPacket();
756 if (filesAllowed
< 1 || !m_udpserver
|| IsConnectedServer(m_udpserver
)) {
757 // Select the next server to ask, must not be the connected server
759 m_udpserver
= m_queueServers
.GetNext();
760 } while (IsConnectedServer(m_udpserver
));
762 m_cRequestsSentToServer
= 0;
763 filesAllowed
= GetMaxFilesPerUDPServerPacket();
767 // Check if we have asked all servers, in which case we are done
768 if (m_udpserver
== NULL
) {
774 // Memoryfile containing the hash of every file to request
775 // 28bytes allocation because 16b + 4b + 8b is the worse case scenario.
776 CMemFile
hashlist( 28 );
778 CPartFile
* file
= m_queueFiles
.GetNext();
780 while ( file
&& filesAllowed
) {
781 uint8 status
= file
->GetStatus();
783 if ( ( status
== PS_READY
|| status
== PS_EMPTY
) && file
->GetSourceCount() < thePrefs::GetMaxSourcePerFileUDP() ) {
784 if (file
->IsLargeFile() && !m_udpserver
->SupportsLargeFilesUDP()) {
785 AddDebugLogLineM(false, logDownloadQueue
, wxT("UDP Request for sources on a large file ignored: server doesn't support it"));
787 ++m_cRequestsSentToServer
;
788 hashlist
.WriteHash( file
->GetFileHash() );
789 // See the notes on TCP packet
790 if ( m_udpserver
->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES2
) {
791 if (file
->IsLargeFile()) {
792 wxASSERT(m_udpserver
->SupportsLargeFilesUDP());
793 hashlist
.WriteUInt32( 0 );
794 hashlist
.WriteUInt64( file
->GetFileSize() );
796 hashlist
.WriteUInt32( file
->GetFileSize() );
803 // Avoid skipping a file if we can't send any more currently
804 if ( filesAllowed
) {
805 file
= m_queueFiles
.GetNext();
809 // See if we have anything to send
810 if ( hashlist
.GetLength() ) {
811 packetSent
= SendGlobGetSourcesUDPPacket(hashlist
);
814 // Check if we've covered every file
815 if ( file
== NULL
) {
816 // Reset the list of asked files so that the loop will start over
817 m_queueFiles
.Reset();
819 // Unset the server so that the next server will be used
828 void CDownloadQueue::StopUDPRequests()
830 wxMutexLocker
lock( m_mutex
);
836 void CDownloadQueue::DoStopUDPRequests()
838 // No need to observe when we wont be using the results
839 theApp
->serverlist
->RemoveObserver( &m_queueServers
);
840 RemoveObserver( &m_queueFiles
);
843 m_lastudpsearchtime
= ::GetTickCount();
847 // Comparison function needed by sort. Returns true if file1 preceeds file2
848 bool ComparePartFiles(const CPartFile
* file1
, const CPartFile
* file2
) {
849 if (file1
->GetDownPriority() != file2
->GetDownPriority()) {
850 // To place high-priority files before low priority files we have to
851 // invert this test, since PR_LOW is lower than PR_HIGH, and since
852 // placing a PR_LOW file before a PR_HIGH file would mean that
853 // the PR_LOW file gets sources before the PR_HIGH file ...
854 return (file1
->GetDownPriority() > file2
->GetDownPriority());
856 int sourcesA
= file1
->GetSourceCount();
857 int sourcesB
= file2
->GetSourceCount();
859 int notSourcesA
= file1
->GetNotCurrentSourcesCount();
860 int notSourcesB
= file2
->GetNotCurrentSourcesCount();
862 int cmp
= CmpAny( sourcesA
- notSourcesA
, sourcesB
- notSourcesB
);
865 cmp
= CmpAny( notSourcesA
, notSourcesB
);
873 void CDownloadQueue::DoSortByPriority()
875 m_lastsorttime
= ::GetTickCount();
876 sort( m_filelist
.begin(), m_filelist
.end(), ComparePartFiles
);
880 void CDownloadQueue::ResetLocalServerRequests()
882 wxMutexLocker
lock( m_mutex
);
884 m_dwNextTCPSrcReq
= 0;
885 m_localServerReqQueue
.clear();
887 for ( uint16 i
= 0; i
< m_filelist
.size(); i
++ ) {
888 m_filelist
[i
]->SetLocalSrcRequestQueued(false);
893 void CDownloadQueue::RemoveLocalServerRequest( CPartFile
* file
)
895 wxMutexLocker
lock( m_mutex
);
897 EraseValue( m_localServerReqQueue
, file
);
899 file
->SetLocalSrcRequestQueued(false);
903 void CDownloadQueue::ProcessLocalRequests()
905 wxMutexLocker
lock( m_mutex
);
907 bool bServerSupportsLargeFiles
= theApp
->serverconnect
908 && theApp
->serverconnect
->GetCurrentServer()
909 && theApp
->serverconnect
->GetCurrentServer()->SupportsLargeFilesTCP();
911 if ( (!m_localServerReqQueue
.empty()) && (m_dwNextTCPSrcReq
< ::GetTickCount()) ) {
912 CMemFile
dataTcpFrame(22);
913 const int iMaxFilesPerTcpFrame
= 15;
915 while (!m_localServerReqQueue
.empty() && iFiles
< iMaxFilesPerTcpFrame
) {
916 // find the file with the longest waitingtime
917 uint32 dwBestWaitTime
= 0xFFFFFFFF;
919 std::list
<CPartFile
*>::iterator posNextRequest
= m_localServerReqQueue
.end();
920 std::list
<CPartFile
*>::iterator it
= m_localServerReqQueue
.begin();
921 while( it
!= m_localServerReqQueue
.end() ) {
922 CPartFile
* cur_file
= (*it
);
923 if (cur_file
->GetStatus() == PS_READY
|| cur_file
->GetStatus() == PS_EMPTY
) {
924 uint8 nPriority
= cur_file
->GetDownPriority();
925 if (nPriority
> PR_HIGH
) {
930 if (cur_file
->GetLastSearchTime() + (PR_HIGH
-nPriority
) < dwBestWaitTime
){
931 dwBestWaitTime
= cur_file
->GetLastSearchTime() + (PR_HIGH
- nPriority
);
937 it
= m_localServerReqQueue
.erase(it
);
938 cur_file
->SetLocalSrcRequestQueued(false);
939 AddDebugLogLineM( false, logDownloadQueue
,
940 CFormat(wxT("Local server source request for file '%s' not sent because of status '%s'"))
941 % cur_file
->GetFileName() % cur_file
->getPartfileStatus());
945 if (posNextRequest
!= m_localServerReqQueue
.end()) {
946 CPartFile
* cur_file
= (*posNextRequest
);
947 cur_file
->SetLocalSrcRequestQueued(false);
948 cur_file
->SetLastSearchTime(::GetTickCount());
949 m_localServerReqQueue
.erase(posNextRequest
);
952 if (!bServerSupportsLargeFiles
&& cur_file
->IsLargeFile()) {
953 AddDebugLogLineM(false, logDownloadQueue
, wxT("TCP Request for sources on a large file ignored: server doesn't support it"));
955 AddDebugLogLineM(false, logDownloadQueue
,
956 CFormat(wxT("Creating local sources request packet for '%s'")) % cur_file
->GetFileName());
957 // create request packet
958 CMemFile
data(16 + (cur_file
->IsLargeFile() ? 8 : 4));
959 data
.WriteHash(cur_file
->GetFileHash());
960 // Kry - lugdunum extended protocol on 17.3 to handle filesize properly.
961 // There is no need to check anything, old server ignore the extra 4 bytes.
962 // As of 17.9, servers accept a 0 32-bits size and then a 64bits size
963 if (cur_file
->IsLargeFile()) {
964 wxASSERT(bServerSupportsLargeFiles
);
966 data
.WriteUInt64(cur_file
->GetFileSize());
968 data
.WriteUInt32(cur_file
->GetFileSize());
971 if (thePrefs::IsClientCryptLayerSupported() && theApp
->serverconnect
->GetCurrentServer() != NULL
&& theApp
->serverconnect
->GetCurrentServer()->SupportsGetSourcesObfuscation()) {
972 byOpcode
= OP_GETSOURCES_OBFU
;
974 byOpcode
= OP_GETSOURCES
;
976 CPacket
packet(data
, OP_EDONKEYPROT
, byOpcode
);
977 dataTcpFrame
.Write(packet
.GetPacket(), packet
.GetRealPacketSize());
982 int iSize
= dataTcpFrame
.GetLength();
984 // create one 'packet' which contains all buffered OP_GETSOURCES ED2K packets to be sent with one TCP frame
985 // server credits: (16+4)*regularfiles + (16+4+8)*largefiles +1
986 CScopedPtr
<CPacket
> packet(new CPacket(new byte
[iSize
], dataTcpFrame
.GetLength(), true, false));
987 dataTcpFrame
.Seek(0, wxFromStart
);
988 dataTcpFrame
.Read(packet
->GetPacket(), iSize
);
989 uint32 size
= packet
->GetPacketSize();
990 theApp
->serverconnect
->SendPacket(packet
.release(), true); // Deletes `packet'.
991 AddDebugLogLineM(false, logDownloadQueue
, wxT("Sent local sources request packet."));
992 theStats::AddUpOverheadServer(size
);
995 // next TCP frame with up to 15 source requests is allowed to be sent in..
996 m_dwNextTCPSrcReq
= ::GetTickCount() + SEC2MS(iMaxFilesPerTcpFrame
*(16+4));
1002 void CDownloadQueue::SendLocalSrcRequest(CPartFile
* sender
)
1004 wxMutexLocker
lock( m_mutex
);
1006 m_localServerReqQueue
.push_back(sender
);
1010 void CDownloadQueue::ResetCatParts(uint8 cat
)
1012 for ( uint16 i
= 0; i
< GetFileCount(); i
++ ) {
1013 CPartFile
* file
= GetFileByIndex( i
);
1015 if ( file
->GetCategory() == cat
) {
1016 // Reset the category
1017 file
->SetCategory( 0 );
1018 } else if ( file
->GetCategory() > cat
) {
1019 // Set to the new position of the original category
1020 file
->SetCategory( file
->GetCategory() - 1 );
1026 void CDownloadQueue::SetCatPrio(uint8 cat
, uint8 newprio
)
1028 for ( uint16 i
= 0; i
< GetFileCount(); i
++ ) {
1029 CPartFile
* file
= GetFileByIndex( i
);
1031 if ( !cat
|| file
->GetCategory() == cat
) {
1032 if ( newprio
== PR_AUTO
) {
1033 file
->SetAutoDownPriority(true);
1035 file
->SetAutoDownPriority(false);
1036 file
->SetDownPriority(newprio
);
1043 void CDownloadQueue::SetCatStatus(uint8 cat
, int newstatus
)
1045 std::list
<CPartFile
*> files
;
1048 wxMutexLocker
lock(m_mutex
);
1050 for ( uint16 i
= 0; i
< m_filelist
.size(); i
++ ) {
1051 if ( m_filelist
[i
]->CheckShowItemInGivenCat(cat
) ) {
1052 files
.push_back( m_filelist
[i
] );
1057 std::list
<CPartFile
*>::iterator it
= files
.begin();
1059 for ( ; it
!= files
.end(); it
++ ) {
1060 switch ( newstatus
) {
1061 case MP_CANCEL
: (*it
)->Delete(); break;
1062 case MP_PAUSE
: (*it
)->PauseFile(); break;
1063 case MP_STOP
: (*it
)->StopFile(); break;
1064 case MP_RESUME
: (*it
)->ResumeFile(); break;
1070 uint16
CDownloadQueue::GetDownloadingFileCount() const
1072 wxMutexLocker
lock( m_mutex
);
1075 for ( uint16 i
= 0; i
< m_filelist
.size(); i
++ ) {
1076 uint8 status
= m_filelist
[i
]->GetStatus();
1077 if ( status
== PS_READY
|| status
== PS_EMPTY
) {
1086 uint16
CDownloadQueue::GetPausedFileCount() const
1088 wxMutexLocker
lock( m_mutex
);
1091 for ( uint16 i
= 0; i
< m_filelist
.size(); i
++ ) {
1092 if ( m_filelist
[i
]->GetStatus() == PS_PAUSED
) {
1101 void CDownloadQueue::CheckDiskspace( const CPath
& path
)
1103 if ( ::GetTickCount() - m_lastDiskCheck
< DISKSPACERECHECKTIME
) {
1107 m_lastDiskCheck
= ::GetTickCount();
1110 // Check if the user has set an explicit limit
1111 if ( thePrefs::IsCheckDiskspaceEnabled() ) {
1112 min
= thePrefs::GetMinFreeDiskSpace();
1115 // The very least acceptable diskspace is a single PART
1116 if ( min
< PARTSIZE
) {
1120 uint64 free
= CPath::GetFreeSpaceAt(path
);
1121 if (free
== static_cast<uint64
>(wxInvalidOffset
)) {
1123 } else if (free
< min
) {
1124 CUserEvents::ProcessEvent(
1125 CUserEvents::OutOfDiskSpace
,
1126 wxT("Temporary partition"));
1129 for (unsigned int i
= 0; i
< m_filelist
.size(); ++i
) {
1130 CPartFile
* file
= m_filelist
[i
];
1132 switch ( file
->GetStatus() ) {
1139 if ( free
>= min
&& file
->GetInsufficient() ) {
1140 // We'll try to resume files if there is enough free space
1141 if ( free
- file
->GetNeededSpace() > min
) {
1144 } else if ( free
< min
&& !file
->IsPaused() ) {
1145 // No space left, stop the files.
1146 file
->PauseFile( true );
1152 int CDownloadQueue::GetMaxFilesPerUDPServerPacket() const
1154 if ( m_udpserver
) {
1155 if ( m_udpserver
->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES
) {
1156 // get max. file ids per packet
1157 if ( m_cRequestsSentToServer
< MAX_REQUESTS_PER_SERVER
) {
1159 MAX_FILES_PER_UDP_PACKET
,
1160 MAX_REQUESTS_PER_SERVER
- m_cRequestsSentToServer
1163 } else if ( m_cRequestsSentToServer
< MAX_REQUESTS_PER_SERVER
) {
1172 bool CDownloadQueue::SendGlobGetSourcesUDPPacket(CMemFile
& data
)
1178 CPacket
packet(data
, OP_EDONKEYPROT
, ((m_udpserver
->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES2
) ? OP_GLOBGETSOURCES2
: OP_GLOBGETSOURCES
));
1180 theStats::AddUpOverheadServer(packet
.GetPacketSize());
1181 theApp
->serverconnect
->SendUDPPacket(&packet
,m_udpserver
,false);
1187 void CDownloadQueue::AddToResolve(const CMD4Hash
& fileid
, const wxString
& pszHostname
, uint16 port
, const wxString
& hash
, uint8 cryptoptions
)
1190 if ( !GetFileByID(fileid
) ) {
1194 wxMutexLocker
lock( m_mutex
);
1196 Hostname_Entry entry
= { fileid
, pszHostname
, port
, hash
, cryptoptions
};
1197 m_toresolve
.push_front(entry
);
1199 // Check if there are other DNS lookups on queue
1200 if (m_toresolve
.size() == 1) {
1201 // Check if it is a simple dot address
1202 uint32 ip
= StringIPtoUint32(pszHostname
);
1205 OnHostnameResolved(ip
);
1207 CAsyncDNS
* dns
= new CAsyncDNS(pszHostname
, DNS_SOURCE
, theApp
);
1209 if ((dns
->Create() != wxTHREAD_NO_ERROR
) || (dns
->Run() != wxTHREAD_NO_ERROR
)) {
1211 m_toresolve
.pop_front();
1218 void CDownloadQueue::OnHostnameResolved(uint32 ip
)
1220 wxMutexLocker
lock( m_mutex
);
1222 wxASSERT( m_toresolve
.size() );
1224 Hostname_Entry resolved
= m_toresolve
.front();
1225 m_toresolve
.pop_front();
1228 CPartFile
* file
= GetFileByID( resolved
.fileid
);
1230 CMemFile
sources(1+4+2);
1231 sources
.WriteUInt8(1); // No. Sources
1232 sources
.WriteUInt32(ip
);
1233 sources
.WriteUInt16(resolved
.port
);
1234 sources
.WriteUInt8(resolved
.cryptoptions
);
1235 if (resolved
.cryptoptions
& 0x80) {
1236 wxASSERT(!resolved
.hash
.IsEmpty());
1237 CMD4Hash sourcehash
;
1238 sourcehash
.Decode(resolved
.hash
);
1239 sources
.WriteHash(sourcehash
);
1241 sources
.Seek(0,wxFromStart
);
1243 file
->AddSources(sources
, 0, 0, SF_LINK
, true);
1247 while (m_toresolve
.size()) {
1248 Hostname_Entry entry
= m_toresolve
.front();
1250 // Check if it is a simple dot address
1251 uint32 tmpIP
= StringIPtoUint32(entry
.strHostname
);
1254 OnHostnameResolved(tmpIP
);
1256 CAsyncDNS
* dns
= new CAsyncDNS(entry
.strHostname
, DNS_SOURCE
, theApp
);
1258 if ((dns
->Create() != wxTHREAD_NO_ERROR
) || (dns
->Run() != wxTHREAD_NO_ERROR
)) {
1260 m_toresolve
.pop_front();
1269 bool CDownloadQueue::AddLink( const wxString
& link
, uint8 category
)
1273 if (link
.compare(0, 7, wxT("magnet:")) == 0) {
1274 uri
= CMagnetED2KConverter(link
);
1276 AddLogLineM(true, CFormat(_("Cannot convert magnet link to eD2k: %s")) % link
);
1281 if (uri
.compare(0, 7, wxT("ed2k://")) == 0) {
1282 return AddED2KLink(uri
, category
);
1284 AddLogLineM(true, CFormat(_("Unknown protocol of link: %s")) % link
);
1290 bool CDownloadQueue::AddED2KLink( const wxString
& link
, uint8 category
)
1292 wxASSERT( !link
.IsEmpty() );
1293 wxString URI
= link
;
1295 // Need the links to end with /, otherwise CreateLinkFromUrl crashes us.
1296 if ( URI
.Last() != wxT('/') ) {
1301 CScopedPtr
<CED2KLink
> uri(CED2KLink::CreateLinkFromUrl(URI
));
1303 return AddED2KLink( uri
.get(), category
);
1304 } catch ( const wxString
& err
) {
1305 AddLogLineM( true, CFormat( _("Invalid eD2k link! ERROR: %s")) % err
);
1312 bool CDownloadQueue::AddED2KLink( const CED2KLink
* link
, uint8 category
)
1314 switch ( link
->GetKind() ) {
1315 case CED2KLink::kFile
:
1316 return AddED2KLink( dynamic_cast<const CED2KFileLink
*>( link
), category
);
1318 case CED2KLink::kServer
:
1319 return AddED2KLink( dynamic_cast<const CED2KServerLink
*>( link
) );
1321 case CED2KLink::kServerList
:
1322 return AddED2KLink( dynamic_cast<const CED2KServerListLink
*>( link
) );
1331 bool CDownloadQueue::AddED2KLink( const CED2KFileLink
* link
, uint8 category
)
1333 CPartFile
* file
= NULL
;
1334 if (IsFileExisting(link
->GetHashKey())) {
1335 // Must be a shared file if we are to add hashes or sources
1336 if ((file
= GetFileByID(link
->GetHashKey())) == NULL
) {
1340 if (link
->GetSize() > OLD_MAX_FILE_SIZE
) {
1341 if (!PlatformSpecific::CanFSHandleLargeFiles(thePrefs::GetTempDir())) {
1342 AddLogLineM(true, _("Filesystem for Temp directory cannot handle large files."));
1344 } else if (!PlatformSpecific::CanFSHandleLargeFiles(theApp
->glob_prefs
->GetCatPath(category
))) {
1345 AddLogLineM(true, _("Filesystem for Incoming directory cannot handle large files."));
1350 file
= new CPartFile(link
);
1352 if (file
->GetStatus() == PS_ERROR
) {
1357 AddDownload(file
, thePrefs::AddNewFilesPaused(), category
);
1360 if (link
->HasValidAICHHash()) {
1361 CAICHHashSet
* hashset
= file
->GetAICHHashset();
1363 if (!hashset
->HasValidMasterHash() || (hashset
->GetMasterHash() != link
->GetAICHHash())) {
1364 hashset
->SetMasterHash(link
->GetAICHHash(), AICH_VERIFIED
);
1365 hashset
->FreeHashSet();
1369 const CED2KFileLink::CED2KLinkSourceList
& list
= link
->m_sources
;
1370 CED2KFileLink::CED2KLinkSourceList::const_iterator it
= list
.begin();
1371 for (; it
!= list
.end(); ++it
) {
1372 AddToResolve(link
->GetHashKey(), it
->addr
, it
->port
, it
->hash
, it
->cryptoptions
);
1379 bool CDownloadQueue::AddED2KLink( const CED2KServerLink
* link
)
1381 CServer
*server
= new CServer( link
->GetPort(), Uint32toStringIP( link
->GetIP() ) );
1383 server
->SetListName( Uint32toStringIP( link
->GetIP() ) );
1385 theApp
->serverlist
->AddServer(server
);
1387 Notify_ServerAdd(server
);
1393 bool CDownloadQueue::AddED2KLink( const CED2KServerListLink
* link
)
1395 theApp
->serverlist
->UpdateServerMetFromURL( link
->GetAddress() );
1401 void CDownloadQueue::ObserverAdded( ObserverType
* o
)
1403 CObservableQueue
<CPartFile
*>::ObserverAdded( o
);
1405 EventType::ValueList list
;
1408 wxMutexLocker
lock(m_mutex
);
1409 list
.reserve( m_filelist
.size() );
1410 list
.insert( list
.begin(), m_filelist
.begin(), m_filelist
.end() );
1413 NotifyObservers( EventType( EventType::INITIAL
, &list
), o
);
1416 void CDownloadQueue::KademliaSearchFile(uint32_t searchID
, const Kademlia::CUInt128
* pcontactID
, const Kademlia::CUInt128
* pbuddyID
, uint8_t type
, uint32_t ip
, uint16_t tcp
, uint16_t udp
, uint32_t buddyip
, uint16_t buddyport
, uint8_t byCryptOptions
)
1418 AddDebugLogLineM(false, logKadSearch
, wxString::Format(wxT("Search result sources (type %i)"),type
));
1420 //Safety measure to make sure we are looking for these sources
1421 CPartFile
* temp
= GetFileByKadFileSearchID(searchID
);
1423 AddDebugLogLineM(false, logKadSearch
, wxT("This is not the file we're looking for..."));
1427 //Do we need more sources?
1428 if(!(!temp
->IsStopped() && thePrefs::GetMaxSourcePerFile() > temp
->GetSourceCount())) {
1429 AddDebugLogLineM(false, logKadSearch
, wxT("No more sources needed for this file"));
1433 uint32_t ED2KID
= wxUINT32_SWAP_ALWAYS(ip
);
1435 if (theApp
->ipfilter
->IsFiltered(ED2KID
)) {
1436 AddDebugLogLineM(false, logKadSearch
, wxT("Source ip got filtered"));
1437 AddDebugLogLineM(false, logIPFilter
, CFormat(wxT("IPfiltered source IP=%s received from Kademlia")) % Uint32toStringIP(ED2KID
));
1441 if( (ip
== Kademlia::CKademlia::GetIPAddress() || ED2KID
== theApp
->GetED2KID()) && tcp
== thePrefs::GetPort()) {
1442 AddDebugLogLineM(false, logKadSearch
, wxT("Trying to add myself as source, ignore"));
1446 CUpDownClient
* ctemp
= NULL
;
1450 // NonFirewalled users
1452 AddDebugLogLineM(false, logKadSearch
, CFormat(wxT("Ignored source (IP=%s) received from Kademlia, no tcp port received")) % Uint32toStringIP(ip
));
1455 if (!IsGoodIP(ED2KID
,thePrefs::FilterLanIPs())) {
1456 AddDebugLogLineM(false, logKadSearch
, CFormat(wxT("%s got filtered")) % Uint32toStringIP(ED2KID
));
1457 AddDebugLogLineM(false, logIPFilter
, CFormat(wxT("Ignored source (IP=%s) received from Kademlia, filtered")) % Uint32toStringIP(ED2KID
));
1460 ctemp
= new CUpDownClient(tcp
, ip
, 0, 0, temp
, false, true);
1461 ctemp
->SetSourceFrom(SF_KADEMLIA
);
1462 // not actually sent or needed for HighID sources
1463 //ctemp->SetServerIP(serverip);
1464 //ctemp->SetServerPort(serverport);
1465 ctemp
->SetKadPort(udp
);
1467 pcontactID
->ToByteArray(cID
);
1468 ctemp
->SetUserHash(CMD4Hash(cID
));
1472 // Don't use this type... Some clients will process it wrong..
1477 // This will be a firewalled client connected to Kad only.
1478 // We set the clientID to 1 as a Kad user only has 1 buddy.
1479 ctemp
= new CUpDownClient(tcp
, 1, 0, 0, temp
, false, true);
1480 // The only reason we set the real IP is for when we get a callback
1481 // from this firewalled source, the compare method will match them.
1482 ctemp
->SetSourceFrom(SF_KADEMLIA
);
1483 ctemp
->SetKadPort(udp
);
1485 pcontactID
->ToByteArray(cID
);
1486 ctemp
->SetUserHash(CMD4Hash(cID
));
1487 pbuddyID
->ToByteArray(cID
);
1488 ctemp
->SetBuddyID(cID
);
1489 ctemp
->SetBuddyIP(buddyip
);
1490 ctemp
->SetBuddyPort(buddyport
);
1494 // firewalled source which supports direct UDP callback
1495 // if we are firewalled ourself, the source is useless to us
1496 if (theApp
->IsFirewalled()) {
1500 if ((byCryptOptions
& 0x08) == 0){
1501 AddDebugLogLineM(false, logKadSearch
, CFormat(wxT("Received Kad source type 6 (direct callback) which has the direct callback flag not set (%s)")) % Uint32toStringIP(ED2KID
));
1505 ctemp
= new CUpDownClient(tcp
, 1, 0, 0, temp
, false, true);
1506 ctemp
->SetSourceFrom(SF_KADEMLIA
);
1507 ctemp
->SetKadPort(udp
);
1508 ctemp
->SetIP(ED2KID
); // need to set the IP address, which cannot be used for TCP but for UDP
1510 pcontactID
->ToByteArray(cID
);
1511 ctemp
->SetUserHash(CMD4Hash(cID
));
1516 // add encryption settings
1517 ctemp
->SetConnectOptions(byCryptOptions
);
1519 AddDebugLogLineM(false, logKadSearch
, CFormat(wxT("Happily adding a source (%s) type %d")) % Uint32_16toStringIP_Port(ED2KID
, ctemp
->GetUserPort()) % type
);
1520 CheckAndAddSource(temp
, ctemp
);
1524 CPartFile
* CDownloadQueue::GetFileByKadFileSearchID(uint32 id
) const
1526 wxMutexLocker
lock( m_mutex
);
1528 for ( uint16 i
= 0; i
< m_filelist
.size(); ++i
) {
1529 if ( id
== m_filelist
[i
]->GetKadFileSearchID()) {
1530 return m_filelist
[ i
];
1537 bool CDownloadQueue::DoKademliaFileRequest()
1539 return ((::GetTickCount() - lastkademliafilerequest
) > KADEMLIAASKTIME
);
1541 // File_checked_for_headers