Move CurlInitializerRAII into CurlHttpClient

This commit is contained in:
Sebastian Messmer 2018-09-16 19:39:50 -07:00
parent b4f1a72bfa
commit 433ead49a1
5 changed files with 38 additions and 59 deletions

View File

@ -15,7 +15,6 @@ set(SOURCES
tempfile/TempDir.cpp
network/HttpClient.cpp
network/CurlHttpClient.cpp
network/CurlInitializerRAII.cpp
network/FakeHttpClient.cpp
io/Console.cpp
io/DontEchoStdinToStdoutRAII.cpp

View File

@ -3,15 +3,35 @@
#include "CurlHttpClient.h"
#include <sstream>
#include <iostream>
#include <curl/easy.h>
using boost::none;
using boost::optional;
using std::string;
using std::ostringstream;
using std::mutex;
using std::unique_lock;
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) {
stream->write(static_cast<const char *>(ptr), size * nmemb);
return size * nmemb;

View File

@ -1,10 +1,11 @@
#pragma once
#ifndef MESSMER_CPPUTILS_NETWORK_HTTPCLIENT_HPP
#define MESSMER_CPPUTILS_NETWORK_HTTPCLIENT_HPP
#ifndef MESSMER_CPPUTILS_NETWORK_CURLHTTPCLIENT_HPP
#define MESSMER_CPPUTILS_NETWORK_CURLHTTPCLIENT_HPP
#include "HttpClient.h"
#include "../macros.h"
#include "CurlInitializerRAII.h"
#include <mutex>
#include <curl/curl.h>
namespace cpputils {
@ -17,6 +18,19 @@ namespace cpputils {
boost::optional <std::string> get(const std::string &url, boost::optional<long> timeoutMsec = boost::none) override;
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;
CURL *curl;

View File

@ -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();
}
}
}

View File

@ -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