Skip to content

Commit

Permalink
Remove curl-helper dependency from switcher-data
Browse files Browse the repository at this point in the history
  • Loading branch information
WarmUpTill committed Dec 24, 2023
1 parent 0280b71 commit eeff6ae
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 74 deletions.
12 changes: 6 additions & 6 deletions src/legacy/switch-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ static std::string getRemoteData(std::string &url)
{
std::string readBuffer;

switcher->curl.SetOpt(CURLOPT_URL, url.c_str());
switcher->curl.SetOpt(CURLOPT_WRITEFUNCTION, WriteCallback);
switcher->curl.SetOpt(CURLOPT_WRITEDATA, &readBuffer);
CurlHelper::SetOpt(CURLOPT_URL, url.c_str());
CurlHelper::SetOpt(CURLOPT_WRITEFUNCTION, WriteCallback);
CurlHelper::SetOpt(CURLOPT_WRITEDATA, &readBuffer);

// Set timeout to at least one second
int timeout = switcher->interval / 1000;
if (timeout == 0) {
timeout = 1;
}
switcher->curl.SetOpt(CURLOPT_TIMEOUT, 1);
switcher->curl.Perform();
CurlHelper::SetOpt(CURLOPT_TIMEOUT, 1);
CurlHelper::Perform();

return readBuffer;
}
Expand Down Expand Up @@ -422,7 +422,7 @@ void AdvSceneSwitcher::SetupFileTab()
obs_module_text("AdvSceneSwitcher.fileTab.remoteFileWarning2"));
ui->remoteFileWarningLabel->hide();

if (switcher->curl.Initialized()) {
if (CurlHelper::Initialized()) {
ui->libcurlWarning->setVisible(false);
}

Expand Down
15 changes: 7 additions & 8 deletions src/macro-core/macro-action-clipboard.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "macro-action-clipboard.hpp"
#include "curl-helper.hpp"
#include "switcher-data.hpp"

#include <QApplication>
#include <QClipboard>
Expand Down Expand Up @@ -34,17 +33,17 @@ static std::optional<QImage> getImageFromUrl(const char *url)
{
QByteArray response;

switcher->curl.SetOpt(CURLOPT_URL, url);
switcher->curl.SetOpt(CURLOPT_HTTPGET, 1L);
switcher->curl.SetOpt(CURLOPT_TIMEOUT_MS, 30000);
switcher->curl.SetOpt(CURLOPT_WRITEFUNCTION, writeCallback);
switcher->curl.SetOpt(CURLOPT_WRITEDATA, &response);
auto code = switcher->curl.Perform();
CurlHelper::SetOpt(CURLOPT_URL, url);
CurlHelper::SetOpt(CURLOPT_HTTPGET, 1L);
CurlHelper::SetOpt(CURLOPT_TIMEOUT_MS, 30000);
CurlHelper::SetOpt(CURLOPT_WRITEFUNCTION, writeCallback);
CurlHelper::SetOpt(CURLOPT_WRITEDATA, &response);
auto code = CurlHelper::Perform();

if (code != CURLE_OK) {
blog(LOG_WARNING,
"Retrieving image failed in %s with error: %s", __func__,
switcher->curl.GetError(code));
CurlHelper::GetError(code));
return {};
}

Expand Down
29 changes: 14 additions & 15 deletions src/macro-core/macro-action-http.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "macro-action-http.hpp"
#include "switcher-data.hpp"
#include "utility.hpp"
#include "curl-helper.hpp"

Expand Down Expand Up @@ -36,44 +35,44 @@ void MacroActionHttp::SetupHeaders()
}
struct curl_slist *headers = nullptr;
for (auto &header : _headers) {
headers = switcher->curl.SlistAppend(headers, header.c_str());
headers = CurlHelper::SlistAppend(headers, header.c_str());
}
if (!_headers.empty()) {
switcher->curl.SetOpt(CURLOPT_HTTPHEADER, headers);
CurlHelper::SetOpt(CURLOPT_HTTPHEADER, headers);
}
}

