-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathUploadQueue.cpp
More file actions
768 lines (681 loc) · 23.8 KB
/
Copy pathUploadQueue.cpp
File metadata and controls
768 lines (681 loc) · 23.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
//
// 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 "UploadQueue.h" // Interface declarations
#include <protocol/Protocols.h>
#include <protocol/ed2k/Client2Client/TCP.h>
#include <common/Macros.h>
#include <common/Constants.h>
#include <cmath>
#include "Types.h" // Do_not_auto_remove (win32)
#ifdef __WINDOWS__
#include <winsock2.h> // Do_not_auto_remove (htonl/ntohs/inet_addr) — legacy <winsock.h> pulls <windows.h> and trips winsock2.h:15 via later wx includes
#else
#include <sys/types.h> // Do_not_auto_remove
#include <netinet/in.h> // Do_not_auto_remove
#include <arpa/inet.h> // Do_not_auto_remove
#endif
#include "ServerConnect.h" // Needed for CServerConnect
#include "KnownFile.h" // Needed for CKnownFile
#include "Packet.h" // Needed for CPacket
#include "ClientTCPSocket.h" // Needed for CClientTCPSocket
#include "SharedFileList.h" // Needed for CSharedFileList
#include "updownclient.h" // Needed for CUpDownClient
#include "amule.h" // Needed for theApp
#include "Preferences.h"
#include "ClientList.h"
#include "Statistics.h" // Needed for theStats
#include "Logger.h"
#include <common/Format.h>
#include "UploadBandwidthThrottler.h"
#include "GuiEvents.h" // Needed for Notify_*
#include "ListenSocket.h"
#include "DownloadQueue.h"
#include "PartFile.h"
//TODO rewrite the whole networkcode, use overlapped sockets
CUploadQueue::CUploadQueue()
{
m_nLastStartUpload = 0;
m_lastSort = 0;
lastupslotHighID = true;
m_allowKicking = true;
m_allUploadingKnownFile = new CKnownFile;
}
void CUploadQueue::SortGetBestClient(CClientRef * bestClient)
{
uint64 tick = GetTickCount64();
m_lastSort = tick;
CClientRefList::iterator it = m_waitinglist.begin();
for (; it != m_waitinglist.end(); ) {
CClientRefList::iterator it2 = it++;
CUpDownClient* cur_client = it2->GetClient();
// clear dead clients
if (tick - cur_client->GetLastUpRequest() > MAX_PURGEQUEUETIME
|| !theApp->sharedfiles->GetFileByID(cur_client->GetUploadFileID())) {
cur_client->ClearWaitStartTime();
RemoveFromWaitingQueue(it2);
if (!cur_client->GetSocket()) {
if (cur_client->Disconnected("AddUpNextClient - purged")) {
cur_client->Safe_Delete();
cur_client = NULL;
}
}
continue;
}
if (cur_client->IsBanned() || IsSuspended(cur_client->GetUploadFileID())) { // Banned client or suspended upload ?
cur_client->ClearScore();
continue;
}
// finished clearing
// Calculate score of current client
uint32 cur_score = cur_client->CalculateScore();
// Check if it's better than that of a previous one, and move it up then.
CClientRefList::iterator it1 = it2;
while (it1 != m_waitinglist.begin()) {
--it1;
if (cur_score > it1->GetClient()->GetScore()) {
// swap them
std::swap(*it2, *it1);
--it2;
} else {
// no need to check further since list is already sorted
break;
}
}
}
// Second Pass:
// - calculate queue rank
// - find best high id client
// - mark all better low id clients as enabled for upload
uint16 rank = 1;
bool bestClientFound = false;
for (it = m_waitinglist.begin(); it != m_waitinglist.end(); ) {
CClientRefList::iterator it2 = it++;
CUpDownClient* cur_client = it2->GetClient();
cur_client->SetUploadQueueWaitingPosition(rank++);
if (bestClientFound) {
// There's a better high id client
cur_client->m_bAddNextConnect = false;
} else {
if (cur_client->HasLowID() && !cur_client->IsConnected()) {
// No better high id client, so start upload to this one once it connects
cur_client->m_bAddNextConnect = true;
} else {
// We found a high id client (or a currently connected low id client)
bestClientFound = true;
cur_client->m_bAddNextConnect = false;
if (bestClient) {
bestClient->Link(cur_client CLIENT_DEBUGSTRING("CUploadQueue::SortGetBestClient"));
RemoveFromWaitingQueue(it2);
rank--;
lastupslotHighID = true; // VQB LowID alternate
}
}
}
}
#ifdef __DEBUG__
AddDebugLogLineN(logLocalClient, CFormat("Current UL queue (%d):") % (rank - 1));
for (it = m_waitinglist.begin(); it != m_waitinglist.end(); ++it) {
CUpDownClient* c = it->GetClient();
AddDebugLogLineN(logLocalClient, CFormat("%4d %7d %s %5d %s")
% c->GetUploadQueueWaitingPosition()
% c->GetScore()
% (c->HasLowID() ? (c->IsConnected() ? "LoCon" : "LowId") : "High ")
% c->ECID()
% c->GetUserName()
);
}
#endif // __DEBUG__
}
void CUploadQueue::AddUpNextClient(CUpDownClient* directadd)
{
CUpDownClient* newclient = NULL;
CClientRef newClientRef;
// select next client or use given client
if (!directadd) {
SortGetBestClient(&newClientRef);
newclient = newClientRef.GetClient();
#if EXTENDED_UPLOADQUEUE
if (!newclient) {
// Nothing to upload. Try to find something from the sources.
if (PopulatePossiblyWaitingList() > 0) {
newClientRef = * m_possiblyWaitingList.begin();
m_possiblyWaitingList.pop_front();
newclient = newClientRef.GetClient();
AddDebugLogLineN( logLocalClient, "Added client from possiblyWaitingList " + newclient->GetFullIP() );
}
}
#endif
if (!newclient) {
return;
}
} else {
// Check if requested file is suspended or not shared (maybe deleted recently)
if (IsSuspended(directadd->GetUploadFileID())
|| !theApp->sharedfiles->GetFileByID(directadd->GetUploadFileID())) {
return;
} else {
newclient = directadd;
}
}
if (IsDownloading(newclient)) {
return;
}
// tell the client that we are now ready to upload
if (!newclient->IsConnected()) {
newclient->SetUploadState(US_CONNECTING);
if (!newclient->TryToConnect(true)) {
return;
}
} else {
CPacket* packet = new CPacket(OP_ACCEPTUPLOADREQ, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN( logLocalClient, "Local Client: OP_ACCEPTUPLOADREQ to " + newclient->GetFullIP() );
newclient->SendPacket(packet,true);
newclient->SetUploadState(US_UPLOADING);
}
newclient->SetUpStartTime();
newclient->ResetSessionUp();
{
// Guard against concurrent iteration by the disk I/O thread.
wxMutexLocker lock(m_uploadingListMutex);
theApp->uploadBandwidthThrottler->AddToStandardList(m_uploadinglist.size(), newclient->GetSocket());
m_uploadinglist.push_back(CCLIENTREF(newclient, "CUploadQueue::AddUpNextClient"));
}
m_allUploadingKnownFile->AddUploadingClient(newclient);
theStats::AddUploadingClient();
// Statistic
CKnownFile* reqfile = const_cast<CKnownFile*>(newclient->GetUploadFile());
if (reqfile) {
reqfile->statistic.AddAccepted();
}
Notify_SharedCtrlRefreshClient(newclient->ECID(), AVAILABLE_SOURCE);
}
void CUploadQueue::Process()
{
// Check if someone's waiting, if there is a slot for him,
// or if we should try to free a slot for him
uint64 tick = GetTickCount64();
// Nobody waiting or upload started recently
// (Actually instead of "empty" it should check for "no HighID clients queued",
// but the cost for that outweighs the benefit. As it is, a slot will be freed
// even if it can't be taken because all of the queue is LowID. But just one,
// and the kicked client will instantly get it back if he has HighID.)
// Also, if we are running out of sockets, don't add new clients, but also don't kick existing ones,
// or uploading will cease right away.
#if EXTENDED_UPLOADQUEUE
if (tick - m_nLastStartUpload < 1000
#else
if (m_waitinglist.empty() || tick - m_nLastStartUpload < 1000
#endif
|| theApp->listensocket->TooManySockets()) {
m_allowKicking = false;
// Already a slot free, try to fill it
} else if (m_uploadinglist.size() < GetMaxSlots()) {
m_allowKicking = false;
m_nLastStartUpload = tick;
AddUpNextClient();
// All slots taken, try to free one
} else {
m_allowKicking = true;
}
// The loop that feeds the upload slots with data.
CClientRefList::iterator it = m_uploadinglist.begin();
while (it != m_uploadinglist.end()) {
// Get the client. Note! Also updates pos as a side effect.
CUpDownClient* cur_client = it++->GetClient();
// It seems chatting or friend slots can get stuck at times in upload.. This needs looked into..
if (!cur_client->GetSocket()) {
RemoveFromUploadQueue(cur_client);
if(cur_client->Disconnected(_T("CUploadQueue::Process"))){
cur_client->Safe_Delete();
}
} else if (cur_client->m_bIOError) {
// Disk I/O thread signaled an error for this client
RemoveFromUploadQueue(cur_client);
} else {
cur_client->SendBlockData();
}
}
// Save used bandwidth for speed calculations
uint64 sentBytes = theApp->uploadBandwidthThrottler->GetNumberOfSentBytesSinceLastCallAndReset();
(void)theApp->uploadBandwidthThrottler->GetNumberOfSentBytesOverheadSinceLastCallAndReset();
// Update statistics
if (sentBytes) {
theStats::AddSentBytes(sentBytes);
}
// Periodically resort queue if it doesn't happen anyway
if ((sint64) (tick - m_lastSort) > MIN2MS(2)) {
SortGetBestClient();
}
}
uint32 CUploadQueue::GetMaxSlots() const
{
uint32 nMaxSlots = 0;
float kBpsUpPerClient = (float)thePrefs::GetSlotAllocation();
if (thePrefs::GetMaxUpload() == UNLIMITED) {
// Speed-based formula plus a floor. The raw "currentRate / slotRate + 2"
// is a chicken-and-egg trap: with 0 B/s observed uplink we'd cap at 2 slots,
// which can't carry enough parallel TCP flows to break cold-start and let
// the uplink ramp up. Floor at N_FLOOR slots regardless of measured rate so
// there is always enough concurrency for the ramp.
const uint32 N_FLOOR = 20;
float kBpsUp = theStats::GetUploadRate() / 1024.0f;
uint32 bySpeed = (uint32)(kBpsUp / kBpsUpPerClient) + 2;
nMaxSlots = bySpeed > N_FLOOR ? bySpeed : N_FLOOR;
} else {
if (thePrefs::GetMaxUpload() >= 10) {
nMaxSlots = (uint32)floor((float)thePrefs::GetMaxUpload() / kBpsUpPerClient + 0.5);
// floor(x + 0.5) is a way of doing round(x) that works with gcc < 3 ...
if (nMaxSlots < MIN_UP_CLIENTS_ALLOWED) {
nMaxSlots=MIN_UP_CLIENTS_ALLOWED;
}
} else {
nMaxSlots = MIN_UP_CLIENTS_ALLOWED;
}
}
if (nMaxSlots > MAX_UP_CLIENTS_ALLOWED) {
nMaxSlots = MAX_UP_CLIENTS_ALLOWED;
}
return nMaxSlots;
}
CUploadQueue::~CUploadQueue()
{
wxASSERT(m_waitinglist.empty());
wxASSERT(m_uploadinglist.empty());
delete m_allUploadingKnownFile;
}
bool CUploadQueue::IsOnUploadQueue(const CUpDownClient* client) const
{
for (CClientRefList::const_iterator it = m_waitinglist.begin(); it != m_waitinglist.end(); ++it) {
if (it->GetClient() == client) {
return true;
}
}
return false;
}
bool CUploadQueue::IsDownloading(const CUpDownClient* client) const
{
for (CClientRefList::const_iterator it = m_uploadinglist.begin(); it != m_uploadinglist.end(); ++it) {
if (it->GetClient() == client) {
return true;
}
}
return false;
}
CUpDownClient* CUploadQueue::GetWaitingClientByIP_UDP(uint32 dwIP, uint16 nUDPPort, bool bIgnorePortOnUniqueIP, bool* pbMultipleIPs)
{
CUpDownClient* pMatchingIPClient = NULL;
int cMatches = 0;
CClientRefList::iterator it = m_waitinglist.begin();
for (; it != m_waitinglist.end(); ++it) {
CUpDownClient* cur_client = it->GetClient();
if ((dwIP == cur_client->GetIP()) && (nUDPPort == cur_client->GetUDPPort())) {
return cur_client;
} else if ((dwIP == cur_client->GetIP()) && bIgnorePortOnUniqueIP) {
pMatchingIPClient = cur_client;
cMatches++;
}
}
if (pbMultipleIPs) {
*pbMultipleIPs = cMatches > 1;
}
if (pMatchingIPClient && cMatches == 1) {
return pMatchingIPClient;
} else {
return NULL;
}
}
void CUploadQueue::AddClientToQueue(CUpDownClient* client)
{
if (theApp->serverconnect->IsConnected() && theApp->serverconnect->IsLowID() && !theApp->serverconnect->IsLocalServer(client->GetServerIP(),client->GetServerPort()) && client->GetDownloadState() == DS_NONE && !client->IsFriend() && theStats::GetWaitingUserCount() > 50) {
// Well, all that issues finish in the same: don't allow to add to the queue
return;
}
if ( client->IsBanned() ) {
return;
}
client->AddAskedCount();
client->SetLastUpRequest();
// Find all clients with the same user-hash
CClientList::SourceList found = theApp->clientlist->GetClientsByHash( client->GetUserHash() );
CClientList::SourceList::iterator it = found.begin();
while (it != found.end()) {
CUpDownClient* cur_client = it++->GetClient();
if ( IsOnUploadQueue( cur_client ) ) {
if ( cur_client == client ) {
// This is where LowID clients get their upload slot assigned.
// They can't be contacted if they reach top of the queue, so they are just marked for uploading.
// When they reconnect next time AddClientToQueue() is called, and they get their slot
// through the connection they initiated.
// Since at that time no slot is free they get assigned an extra slot,
// so then the number of slots exceeds the configured number by one.
// To prevent a further increase no more LowID clients get a slot, until
// - a HighID client has got one (which happens only after two clients
// have been kicked so a slot is free again)
// - or there is a free slot, which means there is no HighID client on queue
if (client->m_bAddNextConnect) {
uint32 maxSlots = GetMaxSlots();
if (lastupslotHighID) {
maxSlots++;
}
if (m_uploadinglist.size() < maxSlots) {
client->m_bAddNextConnect = false;
RemoveFromWaitingQueue(client);
AddUpNextClient(client);
lastupslotHighID = false; // LowID alternate
return;
}
}
client->SendRankingInfo();
Notify_SharedCtrlRefreshClient(client->ECID(), AVAILABLE_SOURCE);
return;
} else {
// Hash-clash, remove unidentified clients (possibly both)
if ( !cur_client->IsIdentified() ) {
// Cur_client isn't identifed, remove it
theApp->clientlist->AddTrackClient( cur_client );
RemoveFromWaitingQueue( cur_client );
if ( !cur_client->GetSocket() ) {
if (cur_client->Disconnected( "AddClientToQueue - same userhash" ) ) {
cur_client->Safe_Delete();
}
}
}
if ( !client->IsIdentified() ) {
// New client isn't identified, remove it
theApp->clientlist->AddTrackClient( client );
if ( !client->GetSocket() ) {
if ( client->Disconnected( "AddClientToQueue - same userhash" ) ) {
client->Safe_Delete();
}
}
return;
}
}
}
}
// Count the number of clients with the same IP-address
found = theApp->clientlist->GetClientsByIP( client->GetIP() );
int ipCount = 0;
for ( it = found.begin(); it != found.end(); ++it ) {
if ( ( it->GetClient() == client ) || IsOnUploadQueue( it->GetClient() ) ) {
ipCount++;
}
}
// We do not accept more than 3 clients from the same IP
if ( ipCount > 3 ) {
return;
} else if ( theApp->clientlist->GetClientsFromIP(client->GetIP()) >= 3 ) {
return;
}
// statistic values
CKnownFile* reqfile = const_cast<CKnownFile*>(client->GetUploadFile());
if (reqfile) {
reqfile->statistic.AddRequest();
}
if (client->IsDownloading()) {
// he's already downloading and wants probably only another file
CPacket* packet = new CPacket(OP_ACCEPTUPLOADREQ, 0, OP_EDONKEYPROT);
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
AddDebugLogLineN( logLocalClient, "Local Client: OP_ACCEPTUPLOADREQ to " + client->GetFullIP() );
client->SendPacket(packet,true);
return;
}
// TODO find better ways to cap the list
if (m_waitinglist.size() >= (thePrefs::GetQueueSize())) {
return;
}
uint64 tick = GetTickCount64();
client->ClearWaitStartTime();
// if possible start upload right away
if (m_waitinglist.empty() && tick - m_nLastStartUpload >= 1000
&& m_uploadinglist.size() < GetMaxSlots()
&& !theApp->listensocket->TooManySockets()) {
AddUpNextClient(client);
m_nLastStartUpload = tick;
} else {
// add to waiting queue
m_waitinglist.push_back(CCLIENTREF(client, "CUploadQueue::AddClientToQueue m_waitinglist.push_back"));
// and sort it to update queue ranks
SortGetBestClient();
theStats::AddWaitingClient();
client->ClearAskedCount();
client->SetUploadState(US_ONUPLOADQUEUE);
client->SendRankingInfo();
//Notify_QlistAddClient(client);
}
}
bool CUploadQueue::RemoveFromUploadQueue(CUpDownClient* client)
{
// Keep track of this client
theApp->clientlist->AddTrackClient(client);
// Guard the find+erase against concurrent iteration by the disk I/O thread.
bool found = false;
{
wxMutexLocker lock(m_uploadingListMutex);
CClientRefList::iterator it = std::find(m_uploadinglist.begin(),
m_uploadinglist.end(), CCLIENTREF(client, ""));
if (it != m_uploadinglist.end()) {
m_uploadinglist.erase(it);
found = true;
}
}
if (found) {
m_allUploadingKnownFile->RemoveUploadingClient(client);
theStats::RemoveUploadingClient();
if( client->GetTransferredUp() ) {
theStats::AddSuccessfulUpload();
theStats::AddUploadTime(client->GetUpStartTimeDelay() / 1000);
} else {
theStats::AddFailedUpload();
}
client->SetUploadState(US_NONE);
client->ClearUploadBlockRequests();
return true;
}
return false;
}
bool CUploadQueue::CheckForTimeOver(CUpDownClient* client)
{
// Don't kick anybody if there's no need to
if (!m_allowKicking) {
return false;
}
// First, check if it is a VIP slot (friend or Release-Prio).
if (client->GetFriendSlot()) {
return false; // never drop the friend
}
// Release-Prio and nobody on queue for it?
if (client->GetUploadFile()->GetUpPriority() == PR_POWERSHARE) {
// Keep it unless half of the UL slots are occupied with friends or Release uploads.
uint16 vips = 0;
for (CClientRefList::iterator it = m_uploadinglist.begin(); it != m_uploadinglist.end(); ++it) {
CUpDownClient* cur_client = it->GetClient();
if (cur_client->GetFriendSlot() || cur_client->GetUploadFile()->GetUpPriority() == PR_POWERSHARE) {
vips++;
}
}
// allow if VIP uploads occupy at most half of the possible upload slots
if (vips <= GetMaxSlots() / 2) {
return false;
}
// Otherwise normal rules apply.
}
// Ordinary slots
// "Transfer full chunks": drop client after 10 MB upload, or after an hour.
// (so average UL speed should at least be 2.84 kB/s)
// We don't track what he is downloading, but if it's all from one chunk he gets it.
if (client->GetUpStartTimeDelay() > 3600000 // time: 1h
|| client->GetSessionUp() > 10485760) { // data: 10MB
m_allowKicking = false; // kick max one client per cycle
return true;
}
return false;
}
/*
* This function removes a file indicated by filehash from suspended_uploads_list.
*/
void CUploadQueue::ResumeUpload( const CMD4Hash& filehash )
{
suspendedUploadsSet.erase(filehash);
AddLogLineN(CFormat( _("Resuming uploads of file: %s" ) )
% filehash.Encode() );
}
/*
* This function stops upload of a file indicated by filehash.
*
* a) terminate == false:
* File is suspended while a download completes. Then it is resumed after completion,
* so it makes sense to keep the client. Such files are kept in suspendedUploadsSet.
* b) terminate == true:
* File is deleted. Then the client is not added to the waiting list.
* Waiting clients are swept out with next run of AddUpNextClient,
* because their file is not shared anymore.
*/
uint16 CUploadQueue::SuspendUpload(const CMD4Hash& filehash, bool terminate)
{
AddLogLineN(CFormat( _("Suspending upload of file: %s" ) )
% filehash.Encode() );
uint16 removed = 0;
//Append the filehash to the list.
if (!terminate) {
suspendedUploadsSet.insert(filehash);
}
CClientRefList::iterator it = m_uploadinglist.begin();
while (it != m_uploadinglist.end()) {
CUpDownClient *potential = it++->GetClient();
//check if the client is uploading the file we need to suspend
if (potential->GetUploadFileID() == filehash) {
// remove the unlucky client from the upload queue
RemoveFromUploadQueue(potential);
// if suspend isn't permanent add it to the waiting queue
if (terminate) {
potential->SetUploadState(US_NONE);
} else {
m_waitinglist.push_back(CCLIENTREF(potential, "CUploadQueue::SuspendUpload"));
theStats::AddWaitingClient();
potential->SetUploadState(US_ONUPLOADQUEUE);
potential->SendRankingInfo();
Notify_SharedCtrlRefreshClient(potential->ECID(), AVAILABLE_SOURCE);
}
removed++;
}
}
return removed;
}
bool CUploadQueue::RemoveFromWaitingQueue(CUpDownClient* client)
{
CClientRefList::iterator it = m_waitinglist.begin();
uint16 rank = 1;
while (it != m_waitinglist.end()) {
CClientRefList::iterator it1 = it++;
if (it1->GetClient() == client) {
RemoveFromWaitingQueue(it1);
// update ranks of remaining queue
while (it != m_waitinglist.end()) {
it->GetClient()->SetUploadQueueWaitingPosition(rank++);
++it;
}
return true;
}
rank++;
}
return false;
}
void CUploadQueue::RemoveFromWaitingQueue(CClientRefList::iterator pos)
{
CUpDownClient* todelete = pos->GetClient();
m_waitinglist.erase(pos);
theStats::RemoveWaitingClient();
if( todelete->IsBanned() ) {
todelete->UnBan();
}
//Notify_QlistRemoveClient(todelete);
todelete->SetUploadState(US_NONE);
todelete->ClearScore();
todelete->SetUploadQueueWaitingPosition(0);
}
#if EXTENDED_UPLOADQUEUE
int CUploadQueue::PopulatePossiblyWaitingList()
{
static uint64 lastPopulate = 0;
uint64 tick = GetTickCount64();
int ret = m_possiblyWaitingList.size();
if (tick - lastPopulate > MIN2MS(15)) {
// repopulate in any case after this time
m_possiblyWaitingList.clear();
} else if (ret || tick - lastPopulate < SEC2MS(30)) {
// don't retry too fast if list is empty
return ret;
}
lastPopulate = tick;
// Get our downloads
int nrDownloads = theApp->downloadqueue->GetFileCount();
for (int idownload = 0; idownload < nrDownloads; idownload++) {
CPartFile * download = theApp->downloadqueue->GetFileByIndex(idownload);
if (!download || download->GetAvailablePartCount() == 0) {
continue;
}
const CKnownFile::SourceSet& sources = download->GetSourceList();
if (sources.empty()) {
continue;
}
// Make a table which parts are available. No need to notify a client
// which needs nothing we have.
uint16 parts = download->GetPartCount();
std::vector<bool> partsAvailable;
partsAvailable.resize(parts);
for (uint32 i = parts; i--;) {
partsAvailable[i] = download->IsComplete(i);
}
for (CKnownFile::SourceSet::const_iterator it = sources.begin(); it != sources.end(); it++) {
// Iterate over our sources, find those where download == upload
CUpDownClient * client = it->GetClient();
if (!client || client->GetUploadFile() != download || client->HasLowID()) {
continue;
}
const BitVector& partStatus = client->GetPartStatus();
if (partStatus.size() != parts) {
continue;
}
// Do we have something for him?
bool haveSomething = false;
for (uint32 i = parts; i--;) {
if (partsAvailable[i] && !partStatus.get(i)) {
haveSomething = true;
break;
}
}
if (haveSomething) {
m_possiblyWaitingList.push_back(*it);
}
}
}
ret = m_possiblyWaitingList.size();
AddDebugLogLineN(logLocalClient, CFormat("Populated PossiblyWaitingList: %d") % ret);
return ret;
}
#endif // EXTENDED_UPLOADQUEUE
// File_checked_for_headers