Only initialize libcurl once.

This commit is contained in:
tastytea 2020-01-04 16:23:52 +01:00
parent 10caa4c7e2
commit 81e579f162
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 22 additions and 1 deletions

View File

@ -26,6 +26,9 @@ namespace mastodonpp
using std::string;
//! Internal use only.
extern bool curl_initialized;
/*!
* @brief Holds the access data of and the connection to an instance.
*
@ -39,6 +42,11 @@ public:
/*!
* @brief Construct a new Instance object.
*
* The first construction of an Instance object will call
* `curl_global_init`, which is not thread-safe. For more information
* consult [curl_global_init(3)]
* (https://curl.haxx.se/libcurl/c/curl_global_init.html).
*
* @param instance The hostname of the instance.
* @param access_token Your access token.
*

View File

@ -24,16 +24,29 @@ namespace mastodonpp
using std::move;
bool curl_initialized{false};
Instance::Instance(string instance, string access_token)
: _instance{move(instance)}
, _access_token{move(access_token)}
, _connection{curl_easy_init()}
{
if (!curl_initialized)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_initialized = true;
}
_connection = curl_easy_init();
setup_curl();
}
Instance::~Instance()
{
curl_easy_cleanup(_connection);
if (curl_initialized)
{
curl_global_cleanup();
curl_initialized = false;
}
}
int Instance::writer(char *data, size_t size, size_t nmemb, string *writerData)