📄 downloadqueue.cpp
字号:
udcounter = 0;
if ((!lastudpsearchtime) || (::GetTickCount() - lastudpsearchtime) > UDPSERVERREASKTIME)
SendNextUDPPacket();
}
}
CPartFile* CDownloadQueue::GetFileByIndex(int index){
int count=0;
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
if (count==index) return filelist.GetAt(pos);
count++;
}
return 0;
}
CPartFile* CDownloadQueue::GetFileByID(uchar* filehash){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
if (!memcmp(filehash,filelist.GetAt(pos)->GetFileHash(),16))
return filelist.GetAt(pos);
}
return 0;
}
bool CDownloadQueue::IsPartFile(void* totest){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos))
if (totest == filelist.GetAt(pos))
return true;
return false;
}
void CDownloadQueue::CheckAndAddSource(CPartFile* sender,CUpDownClient* source){
// if we block loopbacks at this point it should prevent us from connecting to ourself
if( (!source->HasLowID() && (source->GetUserID() & 0xFF) == 0x7F) || sender->IsStopped()) {
delete source;
return;
}
// uses this only for temp. clients
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
for (int sl=0;sl<SOURCESSLOTS;sl++) if (!cur_file->srclists[sl].IsEmpty())
for (POSITION pos2 = cur_file->srclists[sl].GetHeadPosition();pos2 != 0; cur_file->srclists[sl].GetNext(pos2)){
if (cur_file->srclists[sl].GetAt(pos2)->Compare(source)){
if (cur_file == sender){ // this file has already this source
delete source;
return;
}
// set request for this source
if (cur_file->srclists[sl].GetAt(pos2)->AddRequestForAnotherFile(sender)){
// add it to uploadlistctrl
theApp.emuledlg->transferwnd.downloadlistctrl.AddSource(sender,cur_file->srclists[sl].GetAt(pos2),true);
delete source;
return;
}
else{
delete source;
return;
}
}
}
}
//our new source is real new but maybe it is already uploading to us?
//if yes the known client will be attached to the var "source"
//and the old sourceclient will be deleted
if (theApp.clientlist->AttachToAlreadyKnown(&source,0))
source->reqfile = sender;
else
theApp.clientlist->AddClient(source,true);
sender->srclists[source->sourcesslot].AddTail(source);
theApp.emuledlg->transferwnd.downloadlistctrl.AddSource(sender,source,false);
UpdateDisplayedInfo();
}
void CDownloadQueue::CheckAndAddKnownSource(CPartFile* sender,CUpDownClient* source){
if((!source->HasLowID() && (source->GetUserID() & 0xFF) == 0x7F) || sender->IsStopped() )
return;
// use this for client which are already know (downloading for example)
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
for (int sl=0;sl<SOURCESSLOTS;sl++) if (!cur_file->srclists[sl].IsEmpty())
if (cur_file->srclists[sl].Find(source)){
if (cur_file == sender)
return;
if (source->AddRequestForAnotherFile(sender))
theApp.emuledlg->transferwnd.downloadlistctrl.AddSource(sender,source,true);
return;
}
}
source->reqfile = sender;
sender->srclists[source->sourcesslot].AddTail(source);
theApp.emuledlg->transferwnd.downloadlistctrl.AddSource(sender,source,false);
UpdateDisplayedInfo();
}
bool CDownloadQueue::RemoveSource(CUpDownClient* toremove, bool updatewindow){
bool removed = false;
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
for (int sl=0;sl<SOURCESSLOTS;sl++) if (!cur_file->srclists[sl].IsEmpty())
for (POSITION pos2 = cur_file->srclists[sl].GetHeadPosition();pos2 != 0; cur_file->srclists[sl].GetNext(pos2)){
if (toremove == cur_file->srclists[sl].GetAt(pos2)){
cur_file->srclists[sl].RemoveAt(pos2);
cur_file->NewSrcPartsInfo();
removed = true;
cur_file->RemoveDownloadingSource(toremove);
break;
}
}
cur_file->UpdateAvailablePartsCount();
}
if (toremove->GetFileComment().GetLength()>0 || toremove->GetFileRate()>0 )
toremove->reqfile->UpdateFileRatingCommentAvail();
if (updatewindow){
toremove->SetDownloadState(DS_NONE);
theApp.emuledlg->transferwnd.downloadlistctrl.RemoveSource(toremove,0);
}
toremove->reqfile = 0;
return removed;
}
void CDownloadQueue::RemoveFile(CPartFile* toremove){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
if (toremove == filelist.GetAt(pos)){
filelist.RemoveAt(pos);
return;
}
}
}
void CDownloadQueue::DeleteAll(){
POSITION pos;
for (pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
for (int sl=0;sl<SOURCESSLOTS;sl++) if (!cur_file->srclists[sl].IsEmpty())
cur_file->srclists[sl].RemoveAll();
// Barry - Should also remove all requested blocks
// Don't worry about deleting the blocks, that gets handled
// when CUpDownClient is deleted in CClientList::DeleteAll()
cur_file->RemoveAllRequestedBlocks();
}
}
bool CDownloadQueue::SendNextUDPPacket(){
if (filelist.IsEmpty() || !theApp.serverconnect->IsConnected())
return false;
if (!cur_udpserver)
if (!(cur_udpserver = theApp.serverlist->GetNextServer(cur_udpserver))){
TRACE("ERROR:SendNextUDPPacket() no server found");
StopUDPRequests();
};
// get nextfile
CPartFile* nextfile = 0;
while (!(nextfile && (nextfile->GetStatus() == PS_READY ||nextfile->GetStatus() == PS_EMPTY))){
if (lastfile == 0){
nextfile = filelist.GetHead();
lastfile = nextfile;
}
else{
POSITION pos = filelist.Find(lastfile);
if (!pos){
TRACE("Error: CDownloadQueue::SendNextUDPPacket()");
nextfile = filelist.GetHead();
lastfile = nextfile;
}
else{
filelist.GetNext(pos);
if (pos == 0){
cur_udpserver = theApp.serverlist->GetNextServer(cur_udpserver);
if (cur_udpserver == 0){
//TRACE("finished");
lastudpsearchtime = ::GetTickCount();
lastfile = 0;
return false; // finished (processed all file & all servers)
}
nextfile = filelist.GetHead();
lastfile = nextfile;
}
else{
nextfile = filelist.GetAt(pos);
lastfile = nextfile;
}
}
}
}
if( (theApp.glob_prefs->GetMaxSourcePerFileUDP()) < nextfile->GetSourceCount()) //<<--
return true;
Packet packet(OP_GLOBGETSOURCES,16);
memcpy(packet.pBuffer,nextfile->GetFileHash(),16);
if (cur_udpserver != theApp.serverlist->GetServerByAddress(theApp.serverconnect->GetCurrentServer()->GetAddress(),theApp.serverconnect->GetCurrentServer()->GetPort())){
theApp.uploadqueue->AddUpDataOverheadServer(packet.size);
theApp.serverconnect->SendUDPPacket(&packet,cur_udpserver,false);
}
return true;
}
void CDownloadQueue::StopUDPRequests(){
cur_udpserver = 0;
lastudpsearchtime = ::GetTickCount();
lastfile = 0;
}
void CDownloadQueue::SortByPriority(){
POSITION pos1, pos2;
uint16 i = 0;
for( pos1 = filelist.GetHeadPosition(); ( pos2 = pos1 ) != NULL; ){
filelist.GetNext(pos1);
CPartFile* cur_file = filelist.GetAt(pos2);
if (cur_file->GetDownPriority() == PR_HIGH){
filelist.AddHead(cur_file);
filelist.RemoveAt(pos2);
}
else if (cur_file->GetDownPriority() == PR_LOW){
filelist.AddTail(cur_file);
filelist.RemoveAt(pos2);
}
i++;
if (i == filelist.GetCount())
break;
}
}
void CDownloadQueue::GetDownloadStats(int results[]) {
results[0]=0;
results[1]=0;
for (POSITION pos =theApp.downloadqueue->filelist.GetHeadPosition();pos != 0;theApp.downloadqueue->filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
results[0]+=cur_file->GetSourceCount();
results[1]+=cur_file->GetTransferingSrcCount();
}
}
CUpDownClient* CDownloadQueue::GetDownloadClientByIP(uint32 dwIP){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
for (int sl=0;sl<SOURCESSLOTS;sl++) if (!cur_file->srclists[sl].IsEmpty())
for (POSITION pos2 = cur_file->srclists[sl].GetHeadPosition();pos2 != 0; cur_file->srclists[sl].GetNext(pos2)){
if (dwIP == cur_file->srclists[sl].GetAt(pos2)->GetIP()){
return cur_file->srclists[sl].GetAt(pos2);
}
}
}
return NULL;
}
void CDownloadQueue::ResetCatParts(int cat){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if (cur_file->GetCategory()==cat) cur_file->SetCategory(0);
else if (cur_file->GetCategory() > cat) cur_file->SetCategory(cur_file->GetCategory()-1);
}
}
void CDownloadQueue::SetCatPrio(int cat, uint8 newprio){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if (cat==0 || cur_file->GetCategory()==cat)
if (newprio==PR_AUTO) {
cur_file->SetAutoDownPriority(true);
cur_file->SetDownPriority(PR_HIGH);
}
else {
cur_file->SetAutoDownPriority(false);
cur_file->SetDownPriority(newprio);
}
}
}
void CDownloadQueue::SetCatStatus(int cat, uint16 newstatus){
bool reset=false;
POSITION pos= filelist.GetHeadPosition();
while (pos != 0) {
CPartFile* cur_file = filelist.GetAt(pos);
if (!cur_file) continue;
if ( ((cat==0 && !theApp.glob_prefs->ShowAllNotCats()) ||
(cat==0 && theApp.glob_prefs->ShowAllNotCats() && cur_file->GetCategory()==0 )) ||
( cat>0 && cat==cur_file->GetCategory() ) )
switch (newstatus) {
case MP_CANCEL:cur_file->DeleteFile();reset=true;break;
case MP_PAUSE:cur_file->PauseFile();break;
case MP_STOP:cur_file->StopFile();break;
case MP_RESUME:cur_file->ResumeFile();break;
}
filelist.GetNext(pos);
if (reset) {reset=false;pos= filelist.GetHeadPosition();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -