📄 downloadqueue.cpp
字号:
//Copyright (C)2002 Merkur ( merkur-@users.sourceforge.net / http://www.emule-project.net )
//
//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., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "StdAfx.h"
#include "downloadqueue.h"
#include "updownclient.h"
#include "partfile.h"
#include "ed2klink.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CDownloadQueue::CDownloadQueue(CPreferences* in_prefs,CSharedFileList* in_sharedfilelist){
app_prefs = in_prefs;
sharedfilelist = in_sharedfilelist;
filesrdy = 0;
datarate = 0;
cur_udpserver = 0;
lastfile = 0;
lastudpsearchtime = 0;
lastudpstattime = 0;
udcounter = 0;
m_nDownDataRateMSOverhead = 0;
m_nDownDatarateOverhead = 0;
m_nDownDataOverheadSourceExchange = 0;
m_nDownDataOverheadFileRequest = 0;
m_nDownDataOverheadOther = 0;
m_nDownDataOverheadServer = 0;
m_nDownDataOverheadSourceExchangePackets = 0;
m_nDownDataOverheadFileRequestPackets = 0;
m_nDownDataOverheadOtherPackets = 0;
m_nDownDataOverheadServerPackets = 0;
}
void CDownloadQueue::UpdateDisplayedInfo(boolean force) {
DWORD curTick = ::GetTickCount();
if(force || curTick-m_lastRefreshedDLDisplay > MINWAIT_BEFORE_DLDISPLAY_WINDOWUPDATE+(uint32)(rand()/(RAND_MAX/1000))) {
theApp.emuledlg->transferwnd.downloadlistctrl.UpdateItem(this);
m_lastRefreshedDLDisplay = curTick;
}
}
void CDownloadQueue::AddPartFilesToShare(){
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if (cur_file->GetStatus(true) == PS_READY)
sharedfilelist->SafeAddKFile(cur_file,true);
}
}
void CDownloadQueue::CompDownDatarateOverhead(){
m_AvarageDDRO_list.AddTail(m_nDownDataRateMSOverhead);
if (m_AvarageDDRO_list.GetCount() > 150)
m_AvarageDDRO_list.RemoveAt(m_AvarageDDRO_list.GetHeadPosition());
m_nDownDatarateOverhead = 0;
m_nDownDataRateMSOverhead = 0;
for (POSITION pos = m_AvarageDDRO_list.GetHeadPosition();pos != 0;m_AvarageDDRO_list.GetNext(pos))
m_nDownDatarateOverhead += m_AvarageDDRO_list.GetAt(pos);
if(m_AvarageDDRO_list.GetCount() > 10)
m_nDownDatarateOverhead = 10*m_nDownDatarateOverhead/m_AvarageDDRO_list.GetCount();
else
m_nDownDatarateOverhead = 0;
return;
}
void CDownloadQueue::Init(){
// find all part files, read & hash them if needed and store into a list
CFileFind ff;
int count = 0;
//char* searchpath = new char[strlen(app_prefs->GetTempDir())+MAX_PATH];
CString searchPath(app_prefs->GetTempDir());
searchPath += "\\*.part.met";
//sprintf(searchpath,"%s\\*.part.met",app_prefs->GetTempDir());
//delete[] searchpath;
//check all part.met files
bool end = !ff.FindFile(searchPath, 0);
while (!end){
end = !ff.FindNextFile();
if (ff.IsDirectory())
continue;
CPartFile* toadd = new CPartFile();
if (toadd->LoadPartFile(app_prefs->GetTempDir(),ff.GetFileName().GetBuffer())){
count++;
filelist.AddTail(toadd); // to downloadqueue
if (toadd->GetStatus(true) == PS_READY)
sharedfilelist->SafeAddKFile(toadd); // part files are always shared files
theApp.emuledlg->transferwnd.downloadlistctrl.AddFile(toadd);// show in downloadwindow
}
else
delete toadd;
}
ff.Close();
//try recovering any part.met files
searchPath += ".backup";
end = !ff.FindFile(searchPath, 0);
while (!end){
end = !ff.FindNextFile();
if (ff.IsDirectory())
continue;
CPartFile* toadd = new CPartFile();
if (toadd->LoadPartFile(app_prefs->GetTempDir(),ff.GetFileName().GetBuffer())){
toadd->SavePartFile(); // resave backup
count++;
filelist.AddTail(toadd); // to downloadqueue
if (toadd->GetStatus(true) == PS_READY)
sharedfilelist->SafeAddKFile(toadd); // part files are always shared files
theApp.emuledlg->transferwnd.downloadlistctrl.AddFile(toadd);// show in downloadwindow
theApp.emuledlg->AddLogLine(false, GetResString(IDS_RECOVERED_PARTMET), toadd->GetFileName());
}
else {
delete toadd;
}
}
ff.Close();
if(count == 0) {
theApp.emuledlg->AddLogLine(false,GetResString(IDS_NOPARTSFOUND));
} else {
theApp.emuledlg->AddLogLine(false,GetResString(IDS_FOUNDPARTS),count);
SortByPriority();
}
}
CDownloadQueue::~CDownloadQueue(){
for (POSITION pos =filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos))
delete filelist.GetAt(pos);
}
// [InterCeptor]
void CDownloadQueue::SavePartFiles(bool del /*= false*/) {
for (POSITION pos =filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos))
{
filelist.GetAt(pos)->m_hpartfile.Flush();
filelist.GetAt(pos)->SavePartFile();
if (del)
delete filelist.GetAt(pos);
}
}
void CDownloadQueue::AddSearchToDownload(CSearchFile* toadd,uint8 cat){
if (IsFileExisting(toadd->GetFileHash()))
return;
CPartFile* newfile = new CPartFile(toadd);
if (newfile->GetStatus() == PS_ERROR){
delete newfile;
return;
}
newfile->SetCategory(cat);
AddDownload(newfile);
}
void CDownloadQueue::AddSearchToDownload(CString link,uint8 cat){
CPartFile* newfile = new CPartFile(link);
if (newfile->GetStatus() == PS_ERROR){
delete newfile;
return;
}
newfile->SetCategory(cat);
AddDownload(newfile);
}
void CDownloadQueue::StartNextFile(){
if( !theApp.glob_prefs->StartNextFile() )
return;
CPartFile* pfile = NULL;
for (POSITION pos = filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if (cur_file->GetStatus() == PS_PAUSED){
if (!pfile){
pfile = cur_file;
if (pfile->GetDownPriority() == PR_HIGH) break;
}
else{
if (cur_file->GetDownPriority() > pfile->GetDownPriority()){
pfile = cur_file;
if (pfile->GetDownPriority() == PR_HIGH) break;
}
}
}
}
if (pfile) pfile->ResumeFile();
}
void CDownloadQueue::AddFileLinkToDownload(CED2KFileLink* pLink,uint8 cat)
{
CPartFile* newfile = new CPartFile(pLink);
if (newfile->GetStatus() == PS_ERROR){
delete newfile;
newfile=NULL;
}
else {
newfile->SetCategory(cat);
AddDownload(newfile);
}
if(pLink->HasValidSources()) {
if (newfile) newfile->AddClientSources(pLink->SourcesList,1);
else{
CPartFile* partfile = GetFileByID((uchar*)pLink->GetHashKey());
if (partfile) partfile->AddClientSources(pLink->SourcesList,1);
}
}
}
void CDownloadQueue::AddDownload(CPartFile* newfile) {
// Barry - Add in paused mode if required
if (theApp.glob_prefs->AddNewFilesPaused())
newfile->PauseFile();
filelist.AddTail(newfile);
SortByPriority();
theApp.emuledlg->transferwnd.downloadlistctrl.AddFile(newfile);
theApp.emuledlg->AddLogLine(true,GetResString(IDS_NEWDOWNLOAD),newfile->GetFileName());
CString msgTemp;
msgTemp.Format(GetResString(IDS_NEWDOWNLOAD)+"\n",newfile->GetFileName());
theApp.emuledlg->ShowNotifier(msgTemp, TBN_DLOAD);
}
bool CDownloadQueue::IsFileExisting(uchar* fileid){
if (CKnownFile* file = sharedfilelist->GetFileByID((uchar*)fileid)){
if (file->IsPartFile())
theApp.emuledlg->AddLogLine(true, GetResString(IDS_ERR_ALREADY_DOWNLOADING), file->GetFileName() );
else
theApp.emuledlg->AddLogLine(true, GetResString(IDS_ERR_ALREADY_DOWNLOADED), file->GetFileName() );
return true;
}
else if ( file = this->GetFileByID((uchar*)fileid)){
theApp.emuledlg->AddLogLine(true, GetResString(IDS_ERR_ALREADY_DOWNLOADING), file->GetFileName() );
return true;
}
return false;
}
void CDownloadQueue::Process(){
uint32 downspeed = 0;
if (app_prefs->GetMaxDownload() != UNLIMITED && datarate > 1500){
downspeed = (app_prefs->GetMaxDownload()*1024*100)/(datarate+1); //(uint16)((float)((float)(app_prefs->GetMaxDownload()*1024)/(datarate+1)) * 100);
if (downspeed < 50)
downspeed = 50;
else if (downspeed > 200)
downspeed = 200;
//if (app_prefs->GetMaxDownload()*1024 < datarate)
// downspeed = 0xFFF;
}
datarate = 0;
udcounter++;
for (POSITION pos =filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if ((cur_file->GetStatus() == PS_READY || cur_file->GetStatus() == PS_EMPTY) && cur_file->GetDownPriority() == PR_HIGH){
datarate += cur_file->Process(downspeed,udcounter);
}
}
for (POSITION pos =filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if ((cur_file->GetStatus() == PS_READY || cur_file->GetStatus() == PS_EMPTY) && cur_file->GetDownPriority() == PR_NORMAL){
datarate += cur_file->Process(downspeed,udcounter);
}
}
for (POSITION pos =filelist.GetHeadPosition();pos != 0;filelist.GetNext(pos)){
CPartFile* cur_file = filelist.GetAt(pos);
if ((cur_file->GetStatus() == PS_READY || cur_file->GetStatus() == PS_EMPTY) && (cur_file->GetDownPriority() == PR_LOW)){
datarate += cur_file->Process(downspeed,udcounter);
}
}
if (udcounter == 5){
if((!lastudpstattime) || (::GetTickCount() - lastudpstattime) > UDPSERVERSTATTIME){
lastudpstattime = ::GetTickCount();
theApp.serverlist->ServerStats();
}
}
if (udcounter == 10){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -