-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathDownloadQueue.cpp
More file actions
1700 lines (1374 loc) · 48.8 KB
/
Copy pathDownloadQueue.cpp
File metadata and controls
1700 lines (1374 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2026 aMule Team ( https://amule-org.github.io )
// Copyright (c) 2002-2011 Merkur ( [email protected] / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "DownloadQueue.h" // Interface declarations
#include <protocol/Protocols.h>
#include <protocol/kad/Constants.h>
#include <common/Macros.h>
#include <common/MenuIDs.h>
#include <common/Constants.h>
#include <wx/utils.h>
#include "Server.h" // Needed for CServer
#include "Packet.h" // Needed for CPacket
#include "MemFile.h" // Needed for CMemFile
#include "ClientList.h" // Needed for CClientList
#include "updownclient.h" // Needed for CUpDownClient
#include "ServerList.h" // Needed for CServerList
#include "ServerConnect.h" // Needed for CServerConnect
#include "ED2KLink.h" // Needed for CED2KFileLink
#include "SearchList.h" // Needed for CSearchFile
#include "SharedFileList.h" // Needed for CSharedFileList
#include "PartFile.h" // Needed for CPartFile
#include "Preferences.h" // Needed for thePrefs
#include "amule.h" // Needed for theApp
#include "AsyncDNS.h" // Needed for CAsyncDNS
#include "DownloadBandwidthThrottler.h"
#include "Statistics.h" // Needed for theStats
#include "Logger.h"
#include <common/Format.h> // Needed for CFormat
#include "IPFilter.h"
#include <common/FileFunctions.h> // Needed for CDirIterator
#include "GuiEvents.h" // Needed for Notify_*
#include "UserEvents.h"
#include "MagnetURI.h" // Needed for CMagnetED2KConverter
#include "ScopedPtr.h" // Needed for CScopedPtr
#include "PlatformSpecific.h" // Needed for CanFSHandleLargeFiles
#include "kademlia/kademlia/Kademlia.h"
// Max. file IDs per UDP packet
// ----------------------------
// 576 - 30 bytes of header (28 for UDP, 2 for "E3 9A" edonkey proto) = 546 bytes
// 546 / 16 = 34
#define MAX_FILES_PER_UDP_PACKET 31 // 2+16*31 = 498 ... is still less than 512 bytes!!
#define MAX_REQUESTS_PER_SERVER 35
CDownloadQueue::CDownloadQueue()
// Needs to be recursive that that is can own an observer assigned to itself
: m_mutex( wxMUTEX_RECURSIVE )
{
m_datarate = 0;
m_udpserver = 0;
m_lastsorttime = 0;
m_lastudpsearchtime = 0;
m_lastudpstattime = 0;
m_udcounter = 0;
m_nLastED2KLinkCheck = 0;
m_dwNextTCPSrcReq = 0;
m_cRequestsSentToServer = 0;
m_lastDiskCheck = 0;
// Static thresholds until dynamic kicks in.
m_rareFileThreshold = RARE_FILE;
m_commonFileThreshold = 100;
SetLastKademliaFileRequest();
}
CDownloadQueue::~CDownloadQueue()
{
if ( !m_filelist.empty() ) {
for ( FileQueue::size_type i = 0; i < m_filelist.size(); i++ ) {
AddLogLineNS(CFormat(_("Saving PartFile %u of %u")) % (i + 1) % m_filelist.size());
delete m_filelist[i];
}
AddLogLineNS(_("All PartFiles Saved."));
}
}
void CDownloadQueue::LoadMetFiles(const CPath& path)
{
AddLogLineNS(CFormat(_("Loading temp files from %s.")) % path.GetPrintable());
std::vector<CPath> files;
// Locate part-files to be loaded
CDirIterator TempDir(path);
CPath fileName = TempDir.GetFirstFile(CDirIterator::File, "*.part.met");
while (fileName.IsOk()) {
files.push_back(path.JoinPaths(fileName));
fileName = TempDir.GetNextFile();
}
// Loading in order makes it easier to figure which
// file is broken in case of crashes, or the like.
std::sort(files.begin(), files.end());
// Load part-files
for ( size_t i = 0; i < files.size(); i++ ) {
AddLogLineNS(CFormat(_("Loading PartFile %u of %u")) % (i + 1) % files.size());
fileName = files[i].GetFullName();
CPartFile *toadd = new CPartFile();
bool result = toadd->LoadPartFile(path, fileName) != 0;
if (!result) {
// Try from backup
result = toadd->LoadPartFile(path, fileName, true) != 0;
}
if (result && !IsFileExisting(toadd->GetFileHash())) {
{
wxMutexLocker lock(m_mutex);
m_filelist.push_back(toadd);
}
NotifyObservers(EventType(EventType::INSERTED, toadd));
Notify_DownloadCtrlAddFile(toadd);
} else {
wxString msg;
if (result) {
msg << CFormat("WARNING: Duplicate partfile with hash '%s' found, skipping: %s")
% toadd->GetFileHash().Encode() % fileName;
} else {
// If result is false, then reading of both the primary and the backup .met failed
AddLogLineN(_("ERROR: Failed to load backup file. Search https://github.com/amule-org/amule/discussions for .part.met recovery solutions."));
msg << CFormat("ERROR: Failed to load PartFile '%s'") % fileName;
}
AddLogLineCS(msg);
// Delete the partfile object in the end.
delete toadd;
}
}
AddLogLineNS(_("All PartFiles Loaded."));
if ( GetFileCount() == 0 ) {
AddLogLineN(_("No part files found"));
} else {
AddLogLineN(CFormat(wxPLURAL("Found %u part file", "Found %u part files", GetFileCount())) % GetFileCount());
DoSortByPriority();
CheckDiskspace( path );
Notify_ShowUpdateCatTabTitles();
}
}
uint16 CDownloadQueue::GetFileCount() const
{
wxMutexLocker lock( m_mutex );
return m_filelist.size();
}
void CDownloadQueue::CopyFileList(std::vector<CPartFile*>& out_list, bool includeCompleted) const
{
wxMutexLocker lock(m_mutex);
uint32 reserve = m_filelist.size();
if (includeCompleted) {
reserve += m_completedDownloads.size();
}
out_list.reserve(reserve);
for (FileQueue::const_iterator it = m_filelist.begin(); it != m_filelist.end(); ++it) {
out_list.push_back(*it);
}
if (includeCompleted) {
for (FileList::const_iterator it = m_completedDownloads.begin(); it != m_completedDownloads.end(); ++it) {
out_list.push_back(*it);
}
}
}
CServer* CDownloadQueue::GetUDPServer() const
{
wxMutexLocker lock( m_mutex );
return m_udpserver;
}
void CDownloadQueue::SetUDPServer( CServer* server )
{
wxMutexLocker lock( m_mutex );
m_udpserver = server;
}
void CDownloadQueue::SaveSourceSeeds()
{
for ( uint16 i = 0; i < GetFileCount(); i++ ) {
GetFileByIndex( i )->SaveSourceSeeds();
}
}
void CDownloadQueue::LoadSourceSeeds()
{
for ( uint16 i = 0; i < GetFileCount(); i++ ) {
GetFileByIndex( i )->LoadSourceSeeds();
}
}
void CDownloadQueue::AddSearchToDownload(CSearchFile* toadd, uint8 category)
{
if ( IsFileExisting(toadd->GetFileHash()) ) {
return;
}
if (toadd->GetFileSize() > OLD_MAX_FILE_SIZE) {
if (!PlatformSpecific::CanFSHandleLargeFiles(thePrefs::GetTempDir())) {
AddLogLineC(_("Filesystem for Temp directory cannot handle large files."));
return;
} else if (!PlatformSpecific::CanFSHandleLargeFiles(theApp->glob_prefs->GetCatPath(category))) {
AddLogLineC(_("Filesystem for Incoming directory cannot handle large files."));
return;
}
}
CPartFile* newfile = NULL;
try {
newfile = new CPartFile(toadd);
} catch (const CInvalidPacket& WXUNUSED(e)) {
AddDebugLogLineC(logDownloadQueue, "Search-result contained invalid tags, could not add");
}
if ( newfile && newfile->GetStatus() != PS_ERROR ) {
AddDownload( newfile, thePrefs::AddNewFilesPaused(), category );
// Add any possible sources
if (toadd->GetClientID() && toadd->GetClientPort()) {
CMemFile sources(1+4+2);
sources.WriteUInt8(1);
sources.WriteUInt32(toadd->GetClientID());
sources.WriteUInt16(toadd->GetClientPort());
sources.Reset();
newfile->AddSources(sources, toadd->GetClientServerIP(), toadd->GetClientServerPort(), SF_SEARCH_RESULT, false);
}
for (std::list<CSearchFile::ClientStruct>::const_iterator it = toadd->GetClients().begin(); it != toadd->GetClients().end(); ++it) {
CMemFile sources(1+4+2);
sources.WriteUInt8(1);
sources.WriteUInt32(it->m_ip);
sources.WriteUInt16(it->m_port);
sources.Reset();
newfile->AddSources(sources, it->m_serverIP, it->m_serverPort, SF_SEARCH_RESULT, false);
}
} else {
delete newfile;
}
}
struct SFindBestPF
{
void operator()(CPartFile* file) {
// Check if we should filter out other categories
int alphaorder = 0;
if ((m_category != -1) && (file->GetCategory() != m_category)) {
return;
} else if (file->GetStatus() != PS_PAUSED) {
return;
} else if (m_alpha && m_result && ((alphaorder = file->GetFileName().GetPrintable().CmpNoCase(m_result->GetFileName().GetPrintable())) > 0)) {
return;
}
if (!m_result) {
m_result = file;
} else {
if (m_alpha && (alphaorder < 0)) {
m_result = file;
} else if (file->GetDownPriority() > m_result->GetDownPriority()) {
// Either not alpha ordered, or they have the same alpha ordering (could happen if they have same name)
m_result = file;
} else {
// Lower priority file
}
}
}
//! The category to look for, or -1 if any category is good
int m_category;
//! If any acceptable files are found, this variable store their pointer
CPartFile* m_result;
//! If we should order alphabetically
bool m_alpha;
};
void CDownloadQueue::StartNextFile(CPartFile* oldfile)
{
if ( thePrefs::StartNextFile() ) {
SFindBestPF visitor = { -1, NULL, thePrefs::StartNextFileAlpha() };
{
wxMutexLocker lock(m_mutex);
if (thePrefs::StartNextFileSame()) {
// Get a download in the same category
visitor.m_category = oldfile->GetCategory();
visitor = std::for_each(m_filelist.begin(), m_filelist.end(), visitor);
}
if (visitor.m_result == NULL) {
// Get a download, regardless of category
visitor.m_category = -1;
visitor = std::for_each(m_filelist.begin(), m_filelist.end(), visitor);
}
// Alpha doesn't need special cases
}
if (visitor.m_result) {
visitor.m_result->ResumeFile();
}
}
}
void CDownloadQueue::AddDownload(CPartFile* file, bool paused, uint8 category)
{
wxCHECK_RET(!IsFileExisting(file->GetFileHash()), "Adding duplicate part-file");
if (file->GetStatus(true) == PS_ALLOCATING) {
file->PauseFile();
} else if (paused && GetFileCount()) {
file->StopFile();
}
{
wxMutexLocker lock(m_mutex);
m_filelist.push_back( file );
DoSortByPriority();
}
NotifyObservers( EventType( EventType::INSERTED, file ) );
if (category < theApp->glob_prefs->GetCatCount()) {
file->SetCategory(category);
} else {
AddDebugLogLineN( logDownloadQueue, "Tried to add download into invalid category." );
}
Notify_DownloadCtrlAddFile( file );
theApp->searchlist->UpdateSearchFileByHash(file->GetFileHash()); // Update file in the search dialog if it's still open
AddLogLineC(CFormat(_("Downloading %s")) % file->GetFileName() );
}
bool CDownloadQueue::IsFileExisting( const CMD4Hash& fileid ) const
{
if (CKnownFile* file = theApp->sharedfiles->GetFileByID(fileid)) {
if (file->IsPartFile()) {
AddLogLineC(CFormat( _("You are already trying to download the file '%s'") ) % file->GetFileName());
} else {
// Check if the file exists, since otherwise the user is forced to
// manually reload the shares to download a file again.
CPath fullpath = file->GetFilePath().JoinPaths(file->GetFileName());
if (!fullpath.FileExists()) {
// The file is no longer available, unshare it
theApp->sharedfiles->RemoveFile(file);
return false;
}
AddLogLineC(CFormat( _("You already have the file '%s'") ) % file->GetFileName());
}
return true;
} else if ((file = GetFileByID(fileid))) {
AddLogLineC(CFormat( _("You are already trying to download the file %s") ) % file->GetFileName());
return true;
}
return false;
}
#define RARITY_FACTOR 4 // < 25%
#define NORMALITY_FACTOR 2 // <50%
// x > NORMALITY_FACTOR -> High availability.
void CDownloadQueue::Process()
{
// send src requests to local server
ProcessLocalRequests();
const uint64 curTick = ::GetTickCount64();
{
wxMutexLocker lock(m_mutex);
// Refill the global download bucket for this tick. The previous
// per-peer ratio controller (50-200% adaptive against the
// observed aggregate datarate) never actually enforced
// MaxDownload as a literal byte/sec cap. The throttler is a
// single shared atomic budget that every CEMSocket consults
// before each Read(); fast peers can claim unused capacity
// from slow ones within the same tick (demand-aware
// redistribution), and the global cap is the only constraint.
// MaxDownload=0 (UNLIMITED) sets the throttler to bypass mode.
CDownloadBandwidthThrottler::Get().RefillBudget(
thePrefs::GetMaxDownload(), CORE_TIMER_PERIOD);
m_datarate = 0;
m_udcounter++;
uint32 cur_datarate = 0;
uint32 cur_udcounter = m_udcounter;
std::list<int> m_sourcecountlist;
bool mustPreventSleep = false;
for ( FileQueue::size_type i = 0; i < m_filelist.size(); i++ ) {
CPartFile* file = m_filelist[i];
CMutexUnlocker unlocker(m_mutex);
uint8 status = file->GetStatus();
mustPreventSleep |= !(status == PS_ERROR || status == PS_INSUFFICIENT || status == PS_PAUSED || status == PS_COMPLETE);
if (status == PS_READY || status == PS_EMPTY ){
cur_datarate += file->Process( cur_udcounter );
} else {
//This will make sure we don't keep old sources to paused and stopped files..
file->StopPausedFile();
// Drain leftover Phase 3 hash work for paused files: their
// Process() doesn't run (gated above), but pre-pause
// m_aChangedPart entries still need verification.
//
// PS_INSUFFICIENT is intentionally excluded — driving
// FlushBuffer for a disk-full file re-enters its disk-space
// check at PartFile.cpp:3083 every tick, which logs
// "Not enough free disk-space" and re-pauses on every call,
// producing tens of log lines per second. The destructor
// sync-hash drain still covers leftover dirty parts of
// disk-full files at shutdown.
if (status == PS_PAUSED && file->HasPendingHashWork()) {
file->FlushBuffer();
}
}
if (!file->IsPaused() && !file->IsStopped()) {
m_sourcecountlist.push_back(file->GetSourceCount());
}
}
if (thePrefs::GetPreventSleepWhileDownloading()) {
if ((mustPreventSleep == false) && (theStats::GetSessionSentBytes() < theStats::GetSessionReceivedBytes())) {
// I can see right through your clever plan.
mustPreventSleep = true;
}
if (mustPreventSleep) {
PlatformSpecific::PreventSleepMode();
} else {
PlatformSpecific::AllowSleepMode();
}
} else {
// Just in case the value changes while we're preventing.
// Calls to this function are totally inexpensive anyway
PlatformSpecific::AllowSleepMode();
}
// Set the source rarity thresholds
int nSourceGroups = m_sourcecountlist.size();
if (nSourceGroups) {
m_sourcecountlist.sort();
if (nSourceGroups == 1) {
// High anyway.
m_rareFileThreshold = m_sourcecountlist.front() + 1;
m_commonFileThreshold = m_rareFileThreshold + 1;
} else if (nSourceGroups == 2) {
// One high, one low (unless they're both 0, then both high)
m_rareFileThreshold = (m_sourcecountlist.back() > 0) ? (m_sourcecountlist.back() - 1) : 1;
m_commonFileThreshold = m_rareFileThreshold + 1;
} else {
// More than two, time to do some math.
// Lower 25% with the current #define values.
int rare_cut_point = (nSourceGroups / RARITY_FACTOR);
for (int i = 0; i < rare_cut_point; ++ i) {
m_sourcecountlist.pop_front();
}
m_rareFileThreshold = (m_sourcecountlist.front() > 0) ? (m_sourcecountlist.front() - 1) : 1;
// 50% of the non-rare ones, with the current #define values.
int common_cut_point = (nSourceGroups - rare_cut_point) / NORMALITY_FACTOR;
for (int i = 0; i < common_cut_point; ++ i) {
m_sourcecountlist.pop_front();
}
m_commonFileThreshold = (m_sourcecountlist.front() > 0) ? (m_sourcecountlist.front() - 1) : 1;
}
} else {
m_rareFileThreshold = RARE_FILE;
m_commonFileThreshold = 100;
}
m_datarate += cur_datarate;
if (m_udcounter == 5) {
if (theApp->serverconnect->IsUDPSocketAvailable()) {
if( (curTick - m_lastudpstattime) > UDPSERVERSTATTIME) {
m_lastudpstattime = curTick;
CMutexUnlocker unlocker(m_mutex);
theApp->serverlist->ServerStats();
}
}
}
if (m_udcounter == 10) {
m_udcounter = 0;
if (theApp->serverconnect->IsUDPSocketAvailable()) {
if ( (curTick - m_lastudpsearchtime) > UDPSERVERREASKTIME) {
SendNextUDPPacket();
}
}
}
if ( (curTick - m_lastsorttime) > 10000 ) {
DoSortByPriority();
}
// Check if any paused files can be resumed
CheckDiskspace(thePrefs::GetTempDir());
}
// Check for new links once per second.
if ((curTick - m_nLastED2KLinkCheck) >= 1000) {
theApp->AddLinksFromFile();
m_nLastED2KLinkCheck = curTick;
}
}
CPartFile* CDownloadQueue::GetFileByID(const CMD4Hash& filehash) const
{
wxMutexLocker lock( m_mutex );
for ( FileQueue::size_type i = 0; i < m_filelist.size(); ++i ) {
if ( filehash == m_filelist[i]->GetFileHash()) {
return m_filelist[ i ];
}
}
// Check completed too so we can execute remote commands (like change cat) on them
for (FileList::const_iterator it = m_completedDownloads.begin(); it != m_completedDownloads.end(); ++it) {
if ( filehash == (*it)->GetFileHash()) {
return *it;
}
}
return NULL;
}
CPartFile* CDownloadQueue::GetFileByIndex(unsigned int index) const
{
wxMutexLocker lock( m_mutex );
if ( index < m_filelist.size() ) {
return m_filelist[ index ];
}
wxFAIL;
return NULL;
}
bool CDownloadQueue::IsPartFile(const CKnownFile* file) const
{
wxMutexLocker lock(m_mutex);
for (FileQueue::size_type i = 0; i < m_filelist.size(); ++i) {
if (file == m_filelist[i]) {
return true;
}
}
return false;
}
void CDownloadQueue::OnConnectionState(bool bConnected)
{
wxMutexLocker lock(m_mutex);
for (FileQueue::size_type i = 0; i < m_filelist.size(); ++i) {
if ( m_filelist[i]->GetStatus() == PS_READY ||
m_filelist[i]->GetStatus() == PS_EMPTY) {
m_filelist[i]->SetActive(bConnected);
}
}
}
void CDownloadQueue::CheckAndAddSource(CPartFile* sender, CUpDownClient* source)
{
// if we block loopbacks at this point it should prevent us from connecting to ourself
if ( source->HasValidHash() ) {
if ( source->GetUserHash() == thePrefs::GetUserHash() ) {
AddDebugLogLineN( logDownloadQueue, "Tried to add source with matching hash to your own." );
source->Safe_Delete();
return;
}
}
if (sender->IsStopped()) {
source->Safe_Delete();
return;
}
// Filter sources which are known to be dead/useless
if ( theApp->clientlist->IsDeadSource( source ) || sender->IsDeadSource(source) ) {
source->Safe_Delete();
return;
}
// Filter sources which are incompatible with our encryption setting (one requires it, and the other one doesn't supports it)
if ( (source->RequiresCryptLayer() && (!thePrefs::IsClientCryptLayerSupported() || !source->HasValidHash())) || (thePrefs::IsClientCryptLayerRequired() && (!source->SupportsCryptLayer() || !source->HasValidHash()))) {
source->Safe_Delete();
return;
}
// Find all clients with the same hash
if ( source->HasValidHash() ) {
CClientList::SourceList found = theApp->clientlist->GetClientsByHash( source->GetUserHash() );
CClientList::SourceList::iterator it = found.begin();
for ( ; it != found.end(); ++it ) {
CKnownFile* file = it->GetRequestFile();
// Only check files on the download-queue
if ( file ) {
// Is the found source queued for something else?
if ( file != sender ) {
// Try to add a request for the other file
if ( it->GetClient()->AddRequestForAnotherFile(sender)) {
// Add it to downloadlistctrl
Notify_SourceCtrlAddSource(sender, *it, A4AF_SOURCE);
}
}
source->Safe_Delete();
return;
}
}
}
// Our new source is real new but maybe it is already uploading to us?
// If yes the known client will be attached to the var "source" and the old
// source-client will be deleted. However, if the request file of the known
// source is NULL, then we have to treat it almost like a new source and if
// it isn't NULL and not "sender", then we shouldn't move it, but rather add
// a request for the new file.
ESourceFrom nSourceFrom = source->GetSourceFrom();
if ( theApp->clientlist->AttachToAlreadyKnown(&source, 0) ) {
// Already queued for another file?
if ( source->GetRequestFile() ) {
// If we're already queued for the right file, then there's nothing to do
if ( sender != source->GetRequestFile() ) {
// Add the new file to the request list
source->AddRequestForAnotherFile( sender );
}
} else {
// Source was known, but reqfile NULL.
source->SetRequestFile( sender );
source->SetSourceFrom(nSourceFrom);
sender->AddSource( source );
if ( source->GetFileRating() || !source->GetFileComment().IsEmpty() ) {
sender->UpdateFileRatingCommentAvail();
}
Notify_SourceCtrlAddSource(sender, CCLIENTREF(source, "CDownloadQueue::CheckAndAddSource Notify_SourceCtrlAddSource 1"), UNAVAILABLE_SOURCE);
}
} else {
// Unknown client, add it to the clients list
source->SetRequestFile( sender );
theApp->clientlist->AddClient(source);
sender->AddSource( source );
if ( source->GetFileRating() || !source->GetFileComment().IsEmpty() ) {
sender->UpdateFileRatingCommentAvail();
}
Notify_SourceCtrlAddSource(sender, CCLIENTREF(source, "CDownloadQueue::CheckAndAddSource Notify_SourceCtrlAddSource 2"), UNAVAILABLE_SOURCE);
}
}
void CDownloadQueue::CheckAndAddKnownSource(CPartFile* sender,CUpDownClient* source)
{
// Kad reviewed
if (sender->IsStopped()) {
return;
}
// Filter sources which are known to be dead/useless
if ( sender->IsDeadSource(source) ) {
return;
}
// "Filter LAN IPs" -- this may be needed here in case we are connected to the internet and are also connected
// to a LAN and some client from within the LAN connected to us. Though this situation may be supported in future
// by adding that client to the source list and filtering that client's LAN IP when sending sources to
// a client within the internet.
//
// "IPfilter" is not needed here, because that "known" client was already IPfiltered when receiving OP_HELLO.
if (!source->HasLowID()) {
uint32 nClientIP = wxUINT32_SWAP_ALWAYS(source->GetUserIDHybrid());
if (!IsGoodIP(nClientIP, thePrefs::FilterLanIPs())) { // check for 0-IP, localhost and LAN addresses
AddDebugLogLineN(logIPFilter, "Ignored already known source with IP=%s" + Uint32toStringIP(nClientIP));
return;
}
}
// Filter sources which are incompatible with our encryption setting (one requires it, and the other one doesn't supports it)
if ( (source->RequiresCryptLayer() && (!thePrefs::IsClientCryptLayerSupported() || !source->HasValidHash()))
|| (thePrefs::IsClientCryptLayerRequired() && (!source->SupportsCryptLayer() || !source->HasValidHash()))) {
return;
}
CPartFile* file = source->GetRequestFile();
// Check if the file is already queued for something else
if ( file ) {
if ( file != sender ) {
if ( source->AddRequestForAnotherFile( sender ) ) {
Notify_SourceCtrlAddSource( sender, CCLIENTREF(source, "CDownloadQueue::CheckAndAddKnownSource Notify_SourceCtrlAddSource 1"), A4AF_SOURCE );
}
}
} else {
source->SetRequestFile( sender );
if ( source->GetFileRating() || !source->GetFileComment().IsEmpty() ) {
sender->UpdateFileRatingCommentAvail();
}
source->SetSourceFrom(SF_PASSIVE);
sender->AddSource( source );
Notify_SourceCtrlAddSource( sender, CCLIENTREF(source, "CDownloadQueue::CheckAndAddKnownSource Notify_SourceCtrlAddSource 2"), UNAVAILABLE_SOURCE);
}
}
bool CDownloadQueue::RemoveSource(CUpDownClient* toremove, bool WXUNUSED(updatewindow), bool bDoStatsUpdate)
{
bool removed = false;
toremove->DeleteAllFileRequests();
for ( uint16 i = 0; i < GetFileCount(); i++ ) {
CPartFile* cur_file = GetFileByIndex( i );
// Remove from source-list
if ( cur_file->DelSource( toremove ) ) {
// Remove from sourcelist widget
Notify_SourceCtrlRemoveSource(toremove->ECID(), cur_file);
cur_file->RemoveDownloadingSource(toremove);
removed = true;
if ( bDoStatsUpdate ) {
cur_file->UpdatePartsInfo();
}
}
// Remove from A4AF-list
cur_file->RemoveA4AFSource( toremove );
}
if ( !toremove->GetFileComment().IsEmpty() || toremove->GetFileRating()>0) {
toremove->GetRequestFile()->UpdateFileRatingCommentAvail();
}
toremove->SetRequestFile( NULL );
toremove->SetDownloadState(DS_NONE);
toremove->ResetFileStatusInfo();
return removed;
}
void CDownloadQueue::RemoveFile(CPartFile* file, bool keepAsCompleted)
{
RemoveLocalServerRequest( file );
NotifyObservers( EventType( EventType::REMOVED, file ) );
wxMutexLocker lock( m_mutex );
EraseValue( m_filelist, file );
if (keepAsCompleted) {
m_completedDownloads.push_back(file);
}
}
void CDownloadQueue::ClearCompleted(const ListOfUInts32 & ecids)
{
for (ListOfUInts32::const_iterator it1 = ecids.begin(); it1 != ecids.end(); ++it1) {
uint32 ecid = *it1;
for (FileList::iterator it = m_completedDownloads.begin(); it != m_completedDownloads.end(); ++it) {
CPartFile * file = *it;
if (file->ECID() == ecid) {
m_completedDownloads.erase(it);
// get a new EC ID so it is resent and cleared in remote gui
file->RenewECID();
Notify_DownloadCtrlRemoveFile(file);
break;
}
}
}
}
CUpDownClient* CDownloadQueue::GetDownloadClientByIP_UDP(uint32 dwIP, uint16 nUDPPort) const
{
wxMutexLocker lock( m_mutex );
for ( FileQueue::size_type i = 0; i < m_filelist.size(); i++ ) {
const CKnownFile::SourceSet& set = m_filelist[i]->GetSourceList();
for ( CKnownFile::SourceSet::const_iterator it = set.begin(); it != set.end(); ++it ) {
if ( it->GetIP() == dwIP && it->GetUDPPort() == nUDPPort ) {
return it->GetClient();
}
}
}
return NULL;
}
/**
* Checks if the specified server is the one we are connected to.
*/
static bool IsConnectedServer(const CServer* server)
{
if (server && theApp->serverconnect->GetCurrentServer()) {
wxString srvAddr = theApp->serverconnect->GetCurrentServer()->GetAddress();
uint16 srvPort = theApp->serverconnect->GetCurrentServer()->GetPort();
return server->GetAddress() == srvAddr && server->GetPort() == srvPort;
}
return false;
}
bool CDownloadQueue::SendNextUDPPacket()
{
if ( m_filelist.empty() || !theApp->serverconnect->IsUDPSocketAvailable() || !theApp->IsConnectedED2K()) {
return false;
}
// Start monitoring the server and the files list
if ( !m_queueServers.IsActive() ) {
AddObserver( &m_queueFiles );
theApp->serverlist->AddObserver( &m_queueServers );
}
bool packetSent = false;
while ( !packetSent ) {
// Get max files ids per packet for current server
int filesAllowed = GetMaxFilesPerUDPServerPacket();
if (filesAllowed < 1 || !m_udpserver || IsConnectedServer(m_udpserver)) {
// Select the next server to ask, must not be the connected server
do {
m_udpserver = m_queueServers.GetNext();
} while (IsConnectedServer(m_udpserver));
m_cRequestsSentToServer = 0;
filesAllowed = GetMaxFilesPerUDPServerPacket();
}
// Check if we have asked all servers, in which case we are done
if (m_udpserver == NULL) {
DoStopUDPRequests();
return false;
}
// Memoryfile containing the hash of every file to request
// 28bytes allocation because 16b + 4b + 8b is the worse case scenario.
CMemFile hashlist( 28 );
CPartFile* file = m_queueFiles.GetNext();
while ( file && filesAllowed ) {
uint8 status = file->GetStatus();
if ( ( status == PS_READY || status == PS_EMPTY ) && file->GetSourceCount() < thePrefs::GetMaxSourcePerFileUDP() ) {
if (file->IsLargeFile() && !m_udpserver->SupportsLargeFilesUDP()) {
AddDebugLogLineN(logDownloadQueue, "UDP Request for sources on a large file ignored: server doesn't support it");
} else {
++m_cRequestsSentToServer;
hashlist.WriteHash( file->GetFileHash() );
// See the notes on TCP packet
if ( m_udpserver->GetUDPFlags() & SRV_UDPFLG_EXT_GETSOURCES2 ) {
if (file->IsLargeFile()) {
wxASSERT(m_udpserver->SupportsLargeFilesUDP());
hashlist.WriteUInt32( 0 );
hashlist.WriteUInt64( file->GetFileSize() );
} else {
hashlist.WriteUInt32( file->GetFileSize() );
}
}
--filesAllowed;
}
}
// Avoid skipping a file if we can't send any more currently
if ( filesAllowed ) {
file = m_queueFiles.GetNext();
}
}
// See if we have anything to send
if ( hashlist.GetLength() ) {
packetSent = SendGlobGetSourcesUDPPacket(hashlist);
}
// Check if we've covered every file
if ( file == NULL ) {
// Reset the list of asked files so that the loop will start over
m_queueFiles.Reset();
// Unset the server so that the next server will be used
m_udpserver = NULL;
}
}
return true;
}
void CDownloadQueue::StopUDPRequests()
{
wxMutexLocker lock( m_mutex );
DoStopUDPRequests();
}
void CDownloadQueue::DoStopUDPRequests()
{
// No need to observe when we wont be using the results
theApp->serverlist->RemoveObserver( &m_queueServers );
RemoveObserver( &m_queueFiles );
m_udpserver = 0;
m_lastudpsearchtime = ::GetTickCount64();
}
// Comparison function needed by sort. Returns true if file1 precedes file2
static bool ComparePartFiles(const CPartFile* file1, const CPartFile* file2) {
if (file1->GetDownPriority() != file2->GetDownPriority()) {
// To place high-priority files before low priority files we have to
// invert this test, since PR_LOW is lower than PR_HIGH, and since
// placing a PR_LOW file before a PR_HIGH file would mean that
// the PR_LOW file gets sources before the PR_HIGH file ...
return (file1->GetDownPriority() > file2->GetDownPriority());
} else {
int sourcesA = file1->GetSourceCount();