本文整理汇总了C++中Download::getPos方法的典型用法代码示例。如果您正苦于以下问题:C++ Download::getPos方法的具体用法?C++ Download::getPos怎么用?C++ Download::getPos使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Download
的用法示例。
在下文中一共展示了Download::getPos方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: endData
/** Download finished! */
void DownloadManager::endData(UserConnection* aSource) {
dcassert(aSource->getState() == UserConnection::STATE_RUNNING);
Download* d = aSource->getDownload();
dcassert(d != NULL);
if(d->getType() == Transfer::TYPE_TREE) {
d->getFile()->flush();
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, _("Full tree does not match TTH root"));
QueueManager::getInstance()->removeSource(d->getPath(), aSource->getUser(), QueueItem::Source::FLAG_BAD_TREE, false);
QueueManager::getInstance()->putDownload(d, false);
checkDownloads(aSource);
return;
}
d->setTreeValid(true);
} else {
// First, finish writing the file (flushing the buffers and closing the file...)
try {
d->getFile()->flush();
} catch(const Exception& e) {
d->resetPos();
failDownload(aSource, e.getError());
return;
}
aSource->setSpeed(d->getAverageSpeed());
aSource->updateChunkSize(d->getTigerTree().getBlockSize(), d->getSize(), GET_TICK() - d->getStart());
dcdebug("Download finished: %s, size " I64_FMT ", downloaded " I64_FMT "\n", d->getPath().c_str(),
static_cast<long long int>(d->getSize()), static_cast<long long int>(d->getPos()));
if(BOOLSETTING(LOG_DOWNLOADS) && (BOOLSETTING(LOG_FILELIST_TRANSFERS) || d->getType() == Transfer::TYPE_FILE)) {
logDownload(aSource, d);
}
}
removeDownload(d);
fire(DownloadManagerListener::Complete(), d);
QueueManager::getInstance()->putDownload(d, true);
checkDownloads(aSource);
}
示例2: on
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);
fire(DownloadManagerListener::Failed(), d, e.getError());
d->resetPos();
aSource->setDownload(NULL);
removeDownload(d, true);
removeConnection(aSource);
return;
} catch(const FileException& e) {
fire(DownloadManagerListener::Failed(), d, e.getError());
d->resetPos();
aSource->setDownload(NULL);
removeDownload(d, true);
removeConnection(aSource);
return;
} catch(const Exception& e) {
fire(DownloadManagerListener::Failed(), d, e.getError());
// Nuke the bytes we have written, this is probably a compression error
d->resetPos();
aSource->setDownload(NULL);
removeDownload(d, true);
removeConnection(aSource);
return;
}
}
示例3: on
void DownloadManager::on(TimerManagerListener::Second, uint64_t aTick) noexcept {
typedef vector<pair<string, UserPtr> > TargetList;
TargetList dropTargets;
{
Lock l(cs);
DownloadList tickList;
// Tick each ongoing download
for(DownloadList::iterator i = downloads.begin(); i != downloads.end(); ++i) {
if((*i)->getPos() > 0) {
tickList.push_back(*i);
(*i)->tick();
}
}
if(tickList.size() > 0)
fire(DownloadManagerListener::Tick(), tickList);
// Automatically remove or disconnect slow sources
if((uint32_t)(aTick / 1000) % SETTING(AUTODROP_INTERVAL) == 0) {
for(DownloadList::iterator i = downloads.begin(); i != downloads.end(); ++i) {
Download* d = *i;
uint64_t timeElapsed = aTick - d->getStart();
uint64_t timeInactive = aTick - d->getUserConnection().getLastActivity();
uint64_t bytesDownloaded = d->getPos();
bool timeElapsedOk = timeElapsed >= (uint32_t)SETTING(AUTODROP_ELAPSED) * 1000;
bool timeInactiveOk = timeInactive <= (uint32_t)SETTING(AUTODROP_INACTIVITY) * 1000;
bool speedTooLow = timeElapsedOk && timeInactiveOk && bytesDownloaded > 0 ?
bytesDownloaded / timeElapsed * 1000 < (uint32_t)SETTING(AUTODROP_SPEED) : false;
bool isUserList = d->getType() == Transfer::TYPE_FULL_LIST;
bool onlineSourcesOk = isUserList ?
true : QueueManager::getInstance()->countOnlineSources(d->getPath()) >= SETTING(AUTODROP_MINSOURCES);
bool filesizeOk = !isUserList && d->getSize() >= ((int64_t)SETTING(AUTODROP_FILESIZE)) * 1024;
bool dropIt = (isUserList && BOOLSETTING(AUTODROP_FILELISTS)) ||
(filesizeOk && BOOLSETTING(AUTODROP_ALL));
if(speedTooLow && onlineSourcesOk && dropIt) {
if(BOOLSETTING(AUTODROP_DISCONNECT) && isUserList) {
d->getUserConnection().disconnect();
} else {
dropTargets.push_back(make_pair(d->getPath(), d->getUser()));
}
}
}
}
}
for(TargetList::iterator i = dropTargets.begin(); i != dropTargets.end(); ++i) {
QueueManager::getInstance()->removeSource(i->first, i->second, QueueItem::Source::FLAG_SLOW_SOURCE);
}
}
示例4: handleEndData
/** 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);
Download* old = d->getOldDownload();
size_t bl = 1024;
while(bl * old->getTigerTree().getLeaves().size() < old->getSize())
bl *= 2;
old->getTigerTree().setBlockSize(bl);
dcassert(old->getSize() != -1);
old->getTigerTree().setFileSize(old->getSize());
old->getTigerTree().calcRoot();
if(!(*old->getTTH() == old->getTigerTree().getRoot())) {
// This tree is for a different file, remove from queue...
fire(DownloadManagerListener::Failed(), old, STRING(INVALID_TREE));
string target = old->getTarget();
aSource->setDownload(NULL);
removeDownload(old, true);
QueueManager::getInstance()->removeSource(target, aSource->getUser(), QueueItem::Source::FLAG_BAD_TREE, false);
checkDownloads(aSource);
return;
}
d->getOldDownload()->setTreeValid(true);
HashManager::getInstance()->addTree(old->getTarget(), old->getTigerTree());
aSource->setDownload(d->getOldDownload());
delete d;
// Ok, now we can continue to the actual file...
checkDownloads(aSource);
return;
}
u_int32_t crc = 0;
bool hasCrc = (d->getCrcCalc() != NULL);
// First, finish writing the file (flushing the buffers and closing the file...)
try {
d->getFile()->flush();
if(hasCrc)
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) {
fire(DownloadManagerListener::Failed(), d, e.getError());
aSource->setDownload(NULL);
removeDownload(d, true);
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)) {
SFVReader sfv(d->getTarget());
if(sfv.hasCRC()) {
bool crcMatch;
string tgt = d->getDownloadTarget();
if(hasCrc) {
crcMatch = (crc == sfv.getCRC());
} else {
// More complicated, we have to reread the file
try {
File ff(tgt, File::READ, File::OPEN);
CalcInputStream<CRC32Filter, false> f(&ff);
//.........这里部分代码省略.........
示例5: prepareFile
bool DownloadManager::prepareFile(UserConnection* aSource, int64_t newSize /* = -1 */) {
Download* d = aSource->getDownload();
dcassert(d != NULL);
if(newSize != -1) {
d->setSize(newSize);
}
if(d->getPos() >= d->getSize()) {
// Already finished?
aSource->setDownload(NULL);
removeDownload(d, true, true);
removeConnection(aSource);
return false;
}
dcassert(d->getSize() != -1);
string target = d->getDownloadTarget();
File::ensureDirectory(target);
if(d->isSet(Download::FLAG_USER_LIST)) {
if(aSource->isSet(UserConnection::FLAG_SUPPORTS_XML_BZLIST)) {
target += ".xml.bz2";
} else {
target += ".DcLst";
}
}
File* file = NULL;
try {
// Let's check if we can find this file in a any .SFV...
int trunc = d->isSet(Download::FLAG_RESUME) ? 0 : File::TRUNCATE;
file = new File(target, File::RW, File::OPEN | File::CREATE | trunc);
if(d->isSet(Download::FLAG_ANTI_FRAG)) {
file->setSize(d->getSize());
}
file->setPos(d->getPos());
} catch(const FileException& e) {
delete file;
fire(DownloadManagerListener::Failed(), d, STRING(COULD_NOT_OPEN_TARGET_FILE) + e.getError());
aSource->setDownload(NULL);
removeDownload(d, true);
removeConnection(aSource);
return false;
} catch(const Exception& e) {
delete file;
fire(DownloadManagerListener::Failed(), d, e.getError());
aSource->setDownload(NULL);
removeDownload(d, true);
removeConnection(aSource);
return false;
}
d->setFile(file);
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(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);
}
if(d->getPos() == 0) {
if(!d->getTreeValid() && d->getTTH() != NULL && d->getSize() < numeric_limits<size_t>::max()) {
// We make a single node tree...
d->getTigerTree().setFileSize(d->getSize());
d->getTigerTree().setBlockSize((size_t)d->getSize());
d->getTigerTree().getLeaves().push_back(*d->getTTH());
d->getTigerTree().calcRoot();
d->setTreeValid(true);
}
if(d->getTreeValid()) {
d->setFile(new TigerCheckOutputStream<true>(d->getTigerTree(), d->getFile()));
}
}
if(d->isSet(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;
}
示例6: checkDownloads
void DownloadManager::checkDownloads(UserConnection* aConn) {
Download* d = aConn->getDownload();
bool firstTry = false;
if(d == NULL) {
firstTry = true;
bool slotsFull = (SETTING(DOWNLOAD_SLOTS) != 0) && (getDownloads() >= (size_t)SETTING(DOWNLOAD_SLOTS));
bool speedFull = (SETTING(MAX_DOWNLOAD_SPEED) != 0) && (getAverageSpeed() >= (SETTING(MAX_DOWNLOAD_SPEED)*1024));
if( slotsFull || speedFull ) {
bool extraFull = (SETTING(DOWNLOAD_SLOTS) != 0) && (getDownloads() >= (size_t)(SETTING(DOWNLOAD_SLOTS)+3));
if(extraFull || !QueueManager::getInstance()->hasDownload(aConn->getUser(), QueueItem::HIGHEST)) {
removeConnection(aConn);
return;
}
}
d = QueueManager::getInstance()->getDownload(aConn->getUser());
if(d == NULL) {
removeConnection(aConn, true);
return;
}
{
Lock l(cs);
downloads.push_back(d);
}
d->setUserConnection(aConn);
aConn->setDownload(d);
}
if(firstTry && !d->getTreeValid() &&
!d->isSet(Download::FLAG_USER_LIST) && d->getTTH() != NULL)
{
if(HashManager::getInstance()->getTree(d->getTarget(), d->getTTH(), d->getTigerTree())) {
d->setTreeValid(true);
} else if(!d->isSet(Download::FLAG_TREE_TRIED) &&
aConn->isSet(UserConnection::FLAG_SUPPORTS_TTHL))
{
// So, we need to download the tree...
Download* tthd = new Download();
tthd->setOldDownload(d);
tthd->setFlag(Download::FLAG_TREE_DOWNLOAD);
tthd->setTarget(d->getTarget());
tthd->setSource(d->getSource());
tthd->setUserConnection(aConn);
aConn->setDownload(tthd);
aConn->setState(UserConnection::STATE_TREE);
// Hack to get by TTH if possible
tthd->setTTH(d->getTTH());
aConn->send(tthd->getCommand(false, aConn->isSet(UserConnection::FLAG_SUPPORTS_TTHF)));
tthd->setTTH(NULL);
return;
}
}
aConn->setState(UserConnection::STATE_FILELENGTH);
if(d->isSet(Download::FLAG_RESUME)) {
dcassert(d->getSize() != -1);
const string& target = (d->getTempTarget().empty() ? d->getTarget() : d->getTempTarget());
int64_t start = File::getSize(target);
// Only use antifrag if we don't have a previous non-antifrag part
if( BOOLSETTING(ANTI_FRAG) && (start == -1) && (d->getSize() != -1) ) {
int64_t aSize = File::getSize(target + Download::ANTI_FRAG_EXT);
if(aSize == d->getSize())
start = d->getPos();
else
start = 0;
d->setFlag(Download::FLAG_ANTI_FRAG);
}
int rollback = SETTING(ROLLBACK);
if(rollback > start) {
d->setStartPos(0);
} else {
d->setStartPos(start - rollback);
d->setFlag(Download::FLAG_ROLLBACK);
}
} else {
d->setStartPos(0);
}
if(d->isSet(Download::FLAG_USER_LIST)) {
if(aConn->isSet(UserConnection::FLAG_SUPPORTS_XML_BZLIST)) {
d->setSource("files.xml.bz2");
}
}
if(aConn->isSet(UserConnection::FLAG_SUPPORTS_ADCGET) && d->isSet(Download::FLAG_UTF8)) {
//.........这里部分代码省略.........
示例7: getNextSegment
//.........这里部分代码省略.........
targetSize = Util::roundDown(targetSize, blockSize);
} else {
targetSize = blockSize;
}
int64_t start = 0;
int64_t curSize = targetSize;
while(start < getSize()) {
int64_t end = std::min(getSize(), start + curSize);
Segment block(start, end - start);
bool overlaps = false;
for(SegmentConstIter i = done.begin(); !overlaps && i != done.end(); ++i) {
if(curSize <= blockSize) {
int64_t dstart = i->getStart();
int64_t dend = i->getEnd();
// We accept partial overlaps, only consider the block done if it is fully consumed by the done block
if(dstart <= start && dend >= end) {
overlaps = true;
}
} else {
overlaps = block.overlaps(*i);
}
}
for(auto i = downloads.begin(); !overlaps && i != downloads.end(); ++i) {
overlaps = block.overlaps((*i)->getSegment());
}
if(!overlaps) {
if(partialSource) {
// store all chunks we could need
for(vector<int64_t>::const_iterator j = posArray.begin(); j < posArray.end(); j += 2){
if( (*j <= start && start < *(j+1)) || (start <= *j && *j < end) ) {
int64_t b = max(start, *j);
int64_t e = min(end, *(j+1));
// segment must be blockSize aligned
dcassert(b % blockSize == 0);
dcassert(e % blockSize == 0 || e == getSize());
neededParts.push_back(Segment(b, e - b));
}
}
} else {
return block;
}
}
if(overlaps && (curSize > blockSize)) {
curSize -= blockSize;
} else {
start = end;
curSize = targetSize;
}
}
if(!neededParts.empty()) {
// select random chunk for download
dcdebug("Found chunks: %d\n", neededParts.size());
Segment& selected = neededParts[Util::rand(0, neededParts.size())];
selected.setSize(std::min(selected.getSize(), targetSize)); // request only wanted size
return selected;
}
if(partialSource == NULL && BOOLSETTING(OVERLAP_CHUNKS) && lastSpeed > 0) {
// overlap slow running chunk
for(auto i = downloads.begin(); i != downloads.end(); ++i) {
Download* d = *i;
// current chunk mustn't be already overlapped
if(d->getOverlapped())
continue;
// current chunk must be running at least for 4 seconds
if(d->getStart() == 0 || GET_TICK() - d->getStart() < 4000)
continue;
// current chunk mustn't be finished in next 20 seconds
if(d->getSecondsLeft() < 20)
continue;
// overlap current chunk at last block boundary
int64_t pos = d->getPos() - (d->getPos() % blockSize);
int64_t size = d->getSize() - pos;
// new user should finish this chunk more than 2x faster
int64_t newChunkLeft = size / lastSpeed;
if(2 * newChunkLeft < d->getSecondsLeft()) {
dcdebug("Overlapping... old user: %I64d s, new user: %I64d s\n", d->getSecondsLeft(), newChunkLeft);
return Segment(d->getStartPos() + pos, size, true);
}
}
}
return Segment(0, 0);
}
示例8: startData
void DownloadManager::startData(UserConnection* aSource, int64_t start, int64_t bytes, bool z) {
Download* d = aSource->getDownload();
dcassert(d != NULL);
dcdebug("Preparing " I64_FMT ":" I64_FMT ", " I64_FMT ":" I64_FMT"\n",
static_cast<long long int>(d->getStartPos()), static_cast<long long int>(start),
static_cast<long long int>(d->getSize()), static_cast<long long int>(bytes));
if(d->getSize() == -1) {
if(bytes >= 0) {
d->setSize(bytes);
} else {
failDownload(aSource, _("Invalid size"));
return;
}
} else if(d->getSize() != bytes || d->getStartPos() != start) {
// This is not what we requested...
failDownload(aSource, _("Response does not match request"));
return;
}
try {
QueueManager::getInstance()->setFile(d);
} catch(const FileException& e) {
failDownload(aSource, str(F_("Could not open target file: %1%") % e.getError()));
return;
} catch(const Exception& e) {
failDownload(aSource, e.getError());
return;
}
if((d->getType() == Transfer::TYPE_FILE || d->getType() == Transfer::TYPE_FULL_LIST) && SETTING(BUFFER_SIZE) > 0 ) {
d->setFile(new BufferedOutputStream<true>(d->getFile()));
}
if(d->getType() == Transfer::TYPE_FILE) {
typedef MerkleCheckOutputStream<TigerTree, true> MerkleStream;
d->setFile(new MerkleStream(d->getTigerTree(), d->getFile(), d->getStartPos()));
d->setFlag(Download::FLAG_TTH_CHECK);
}
// Check that we don't get too many bytes
d->setFile(new LimitedOutputStream<true>(d->getFile(), bytes));
if(z) {
d->setFlag(Download::FLAG_ZDOWNLOAD);
d->setFile(new FilteredOutputStream<UnZFilter, true>(d->getFile()));
}
d->setStart(GET_TICK());
d->tick();
aSource->setState(UserConnection::STATE_RUNNING);
fire(DownloadManagerListener::Starting(), d);
if(d->getPos() == d->getSize()) {
try {
// Already finished? A zero-byte file list could cause this...
endData(aSource);
} catch(const Exception& e) {
failDownload(aSource, e.getError());
}
} else {
aSource->setDataMode();
}
}