void MacroActionHttp::Get()
{
switcher->curl.SetOpt(CURLOPT_URL, _url.c_str());
switcher->curl.SetOpt(CURLOPT_HTTPGET, 1L);
switcher->curl.SetOpt(CURLOPT_TIMEOUT_MS, _timeout.Milliseconds());
CurlHelper::SetOpt(CURLOPT_URL, _url.c_str());
CurlHelper::SetOpt(CURLOPT_HTTPGET, 1L);
CurlHelper::SetOpt(CURLOPT_TIMEOUT_MS, _timeout.Milliseconds());
SetupHeaders();

std::string response;
if (IsReferencedInVars()) {
switcher->curl.SetOpt(CURLOPT_WRITEFUNCTION, WriteCB);
CurlHelper::SetOpt(CURLOPT_WRITEFUNCTION, WriteCB);
} else {
switcher->curl.SetOpt(CURLOPT_WRITEFUNCTION, DropCB);
CurlHelper::SetOpt(CURLOPT_WRITEFUNCTION, DropCB);
}
switcher->curl.SetOpt(CURLOPT_WRITEDATA, &response);
switcher->curl.Perform();
CurlHelper::SetOpt(CURLOPT_WRITEDATA, &response);
CurlHelper::Perform();

SetVariableValue(response);
}

void MacroActionHttp::Post()
{
switcher->curl.SetOpt(CURLOPT_URL, _url.c_str());
switcher->curl.SetOpt(CURLOPT_POSTFIELDS, _data.c_str());
switcher->curl.SetOpt(CURLOPT_TIMEOUT_MS, _timeout.Milliseconds());
CurlHelper::SetOpt(CURLOPT_URL, _url.c_str());
CurlHelper::SetOpt(CURLOPT_POSTFIELDS, _data.c_str());
CurlHelper::SetOpt(CURLOPT_TIMEOUT_MS, _timeout.Milliseconds());
SetupHeaders();
switcher->curl.Perform();
CurlHelper::Perform();
}

