Move CurlInitializerRAII into CurlHttpClient
This commit is contained in:
parent
b4f1a72bfa
commit
433ead49a1
@ -15,7 +15,6 @@ set(SOURCES
|
|||||||
tempfile/TempDir.cpp
|
tempfile/TempDir.cpp
|
||||||
network/HttpClient.cpp
|
network/HttpClient.cpp
|
||||||
network/CurlHttpClient.cpp
|
network/CurlHttpClient.cpp
|
||||||
network/CurlInitializerRAII.cpp
|
|
||||||
network/FakeHttpClient.cpp
|
network/FakeHttpClient.cpp
|
||||||
io/Console.cpp
|
io/Console.cpp
|
||||||
io/DontEchoStdinToStdoutRAII.cpp
|
io/DontEchoStdinToStdoutRAII.cpp
|
||||||
|
@ -3,15 +3,35 @@
|
|||||||
#include "CurlHttpClient.h"
|
#include "CurlHttpClient.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <curl/easy.h>
|
|
||||||
|
|
||||||
using boost::none;
|
using boost::none;
|
||||||
using boost::optional;
|
using boost::optional;
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::ostringstream;
|
using std::ostringstream;
|
||||||
|
using std::mutex;
|
||||||
|
using std::unique_lock;
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
|
|
||||||
|
mutex CurlHttpClient::CurlInitializerRAII::_mutex;
|
||||||
|
uint32_t CurlHttpClient::CurlInitializerRAII::_refcount = 0;
|
||||||
|
|
||||||
|
CurlHttpClient::CurlInitializerRAII::CurlInitializerRAII() {
|
||||||
|
unique_lock<mutex> lock(_mutex);
|
||||||
|
if (0 == _refcount) {
|
||||||
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
}
|
||||||
|
_refcount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
CurlHttpClient::CurlInitializerRAII::~CurlInitializerRAII() {
|
||||||
|
unique_lock<mutex> lock(_mutex);
|
||||||
|
_refcount -= 1;
|
||||||
|
if (0 == _refcount) {
|
||||||
|
curl_global_cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t CurlHttpClient::write_data(void *ptr, size_t size, size_t nmemb, ostringstream *stream) {
|
size_t CurlHttpClient::write_data(void *ptr, size_t size, size_t nmemb, ostringstream *stream) {
|
||||||
stream->write(static_cast<const char *>(ptr), size * nmemb);
|
stream->write(static_cast<const char *>(ptr), size * nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifndef MESSMER_CPPUTILS_NETWORK_HTTPCLIENT_HPP
|
#ifndef MESSMER_CPPUTILS_NETWORK_CURLHTTPCLIENT_HPP
|
||||||
#define MESSMER_CPPUTILS_NETWORK_HTTPCLIENT_HPP
|
#define MESSMER_CPPUTILS_NETWORK_CURLHTTPCLIENT_HPP
|
||||||
|
|
||||||
#include "HttpClient.h"
|
#include "HttpClient.h"
|
||||||
#include "../macros.h"
|
#include "../macros.h"
|
||||||
#include "CurlInitializerRAII.h"
|
#include <mutex>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
|
|
||||||
@ -17,6 +18,19 @@ namespace cpputils {
|
|||||||
boost::optional <std::string> get(const std::string &url, boost::optional<long> timeoutMsec = boost::none) override;
|
boost::optional <std::string> get(const std::string &url, boost::optional<long> timeoutMsec = boost::none) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// 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);
|
||||||
|
};
|
||||||
|
|
||||||
CurlInitializerRAII curlInitializer;
|
CurlInitializerRAII curlInitializer;
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
|
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
#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…
Reference in New Issue
Block a user