commit 02070e64b4dee1d399ffb4cd437a03ec78ce3b39
parent fd857c4c7f29a83cacc78fd9b0ad4871eb2be48a
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 23 Nov 2015 22:43:38 -0500
report HTTP error descriptions as well
and use simple C-style strings instead of std::string
Diffstat:
3 files changed, 43 insertions(+), 20 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -10,9 +10,11 @@ std::vector<Download *> Download::s_active;
static const int DOWNLOAD_TIMEOUT = 10;
Download::Download(const char *url)
- : m_threadHandle(0), m_aborted(false), m_finished(false), m_status(0)
+ : m_threadHandle(0), m_contents(0)
{
m_url = url;
+
+ reset();
}
Download::~Download()
@@ -23,6 +25,21 @@ Download::~Download()
// call stop after removing from the active list to prevent
// bad access from timeTick -> execCallbacks
stop();
+
+ // free the content buffer
+ reset();
+}
+
+void Download::reset()
+{
+ m_aborted = false;
+ m_finished = false;
+ m_status = 0;
+
+ if(m_contents) {
+ delete[] m_contents;
+ m_contents = 0;
+ }
}
void Download::addCallback(const DownloadCallback &callback)
@@ -58,9 +75,7 @@ Download::StartCode Download::start()
if(m_threadHandle)
return AlreadyRunning;
- m_finished = false;
- m_status = 0;
- m_contents.clear();
+ reset();
s_active.push_back(this);
plugin_register("timer", (void*)timerTick);
@@ -79,6 +94,9 @@ void Download::stop()
WaitForSingleObject(m_threadHandle, INFINITE);
CloseHandle(m_threadHandle);
m_threadHandle = 0;
+
+ // do not call reset() here, m_finished must stay to true
+ // otherwise the callback will not be called
};
DWORD WINAPI Download::worker(void *ptr) // static
@@ -96,12 +114,12 @@ DWORD WINAPI Download::worker(void *ptr) // static
while(agent.run() == 0) {
if(download->isAborted()) {
- download->finish(-2, "aborted");
+ download->finish(-2, strdup("aborted"));
JNL::close_socketlib();
return 1;
}
else if(time(NULL) - startTime >= DOWNLOAD_TIMEOUT) {
- download->finish(-3, "timeout");
+ download->finish(-3, strdup("timeout"));
JNL::close_socketlib();
return 1;
}
@@ -112,23 +130,21 @@ DWORD WINAPI Download::worker(void *ptr) // static
const int status = agent.getreplycode();
const int size = agent.bytes_available();
- if(status) {
+ if(status == 200) {
char *buffer = new char[size];
agent.get_bytes(buffer, size);
download->finish(status, buffer);
-
- delete[] buffer;
}
else
- download->finish(status, agent.geterrorstr());
+ download->finish(status, strdup(agent.geterrorstr()));
JNL::close_socketlib();
return 0;
}
-void Download::finish(const int status, const std::string &contents)
+void Download::finish(const int status, const char *contents)
{
WDL_MutexLock lock(&m_mutex);
@@ -160,7 +176,7 @@ int Download::status()
return m_status;
}
-const std::string &Download::contents()
+const char *Download::contents()
{
WDL_MutexLock lock(&m_mutex);
diff --git a/src/download.hpp b/src/download.hpp
@@ -2,14 +2,13 @@
#define REAPACK_DOWNLOAD_HPP
#include <functional>
-#include <string>
#include <vector>
#include <WDL/mutex.h>
#include <reaper_plugin.h>
-typedef std::function<void(int, const std::string &)> DownloadCallback;
+typedef std::function<void(int, const char *)> DownloadCallback;
class Download {
public:
@@ -28,7 +27,7 @@ public:
bool isFinished();
bool isAborted();
int status();
- const std::string &contents();
+ const char *contents();
void addCallback(const DownloadCallback &);
StartCode start();
@@ -40,15 +39,16 @@ private:
static void timerTick();
static DWORD WINAPI worker(void *ptr);
- void finish(const int status, const std::string &contents);
+ void finish(const int status, const char *contents);
void execCallbacks();
void abort();
+ void reset();
HANDLE m_threadHandle;
bool m_aborted;
bool m_finished;
int m_status;
- std::string m_contents;
+ const char *m_contents;
const char *m_url;
std::vector<DownloadCallback> m_callback;
diff --git a/src/main.cpp b/src/main.cpp
@@ -1,6 +1,8 @@
#include "reapack.hpp"
#include "download.hpp"
+#include <cstdlib>
+
#define REAPERAPI_IMPLEMENT
#include <reaper_plugin_functions.h>
@@ -20,9 +22,14 @@ extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
if(REAPERAPI_LoadAPI(rec->GetFunc) > 0)
return 0;
- Download *down = new Download("http://cfillion.tk/");
- down->addCallback([=](const int status, const std::string &contents) {
- ShowMessageBox(contents.c_str(), std::to_string(status).c_str(), 0);
+ printf("%s\n", GetResourcePath());
+
+ Download *down = new Download("http://perdu.com/3");
+ down->addCallback([=](const int status, const char *contents) {
+ char strStatus[3];
+ sprintf(strStatus, "%d", status);
+
+ ShowMessageBox(contents, strStatus, 0);
delete down;
});
down->start();