📄 downloadmanager.cpp
字号:
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
return false;
} catch(const Exception& e) {
delete file;
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, e.getError());
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
return false;
}
d->setFile(file);
if(SETTING(BUFFER_SIZE) > 0 ) {
d->setFile(new BufferedOutputStream<true>(d->getFile()));
}
bool sfvcheck = BOOLSETTING(SFV_CHECK) && (d->getPos() == 0) && (SFVReader(d->getTarget()).hasCRC());
if(sfvcheck) {
d->setFlag(Download::FLAG_CALC_CRC32);
Download::CrcOS* crc = new Download::CrcOS(d->getFile());
d->setCrcCalc(crc);
d->setFile(crc);
}
/** @todo something when resuming... */
if(d->getTreeValid()) {
if((d->getPos() % d->getTigerTree().getBlockSize()) == 0) {
d->setFile(new MerkleCheckOutputStream<TigerTree, true>(d->getTigerTree(), d->getFile(), d->getPos()));
}
}
if(d->isSet(Download::FLAG_ROLLBACK)) {
d->setFile(new RollbackOutputStream<true>(file, d->getFile(), (size_t)min((int64_t)SETTING(ROLLBACK), d->getSize() - d->getPos())));
}
}
if(z) {
d->setFlag(Download::FLAG_ZDOWNLOAD);
d->setFile(new FilteredOutputStream<UnZFilter, true>(d->getFile()));
}
dcassert(d->getPos() != -1);
d->setStart(GET_TICK());
aSource->setState(UserConnection::STATE_DONE);
fire(DownloadManagerListener::Starting(), d);
return true;
}
void DownloadManager::on(UserConnectionListener::Data, UserConnection* aSource, const u_int8_t* aData, size_t aLen) throw() {
Download* d = aSource->getDownload();
dcassert(d != NULL);
try {
d->addPos(d->getFile()->write(aData, aLen), aLen);
if(d->getPos() > d->getSize()) {
throw Exception(STRING(TOO_MUCH_DATA));
} else if(d->getPos() == d->getSize()) {
handleEndData(aSource);
aSource->setLineMode();
}
} catch(const RollbackException& e) {
string target = d->getTarget();
QueueManager::getInstance()->removeSource(target, aSource->getUser(), QueueItem::Source::FLAG_ROLLBACK_INCONSISTENCY);
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, e.getError());
d->resetPos();
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
return;
} catch(const FileException& e) {
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, e.getError());
d->resetPos();
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
return;
} catch(const Exception& e) {
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, e.getError());
// Nuke the bytes we have written, this is probably a compression error
d->resetPos();
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
return;
}
}
/** Download finished! */
void DownloadManager::handleEndData(UserConnection* aSource) {
dcassert(aSource->getState() == UserConnection::STATE_DONE);
Download* d = aSource->getDownload();
dcassert(d != NULL);
if(d->isSet(Download::FLAG_TREE_DOWNLOAD)) {
d->getFile()->flush();
delete d->getFile();
d->setFile(NULL);
int64_t bl = 1024;
while(bl * (int64_t)d->getTigerTree().getLeaves().size() < d->getTigerTree().getFileSize())
bl *= 2;
d->getTigerTree().setBlockSize(bl);
d->getTigerTree().calcRoot();
if(!(*d->getTTH() == d->getTigerTree().getRoot())) {
// This tree is for a different file, remove from queue...
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, STRING(INVALID_TREE));
string target = d->getTarget();
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
QueueManager::getInstance()->removeSource(target, aSource->getUser(), QueueItem::Source::FLAG_BAD_TREE, false);
checkDownloads(aSource);
return;
}
d->setTreeValid(true);
} else {
// Hm, if the real crc == 0, we'll get a file reread extra, but what the heck...
u_int32_t crc = 0;
// First, finish writing the file (flushing the buffers and closing the file...)
try {
d->getFile()->flush();
if(d->getCrcCalc() != NULL)
crc = d->getCrcCalc()->getFilter().getValue();
delete d->getFile();
d->setFile(NULL);
d->setCrcCalc(NULL);
// Check if we're anti-fragging...
if(d->isSet(Download::FLAG_ANTI_FRAG)) {
// Ok, rename the file to what we expect it to be...
try {
const string& tgt = d->getTempTarget().empty() ? d->getTarget() : d->getTempTarget();
File::renameFile(d->getDownloadTarget(), tgt);
d->unsetFlag(Download::FLAG_ANTI_FRAG);
} catch(const FileException& e) {
dcdebug("AntiFrag: %s\n", e.getError().c_str());
// Now what?
}
}
} catch(const FileException& e) {
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, e.getError());
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
return;
}
dcassert(d->getPos() == d->getSize());
dcdebug("Download finished: %s, size " I64_FMT ", downloaded " I64_FMT "\n", d->getTarget().c_str(), d->getSize(), d->getTotal());
// Check if we have some crc:s...
if(BOOLSETTING(SFV_CHECK)) {
if(!checkSfv(aSource, d, crc))
return;
}
if(BOOLSETTING(LOG_DOWNLOADS) && (BOOLSETTING(LOG_FILELIST_TRANSFERS) || !d->isSet(Download::FLAG_USER_LIST)) && !d->isSet(Download::FLAG_TREE_DOWNLOAD)) {
logDownload(aSource, d);
}
// Check if we need to move the file
if( !d->getTempTarget().empty() && (Util::stricmp(d->getTarget().c_str(), d->getTempTarget().c_str()) != 0) ) {
moveFile(d->getTempTarget(), d->getTarget());
}
}
removeDownload(d);
fire(DownloadManagerListener::Complete(), d);
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, true);
checkDownloads(aSource);
}
u_int32_t DownloadManager::calcCrc32(const string& file) throw(FileException) {
File ff(file, File::READ, File::OPEN);
CalcInputStream<CRC32Filter, false> f(&ff);
const size_t BUF_SIZE = 1024*1024;
AutoArray<u_int8_t> b(BUF_SIZE);
size_t n = BUF_SIZE;
while(f.read((u_int8_t*)b, n) > 0)
; // Keep on looping...
return f.getFilter().getValue();
}
bool DownloadManager::checkSfv(UserConnection* aSource, Download* d, u_int32_t crc) {
SFVReader sfv(d->getTarget());
if(sfv.hasCRC()) {
bool crcMatch = (crc == sfv.getCRC());
if(!crcMatch && crc == 0) {
// Blah. We have to reread the file...
try {
crcMatch = (calcCrc32(d->getDownloadTarget()) == sfv.getCRC());
} catch(const FileException& ) {
// Couldn't read the file to get the CRC(!!!)
crcMatch = false;
}
}
if(!crcMatch) {
File::deleteFile(d->getDownloadTarget());
dcdebug("DownloadManager: CRC32 mismatch for %s\n", d->getTarget().c_str());
LogManager::getInstance()->message(STRING(SFV_INCONSISTENCY) + " (" + STRING(FILE) + ": " + d->getTarget() + ")");
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, STRING(SFV_INCONSISTENCY));
string target = d->getTarget();
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
QueueManager::getInstance()->removeSource(target, aSource->getUser(), QueueItem::Source::FLAG_CRC_WARN, false);
checkDownloads(aSource);
return false;
}
d->setFlag(Download::FLAG_CRC32_OK);
dcdebug("DownloadManager: CRC32 match for %s\n", d->getTarget().c_str());
}
return true;
}
void DownloadManager::logDownload(UserConnection* aSource, Download* d) {
StringMap params;
params["target"] = d->getTarget();
params["user"] = aSource->getUser()->getNick();
params["userip"] = aSource->getRemoteIp();
params["hub"] = aSource->getUser()->getLastHubName();
params["hubip"] = aSource->getUser()->getLastHubAddress();
params["size"] = Util::toString(d->getSize());
params["sizeshort"] = Util::formatBytes(d->getSize());
params["chunksize"] = Util::toString(d->getTotal());
params["chunksizeshort"] = Util::formatBytes(d->getTotal());
params["actualsize"] = Util::toString(d->getActual());
params["actualsizeshort"] = Util::formatBytes(d->getActual());
params["speed"] = Util::formatBytes(d->getAverageSpeed()) + "/s";
params["time"] = Util::formatSeconds((GET_TICK() - d->getStart()) / 1000);
params["sfv"] = Util::toString(d->isSet(Download::FLAG_CRC32_OK) ? 1 : 0);
TTHValue *hash = d->getTTH();
if(hash != NULL) {
params["tth"] = d->getTTH()->toBase32();
}
LOG(LogManager::DOWNLOAD, params);
}
void DownloadManager::moveFile(const string& source, const string& target) {
try {
File::ensureDirectory(target);
if(File::getSize(source) > MOVER_LIMIT) {
mover.moveFile(source, target);
} else {
File::renameFile(source, target);
}
} catch(const FileException&) {
try {
if(!SETTING(DOWNLOAD_DIRECTORY).empty()) {
File::renameFile(source, SETTING(DOWNLOAD_DIRECTORY) + Util::getFileName(target));
} else {
File::renameFile(source, Util::getFilePath(source) + Util::getFileName(target));
}
} catch(const FileException&) {
try {
File::renameFile(source, Util::getFilePath(source) + Util::getFileName(target));
} catch(const FileException&) {
// Ignore...
}
}
}
}
void DownloadManager::on(UserConnectionListener::MaxedOut, UserConnection* aSource) throw() {
noSlots(aSource);
}
void DownloadManager::noSlots(UserConnection* aSource) {
if(aSource->getState() != UserConnection::STATE_FILELENGTH && aSource->getState() != UserConnection::STATE_TREE) {
dcdebug("DM::onMaxedOut Bad state, ignoring\n");
return;
}
Download* d = aSource->getDownload();
dcassert(d != NULL);
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, STRING(NO_SLOTS_AVAILABLE));
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource, false, !aSource->isSet(UserConnection::FLAG_NMDC));
}
void DownloadManager::on(UserConnectionListener::Failed, UserConnection* aSource, const string& aError) throw() {
Download* d = aSource->getDownload();
if(d == NULL) {
removeConnection(aSource);
return;
}
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, aError);
string target = d->getTarget();
aSource->setDownload(NULL);
QueueManager::getInstance()->putDownload(d, false);
removeConnection(aSource);
}
void DownloadManager::removeDownload(Download* d) {
if(d->getFile()) {
if(d->getActual() > 0) {
try {
d->getFile()->flush();
} catch(const Exception&) {
}
}
delete d->getFile();
d->setFile(NULL);
d->setCrcCalc(NULL);
if(d->isSet(Download::FLAG_ANTI_FRAG)) {
// Ok, set the pos to whereever it was last writing and hope for the best...
d->unsetFlag(Download::FLAG_ANTI_FRAG);
}
}
{
Lock l(cs);
// Either I'm stupid or the msvc7 optimizer is doing something _very_ strange here...
// STL-port -D_STL_DEBUG complains that .begin() and .end() don't have the same owner (!),
// but only in release build
dcassert(find(downloads.begin(), downloads.end(), d) != downloads.end());
// downloads.erase(find(downloads.begin(), downloads.end(), d));
for(Download::Iter i = downloads.begin(); i != downloads.end(); ++i) {
if(*i == d) {
downloads.erase(i);
break;
}
}
}
}
void DownloadManager::abortDownload(const string& aTarget) {
Lock l(cs);
for(Download::Iter i = downloads.begin(); i != downloads.end(); ++i) {
Download* d = *i;
if(d->getTarget() == aTarget) {
dcassert(d->getUserConnection() != NULL);
d->getUserConnection()->disconnect();
break;
}
}
}
void DownloadManager::on(UserConnectionListener::FileNotAvailable, UserConnection* aSource) throw() {
fileNotAvailable(aSource);
}
/** @todo Handle errors better */
void DownloadManager::on(AdcCommand::STA, UserConnection* aSource, const AdcCommand& cmd) throw() {
if(cmd.getParameters().size() < 2) {
aSource->disconnect();
return;
}
const string& err = cmd.getParameters()[0];
if(err.length() < 3) {
aSource->disconnect();
return;
}
switch(Util::toInt(err.substr(0, 1))) {
case AdcCommand::SEV_FATAL:
aSource->disconnect();
return;
case AdcCommand::SEV_RECOVERABLE:
switch(Util::toInt(err.substr(1))) {
case AdcCommand::ERROR_FILE_NOT_AVAILABLE:
fileNotAvailable(aSource);
return;
case AdcCommand::ERROR_SLOTS_FULL:
noSlots(aSource);
return;
}
}
aSource->disconnect();
}
void DownloadManager::fileNotAvailable(UserConnection* aSource) {
Download* d = aSource->getDownload();
dcassert(d != NULL);
dcdebug("File Not Available: %s\n", d->getTarget().c_str());
if(d->getFile()) {
delete d->getFile();
d->setFile(NULL);
d->setCrcCalc(NULL);
}
removeDownload(d);
fire(DownloadManagerListener::Failed(), d, d->getTargetFileName() + ": " + STRING(FILE_NOT_AVAILABLE));
aSource->setDownload(NULL);
QueueManager::getInstance()->removeSource(d->getTarget(), aSource->getUser(), d->isSet(Download::FLAG_TREE_DOWNLOAD) ? QueueItem::Source::FLAG_NO_TREE : QueueItem::Source::FLAG_FILE_NOT_AVAILABLE, false);
QueueManager::getInstance()->putDownload(d, false);
checkDownloads(aSource);
}
/**
* @file
* $Id: DownloadManager.cpp,v 1.149 2005/03/19 09:02:45 arnetheduck Exp $
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -