Use curl_global_init/curl_global_cleanup for cleaner shutdown of executable
This commit is contained in:
parent
6180ff5a9f
commit
681f415272
@ -12,6 +12,7 @@ set(SOURCES
|
||||
tempfile/TempDir.cpp
|
||||
network/HttpClient.cpp
|
||||
network/CurlHttpClient.cpp
|
||||
network/CurlInitializerRAII.cpp
|
||||
network/FakeHttpClient.cpp
|
||||
io/Console.cpp
|
||||
io/DontEchoStdinToStdoutRAII.cpp
|
||||
|
@ -17,7 +17,7 @@ namespace cpputils {
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
CurlHttpClient::CurlHttpClient() {
|
||||
CurlHttpClient::CurlHttpClient(): curlInitializer(), curl() {
|
||||
curl = curl_easy_init();
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#include "HttpClient.h"
|
||||
#include "../macros.h"
|
||||
#include <curl/curl.h>
|
||||
#include "CurlInitializerRAII.h"
|
||||
|
||||
namespace cpputils {
|
||||
|
||||
@ -17,6 +17,7 @@ namespace cpputils {
|
||||
boost::optional <std::string> get(const std::string &url, boost::optional<long> timeoutMsec = boost::none) override;
|
||||
|
||||
private:
|
||||
CurlInitializerRAII curlInitializer;
|
||||
CURL *curl;
|
||||
|
||||
static size_t write_data(void *ptr, size_t size, size_t nmemb, std::ostringstream *stream);
|
||||
|
27
src/cpp-utils/network/CurlInitializerRAII.cpp
Normal file
27
src/cpp-utils/network/CurlInitializerRAII.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "CurlInitializerRAII.h"
|
||||
|
||||
using std::mutex;
|
||||
using std::unique_lock;
|
||||
|
||||
namespace cpputils {
|
||||
|
||||
mutex CurlInitializerRAII::_mutex;
|
||||
uint32_t CurlInitializerRAII::_refcount = 0;
|
||||
|
||||
CurlInitializerRAII::CurlInitializerRAII() {
|
||||
unique_lock<mutex> lock(_mutex);
|
||||
if (0 == _refcount) {
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
}
|
||||
_refcount += 1;
|
||||
}
|
||||
|
||||
CurlInitializerRAII::~CurlInitializerRAII() {
|
||||
unique_lock<mutex> lock(_mutex);
|
||||
_refcount -= 1;
|
||||
if (0 == _refcount) {
|
||||
curl_global_cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
27
src/cpp-utils/network/CurlInitializerRAII.h
Normal file
27
src/cpp-utils/network/CurlInitializerRAII.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#ifndef MESSMER_CPPUTILS_NETWORK_CURLINITIALIZERRAII_HPP
|
||||
#define MESSMER_CPPUTILS_NETWORK_CURLINITIALIZERRAII_HPP
|
||||
|
||||
#include <cpp-utils/macros.h>
|
||||
#include <mutex>
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace cpputils {
|
||||
// TODO Test
|
||||
|
||||
// When the first object of this class is created, it will initialize curl using curl_global_init().
|
||||
// When the last object is destroyed, it will deinitialize curl using curl_global_cleanup().
|
||||
class CurlInitializerRAII final {
|
||||
public:
|
||||
CurlInitializerRAII();
|
||||
~CurlInitializerRAII();
|
||||
private:
|
||||
static std::mutex _mutex;
|
||||
static uint32_t _refcount;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CurlInitializerRAII);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user