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 "UploadQueue.h" // Interface declarations
28 #include <protocol/Protocols.h>
29 #include <protocol/ed2k/Client2Client/TCP.h>
30 #include <common/Macros.h>
31 #include <common/Constants.h>
35 #include "Types.h" // Do_not_auto_remove (win32)
38 #include <winsock.h> // Do_not_auto_remove
40 #include <sys/types.h> // Do_not_auto_remove
41 #include <netinet/in.h> // Do_not_auto_remove
42 #include <arpa/inet.h> // Do_not_auto_remove
45 #include "ServerConnect.h" // Needed for CServerConnect
46 #include "KnownFile.h" // Needed for CKnownFile
47 #include "Packet.h" // Needed for CPacket
48 #include "ClientTCPSocket.h" // Needed for CClientTCPSocket
49 #include "SharedFileList.h" // Needed for CSharedFileList
50 #include "updownclient.h" // Needed for CUpDownClient
51 #include "amule.h" // Needed for theApp
52 #include "Preferences.h"
53 #include "ClientList.h"
54 #include "Statistics.h" // Needed for theStats
56 #include <common/Format.h>
57 #include "UploadBandwidthThrottler.h"
58 #include "GuiEvents.h" // Needed for Notify_*
61 //TODO rewrite the whole networkcode, use overlapped sockets
63 CUploadQueue::CUploadQueue()
65 m_nLastStartUpload
= 0;
67 lastupslotHighID
= true;
71 void CUploadQueue::AddUpNextClient(CUpDownClient
* directadd
)
73 CClientPtrList::iterator toadd
= m_waitinglist
.end();
74 CClientPtrList::iterator toaddlow
= m_waitinglist
.end();
76 uint32_t bestscore
= 0;
77 uint32_t bestlowscore
= 0;
79 CUpDownClient
* newclient
;
80 // select next client or use given client
82 // Track if we purged any clients from the queue, as to only send one notify in total
85 CClientPtrList::iterator it
= m_waitinglist
.begin();
86 for (; it
!= m_waitinglist
.end(); ) {
87 CClientPtrList::iterator tmp_it
= it
++;
88 CUpDownClient
* cur_client
= *tmp_it
;
91 if ( (::GetTickCount() - cur_client
->GetLastUpRequest() > MAX_PURGEQUEUETIME
) || !theApp
->sharedfiles
->GetFileByID(cur_client
->GetUploadFileID()) ) {
93 cur_client
->ClearWaitStartTime();
94 RemoveFromWaitingQueue(tmp_it
);
95 if (!cur_client
->GetSocket()) {
96 if(cur_client
->Disconnected(wxT("AddUpNextClient - purged"))) {
97 cur_client
->Safe_Delete();
104 suspendlist::iterator it2
= std::find( suspended_uploads_list
.begin(),
105 suspended_uploads_list
.end(),
106 cur_client
->GetUploadFileID() );
107 if (cur_client
->IsBanned() || it2
!= suspended_uploads_list
.end() ) { // Banned client or suspended upload ?
112 uint32_t cur_score
= cur_client
->GetScore(true);
113 if (cur_score
> bestscore
) {
114 bestscore
= cur_score
;
117 cur_score
= cur_client
->GetScore(false);
118 if ((cur_score
> bestlowscore
) && !cur_client
->m_bAddNextConnect
){
119 bestlowscore
= cur_score
;
125 // Update the count on GUI if any clients were purged
127 Notify_ShowQueueCount(m_waitinglist
.size());
130 if (bestlowscore
> bestscore
){
131 newclient
= *toaddlow
;
132 newclient
->m_bAddNextConnect
= true;
135 if (toadd
== m_waitinglist
.end()) {
140 lastupslotHighID
= true; // VQB LowID alternate
141 RemoveFromWaitingQueue(toadd
);
142 Notify_ShowQueueCount(m_waitinglist
.size());
144 //prevent another potential access of a suspended upload
146 suspendlist::iterator it
= std::find( suspended_uploads_list
.begin(),
147 suspended_uploads_list
.end(),
148 directadd
->GetUploadFileID() );
149 if ( it
!= suspended_uploads_list
.end() ) {
152 newclient
= directadd
;
156 if (IsDownloading(newclient
)) {
159 // tell the client that we are now ready to upload
160 if (!newclient
->IsConnected()) {
161 newclient
->SetUploadState(US_CONNECTING
);
162 if (!newclient
->TryToConnect(true)) {
166 CPacket
* packet
= new CPacket(OP_ACCEPTUPLOADREQ
, 0, OP_EDONKEYPROT
);
167 theStats::AddUpOverheadFileRequest(packet
->GetPacketSize());
168 AddDebugLogLineM( false, logLocalClient
, wxT("Local Client: OP_ACCEPTUPLOADREQ to ") + newclient
->GetFullIP() );
169 newclient
->SendPacket(packet
,true);
170 newclient
->SetUploadState(US_UPLOADING
);
172 newclient
->SetUpStartTime();
173 newclient
->ResetSessionUp();
175 theApp
->uploadBandwidthThrottler
->AddToStandardList(m_uploadinglist
.size(), newclient
->GetSocket());
176 m_uploadinglist
.push_back(newclient
);
177 theStats::AddUploadingClient();
180 CKnownFile
* reqfile
= (CKnownFile
*) newclient
->GetUploadFile();
182 reqfile
->statistic
.AddAccepted();
184 Notify_UploadCtrlAddClient(newclient
);
187 void CUploadQueue::Process()
189 if (AcceptNewClient() && !m_waitinglist
.empty()) {
190 m_nLastStartUpload
= ::GetTickCount();
194 // The loop that feeds the upload slots with data.
195 CClientPtrList::iterator it
= m_uploadinglist
.begin();
196 while (it
!= m_uploadinglist
.end()) {
197 // Get the client. Note! Also updates pos as a side effect.
198 CUpDownClient
* cur_client
= *it
++;
200 // It seems chatting or friend slots can get stuck at times in upload.. This needs looked into..
201 if (!cur_client
->GetSocket()) {
202 RemoveFromUploadQueue(cur_client
, true);
203 if(cur_client
->Disconnected(_T("CUploadQueue::Process"))){
204 cur_client
->Safe_Delete();
207 cur_client
->SendBlockData();
211 // Save used bandwidth for speed calculations
212 uint64 sentBytes
= theApp
->uploadBandwidthThrottler
->GetNumberOfSentBytesSinceLastCallAndReset();
213 (void)theApp
->uploadBandwidthThrottler
->GetNumberOfSentBytesOverheadSinceLastCallAndReset();
217 theStats::AddSentBytes(sentBytes
);
222 bool CUploadQueue::AcceptNewClient()
224 // check if we can allow a new client to start downloading from us
225 if (::GetTickCount() - m_nLastStartUpload
< 1000 || m_uploadinglist
.size() >= MAX_UP_CLIENTS_ALLOWED
) {
229 float kBpsUpPerClient
= (float)thePrefs::GetSlotAllocation();
230 float kBpsUp
= theStats::GetUploadRate() / 1024.0f
;
231 if (thePrefs::GetMaxUpload() == UNLIMITED
) {
232 if (m_uploadinglist
.size() < ((uint32
)((kBpsUp
)/kBpsUpPerClient
)+2)) {
236 uint16 nMaxSlots
= 0;
237 if (thePrefs::GetMaxUpload() >= 10) {
238 nMaxSlots
= (uint16
)floor((float)thePrefs::GetMaxUpload() / kBpsUpPerClient
+ 0.5);
239 // floor(x + 0.5) is a way of doing round(x) that works with gcc < 3 ...
240 if (nMaxSlots
< MIN_UP_CLIENTS_ALLOWED
) {
241 nMaxSlots
=MIN_UP_CLIENTS_ALLOWED
;
244 nMaxSlots
= MIN_UP_CLIENTS_ALLOWED
;
247 if (m_uploadinglist
.size() < nMaxSlots
) {
255 CUploadQueue::~CUploadQueue()
257 wxASSERT(m_waitinglist
.empty());
258 wxASSERT(m_uploadinglist
.empty());
262 bool CUploadQueue::IsOnUploadQueue(const CUpDownClient
* client
) const
264 return std::find(m_waitinglist
.begin(), m_waitinglist
.end(), client
)
265 != m_waitinglist
.end();
269 bool CUploadQueue::IsDownloading(CUpDownClient
* client
) const
271 return std::find(m_uploadinglist
.begin(), m_uploadinglist
.end(), client
)
272 != m_uploadinglist
.end();
276 CUpDownClient
* CUploadQueue::GetWaitingClientByIP_UDP(uint32 dwIP
, uint16 nUDPPort
, bool bIgnorePortOnUniqueIP
, bool* pbMultipleIPs
)
278 CUpDownClient
* pMatchingIPClient
= NULL
;
282 CClientPtrList::iterator it
= m_waitinglist
.begin();
283 for (; it
!= m_waitinglist
.end(); ++it
) {
284 CUpDownClient
* cur_client
= *it
;
286 if ((dwIP
== cur_client
->GetIP()) && (nUDPPort
== cur_client
->GetUDPPort())) {
288 } else if ((dwIP
== cur_client
->GetIP()) && bIgnorePortOnUniqueIP
) {
289 pMatchingIPClient
= cur_client
;
295 *pbMultipleIPs
= cMatches
> 1;
298 if (pMatchingIPClient
&& cMatches
== 1) {
299 return pMatchingIPClient
;
306 void CUploadQueue::AddClientToQueue(CUpDownClient
* client
)
308 if (theApp
->serverconnect
->IsConnected() && theApp
->serverconnect
->IsLowID() && !theApp
->serverconnect
->IsLocalServer(client
->GetServerIP(),client
->GetServerPort()) && client
->GetDownloadState() == DS_NONE
&& !client
->IsFriend() && theStats::GetWaitingUserCount() > 50) {
309 // Well, all that issues finish in the same: don't allow to add to the queue
313 if ( client
->IsBanned() ) {
317 client
->AddAskedCount();
318 client
->SetLastUpRequest();
320 // Find all clients with the same user-hash
321 CClientList::SourceList found
= theApp
->clientlist
->GetClientsByHash( client
->GetUserHash() );
323 CClientList::SourceList::iterator it
= found
.begin();
324 while (it
!= found
.end()) {
325 CUpDownClient
* cur_client
= *it
++;
327 if ( IsOnUploadQueue( cur_client
) ) {
328 if ( cur_client
== client
) {
329 if ( client
->m_bAddNextConnect
&& ( ( m_uploadinglist
.size() < thePrefs::GetMaxUpload() ) || ( thePrefs::GetMaxUpload() == UNLIMITED
) ) ) {
330 if (lastupslotHighID
) {
331 client
->m_bAddNextConnect
= false;
332 RemoveFromWaitingQueue(client
, true);
333 AddUpNextClient(client
);
334 lastupslotHighID
= false; // LowID alternate
339 client
->SendRankingInfo();
340 Notify_QlistRefreshClient(client
);
344 // Hash-clash, remove unidentified clients (possibly both)
346 if ( !cur_client
->IsIdentified() ) {
347 // Cur_client isn't identifed, remove it
348 theApp
->clientlist
->AddTrackClient( cur_client
);
350 RemoveFromWaitingQueue( cur_client
);
351 if ( !cur_client
->GetSocket() ) {
352 if (cur_client
->Disconnected( wxT("AddClientToQueue - same userhash") ) ) {
353 cur_client
->Safe_Delete();
358 if ( !client
->IsIdentified() ) {
359 // New client isn't identified, remove it
360 theApp
->clientlist
->AddTrackClient( client
);
362 if ( !client
->GetSocket() ) {
363 if ( client
->Disconnected( wxT("AddClientToQueue - same userhash") ) ) {
364 client
->Safe_Delete();
374 // Count the number of clients with the same IP-address
375 found
= theApp
->clientlist
->GetClientsByIP( client
->GetIP() );
378 for ( it
= found
.begin(); it
!= found
.end(); it
++ ) {
379 if ( ( *it
== client
) || IsOnUploadQueue( *it
) ) {
384 // We do not accept more than 3 clients from the same IP
387 } else if ( theApp
->clientlist
->GetClientsFromIP(client
->GetIP()) >= 3 ) {
392 CKnownFile
* reqfile
= (CKnownFile
*) client
->GetUploadFile();
394 reqfile
->statistic
.AddRequest();
397 // TODO find better ways to cap the list
398 if (m_waitinglist
.size() >= (thePrefs::GetQueueSize())) {
402 if (client
->IsDownloading()) {
403 // he's already downloading and wants probably only another file
404 CPacket
* packet
= new CPacket(OP_ACCEPTUPLOADREQ
, 0, OP_EDONKEYPROT
);
405 theStats::AddUpOverheadFileRequest(packet
->GetPacketSize());
406 AddDebugLogLineM( false, logLocalClient
, wxT("Local Client: OP_ACCEPTUPLOADREQ to ") + client
->GetFullIP() );
407 client
->SendPacket(packet
,true);
411 if (m_waitinglist
.empty() && AcceptNewClient()) {
412 AddUpNextClient(client
);
413 m_nLastStartUpload
= ::GetTickCount();
415 m_waitinglist
.push_back(client
);
416 theStats::AddWaitingClient();
417 client
->ClearWaitStartTime();
418 client
->ClearAskedCount();
419 client
->SetUploadState(US_ONUPLOADQUEUE
);
420 client
->SendRankingInfo();
421 Notify_QlistAddClient(client
);
422 Notify_ShowQueueCount(m_waitinglist
.size());
427 bool CUploadQueue::RemoveFromUploadQueue(CUpDownClient
* client
, bool updatewindow
)
429 // Keep track of this client
430 theApp
->clientlist
->AddTrackClient(client
);
432 CClientPtrList::iterator it
= std::find(m_uploadinglist
.begin(),
433 m_uploadinglist
.end(), client
);
435 if (it
!= m_uploadinglist
.end()) {
437 Notify_UploadCtrlRemoveClient(client
);
439 m_uploadinglist
.erase(it
);
440 theStats::RemoveUploadingClient();
441 if( client
->GetTransferredUp() ) {
442 theStats::AddSuccessfulUpload();
443 theStats::AddUploadTime(client
->GetUpStartTimeDelay() / 1000);
445 theStats::AddFailedUpload();
447 client
->SetUploadState(US_NONE
);
448 client
->ClearUploadBlockRequests();
456 bool CUploadQueue::CheckForTimeOver(CUpDownClient
* client
)
458 if (thePrefs::TransferFullChunks()) {
459 if( client
->GetUpStartTimeDelay() > 3600000 ) { // Try to keep the clients from downloading forever.
462 // For some reason, some clients can continue to download after a chunk size.
463 // Are they redownloading the same chunk over and over????
464 if( client
->GetSessionUp() > 10485760 ) {
468 CClientPtrList::iterator it
= m_waitinglist
.begin();
469 for (; it
!= m_waitinglist
.end(); ++it
) {
470 if (client
->GetScore(true,true) < (*it
)->GetScore(true,false)) {
480 uint16
CUploadQueue::GetWaitingPosition(const CUpDownClient
*client
) const
482 if ( !IsOnUploadQueue(client
) ) {
487 const uint32 myscore
= client
->GetScore(false);
488 CClientPtrList::const_iterator it
= m_waitinglist
.begin();
489 for (; it
!= m_waitinglist
.end(); ++it
) {
490 if ((*it
)->GetScore(false) > myscore
) {
500 * This function removes a file indicated by filehash from suspended_uploads_list.
502 void CUploadQueue::ResumeUpload( const CMD4Hash
& filehash
)
504 //Find the position of the filehash in the list and remove it.
505 suspendlist::iterator it
= std::find( suspended_uploads_list
.begin(),
506 suspended_uploads_list
.end(),
508 if ( it
!= suspended_uploads_list
.end() )
509 suspended_uploads_list
.erase( it
);
511 AddLogLineM( false, CFormat( _("Resuming uploads of file: %s" ) )
512 % filehash
.Encode() );
516 * This function adds a file indicated by filehash to suspended_uploads_list
518 void CUploadQueue::SuspendUpload( const CMD4Hash
& filehash
)
520 AddLogLineM( false, CFormat( _("Suspending upload of file: %s" ) )
521 % filehash
.Encode() );
523 //Append the filehash to the list.
524 suspended_uploads_list
.push_back(filehash
);
525 wxString base16hash
= filehash
.Encode();
527 CClientPtrList::iterator it
= m_uploadinglist
.begin();
528 while (it
!= m_uploadinglist
.end()) {
529 CUpDownClient
*potential
= *it
++;
530 //check if the client is uploading the file we need to suspend
531 if(potential
->GetUploadFileID() == filehash
) {
532 //remove the unlucky client from the upload queue and add to the waiting queue
533 RemoveFromUploadQueue(potential
, true);
535 m_waitinglist
.push_back(potential
);
536 theStats::AddWaitingClient();
537 potential
->SetUploadState(US_ONUPLOADQUEUE
);
538 potential
->SendRankingInfo();
539 Notify_QlistRefreshClient(potential
);
540 Notify_ShowQueueCount(m_waitinglist
.size());
545 bool CUploadQueue::RemoveFromWaitingQueue(CUpDownClient
* client
, bool updatewindow
)
547 CClientPtrList::iterator it
= std::find(m_waitinglist
.begin(),
548 m_waitinglist
.end(), client
);
550 if (it
!= m_waitinglist
.end()) {
551 RemoveFromWaitingQueue(it
);
553 Notify_ShowQueueCount(m_waitinglist
.size());
562 void CUploadQueue::RemoveFromWaitingQueue(CClientPtrList::iterator pos
)
564 CUpDownClient
* todelete
= *pos
;
565 m_waitinglist
.erase(pos
);
566 theStats::RemoveWaitingClient();
567 if( todelete
->IsBanned() ) {
570 Notify_QlistRemoveClient(todelete
);
571 todelete
->SetUploadState(US_NONE
);
574 // File_checked_for_headers