Upstream tarball 10143
[amule.git] / src / DownloadClient.cpp
blob94e822fd60fd960d9a365a48e38b4fb252407a7e
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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.
20 //
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 "updownclient.h" // Interface declarations
28 #include <protocol/Protocols.h>
29 #include <protocol/ed2k/Client2Client/TCP.h>
30 #include <protocol/ed2k/Client2Client/UDP.h>
31 #include <common/EventIDs.h>
32 #include <common/Macros.h>
33 #include <common/Constants.h>
35 #include <zlib.h>
36 #include <cmath> // Needed for std:exp
38 #include "ClientCredits.h" // Needed for CClientCredits
39 #include "ClientUDPSocket.h" // Needed for CClientUDPSocket
40 #include "DownloadQueue.h" // Needed for CDownloadQueue
41 #include "Preferences.h" // Needed for thePrefs
42 #include "Packet.h" // Needed for CPacket
43 #include "MemFile.h" // Needed for CMemFile
44 #include "ClientTCPSocket.h"// Needed for CClientTCPSocket
45 #include "ListenSocket.h" // Needed for CListenSocket
46 #include "amule.h" // Needed for theApp
47 #include "PartFile.h" // Needed for CPartFile
48 #include "SharedFileList.h"
49 #include "Statistics.h" // Needed for theStats
50 #include "Logger.h"
51 #include "GuiEvents.h" // Needed for Notify_*
54 #ifdef __MULE_UNUSED_CODE__
55 // This function is left as a reminder.
56 // Changes here _must_ be reflected in CClientList::FindMatchingClient.
57 bool CUpDownClient::Compare(const CUpDownClient* tocomp, bool bIgnoreUserhash) const
59 if (!tocomp) {
60 // should we wxASSERT here?
61 return false;
64 //Compare only the user hash..
65 if(!bIgnoreUserhash && HasValidHash() && tocomp->HasValidHash()) {
66 return GetUserHash() == tocomp->GetUserHash();
69 if (HasLowID()) {
70 //User is firewalled.. Must do two checks..
71 if (GetIP()!=0 && GetIP() == tocomp->GetIP()) {
72 //The IP of both match
73 if (GetUserPort()!=0 && GetUserPort() == tocomp->GetUserPort()) {
74 //IP-UserPort matches
75 return true;
77 if (GetKadPort()!=0 && GetKadPort() == tocomp->GetKadPort()) {
78 //IP-KadPort Matches
79 return true;
83 if (GetUserIDHybrid()!=0
84 && GetUserIDHybrid() == tocomp->GetUserIDHybrid()
85 && GetServerIP()!=0
86 && GetServerIP() == tocomp->GetServerIP()
87 && GetServerPort()!=0
88 && GetServerPort() == tocomp->GetServerPort()) {
89 //Both have the same lowID, Same serverIP and Port..
90 return true;
93 //Both IP, and Server do not match..
94 return false;
97 //User is not firewalled.
98 if (GetUserPort()!=0) {
99 //User has a Port, lets check the rest.
100 if (GetIP() != 0 && tocomp->GetIP() != 0) {
101 //Both clients have a verified IP..
102 if(GetIP() == tocomp->GetIP() && GetUserPort() == tocomp->GetUserPort()) {
103 //IP and UserPort match..
104 return true;
106 } else {
107 //One of the two clients do not have a verified IP
108 if (GetUserIDHybrid() == tocomp->GetUserIDHybrid() && GetUserPort() == tocomp->GetUserPort()) {
109 //ID and Port Match..
110 return true;
115 if(GetKadPort()!=0) {
116 //User has a Kad Port.
117 if(GetIP() != 0 && tocomp->GetIP() != 0) {
118 //Both clients have a verified IP.
119 if(GetIP() == tocomp->GetIP() && GetKadPort() == tocomp->GetKadPort()) {
120 //IP and KadPort Match..
121 return true;
123 } else {
124 //One of the users do not have a verified IP.
125 if (GetUserIDHybrid() == tocomp->GetUserIDHybrid() && GetKadPort() == tocomp->GetKadPort()) {
126 //ID and KadProt Match..
127 return true;
132 //No Matches..
133 return false;
135 #endif
138 bool CUpDownClient::AskForDownload()
140 // 0.42e
141 if (theApp->listensocket->TooManySockets()) {
142 if (!m_socket) {
143 if (GetDownloadState() != DS_TOOMANYCONNS) {
144 SetDownloadState(DS_TOOMANYCONNS);
146 return true;
147 } else if (!m_socket->IsConnected()) {
148 if (GetDownloadState() != DS_TOOMANYCONNS) {
149 SetDownloadState(DS_TOOMANYCONNS);
151 return true;
154 m_bUDPPending = false;
155 m_dwLastAskedTime = ::GetTickCount();
156 SetDownloadState(DS_CONNECTING);
157 SetSentCancelTransfer(0);
158 return TryToConnect();
162 void CUpDownClient::SendStartupLoadReq()
164 // 0.42e
165 if (m_socket==NULL || m_reqfile==NULL) {
166 return;
168 SetDownloadState(DS_ONQUEUE);
169 CMemFile dataStartupLoadReq(16);
170 dataStartupLoadReq.WriteHash(m_reqfile->GetFileHash());
171 CPacket* packet = new CPacket(dataStartupLoadReq, OP_EDONKEYPROT, OP_STARTUPLOADREQ);
172 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
173 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_STARTUPLOADREQ to ") + GetFullIP());
174 SendPacket(packet, true, true);
178 bool CUpDownClient::IsSourceRequestAllowed()
180 //#warning REWRITE - Source swapping from eMule.
181 // 0.42e
182 uint32 dwTickCount = ::GetTickCount() + CONNECTION_LATENCY;
183 uint32 nTimePassedClient = dwTickCount - GetLastSrcAnswerTime();
184 uint32 nTimePassedFile = dwTickCount - m_reqfile->GetLastAnsweredTime();
185 bool bNeverAskedBefore = (GetLastAskedForSources() == 0);
187 uint32 uSources = m_reqfile->GetSourceCount();
188 return (
189 // if client has the correct extended protocol
190 ExtProtocolAvailable() && (SupportsSourceExchange2() || GetSourceExchange1Version() > 1) &&
191 // AND if we need more sources
192 thePrefs::GetMaxSourcePerFileSoft() > uSources &&
193 // AND if...
195 //source is not complete and file is very rare
196 ( !m_bCompleteSource
197 && (bNeverAskedBefore || nTimePassedClient > SOURCECLIENTREASKS)
198 && (uSources <= RARE_FILE/5)
199 ) ||
200 //source is not complete and file is rare
201 ( !m_bCompleteSource
202 && (bNeverAskedBefore || nTimePassedClient > SOURCECLIENTREASKS)
203 && (uSources <= RARE_FILE || uSources - m_reqfile->GetValidSourcesCount() <= RARE_FILE / 2)
204 && (nTimePassedFile > SOURCECLIENTREASKF)
205 ) ||
206 // OR if file is not rare
207 ( (bNeverAskedBefore || nTimePassedClient > (unsigned)(SOURCECLIENTREASKS * MINCOMMONPENALTY))
208 && (nTimePassedFile > (unsigned)(SOURCECLIENTREASKF * MINCOMMONPENALTY))
215 void CUpDownClient::SendFileRequest()
217 wxCHECK_RET(m_reqfile, wxT("Cannot request file when no reqfile is set"));
219 CMemFile dataFileReq(16+16);
220 dataFileReq.WriteHash(m_reqfile->GetFileHash());
222 if (SupportMultiPacket()) {
223 wxString sent_opcodes;
225 if (SupportExtMultiPacket()) {
226 dataFileReq.WriteUInt64(m_reqfile->GetFileSize());
229 AddDebugLogLineM(false, logClient, wxT("Sending file request to client"));
231 dataFileReq.WriteUInt8(OP_REQUESTFILENAME);
232 sent_opcodes += wxT("|RFNM|");
233 // Extended information
234 if (GetExtendedRequestsVersion() > 0) {
235 m_reqfile->WritePartStatus(&dataFileReq);
237 if (GetExtendedRequestsVersion() > 1) {
238 m_reqfile->WriteCompleteSourcesCount(&dataFileReq);
240 if (m_reqfile->GetPartCount() > 1) {
241 sent_opcodes += wxT("|RFID|");
242 dataFileReq.WriteUInt8(OP_SETREQFILEID);
244 if (IsEmuleClient()) {
245 SetRemoteQueueFull( true );
246 SetRemoteQueueRank(0);
248 if (IsSourceRequestAllowed()) {
249 if (SupportsSourceExchange2()){
250 sent_opcodes += wxT("|RSRC2|");
251 dataFileReq.WriteUInt8(OP_REQUESTSOURCES2);
252 dataFileReq.WriteUInt8(SOURCEEXCHANGE2_VERSION);
253 const uint16 nOptions = 0; // 16 ... Reserved
254 dataFileReq.WriteUInt16(nOptions);
255 } else{
256 sent_opcodes += wxT("|RSRC|");
257 dataFileReq.WriteUInt8(OP_REQUESTSOURCES);
259 m_reqfile->SetLastAnsweredTimeTimeout();
260 SetLastAskedForSources();
262 if (IsSupportingAICH()) {
263 sent_opcodes += wxT("|AFHR|");
264 dataFileReq.WriteUInt8(OP_AICHFILEHASHREQ);
266 CPacket* packet = new CPacket(dataFileReq, OP_EMULEPROT, (SupportExtMultiPacket() ? OP_MULTIPACKET_EXT : OP_MULTIPACKET));
267 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
268 AddDebugLogLineM( false, logLocalClient, wxString::Format(wxT("Local Client: %s "), (SupportExtMultiPacket() ? wxT("OP_MULTIPACKET_EXT (") : wxT("OP_MULTIPACKET (") )) + sent_opcodes + wxT(") to ") + GetFullIP());
269 SendPacket(packet, true);
270 } else {
271 //This is extended information
272 if (GetExtendedRequestsVersion() > 0 ) {
273 m_reqfile->WritePartStatus(&dataFileReq);
275 if (GetExtendedRequestsVersion() > 1 ) {
276 m_reqfile->WriteCompleteSourcesCount(&dataFileReq);
278 CPacket* packet = new CPacket(dataFileReq, OP_EDONKEYPROT, OP_REQUESTFILENAME);
279 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
280 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_REQUESTFILENAME to ") + GetFullIP() );
281 SendPacket(packet, true);
283 // 26-Jul-2003: removed requesting the file status for files <= PARTSIZE for better compatibility with ed2k protocol (eDonkeyHybrid).
284 // if the remote client answers the OP_REQUESTFILENAME with OP_REQFILENAMEANSWER the file is shared by the remote client. if we
285 // know that the file is shared, we know also that the file is complete and don't need to request the file status.
287 // Sending the packet could have deleted the client, check m_reqfile
288 if (m_reqfile && (m_reqfile->GetPartCount() > 1)) {
289 CMemFile dataSetReqFileID(16);
290 dataSetReqFileID.WriteHash(m_reqfile->GetFileHash());
291 packet = new CPacket(dataSetReqFileID, OP_EDONKEYPROT, OP_SETREQFILEID);
292 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
293 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_SETREQFILEID to ") + GetFullIP());
294 SendPacket(packet, true);
297 if (IsEmuleClient()) {
298 SetRemoteQueueFull( true );
299 SetRemoteQueueRank(0);
302 // Sending the packet could have deleted the client, check m_reqfile
303 if (m_reqfile && IsSourceRequestAllowed()) {
304 m_reqfile->SetLastAnsweredTimeTimeout();
306 CMemFile packetdata;
308 if (SupportsSourceExchange2()) {
309 packetdata.WriteUInt8(SOURCEEXCHANGE2_VERSION);
310 packetdata.WriteUInt16(0 /* Reserved */);
313 packetdata.WriteHash(m_reqfile->GetFileHash());
315 packet = new CPacket(packetdata, OP_EMULEPROT, SupportsSourceExchange2() ? OP_REQUESTSOURCES2 : OP_REQUESTSOURCES);
317 theStats::AddUpOverheadSourceExchange(packet->GetPacketSize());
318 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_REQUESTSOURCES to ") + GetFullIP() );
319 SendPacket(packet,true,true);
320 SetLastAskedForSources();
323 // Sending the packet could have deleted the client, check m_reqfile
324 if (m_reqfile && IsSupportingAICH()) {
325 packet = new CPacket(OP_AICHFILEHASHREQ,16,OP_EMULEPROT);
326 packet->Copy16ToDataBuffer((const char *)m_reqfile->GetFileHash().GetHash());
327 theStats::AddUpOverheadOther(packet->GetPacketSize());
328 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_AICHFILEHASHREQ to ") + GetFullIP());
329 SendPacket(packet,true,true);
335 void CUpDownClient::ProcessFileInfo(const CMemFile* data, const CPartFile* file)
337 // 0.42e
338 if (file==NULL) {
339 throw wxString(wxT("ERROR: Wrong file ID (ProcessFileInfo; file==NULL)"));
341 if (m_reqfile==NULL) {
342 throw wxString(wxT("ERROR: Wrong file ID (ProcessFileInfo; m_reqfile==NULL)"));
344 if (file != m_reqfile) {
345 throw wxString(wxT("ERROR: Wrong file ID (ProcessFileInfo; m_reqfile!=file)"));
348 m_clientFilename = data->ReadString((GetUnicodeSupport() != utf8strNone));
350 // 26-Jul-2003: removed requesting the file status for files <= PARTSIZE for better compatibility with ed2k protocol (eDonkeyHybrid).
351 // if the remote client answers the OP_REQUESTFILENAME with OP_REQFILENAMEANSWER the file is shared by the remote client. if we
352 // know that the file is shared, we know also that the file is complete and don't need to request the file status.
353 if (m_reqfile->GetPartCount() == 1) {
354 m_nPartCount = m_reqfile->GetPartCount();
356 m_reqfile->UpdatePartsFrequency( this, false ); // Decrement
357 m_downPartStatus.clear();
358 m_downPartStatus.resize( m_nPartCount, 1 );
359 m_reqfile->UpdatePartsFrequency( this, true ); // Increment
361 m_bCompleteSource = true;
363 UpdateDisplayedInfo();
364 // even if the file is <= PARTSIZE, we _may_ need the hashset for that file (if the file size == PARTSIZE)
365 if (m_reqfile->IsHashSetNeeded()) {
366 if (m_socket) {
367 CPacket* packet = new CPacket(OP_HASHSETREQUEST,16, OP_EDONKEYPROT);
368 packet->Copy16ToDataBuffer((const char *)m_reqfile->GetFileHash().GetHash());
369 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
370 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_HASHSETREQUEST to ") + GetFullIP());
371 SendPacket(packet,true,true);
372 SetDownloadState(DS_REQHASHSET);
373 m_fHashsetRequesting = 1;
374 m_reqfile->SetHashSetNeeded(false);
375 } else {
376 wxFAIL;
378 } else {
379 SendStartupLoadReq();
381 m_reqfile->UpdatePartsInfo();
385 void CUpDownClient::ProcessFileStatus(bool bUdpPacket, const CMemFile* data, const CPartFile* file)
387 // 0.42e
388 wxString strReqFileNull(wxT("ERROR: Wrong file ID (ProcessFileStatus; m_reqfile==NULL)"));
390 if ( !m_reqfile || file != m_reqfile ){
391 if (!m_reqfile) {
392 throw strReqFileNull;
394 throw wxString(wxT("ERROR: Wrong file ID (ProcessFileStatus; m_reqfile!=file)"));
397 uint16 nED2KPartCount = data->ReadUInt16();
399 m_reqfile->UpdatePartsFrequency( this, false ); // Decrement
400 m_downPartStatus.clear();
402 bool bPartsNeeded = false;
403 int iNeeded = 0;
404 if (!nED2KPartCount)
406 m_nPartCount = m_reqfile->GetPartCount();
407 m_downPartStatus.resize( m_nPartCount, 1);
408 bPartsNeeded = true;
409 m_bCompleteSource = true;
411 else
413 // Somehow this happened.
414 if (!m_reqfile) {
415 throw strReqFileNull;
417 if (m_reqfile->GetED2KPartCount() != nED2KPartCount)
419 wxString strError;
420 strError << wxT("ProcessFileStatus - wrong part number recv=") << nED2KPartCount <<
421 wxT(" expected=") << m_reqfile->GetED2KPartCount() << wxT(" ") <<
422 m_reqfile->GetFileHash().Encode();
423 m_nPartCount = 0;
424 throw strError;
426 m_nPartCount = m_reqfile->GetPartCount();
428 m_bCompleteSource = false;
429 m_downPartStatus.resize( m_nPartCount, 0 );
430 uint16 done = 0;
432 try {
433 while (done != m_nPartCount) {
434 uint8 toread = data->ReadUInt8();
436 for ( uint8 i = 0;i < 8; i++ ) {
437 m_downPartStatus[done] = ((toread>>i)&1)? 1:0;
439 if ( m_downPartStatus[done] ) {
440 if (!m_reqfile->IsComplete(done)){
441 bPartsNeeded = true;
442 iNeeded++;
445 done++;
446 if (done == m_nPartCount) {
447 break;
451 } catch( ... ) {
452 // We want the counts to be updated, even if we fail to read everything
453 m_reqfile->UpdatePartsFrequency( this, true ); // Increment
455 throw;
459 m_reqfile->UpdatePartsFrequency( this, true ); // Increment
461 UpdateDisplayedInfo();
463 // NOTE: This function is invoked from TCP and UDP socket!
464 if (!bUdpPacket) {
465 if (!bPartsNeeded) {
466 SetDownloadState(DS_NONEEDEDPARTS);
467 } else if (m_reqfile->IsHashSetNeeded()) {
468 //If we are using the eMule filerequest packets, this is taken care of in the Multipacket!
469 if (m_socket) {
470 CPacket* packet = new CPacket(OP_HASHSETREQUEST,16, OP_EDONKEYPROT);
471 packet->Copy16ToDataBuffer((const char *)m_reqfile->GetFileHash().GetHash());
472 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
473 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_HASHSETREQUEST to ") + GetFullIP());
474 SendPacket(packet, true, true);
475 SetDownloadState(DS_REQHASHSET);
476 m_fHashsetRequesting = 1;
477 m_reqfile->SetHashSetNeeded(false);
478 } else {
479 wxFAIL;
482 else {
483 SendStartupLoadReq();
486 else {
487 if (!bPartsNeeded) {
488 SetDownloadState(DS_NONEEDEDPARTS);
489 } else {
490 SetDownloadState(DS_ONQUEUE);
493 m_reqfile->UpdatePartsInfo();
496 bool CUpDownClient::AddRequestForAnotherFile(CPartFile* file)
498 if ( m_A4AF_list.find( file ) == m_A4AF_list.end() ) {
499 // When we access a non-existing entry entry, it will be zeroed by default,
500 // so we have to set NeededParts. All in one go.
501 m_A4AF_list[file].NeededParts = true;
502 file->AddA4AFSource( this );
503 return true;
504 } else {
505 return false;
509 bool CUpDownClient::DeleteFileRequest(CPartFile* file)
511 return (m_A4AF_list.erase( file ) > 0);
514 void CUpDownClient::DeleteAllFileRequests()
516 m_A4AF_list.clear();
520 /* eMule 0.30c implementation, i give it a try (Creteil) BEGIN ... */
521 void CUpDownClient::SetDownloadState(uint8 byNewState)
523 if (m_nDownloadState != byNewState) {
524 if (m_reqfile) {
525 // Notify the client that this source has changed its state
526 m_reqfile->ClientStateChanged( m_nDownloadState, byNewState );
528 if (byNewState == DS_DOWNLOADING) {
529 m_reqfile->AddDownloadingSource(this);
530 } else if (m_nDownloadState == DS_DOWNLOADING) {
531 m_reqfile->RemoveDownloadingSource(this);
534 if (byNewState == DS_DOWNLOADING) {
535 msReceivedPrev = GetTickCount();
536 theStats::AddDownloadingSource();
537 } else if (m_nDownloadState == DS_DOWNLOADING) {
538 theStats::RemoveDownloadingSource();
541 if (m_nDownloadState == DS_DOWNLOADING) {
542 m_nDownloadState = byNewState;
543 ClearDownloadBlockRequests();
545 kBpsDown = 0.0;
546 bytesReceivedCycle = 0;
547 msReceivedPrev = 0;
548 if (byNewState == DS_NONE) {
549 if (m_reqfile) {
550 m_reqfile->UpdatePartsFrequency( this, false ); // Decrement
552 m_downPartStatus.clear();
553 m_nPartCount = 0;
555 if (m_socket && byNewState != DS_ERROR) {
556 m_socket->DisableDownloadLimit();
559 m_nDownloadState = byNewState;
560 if(GetDownloadState() == DS_DOWNLOADING) {
561 if (IsEmuleClient()) {
562 SetRemoteQueueFull(false);
564 SetRemoteQueueRank(0); // eMule 0.30c set like this ...
566 UpdateDisplayedInfo(true);
569 /* eMule 0.30c implementation, i give it a try (Creteil) END ... */
571 void CUpDownClient::ProcessHashSet(const byte* packet, uint32 size)
573 if ((!m_reqfile) || md4cmp(packet,m_reqfile->GetFileHash().GetHash())) {
574 throw wxString(wxT("Wrong fileid sent (ProcessHashSet)"));
576 if (!m_fHashsetRequesting) {
577 throw wxString(wxT("Received unsolicited hashset, ignoring it."));
579 CMemFile data(packet,size);
580 if (m_reqfile->LoadHashsetFromFile(&data,true)) {
581 m_fHashsetRequesting = 0;
582 } else {
583 m_reqfile->SetHashSetNeeded(true);
584 throw wxString(wxT("Corrupted or invalid hashset received"));
586 SendStartupLoadReq();
589 void CUpDownClient::SendBlockRequests()
591 uint32 current_time = ::GetTickCount();
592 if (GetVBTTags()) {
594 // Ask new blocks only when all completed
595 if (m_PendingBlocks_list.size()) {
596 return;
599 if ((m_dwLastBlockReceived + SEC2MS(5)) > current_time) {
600 // We received last block in less than 5 secs? Let's request faster.
601 m_MaxBlockRequests = m_MaxBlockRequests << 1;
602 if ( m_MaxBlockRequests > 0x20) {
603 m_MaxBlockRequests = 0x20;
605 } else {
606 m_MaxBlockRequests = m_MaxBlockRequests >> 1;
607 if ( m_MaxBlockRequests < STANDARD_BLOCKS_REQUEST) {
608 m_MaxBlockRequests = STANDARD_BLOCKS_REQUEST;
613 m_dwLastBlockReceived = current_time;
615 if (!m_reqfile) {
616 return;
619 uint8 version = GetVBTTags() ? 2 : 1;
621 if (m_DownloadBlocks_list.empty()) {
622 // Barry - instead of getting 3, just get how many is needed
623 uint16 count = m_MaxBlockRequests - m_PendingBlocks_list.size();
624 std::vector<Requested_Block_Struct*> toadd;
625 if (m_reqfile->GetNextRequestedBlock(this, toadd, count)) {
626 for (int i = 0; i != count; i++) {
627 m_DownloadBlocks_list.push_back(toadd[i]);
632 // Barry - Why are unfinished blocks requested again, not just new ones?
634 while (m_PendingBlocks_list.size() < m_MaxBlockRequests && !m_DownloadBlocks_list.empty()) {
635 Pending_Block_Struct* pblock = new Pending_Block_Struct;
636 pblock->block = m_DownloadBlocks_list.front();
637 pblock->zStream = NULL;
638 pblock->totalUnzipped = 0;
639 pblock->fZStreamError = 0;
640 pblock->fRecovered = 0;
641 m_PendingBlocks_list.push_back(pblock);
642 m_DownloadBlocks_list.pop_front();
646 if (m_PendingBlocks_list.empty()) {
648 CUpDownClient* slower_client = NULL;
650 if (thePrefs::GetDropSlowSources()) {
651 slower_client = m_reqfile->GetSlowerDownloadingClient(m_lastaverage, this);
654 if (slower_client == NULL) {
655 slower_client = this;
658 if (!slower_client->GetSentCancelTransfer()) {
659 CPacket* packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
660 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
661 if (slower_client != this) {
662 // printf("Dropped client %p to allow client %p to download\n",slower_client, this);
664 slower_client->ClearDownloadBlockRequests();
665 slower_client->SendPacket(packet,true,true);
666 slower_client->SetSentCancelTransfer(1);
669 slower_client->SetDownloadState(DS_NONEEDEDPARTS);
671 if (slower_client != this) {
672 // Re-request freed blocks.
673 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_CANCELTRANSFER (faster source eager to transfer) to ") + slower_client->GetFullIP() );
674 wxASSERT(m_DownloadBlocks_list.empty());
675 wxASSERT(m_PendingBlocks_list.empty());
676 uint16 count = m_MaxBlockRequests;
677 std::vector<Requested_Block_Struct*> toadd;
678 if (m_reqfile->GetNextRequestedBlock(this, toadd, count)) {
679 for (int i = 0; i != count; i++) {
680 Pending_Block_Struct* pblock = new Pending_Block_Struct;
681 pblock->block = toadd[i];
682 pblock->zStream = NULL;
683 pblock->totalUnzipped = 0;
684 pblock->fZStreamError = 0;
685 pblock->fRecovered = 0;
686 m_PendingBlocks_list.push_back(pblock);
688 } else {
689 // WTF, we just freed blocks.
690 wxFAIL;
691 return;
693 } else {
694 // Drop this one.
695 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_CANCELTRANSFER (no free blocks) to ") + GetFullIP() );
696 //#warning Kry - Would be nice to swap A4AF here.
697 return;
701 CPacket* packet = NULL;
703 switch (version) {
704 case 2: {
705 // ED2Kv2 packet...
706 // Most common scenario: hash + blocks to request + every one
707 // having 2 uint32 tags
709 uint8 nBlocks = m_PendingBlocks_list.size();
710 if (nBlocks > m_MaxBlockRequests) {
711 nBlocks = m_MaxBlockRequests;
714 CMemFile data(16 + 1 + nBlocks*((2+4)*2));
716 data.WriteHash(m_reqfile->GetFileHash());
718 data.WriteUInt8(nBlocks);
720 std::list<Pending_Block_Struct*>::iterator it = m_PendingBlocks_list.begin();
721 while (nBlocks) {
722 wxASSERT(it != m_PendingBlocks_list.end());
723 wxASSERT( (*it)->block->StartOffset <= (*it)->block->EndOffset );
724 (*it)->fZStreamError = 0;
725 (*it)->fRecovered = 0;
726 CTagVarInt(/*Noname*/0,(*it)->block->StartOffset).WriteTagToFile(&data);
727 CTagVarInt(/*Noname*/0,(*it)->block->EndOffset).WriteTagToFile(&data);
728 ++it;
729 nBlocks--;
732 packet = new CPacket(data, OP_ED2KV2HEADER, OP_REQUESTPARTS);
733 AddDebugLogLineM( false, logLocalClient, CFormat(wxT("Local Client ED2Kv2: OP_REQUESTPARTS(%i) to %s"))
734 % (m_PendingBlocks_list.size()<m_MaxBlockRequests ? m_PendingBlocks_list.size() : m_MaxBlockRequests) % GetFullIP() );
736 break;
738 case 1: {
739 wxASSERT(m_MaxBlockRequests == STANDARD_BLOCKS_REQUEST);
741 //#warning Kry - I dont specially like this approach, we iterate one time too many
743 bool bHasLongBlocks = false;
745 std::list<Pending_Block_Struct*>::iterator it = m_PendingBlocks_list.begin();
746 for (uint32 i = 0; i != m_MaxBlockRequests; i++){
747 if (it != m_PendingBlocks_list.end()) {
748 Pending_Block_Struct* pending = *it++;
749 wxASSERT( pending->block->StartOffset <= pending->block->EndOffset );
750 if (pending->block->StartOffset > 0xFFFFFFFF || pending->block->EndOffset > 0xFFFFFFFF){
751 bHasLongBlocks = true;
752 if (!SupportsLargeFiles()){
753 // Requesting a large block from a client that doesn't support large files?
754 wxFAIL;
755 if (!GetSentCancelTransfer()){
756 CPacket* cancel_packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
757 theStats::AddUpOverheadFileRequest(cancel_packet->GetPacketSize());
758 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_CANCELTRANSFER to ") + GetFullIP() );
759 SendPacket(cancel_packet,true,true);
760 SetSentCancelTransfer(1);
762 SetDownloadState(DS_ERROR);
764 break;
769 CMemFile data(16 /*Hash*/ + (m_MaxBlockRequests*(bHasLongBlocks ? 8 : 4) /* uint32/64 start*/) + (3*(bHasLongBlocks ? 8 : 4)/* uint32/64 end*/));
770 data.WriteHash(m_reqfile->GetFileHash());
772 it = m_PendingBlocks_list.begin();
773 for (uint32 i = 0; i != m_MaxBlockRequests; i++) {
774 if (it != m_PendingBlocks_list.end()) {
775 Pending_Block_Struct* pending = *it++;
776 wxASSERT( pending->block->StartOffset <= pending->block->EndOffset );
777 pending->fZStreamError = 0;
778 pending->fRecovered = 0;
779 if (bHasLongBlocks) {
780 data.WriteUInt64(pending->block->StartOffset);
781 } else {
782 data.WriteUInt32(pending->block->StartOffset);
784 } else {
785 if (bHasLongBlocks) {
786 data.WriteUInt64(0);
787 } else {
788 data.WriteUInt32(0);
793 it = m_PendingBlocks_list.begin();
794 for (uint32 i = 0; i != m_MaxBlockRequests; i++) {
795 if (it != m_PendingBlocks_list.end()) {
796 Requested_Block_Struct* block = (*it++)->block;
797 if (bHasLongBlocks) {
798 data.WriteUInt64(block->EndOffset+1);
799 } else {
800 data.WriteUInt32(block->EndOffset+1);
802 } else {
803 if (bHasLongBlocks) {
804 data.WriteUInt64(0);
805 } else {
806 data.WriteUInt32(0);
810 packet = new CPacket(data, (bHasLongBlocks ? OP_EMULEPROT : OP_EDONKEYPROT), (bHasLongBlocks ? (uint8)OP_REQUESTPARTS_I64 : (uint8)OP_REQUESTPARTS));
811 AddDebugLogLineM( false, logLocalClient, wxString::Format(wxT("Local Client: %s to "),(bHasLongBlocks ? wxT("OP_REQUESTPARTS_I64") : wxT("OP_REQUESTPARTS"))) + GetFullIP() );
812 break;
814 default:
815 wxFAIL;
818 if (packet) {
819 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
820 SendPacket(packet, true, true);
821 } else {
822 wxFAIL;
827 Barry - Originally this only wrote to disk when a full 180k block
828 had been received from a client, and only asked for data in
829 180k blocks.
831 This meant that on average 90k was lost for every connection
832 to a client data source. That is a lot of wasted data.
834 To reduce the lost data, packets are now written to a buffer
835 and flushed to disk regularly regardless of size downloaded.
837 This includes compressed packets.
839 Data is also requested only where gaps are, not in 180k blocks.
840 The requests will still not exceed 180k, but may be smaller to
841 fill a gap.
844 void CUpDownClient::ProcessBlockPacket(const byte* packet, uint32 size, bool packed, bool largeblocks)
846 // Ignore if no data required
847 if (!(GetDownloadState() == DS_DOWNLOADING || GetDownloadState() == DS_NONEEDEDPARTS)) {
848 return;
851 // This vars are defined here to be able to use them on the catch
852 int header_size = 16;
853 uint64 nStartPos = 0;
854 uint64 nEndPos = 0;
855 uint32 nBlockSize = 0;
856 uint32 lenUnzipped = 0;
858 // Update stats
859 m_dwLastBlockReceived = ::GetTickCount();
861 try {
863 // Read data from packet
864 const CMemFile data(packet, size);
866 // Check that this data is for the correct file
867 if ((!m_reqfile) || data.ReadHash() != m_reqfile->GetFileHash()) {
868 throw wxString(wxT("Wrong fileid sent (ProcessBlockPacket)"));
871 // Find the start & end positions, and size of this chunk of data
873 if (largeblocks) {
874 nStartPos = data.ReadUInt64();
875 header_size += 8;
876 } else {
877 nStartPos = data.ReadUInt32();
878 header_size += 4;
881 if (packed) {
882 nBlockSize = data.ReadUInt32();
883 header_size += 4;
884 nEndPos = nStartPos + (size - header_size);
885 } else {
886 if (largeblocks) {
887 nEndPos = data.ReadUInt64();
888 header_size += 8;
889 } else {
890 nEndPos = data.ReadUInt32();
891 header_size += 4;
895 // Check that packet size matches the declared data size + header size
896 if ( nEndPos == nStartPos || size != ((nEndPos - nStartPos) + header_size)) {
897 throw wxString(wxT("Corrupted or invalid DataBlock received (ProcessBlockPacket)"));
899 theStats::AddDownloadFromSoft(GetClientSoft(),size - header_size);
900 bytesReceivedCycle += size - header_size;
902 credits->AddDownloaded(size - header_size, GetIP(), theApp->CryptoAvailable());
904 // Move end back one, should be inclusive
905 nEndPos--;
907 // Loop through to find the reserved block that this is within
908 std::list<Pending_Block_Struct*>::iterator it = m_PendingBlocks_list.begin();
909 for (; it != m_PendingBlocks_list.end(); ++it) {
910 Pending_Block_Struct* cur_block = *it;
912 if ((cur_block->block->StartOffset <= nStartPos) && (cur_block->block->EndOffset >= nStartPos)) {
913 // Found reserved block
915 if (cur_block->block->StartOffset == nStartPos) {
916 // This block just started transfering. Set the start time.
917 m_last_block_start = ::GetTickCountFullRes();
920 if (cur_block->fZStreamError){
921 AddDebugLogLineM( false, logZLib,
922 CFormat(wxT("Ignoring %u bytes of block %u-%u because of erroneous zstream state for file: %s"))
923 % (size - header_size) % nStartPos % nEndPos % m_reqfile->GetFileName());
924 m_reqfile->RemoveBlockFromList(cur_block->block->StartOffset, cur_block->block->EndOffset);
925 return;
928 // Remember this start pos, used to draw part downloading in list
929 m_nLastBlockOffset = nStartPos;
931 // Occasionally packets are duplicated, no point writing it twice
932 // This will be 0 in these cases, or the length written otherwise
933 uint32 lenWritten = 0;
935 // Handle differently depending on whether packed or not
936 if (!packed) {
937 // security sanitize check
938 if (nEndPos > cur_block->block->EndOffset) {
939 AddDebugLogLineM(false, logRemoteClient, CFormat(wxT("Received Blockpacket exceeds requested boundaries (requested end: %u, Part: %u, received end: %u, Part: %u), file: %s remote IP: %s")) % cur_block->block->EndOffset % (uint32)(cur_block->block->EndOffset / PARTSIZE) % nEndPos % (uint32)(nEndPos / PARTSIZE) % m_reqfile->GetFileName() % Uint32toStringIP(GetIP()));
940 m_reqfile->RemoveBlockFromList(cur_block->block->StartOffset, cur_block->block->EndOffset);
941 return;
943 // Write to disk (will be buffered in part file class)
944 lenWritten = m_reqfile->WriteToBuffer( size - header_size, (byte*)(packet + header_size), nStartPos, nEndPos, cur_block->block, this);
945 } else {
946 // Packed
947 wxASSERT( (long int)size > 0 );
948 // Create space to store unzipped data, the size is
949 // only an initial guess, will be resized in unzip()
950 // if not big enough
951 lenUnzipped = (size * 2);
952 // Don't get too big
953 if (lenUnzipped > (BLOCKSIZE + 300)) {
954 lenUnzipped = (BLOCKSIZE + 300);
956 byte *unzipped = new byte[lenUnzipped];
958 // Try to unzip the packet
959 int result = unzip(cur_block, (byte*)(packet + header_size), (size - header_size), &unzipped, &lenUnzipped);
961 // no block can be uncompressed to >2GB, 'lenUnzipped' is obviously erroneous.
962 if (result == Z_OK && ((int)lenUnzipped >= 0)) {
964 // Write any unzipped data to disk
965 if (lenUnzipped > 0) {
966 wxASSERT( (int)lenUnzipped > 0 );
968 // Use the current start and end positions for the uncompressed data
969 nStartPos = cur_block->block->StartOffset + cur_block->totalUnzipped - lenUnzipped;
970 nEndPos = cur_block->block->StartOffset + cur_block->totalUnzipped - 1;
972 if (nStartPos > cur_block->block->EndOffset || nEndPos > cur_block->block->EndOffset) {
973 AddDebugLogLineM( false, logZLib,
974 CFormat(wxT("Corrupted compressed packet for '%s' received (error 666)")) % m_reqfile->GetFileName());
975 m_reqfile->RemoveBlockFromList(cur_block->block->StartOffset, cur_block->block->EndOffset);
976 } else {
977 // Write uncompressed data to file
978 lenWritten = m_reqfile->WriteToBuffer( size - header_size,
979 unzipped,
980 nStartPos,
981 nEndPos,
982 cur_block->block,
983 this);
986 } else {
987 wxString strZipError;
988 if (cur_block->zStream && cur_block->zStream->msg) {
989 strZipError = wxT(" - ") + wxString::FromAscii(cur_block->zStream->msg);
992 AddDebugLogLineM( false, logZLib,
993 CFormat(wxT("Corrupted compressed packet for '%s' received (error %i): %s"))
994 % m_reqfile->GetFileName() % result % strZipError);
996 m_reqfile->RemoveBlockFromList(cur_block->block->StartOffset, cur_block->block->EndOffset);
998 // If we had an zstream error, there is no chance that we could recover from it nor that we
999 // could use the current zstream (which is in error state) any longer.
1000 if (cur_block->zStream){
1001 inflateEnd(cur_block->zStream);
1002 delete cur_block->zStream;
1003 cur_block->zStream = NULL;
1006 // Although we can't further use the current zstream, there is no need to disconnect the sending
1007 // client because the next zstream (a series of 10K-blocks which build a 180K-block) could be
1008 // valid again. Just ignore all further blocks for the current zstream.
1009 cur_block->fZStreamError = 1;
1010 cur_block->totalUnzipped = 0; // bluecow's fix
1012 delete [] unzipped;
1014 // These checks only need to be done if any data was written
1015 if (lenWritten > 0) {
1016 m_nTransferredDown += lenWritten;
1018 // If finished reserved block
1019 if (nEndPos == cur_block->block->EndOffset) {
1021 // Save last average speed based on data and time.
1022 // This should do bytes/sec.
1023 uint32 average_time = (::GetTickCountFullRes() - m_last_block_start);
1025 // Avoid divide by 0.
1026 if (average_time == 0) {
1027 average_time++;
1030 m_lastaverage = ((cur_block->block->EndOffset - cur_block->block->StartOffset) * 1000) / average_time;
1032 m_reqfile->RemoveBlockFromList(cur_block->block->StartOffset, cur_block->block->EndOffset);
1033 delete cur_block->block;
1034 // Not always allocated
1035 if (cur_block->zStream) {
1036 inflateEnd(cur_block->zStream);
1037 delete cur_block->zStream;
1039 delete cur_block;
1040 m_PendingBlocks_list.erase(it);
1042 // Request next block
1043 SendBlockRequests();
1046 // Stop looping and exit method
1047 return;
1050 } catch (const CEOFException& e) {
1051 wxString error = wxString(wxT("Error reading "));
1052 if (packed) error += CFormat(wxT("packed (LU: %i) largeblocks ")) % lenUnzipped;
1053 error += CFormat(wxT("data packet: RS: %i HS: %i SP: %i EP: %i BS: %i -> "))
1054 % size % header_size % nStartPos % nEndPos % nBlockSize;
1055 AddDebugLogLineM(true, logRemoteClient, error + e.what());
1056 return;
1060 int CUpDownClient::unzip(Pending_Block_Struct *block, byte *zipped, uint32 lenZipped, byte **unzipped, uint32 *lenUnzipped, int iRecursion)
1062 int err = Z_DATA_ERROR;
1064 // Save some typing
1065 z_stream *zS = block->zStream;
1067 // Is this the first time this block has been unzipped
1068 if (zS == NULL) {
1069 // Create stream
1070 block->zStream = new z_stream;
1071 zS = block->zStream;
1073 // Initialise stream values
1074 zS->zalloc = (alloc_func)0;
1075 zS->zfree = (free_func)0;
1076 zS->opaque = (voidpf)0;
1078 // Set output data streams, do this here to avoid overwriting on recursive calls
1079 zS->next_out = (*unzipped);
1080 zS->avail_out = (*lenUnzipped);
1082 // Initialise the z_stream
1083 err = inflateInit(zS);
1084 if (err != Z_OK) {
1085 return err;
1089 // Use whatever input is provided
1090 zS->next_in = zipped;
1091 zS->avail_in = lenZipped;
1093 // Only set the output if not being called recursively
1094 if (iRecursion == 0) {
1095 zS->next_out = (*unzipped);
1096 zS->avail_out = (*lenUnzipped);
1099 // Try to unzip the data
1100 err = inflate(zS, Z_SYNC_FLUSH);
1102 // Is zip finished reading all currently available input and writing
1103 // all generated output
1104 if (err == Z_STREAM_END) {
1105 // Finish up
1106 err = inflateEnd(zS);
1107 if (err != Z_OK) {
1108 return err;
1111 // Got a good result, set the size to the amount unzipped in this call
1112 // (including all recursive calls)
1113 (*lenUnzipped) = (zS->total_out - block->totalUnzipped);
1114 block->totalUnzipped = zS->total_out;
1115 } else if ((err == Z_OK) && (zS->avail_out == 0) && (zS->avail_in != 0)) {
1117 // Output array was not big enough,
1118 // call recursively until there is enough space
1120 // What size should we try next
1121 uint32 newLength = (*lenUnzipped) *= 2;
1122 if (newLength == 0) {
1123 newLength = lenZipped * 2;
1125 // Copy any data that was successfully unzipped to new array
1126 byte *temp = new byte[newLength];
1127 wxASSERT( zS->total_out - block->totalUnzipped <= newLength );
1128 memcpy(temp, (*unzipped), (zS->total_out - block->totalUnzipped));
1129 delete [] (*unzipped);
1130 (*unzipped) = temp;
1131 (*lenUnzipped) = newLength;
1133 // Position stream output to correct place in new array
1134 zS->next_out = (*unzipped) + (zS->total_out - block->totalUnzipped);
1135 zS->avail_out = (*lenUnzipped) - (zS->total_out - block->totalUnzipped);
1137 // Try again
1138 err = unzip(block, zS->next_in, zS->avail_in, unzipped, lenUnzipped, iRecursion + 1);
1139 } else if ((err == Z_OK) && (zS->avail_in == 0)) {
1140 // All available input has been processed, everything ok.
1141 // Set the size to the amount unzipped in this call
1142 // (including all recursive calls)
1143 (*lenUnzipped) = (zS->total_out - block->totalUnzipped);
1144 block->totalUnzipped = zS->total_out;
1145 } else {
1146 // Should not get here unless input data is corrupt
1147 wxString strZipError;
1149 if ( zS->msg ) {
1150 strZipError = wxString::Format(wxT(" %d '"), err) + wxString::FromAscii(zS->msg) + wxT("'");
1151 } else if (err != Z_OK) {
1152 strZipError = wxString::Format(wxT(" %d"), err);
1155 AddDebugLogLineM(false, logZLib,
1156 CFormat(wxT("Unexpected zip error %s in file '%s'"))
1157 % strZipError % (m_reqfile ? m_reqfile->GetFileName() : CPath(wxT("?"))));
1160 if (err != Z_OK) {
1161 (*lenUnzipped) = 0;
1164 return err;
1168 // Speed is now updated only when data was received, calculated as
1169 // (data received) / (time since last receiption)
1170 // and slightly filtered (10s average).
1171 // Result is quite precise now and makes the DownloadRateAdjust workaround obsolete.
1173 float CUpDownClient::CalculateKBpsDown()
1175 const float tAverage = 10.0;
1176 uint32 msCur = GetTickCount();
1178 if (bytesReceivedCycle) {
1179 float dt = (msCur - msReceivedPrev) / 1000.0; // time since last reception
1180 if (dt < 0.01) { // (safeguard against divide-by-zero)
1181 dt = 0.01f; // diff should be 100ms actually
1183 float kBpsDownCur = bytesReceivedCycle / 1024.0 / dt;
1184 if (dt >= tAverage) {
1185 kBpsDown = kBpsDownCur;
1186 } else {
1187 kBpsDown = (kBpsDown * (tAverage - dt) + kBpsDownCur * dt) / tAverage;
1189 //AddDebugLogLineM( false, logLocalClient, CFormat(wxT("CalculateKBpsDown %p kbps %.1f kbpsCur %.1f dt %.3f rcv %d "))
1190 // % this % kBpsDown % kBpsDownCur % dt % bytesReceivedCycle);
1191 bytesReceivedCycle = 0;
1192 msReceivedPrev = msCur;
1195 m_cShowDR++;
1196 if (m_cShowDR == 30){
1197 m_cShowDR = 0;
1198 UpdateDisplayedInfo();
1200 if (msCur - m_dwLastBlockReceived > DOWNLOADTIMEOUT) {
1201 if (!GetSentCancelTransfer()){
1202 CPacket* packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
1203 theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
1204 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_CANCELTRANSFER to ") + GetFullIP() );
1205 SendPacket(packet,true,true);
1206 SetSentCancelTransfer(1);
1208 SetDownloadState(DS_ONQUEUE);
1211 return kBpsDown;
1214 uint16 CUpDownClient::GetAvailablePartCount() const
1216 uint16 result = 0;
1217 for (int i = 0;i != m_nPartCount;i++){
1218 if (IsPartAvailable(i))
1219 result++;
1221 return result;
1224 void CUpDownClient::SetRemoteQueueRank(uint16 nr)
1226 m_nOldRemoteQueueRank = m_nRemoteQueueRank;
1227 m_nRemoteQueueRank = nr;
1228 UpdateDisplayedInfo();
1231 void CUpDownClient::UDPReaskACK(uint16 nNewQR)
1233 // 0.42e
1234 m_bUDPPending = false;
1235 SetRemoteQueueRank(nNewQR);
1236 m_dwLastAskedTime = ::GetTickCount();
1239 void CUpDownClient::UDPReaskFNF()
1241 m_bUDPPending = false;
1243 // avoid premature deletion of 'this' client
1244 if (GetDownloadState() != DS_DOWNLOADING){
1245 if (m_reqfile) {
1246 m_reqfile->AddDeadSource(this);
1249 theApp->downloadqueue->RemoveSource(this);
1250 if (!m_socket) {
1251 if (Disconnected(wxT("UDPReaskFNF m_socket=NULL"))) {
1252 Safe_Delete();
1255 } else {
1256 AddDebugLogLineM( false, logRemoteClient, wxT("UDP ANSWER FNF : ") + GetUserName() + wxT(" - did not remove client because of current download state") );
1260 void CUpDownClient::UDPReaskForDownload()
1263 wxASSERT(m_reqfile);
1265 if(!m_reqfile || m_bUDPPending ) {
1266 return;
1269 //#warning We should implement the quality tests for udp reliability
1271 if( m_nTotalUDPPackets > 3 && ((float)(m_nFailedUDPPackets/m_nTotalUDPPackets) > .3)) {
1272 return;
1276 if (thePrefs::GetEffectiveUDPPort() == 0) {
1277 return;
1280 if (m_nUDPPort != 0 && !theApp->IsFirewalled() && !IsConnected()) {
1281 //don't use udp to ask for sources
1282 if(IsSourceRequestAllowed()) {
1283 return;
1286 m_bUDPPending = true;
1288 CMemFile data(128);
1289 data.WriteHash(m_reqfile->GetFileHash());
1291 if (GetUDPVersion() > 3) {
1292 if (m_reqfile->IsPartFile()) {
1293 ((CPartFile*)m_reqfile)->WritePartStatus(&data);
1295 else {
1296 data.WriteUInt16(0);
1300 if (GetUDPVersion() > 2) {
1301 data.WriteUInt16(m_reqfile->m_nCompleteSourcesCount);
1304 CPacket* response = new CPacket(data, OP_EMULEPROT, OP_REASKFILEPING);
1305 AddDebugLogLineM( false, logClientUDP, wxT("Client UDP socket: send OP_REASKFILEPING") );
1306 theStats::AddUpOverheadFileRequest(response->GetPacketSize());
1307 theApp->clientudp->SendPacket(response,GetConnectIP(),GetUDPPort(), ShouldReceiveCryptUDPPackets(), GetUserHash().GetHash(), false, 0);
1308 } else if (HasLowID() && GetBuddyIP() && GetBuddyPort() && HasValidBuddyID()) {
1310 m_bUDPPending = true;
1312 CMemFile data(128);
1314 data.WriteHash(CMD4Hash(GetBuddyID()));
1315 data.WriteHash(m_reqfile->GetFileHash());
1317 if (GetUDPVersion() > 3) {
1318 if (m_reqfile->IsPartFile()) {
1319 ((CPartFile*)m_reqfile)->WritePartStatus(&data);
1320 } else {
1321 data.WriteUInt16(0);
1325 if (GetUDPVersion() > 2) {
1326 data.WriteUInt16(m_reqfile->m_nCompleteSourcesCount);
1329 CPacket* response = new CPacket(data, OP_EMULEPROT, OP_REASKCALLBACKUDP);
1330 AddDebugLogLineM( false, logClientUDP, wxT("Client UDP socket: send OP_REASKCALLBACKUDP") );
1331 theStats::AddUpOverheadFileRequest(response->GetPacketSize());
1332 theApp->clientudp->SendPacket(response, GetBuddyIP(), GetBuddyPort(), false, NULL, true, 0 );
1337 //! Barry - Sets string to show parts downloading, eg NNNYNNNNYYNYN
1338 wxString CUpDownClient::ShowDownloadingParts() const
1340 // Initialise to all N's
1341 wxString Parts(wxT('N'), m_nPartCount);
1343 std::list<Pending_Block_Struct*>::const_iterator it = m_PendingBlocks_list.begin();
1344 for (; it != m_PendingBlocks_list.end(); ++it) {
1345 Parts.SetChar(((*it)->block->StartOffset / PARTSIZE), 'Y');
1348 return Parts;
1352 void CUpDownClient::UpdateDisplayedInfo(bool force)
1354 uint32 curTick = ::GetTickCount();
1355 if(force || curTick-m_lastRefreshedDLDisplay > MINWAIT_BEFORE_DLDISPLAY_WINDOWUPDATE) {
1356 // Check if we actually need to notify of changes
1357 bool update = m_reqfile && m_reqfile->ShowSources();
1359 // Check A4AF files only if needed
1360 if ( !update ) {
1361 A4AFList::iterator it = m_A4AF_list.begin();
1362 for ( ; it != m_A4AF_list.end(); ++it ) {
1363 if ( it->first->ShowSources() ) {
1364 update = true;
1365 break;
1370 // And finnaly trigger an event if there's any reason
1371 if ( update ) {
1372 SourceItemType type = A4AF_SOURCE;
1373 switch (GetDownloadState()) {
1374 case DS_DOWNLOADING:
1375 case DS_ONQUEUE:
1376 // We will send A4AF, which will be checked.
1377 break;
1378 default:
1379 type = UNAVAILABLE_SOURCE;
1380 break;
1383 Notify_SourceCtrlUpdateSource(this, type );
1384 Notify_SharedCtrlRefreshClient(this, AVAILABLE_SOURCE);
1387 m_lastRefreshedDLDisplay = curTick;
1392 // IgnoreNoNeeded = will switch to files of which this source has no needed parts (if no better fiels found)
1393 // ignoreSuspensions = ignore timelimit for A4Af jumping
1394 // bRemoveCompletely = do not readd the file which the source is swapped from to the A4AF lists (needed if deleting or stopping a file)
1395 // toFile = Try to swap to this partfile only
1397 bool CUpDownClient::SwapToAnotherFile(bool bIgnoreNoNeeded, bool ignoreSuspensions, bool bRemoveCompletely, CPartFile* toFile)
1399 // Fail if m_reqfile is invalid
1400 if ( m_reqfile == NULL ) {
1401 return false;
1404 // It would be stupid to swap away a downloading source
1405 if (GetDownloadState() == DS_DOWNLOADING) {
1406 return false;
1409 // The iterator of the final target
1410 A4AFList::iterator target = m_A4AF_list.end();
1412 // Do we want to swap to a specific file?
1413 if ( toFile != NULL ) {
1414 A4AFList::iterator it = m_A4AF_list.find( toFile );
1415 if ( it != m_A4AF_list.end() ) {
1417 // We force ignoring of noneeded flag and timestamps
1418 if ( IsValidSwapTarget( it, true, true ) ) {
1419 // Set the target
1420 target = it;
1423 } else {
1424 // We want highest priority possible, but need to start with
1425 // a value less than any other priority
1426 char priority = -1;
1428 A4AFList::iterator it = m_A4AF_list.begin();
1429 for ( ; it != m_A4AF_list.end(); ++it ) {
1430 if ( IsValidSwapTarget( it, bIgnoreNoNeeded, ignoreSuspensions ) ) {
1431 char cur_priority = it->first->GetDownPriority();
1433 // We would prefer to get files with needed parts, thus rate them higher.
1434 // However, this really only matters if bIgnoreNoNeeded is true.
1435 if ( it->second.NeededParts )
1436 cur_priority += 10;
1438 // Change target if the current file has a higher rate than the previous
1439 if ( cur_priority > priority ) {
1440 priority = cur_priority;
1442 // Set the new target
1443 target = it;
1445 // Break on the first High-priority file with needed parts
1446 if ( priority == PR_HIGH + 10 ) {
1447 break;
1454 // Try to swap if we found a valid target
1455 if ( target != m_A4AF_list.end() ) {
1457 // Sainity check, if reqfile doesn't own the source, then something
1458 // is wrong and the swap cannot proceed.
1459 if ( m_reqfile->DelSource( this ) ) {
1460 CPartFile* SwapTo = target->first;
1462 // remove this client from the A4AF list of our new m_reqfile
1463 if ( SwapTo->RemoveA4AFSource( this ) ) {
1464 Notify_SourceCtrlRemoveSource(this, SwapTo);
1467 m_reqfile->RemoveDownloadingSource( this );
1469 // Do we want to remove it completly? Say if the old file is getting deleted
1470 if ( !bRemoveCompletely ) {
1471 m_reqfile->AddA4AFSource( this );
1473 // Set the status of the old file
1474 m_A4AF_list[m_reqfile].NeededParts = (GetDownloadState() != DS_NONEEDEDPARTS);
1476 // Avoid swapping to this file for a while
1477 m_A4AF_list[m_reqfile].timestamp = ::GetTickCount();
1479 Notify_SourceCtrlAddSource(m_reqfile, this, A4AF_SOURCE);
1480 } else {
1481 Notify_SourceCtrlRemoveSource( this, m_reqfile );
1484 SetDownloadState(DS_NONE);
1485 ResetFileStatusInfo();
1487 m_nRemoteQueueRank = 0;
1488 m_nOldRemoteQueueRank = 0;
1490 m_reqfile->UpdatePartsInfo();
1492 SetRequestFile( SwapTo );
1494 SwapTo->AddSource( this );
1496 Notify_SourceCtrlAddSource(SwapTo, this, UNAVAILABLE_SOURCE);
1498 // Remove the new reqfile from the list of other files
1499 m_A4AF_list.erase( target );
1501 return true;
1505 return false;
1509 bool CUpDownClient::IsValidSwapTarget( A4AFList::iterator it, bool ignorenoneeded, bool ignoresuspended )
1511 wxASSERT( it != m_A4AF_list.end() && it->first );
1513 // Check if this file has been suspended
1514 if ( !ignoresuspended ) {
1515 if ( ::GetTickCount() - it->second.timestamp >= PURGESOURCESWAPSTOP ) {
1516 // The wait-time has been exceeded and the file is now a valid target
1517 it->second.timestamp = 0;
1518 } else {
1519 // The file was still suspended and we are not ignoring suspensions
1520 return false;
1524 // Check if the client has needed parts
1525 if ( !ignorenoneeded ) {
1526 if ( !it->second.NeededParts ) {
1527 return false;
1531 // Final checks to see if the client is a valid target
1532 CPartFile* cur_file = it->first;
1533 if ( ( cur_file != m_reqfile && !cur_file->IsStopped() ) &&
1534 ( cur_file->GetStatus() == PS_READY || cur_file->GetStatus() == PS_EMPTY ) &&
1535 ( cur_file->IsPartFile() ) )
1537 return true;
1538 } else {
1539 return false;
1544 void CUpDownClient::SetRequestFile(CPartFile* reqfile)
1546 if ( m_reqfile != reqfile ) {
1547 // Decrement the source-count of the old request-file
1548 if ( m_reqfile ) {
1549 m_reqfile->ClientStateChanged( GetDownloadState(), -1 );
1550 m_reqfile->UpdatePartsFrequency( this, false );
1553 m_nPartCount = 0;
1554 m_downPartStatus.clear();
1556 m_reqfile = reqfile;
1558 if ( reqfile ) {
1559 // Increment the source-count of the new request-file
1560 m_reqfile->ClientStateChanged( -1, GetDownloadState() );
1562 m_nPartCount = reqfile->GetPartCount();
1567 void CUpDownClient::SetReqFileAICHHash(CAICHHash* val){
1568 if(m_pReqFileAICHHash != NULL && m_pReqFileAICHHash != val)
1569 delete m_pReqFileAICHHash;
1570 m_pReqFileAICHHash = val;
1573 void CUpDownClient::SendAICHRequest(CPartFile* pForFile, uint16 nPart){
1574 CAICHRequestedData request;
1575 request.m_nPart = nPart;
1576 request.m_pClient = this;
1577 request.m_pPartFile = pForFile;
1578 CAICHHashSet::m_liRequestedData.push_back(request);
1579 m_fAICHRequested = TRUE;
1580 CMemFile data;
1581 data.WriteHash(pForFile->GetFileHash());
1582 data.WriteUInt16(nPart);
1583 pForFile->GetAICHHashset()->GetMasterHash().Write(&data);
1584 CPacket* packet = new CPacket(data, OP_EMULEPROT, OP_AICHREQUEST);
1585 theStats::AddUpOverheadOther(packet->GetPacketSize());
1586 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_AICHREQUEST to") + GetFullIP());
1587 SafeSendPacket(packet);
1590 void CUpDownClient::ProcessAICHAnswer(const byte* packet, uint32 size)
1592 if (m_fAICHRequested == FALSE){
1593 throw wxString(wxT("Received unrequested AICH Packet"));
1595 m_fAICHRequested = FALSE;
1597 CMemFile data(packet, size);
1598 if (size <= 16){
1599 CAICHHashSet::ClientAICHRequestFailed(this);
1600 return;
1603 CMD4Hash hash = data.ReadHash();
1604 CPartFile* pPartFile = theApp->downloadqueue->GetFileByID(hash);
1605 CAICHRequestedData request = CAICHHashSet::GetAICHReqDetails(this);
1606 uint16 nPart = data.ReadUInt16();
1607 if (pPartFile != NULL && request.m_pPartFile == pPartFile && request.m_pClient == this && nPart == request.m_nPart){
1608 CAICHHash ahMasterHash(&data);
1609 if ( (pPartFile->GetAICHHashset()->GetStatus() == AICH_TRUSTED || pPartFile->GetAICHHashset()->GetStatus() == AICH_VERIFIED)
1610 && ahMasterHash == pPartFile->GetAICHHashset()->GetMasterHash())
1612 if(pPartFile->GetAICHHashset()->ReadRecoveryData(request.m_nPart*PARTSIZE, &data)){
1613 // finally all checks passed, everythings seem to be fine
1614 AddDebugLogLineM( false, logAICHTransfer, wxT("AICH Packet Answer: Succeeded to read and validate received recoverydata"));
1615 CAICHHashSet::RemoveClientAICHRequest(this);
1616 pPartFile->AICHRecoveryDataAvailable(request.m_nPart);
1617 return;
1618 } else {
1619 AddDebugLogLineM( false, logAICHTransfer, wxT("AICH Packet Answer: Succeeded to read and validate received recoverydata"));
1621 } else {
1622 AddDebugLogLineM( false, logAICHTransfer, wxT("AICH Packet Answer: Masterhash differs from packethash or hashset has no trusted Masterhash") );
1624 } else {
1625 AddDebugLogLineM( false, logAICHTransfer, wxT("AICH Packet Answer: requested values differ from values in packet") );
1628 CAICHHashSet::ClientAICHRequestFailed(this);
1632 void CUpDownClient::ProcessAICHRequest(const byte* packet, uint32 size)
1634 if (size != 16 + 2 + CAICHHash::GetHashSize()) {
1635 throw wxString(wxT("Received AICH Request Packet with wrong size"));
1638 CMemFile data(packet, size);
1640 CMD4Hash hash = data.ReadHash();
1641 uint16 nPart = data.ReadUInt16();
1642 CAICHHash ahMasterHash(&data);
1643 CKnownFile* pKnownFile = theApp->sharedfiles->GetFileByID(hash);
1644 if (pKnownFile != NULL){
1645 if (pKnownFile->GetAICHHashset()->GetStatus() == AICH_HASHSETCOMPLETE && pKnownFile->GetAICHHashset()->HasValidMasterHash()
1646 && pKnownFile->GetAICHHashset()->GetMasterHash() == ahMasterHash && pKnownFile->GetPartCount() > nPart
1647 && pKnownFile->GetFileSize() > EMBLOCKSIZE && pKnownFile->GetFileSize() - PARTSIZE*nPart > EMBLOCKSIZE)
1649 CMemFile fileResponse;
1650 fileResponse.WriteHash(pKnownFile->GetFileHash());
1651 fileResponse.WriteUInt16(nPart);
1652 pKnownFile->GetAICHHashset()->GetMasterHash().Write(&fileResponse);
1653 if (pKnownFile->GetAICHHashset()->CreatePartRecoveryData(nPart*PARTSIZE, &fileResponse)){
1654 AddDebugLogLineM(false, logAICHTransfer,
1655 CFormat(wxT("AICH Packet Request: Sucessfully created and send recoverydata for '%s' to %s"))
1656 % pKnownFile->GetFileName() % GetClientFullInfo());
1658 CPacket* packAnswer = new CPacket(fileResponse, OP_EMULEPROT, OP_AICHANSWER);
1659 theStats::AddUpOverheadOther(packAnswer->GetPacketSize());
1660 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_AICHANSWER to") + GetFullIP());
1661 SafeSendPacket(packAnswer);
1662 return;
1663 } else {
1664 AddDebugLogLineM(false, logAICHTransfer,
1665 CFormat(wxT("AICH Packet Request: Failed to create recoverydata for '%s' to %s"))
1666 % pKnownFile->GetFileName() % GetClientFullInfo());
1668 } else {
1669 AddDebugLogLineM(false, logAICHTransfer,
1670 CFormat(wxT("AICH Packet Request: Failed to create recoverydata - Hashset not ready or requested Hash differs from Masterhash for '%s' to %s"))
1671 % pKnownFile->GetFileName() % GetClientFullInfo());
1673 } else {
1674 AddDebugLogLineM( false, logAICHTransfer, wxT("AICH Packet Request: Failed to find requested shared file - ") + GetClientFullInfo() );
1677 CPacket* packAnswer = new CPacket(OP_AICHANSWER, 16, OP_EMULEPROT);
1678 packAnswer->Copy16ToDataBuffer(hash.GetHash());
1679 theStats::AddUpOverheadOther(packAnswer->GetPacketSize());
1680 AddDebugLogLineM( false, logLocalClient, wxT("Local Client: OP_AICHANSWER to") + GetFullIP());
1681 SafeSendPacket(packAnswer);
1684 void CUpDownClient::ProcessAICHFileHash(CMemFile* data, const CPartFile* file){
1685 CPartFile* pPartFile;
1686 if (file == NULL){
1687 pPartFile = theApp->downloadqueue->GetFileByID(data->ReadHash());
1688 } else {
1689 pPartFile = (CPartFile*)file;
1691 CAICHHash ahMasterHash(data);
1693 if(pPartFile != NULL && pPartFile == GetRequestFile()){
1694 SetReqFileAICHHash(new CAICHHash(ahMasterHash));
1695 pPartFile->GetAICHHashset()->UntrustedHashReceived(ahMasterHash, GetConnectIP());
1696 } else {
1697 AddDebugLogLineM( false, logAICHTransfer, wxT("ProcessAICHFileHash(): PartFile not found or Partfile differs from requested file, ") + GetClientFullInfo() );
1700 // File_checked_for_headers