commit ca2323e4c76bc138b9c5593c1c6d09133a2883e7
parent 4e3d4c6ee26f32765a679eb435c1ceda032bbf8b
Author: cfillion <cfillion@users.noreply.github.com>
Date: Wed, 20 Jan 2016 23:46:49 -0500
overhaul download state handling
Diffstat:
3 files changed, 38 insertions(+), 23 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -53,9 +53,8 @@ Download::~Download()
void Download::reset()
{
+ m_state = Idle;
m_aborted = false;
- m_running = false;
- m_status = 0;
m_contents.clear();
}
@@ -82,7 +81,7 @@ void Download::start()
reset();
- m_running = true;
+ m_state = Running;
m_onStart();
RegisterStart();
@@ -110,12 +109,12 @@ DWORD WINAPI Download::Worker(void *ptr)
const CURLcode res = curl_easy_perform(curl);
if(download->isAborted()) {
- download->finish(-2, "aborted");
+ download->finish(Aborted, "aborted");
curl_easy_cleanup(curl);
return 1;
}
else if(res != CURLE_OK) {
- download->finish(0, curl_easy_strerror(res));
+ download->finish(Failure, curl_easy_strerror(res));
curl_easy_cleanup(curl);
return 1;
}
@@ -124,21 +123,25 @@ DWORD WINAPI Download::Worker(void *ptr)
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
// accept a status of 0 for the file:// protocol
- if(status == 200 || status == 0) {
+ switch(status) {
+ case 0: // for the file:// protocol
+ case 200: // HTTP OK
+ {
// strip headers
long headerSize = 0;
curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &headerSize);
contents.erase(0, headerSize);
- // always send 200, even for file://
- download->finish(200, contents);
+ download->finish(Success, contents);
+ break;
}
- else {
- // strip body
+ default:
+ // strip body, only keep error description
contents.erase(contents.find("\n"));
contents.erase(0, contents.find("\x20") + 1);
- download->finish(status, contents);
+ download->finish(Failure, contents);
+ break;
}
curl_easy_cleanup(curl);
@@ -191,14 +194,13 @@ Download *Download::NextFinished()
return dl;
}
-void Download::finish(const int status, const string &contents)
+void Download::finish(const State state, const string &contents)
{
// called from the worker thread
WDL_MutexLock lock(&m_mutex);
- m_running = false;
- m_status = status;
+ m_state = state;
m_contents = contents;
MarkAsFinished(this);
@@ -215,11 +217,18 @@ void Download::finishInMainThread()
m_onDestroy();
}
-bool Download::isRunning()
+auto Download::state() -> State
{
WDL_MutexLock lock(&m_mutex);
- return m_running;
+ return m_state;
+}
+
+const string &Download::contents()
+{
+ WDL_MutexLock lock(&m_mutex);
+
+ return m_contents;
}
bool Download::isAborted()
diff --git a/src/download.hpp b/src/download.hpp
@@ -33,6 +33,14 @@ public:
typedef boost::signals2::signal<void ()> Signal;
typedef Signal::slot_type Callback;
+ enum State {
+ Idle,
+ Running,
+ Success,
+ Failure,
+ Aborted,
+ };
+
static void Init();
static void Cleanup();
@@ -41,10 +49,9 @@ public:
const std::string &name() const { return m_name; }
const std::string &url() const { return m_url; }
- int status() const { return m_status; }
- const std::string &contents() const { return m_contents; }
- bool isRunning();
+ State state();
+ const std::string &contents();
bool isAborted();
void onStart(const Callback &callback) { m_onStart.connect(callback); }
@@ -67,7 +74,7 @@ private:
static size_t WriteData(char *, size_t, size_t, void *);
static DWORD WINAPI Worker(void *ptr);
- void finish(const int status, const std::string &contents);
+ void finish(const State state, const std::string &contents);
void finishInMainThread();
void reset();
@@ -77,9 +84,8 @@ private:
WDL_Mutex m_mutex;
HANDLE m_threadHandle;
+ State m_state;
bool m_aborted;
- bool m_running;
- int m_status;
std::string m_contents;
Signal m_onStart;
diff --git a/src/transaction.cpp b/src/transaction.cpp
@@ -204,7 +204,7 @@ void Transaction::cancel()
bool Transaction::saveFile(Download *dl, const Path &path)
{
- if(dl->status() != 200) {
+ if(dl->state() != Download::Success) {
addError(dl->contents(), dl->url());
return false;
}