commit b4e46f426cfc6faf5fd853cec3ed9703a33cb64e
parent ecdc99394a260ea6fe802af996d6e9fd38a63168
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 26 Feb 2017 18:23:51 -0500
refactor curl context creation and cleanup
Diffstat:
5 files changed, 47 insertions(+), 43 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -20,7 +20,6 @@
#include "reapack.hpp"
#include <boost/format.hpp>
-#include <curl/curl.h>
#include <reaper_plugin_functions.h>
@@ -44,7 +43,7 @@ static void UnlockCurlMutex(CURL *, curl_lock_data, curl_lock_access, void *)
g_curlMutex.Leave();
}
-void Download::GlobalInit()
+void DownloadContext::GlobalInit()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
@@ -58,36 +57,34 @@ void Download::GlobalInit()
curl_share_setopt(g_curlShare, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
}
-void Download::GlobalCleanup()
+void DownloadContext::GlobalCleanup()
{
curl_share_cleanup(g_curlShare);
curl_global_cleanup();
}
-void *Download::ContextInit()
+DownloadContext::DownloadContext()
{
- CURL *curl = curl_easy_init();
+ m_curl = curl_easy_init();
const auto userAgent = format("ReaPack/%s REAPER/%s")
% ReaPack::VERSION % GetAppVersion();
- curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.str().c_str());
- curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
- curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, DOWNLOAD_TIMEOUT);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, DOWNLOAD_TIMEOUT);
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
- curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
- curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
- curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
- curl_easy_setopt(curl, CURLOPT_SHARE, g_curlShare);
- curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
-
- return curl;
+ curl_easy_setopt(m_curl, CURLOPT_USERAGENT, userAgent.str().c_str());
+ curl_easy_setopt(m_curl, CURLOPT_LOW_SPEED_LIMIT, 1);
+ curl_easy_setopt(m_curl, CURLOPT_LOW_SPEED_TIME, DOWNLOAD_TIMEOUT);
+ curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, DOWNLOAD_TIMEOUT);
+ curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, true);
+ curl_easy_setopt(m_curl, CURLOPT_MAXREDIRS, 5);
+ curl_easy_setopt(m_curl, CURLOPT_ACCEPT_ENCODING, "");
+ curl_easy_setopt(m_curl, CURLOPT_FAILONERROR, true);
+ curl_easy_setopt(m_curl, CURLOPT_SHARE, g_curlShare);
+ curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, false);
}
-void Download::ContextCleanup(void *ctx)
+DownloadContext::~DownloadContext()
{
- curl_easy_cleanup(static_cast<CURL *>(ctx));
+ curl_easy_cleanup(m_curl);
}
size_t Download::WriteData(char *data, size_t rawsize, size_t nmemb, void *ptr)
@@ -123,7 +120,7 @@ void Download::start()
onFinish([thread] { delete thread; });
}
-void Download::run(CURL *curl)
+void Download::run(DownloadContext *ctx)
{
const auto finish = [&](const State state, const string &contents) {
m_contents = contents;
@@ -140,25 +137,25 @@ void Download::run(CURL *curl)
string contents;
- curl_easy_setopt(curl, CURLOPT_URL, m_url.c_str());
- curl_easy_setopt(curl, CURLOPT_PROXY, m_opts.proxy.c_str());
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, m_opts.verifyPeer);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_URL, m_url.c_str());
+ curl_easy_setopt(ctx->m_curl, CURLOPT_PROXY, m_opts.proxy.c_str());
+ curl_easy_setopt(ctx->m_curl, CURLOPT_SSL_VERIFYPEER, m_opts.verifyPeer);
- curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, UpdateProgress);
- curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_PROGRESSFUNCTION, UpdateProgress);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_PROGRESSDATA, this);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &contents);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_WRITEFUNCTION, WriteData);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_WRITEDATA, &contents);
curl_slist *headers = nullptr;
if(has(Download::NoCacheFlag))
headers = curl_slist_append(headers, "Cache-Control: no-cache");
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_HTTPHEADER, headers);
char errbuf[CURL_ERROR_SIZE] = "No details";
- curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
+ curl_easy_setopt(ctx->m_curl, CURLOPT_ERRORBUFFER, errbuf);
- const CURLcode res = curl_easy_perform(curl);
+ const CURLcode res = curl_easy_perform(ctx->m_curl);
if(m_abort)
finish(Aborted, "aborted by user");
diff --git a/src/download.hpp b/src/download.hpp
@@ -23,17 +23,24 @@
#include <string>
+#include <curl/curl.h>
+
+struct DownloadContext {
+ static void GlobalInit();
+ static void GlobalCleanup();
+
+ DownloadContext();
+ ~DownloadContext();
+
+ CURL *m_curl;
+};
+
class Download : public ThreadTask {
public:
enum Flag {
NoCacheFlag = 1<<0,
};
- static void GlobalInit();
- static void GlobalCleanup();
- static void *ContextInit();
- static void ContextCleanup(void *);
-
Download(const std::string &name, const std::string &url,
const NetworkOpts &, int flags = 0);
@@ -45,7 +52,7 @@ public:
const std::string &contents() { return m_contents; }
void start();
- void run(void *) override;
+ void run(DownloadContext *) override;
private:
static size_t WriteData(char *, size_t, size_t, void *);
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -81,7 +81,7 @@ ReaPack::ReaPack(REAPER_PLUGIN_HINSTANCE instance)
m_mainWindow = GetMainHwnd();
m_useRootPath = new UseRootPath(resourcePath());
- Download::GlobalInit();
+ DownloadContext::GlobalInit();
RichEdit::Init();
FS::mkdir(Path::CACHE);
@@ -106,7 +106,7 @@ ReaPack::~ReaPack()
m_config->write();
delete m_config;
- Download::GlobalCleanup();
+ DownloadContext::GlobalCleanup();
delete m_useRootPath;
}
diff --git a/src/thread.cpp b/src/thread.cpp
@@ -77,18 +77,16 @@ DWORD WINAPI WorkerThread::run(void *ptr)
{
WorkerThread *thread = static_cast<WorkerThread *>(ptr);
- void *context = Download::ContextInit();
+ DownloadContext context;
while(!thread->m_exit) {
while(ThreadTask *task = thread->nextTask())
- task->run(context);
+ task->run(&context);
ResetEvent(thread->m_wake);
WaitForSingleObject(thread->m_wake, INFINITE);
}
- Download::ContextCleanup(context);
-
return 0;
}
diff --git a/src/thread.hpp b/src/thread.hpp
@@ -33,6 +33,8 @@
#include <swell-types.h>
#endif
+struct DownloadContext;
+
class ThreadTask {
public:
enum State {
@@ -50,7 +52,7 @@ public:
virtual ~ThreadTask();
virtual std::string summary() const = 0;
- virtual void run(void *) = 0;
+ virtual void run(DownloadContext *) = 0;
void setState(State);
State state() const { return m_state; }