diff options
author | Arno <arno@disconnect.de> | 2018-02-01 07:24:43 +0100 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2018-02-01 07:24:43 +0100 |
commit | f2604b62b777d7cab001bb5b7d80ad25b586e554 (patch) | |
tree | 7b8ef0abd8b87e9e71e33d246f705415aaa11814 /filecopier.cpp | |
parent | d6a44b289027db999e50a2b346327e6650a4f43c (diff) | |
download | ShemovCleaner-f2604b62b777d7cab001bb5b7d80ad25b586e554.tar.gz ShemovCleaner-f2604b62b777d7cab001bb5b7d80ad25b586e554.tar.bz2 ShemovCleaner-f2604b62b777d7cab001bb5b7d80ad25b586e554.zip |
Improve FileCopier
This endeavor started out quite innocently: fix the connect syntax in
ProgressDialog, but it quickly became much more. Vom Höxchen aufs
Stöxchen :)
First I thought it would be nice to add a total count to the
ProgressDialog. Then I realized that I had QElapsedTimer::restart()
totally wrong. It returns the ms *since the last restart*! It doesn't
reset to zero, so fix that.
While testing that I noticed that the download speed was quite below
average, so change the buffer size and only check if we were cancelled
when the timer elapsed. But that wasn't it. Eventually I dug into my
firewall rules and routings to get it right.
Diffstat (limited to 'filecopier.cpp')
-rw-r--r-- | filecopier.cpp | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/filecopier.cpp b/filecopier.cpp index 001b317..1588d6e 100644 --- a/filecopier.cpp +++ b/filecopier.cpp @@ -4,24 +4,21 @@ #include "filecopier.h" -FileCopier::FileCopier(QObject *parent) : QThread(parent), mCancel(false){} +FileCopier::FileCopier(QObject *parent) : QThread(parent), mCancel(false), mTotal(0), mCopied(0){} void FileCopier::addJob(const QString &source, const QString &dest){ QMutexLocker l(&mAddJobMutex); if(!mJobs.contains(source)){ mJobs[source] = dest; + ++mTotal; } } -QHash<QString, QString> FileCopier::jobs(){ - QMutexLocker l(&mAddJobMutex); - return mJobs; -} - void FileCopier::run(){ - int bufsize = 16 * 1024 * 1024; + int bufsize = 32 * 1024 * 1024; char *buf = new char[bufsize]; mCancel = false; + mCopied = 0; QElapsedTimer et; while(!mJobs.isEmpty()){ mAddJobMutex.lock(); @@ -45,31 +42,40 @@ void FileCopier::run(){ qint64 bytesSinceEl = 0; et.start(); while(!sFile.atEnd()){ - mCancelMutex.lock(); - bool cancel = mCancel; - mCancelMutex.unlock(); - if(cancel){ - QFile::remove(dest); - emit success(false, source); - goto cleanup; - } read = sFile.read(buf, bufsize); dFile.write(buf, read); total += read; bytesSinceEl += read; - elapsed += et.restart(); - if(elapsed > 5000){ //5 seconds - emit bytesReadIntval(bytesSinceEl, elapsed); + elapsed = et.elapsed(); + if(elapsed > 1000){ + mCancelMutex.lock(); + bool cancel = mCancel; + mCancelMutex.unlock(); + if(cancel){ + dFile.close(); + QFile::remove(dest); + emit success(false, source); + goto cleanup; + } + mCountMutex.lock(); + emit bytesReadIntval(bytesSinceEl, elapsed, mTotal, mCopied); + mCountMutex.unlock(); elapsed = 0; bytesSinceEl = 0; + et.restart(); } emit bytesRead(total); } emit success(true, source); + ++mCopied; + emit bytesReadIntval(bytesSinceEl, elapsed, mTotal, mCopied); + et.restart(); } cleanup: delete buf; + mTotal = 0; + mJobs.clear(); } void FileCopier::cancel(){ |