bool MacroActionHttp::PerformAction()
{
if (!switcher->curl.Initialized()) {
if (!CurlHelper::Initialized()) {
blog(LOG_WARNING,
"cannot perform http action (curl not found)");
return true;
Expand Down
14 changes: 7 additions & 7 deletions src/macro-core/macro-condition-file.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "macro-condition-file.hpp"
#include "utility.hpp"
#include "switcher-data.hpp"
#include "curl-helper.hpp"
#include "plugin-state-helpers.hpp"

#include <QTextStream>
#include <QFileDialog>
Expand All @@ -28,16 +28,16 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb,
static std::string getRemoteData(std::string &url)
{
std::string readBuffer;
switcher->curl.SetOpt(CURLOPT_URL, url.c_str());
switcher->curl.SetOpt(CURLOPT_WRITEFUNCTION, WriteCallback);
switcher->curl.SetOpt(CURLOPT_WRITEDATA, &readBuffer);
CurlHelper::SetOpt(CURLOPT_URL, url.c_str());
CurlHelper::SetOpt(CURLOPT_WRITEFUNCTION, WriteCallback);
CurlHelper::SetOpt(CURLOPT_WRITEDATA, &readBuffer);
// Set timeout to at least one second
int timeout = switcher->interval / 1000;
int timeout = GetIntervalValue() / 1000;
if (timeout == 0) {
timeout = 1;
}
switcher->curl.SetOpt(CURLOPT_TIMEOUT, 1);
switcher->curl.Perform();
CurlHelper::SetOpt(CURLOPT_TIMEOUT, 1);
CurlHelper::Perform();
return readBuffer;
}

Expand Down
3 changes: 0 additions & 3 deletions src/switcher-data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "macro-properties.hpp"
#include "duration-control.hpp"
#include "curl-helper.hpp"
#include "priority-helper.hpp"
#include "log-helper.hpp"
#include "plugin-state-helpers.hpp"
Expand All @@ -29,7 +28,6 @@
#include <mutex>
#include <QDateTime>
#include <QThread>
#include <curl/curl.h>
#include <unordered_map>

namespace advss {
Expand Down Expand Up @@ -160,7 +158,6 @@ class SwitcherData {
AudioFadeInfo masterAudioFade;
std::unordered_map<std::string, AudioFadeInfo> activeAudioFades;

Curlhelper curl;
std::deque<std::shared_ptr<Item>> variables;

std::string lastTitle;
Expand Down
40 changes: 27 additions & 13 deletions src/utils/curl-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ constexpr auto curl_library_name = "libcurl.4.dylib";
constexpr auto curl_library_name = "libcurl.so.4";
#endif

Curlhelper::Curlhelper()
CurlHelper::CurlHelper()
{
if (LoadLib()) {
_curl = _init();
_initialized = true;
}
}

Curlhelper::~Curlhelper()
CurlHelper::~CurlHelper()
{
if (_lib) {
if (_cleanup) {
Expand All @@ -34,32 +34,46 @@ Curlhelper::~Curlhelper()
}
}

curl_slist *Curlhelper::SlistAppend(curl_slist *list, const char *string)
CurlHelper &CurlHelper::getInstance()
{
if (!_initialized) {
static CurlHelper curl;
return curl;
}

bool CurlHelper::Initialized()
{
return getInstance()._initialized;
}

curl_slist *CurlHelper::SlistAppend(curl_slist *list, const char *string)
{
auto &curl = getInstance();
if (!curl._initialized) {
return nullptr;
}
return _slistAppend(list, string);
return curl._slistAppend(list, string);
}

CURLcode Curlhelper::Perform()
CURLcode CurlHelper::Perform()
{
if (!_initialized) {
auto &curl = getInstance();
if (!curl._initialized) {
return CURLE_FAILED_INIT;
}
return _perform(_curl);
return curl._perform(curl._curl);
}

char *Curlhelper::GetError(CURLcode code)
char *CurlHelper::GetError(CURLcode code)
{
if (!_initialized) {
auto &curl = getInstance();
if (!curl._initialized) {
return (char *)"CURL initialization failed";
}

return _error(code);
return curl._error(code);
}

bool Curlhelper::LoadLib()
bool CurlHelper::LoadLib()
{
_lib = new QLibrary(curl_library_name, nullptr);
if (Resolve()) {
Expand Down Expand Up @@ -103,7 +117,7 @@ bool Curlhelper::LoadLib()
return false;
}

bool Curlhelper::Resolve()
bool CurlHelper::Resolve()
{
_init = (initFunction)_lib->resolve("curl_easy_init");
_setopt = (setOptFunction)_lib->resolve("curl_easy_setopt");
Expand Down
59 changes: 37 additions & 22 deletions src/utils/curl-helper.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
#pragma once
#include <curl/curl.h>
#include <QLibrary>
#include <atomic>

namespace advss {

typedef CURL *(*initFunction)(void);
typedef CURLcode (*setOptFunction)(CURL *, CURLoption, ...);
typedef struct curl_slist *(*slistAppendFunction)(struct curl_slist *list,
const char *string);
typedef CURLcode (*performFunction)(CURL *);
typedef void (*cleanupFunction)(CURL *);
typedef char *(*errorFunction)(CURLcode);

class Curlhelper {
class CurlHelper {
public:
Curlhelper();
~Curlhelper();
static bool Initialized();
template<typename... Args> static CURLcode SetOpt(CURLoption, Args...);
static struct curl_slist *SlistAppend(struct curl_slist *list,
const char *string);
static CURLcode Perform();
static char *GetError(CURLcode code);

bool Initialized() { return _initialized; }
private:
CurlHelper();
CurlHelper(const CurlHelper &) = delete;
CurlHelper &operator=(const CurlHelper &) = delete;
~CurlHelper();

template<typename... Args> CURLcode SetOpt(CURLoption, Args...);
struct curl_slist *SlistAppend(struct curl_slist *list,
const char *string);
CURLcode Perform();
char *GetError(CURLcode code);
/*
using initFunction = std::function<CURL *(void)>;
using setOptFunction = std::function<CURLcode(CURL *, CURLoption, ...)>;
using slistAppendFunction = std::function<struct curl_slist *(
struct curl_slist *list, const char *string)>;
using performFunction = std::function<CURLcode(CURL *)>;
using cleanupFunction = std::function<void(CURL *)>;
using errorFunction = std::function<char *(CURLcode)>;
*/

typedef CURL *(*initFunction)(void);
typedef CURLcode (*setOptFunction)(CURL *, CURLoption, ...);
typedef struct curl_slist *(*slistAppendFunction)(
struct curl_slist *list, const char *string);
typedef CURLcode (*performFunction)(CURL *);
typedef void (*cleanupFunction)(CURL *);
typedef char *(*errorFunction)(CURLcode);

static CurlHelper &getInstance();

private:
bool LoadLib();
bool Resolve();

Expand All @@ -37,16 +51,17 @@ class Curlhelper {
errorFunction _error = nullptr;
CURL *_curl = nullptr;
QLibrary *_lib;
bool _initialized = false;
std::atomic_bool _initialized = {false};
};

template<typename... Args>
inline CURLcode Curlhelper::SetOpt(CURLoption option, Args... args)
static inline CURLcode CurlHelper::SetOpt(CURLoption option, Args... args)
{
if (!_initialized) {
auto &curl = getInstance();
if (!curl._initialized) {
return CURLE_FAILED_INIT;
}
return _setopt(_curl, option, args...);
return curl._setopt(curl._curl, option, args...);
}

} // namespace advss

0 comments on commit eeff6ae

Please sign in to comment.