Switched bitcoin-cli.cpp to use RAII unique pointers with deleters.
This commit is contained in:
parent
e5534d2f01
commit
7f7f102b8d
1 changed files with 12 additions and 20 deletions
|
@ -17,10 +17,9 @@
|
||||||
#include <boost/filesystem/operations.hpp>
|
#include <boost/filesystem/operations.hpp>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <event2/event.h>
|
|
||||||
#include <event2/http.h>
|
|
||||||
#include <event2/buffer.h>
|
#include <event2/buffer.h>
|
||||||
#include <event2/keyvalq_struct.h>
|
#include <event2/keyvalq_struct.h>
|
||||||
|
#include "support/events.h"
|
||||||
|
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
|
|
||||||
|
@ -190,23 +189,19 @@ UniValue CallRPC(const std::string& strMethod, const UniValue& params)
|
||||||
std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
|
std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
|
||||||
int port = GetArg("-rpcport", BaseParams().RPCPort());
|
int port = GetArg("-rpcport", BaseParams().RPCPort());
|
||||||
|
|
||||||
// Create event base
|
// Obtain event base
|
||||||
struct event_base *base = event_base_new(); // TODO RAII
|
raii_event_base base = obtain_event_base();
|
||||||
if (!base)
|
|
||||||
throw std::runtime_error("cannot create event_base");
|
|
||||||
|
|
||||||
// Synchronously look up hostname
|
// Synchronously look up hostname
|
||||||
struct evhttp_connection *evcon = evhttp_connection_base_new(base, NULL, host.c_str(), port); // TODO RAII
|
raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port);
|
||||||
if (evcon == NULL)
|
evhttp_connection_set_timeout(evcon.get(), GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT));
|
||||||
throw std::runtime_error("create connection failed");
|
|
||||||
evhttp_connection_set_timeout(evcon, GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT));
|
|
||||||
|
|
||||||
HTTPReply response;
|
HTTPReply response;
|
||||||
struct evhttp_request *req = evhttp_request_new(http_request_done, (void*)&response); // TODO RAII
|
raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void*)&response);
|
||||||
if (req == NULL)
|
if (req == NULL)
|
||||||
throw std::runtime_error("create http request failed");
|
throw std::runtime_error("create http request failed");
|
||||||
#if LIBEVENT_VERSION_NUMBER >= 0x02010300
|
#if LIBEVENT_VERSION_NUMBER >= 0x02010300
|
||||||
evhttp_request_set_error_cb(req, http_error_cb);
|
evhttp_request_set_error_cb(req.get(), http_error_cb);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Get credentials
|
// Get credentials
|
||||||
|
@ -223,7 +218,7 @@ UniValue CallRPC(const std::string& strMethod, const UniValue& params)
|
||||||
strRPCUserColonPass = mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"];
|
strRPCUserColonPass = mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"];
|
||||||
}
|
}
|
||||||
|
|
||||||
struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
|
struct evkeyvalq* output_headers = evhttp_request_get_output_headers(req.get());
|
||||||
assert(output_headers);
|
assert(output_headers);
|
||||||
evhttp_add_header(output_headers, "Host", host.c_str());
|
evhttp_add_header(output_headers, "Host", host.c_str());
|
||||||
evhttp_add_header(output_headers, "Connection", "close");
|
evhttp_add_header(output_headers, "Connection", "close");
|
||||||
|
@ -231,20 +226,17 @@ UniValue CallRPC(const std::string& strMethod, const UniValue& params)
|
||||||
|
|
||||||
// Attach request data
|
// Attach request data
|
||||||
std::string strRequest = JSONRPCRequestObj(strMethod, params, 1).write() + "\n";
|
std::string strRequest = JSONRPCRequestObj(strMethod, params, 1).write() + "\n";
|
||||||
struct evbuffer * output_buffer = evhttp_request_get_output_buffer(req);
|
struct evbuffer* output_buffer = evhttp_request_get_output_buffer(req.get());
|
||||||
assert(output_buffer);
|
assert(output_buffer);
|
||||||
evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
|
evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
|
||||||
|
|
||||||
int r = evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/");
|
int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, "/");
|
||||||
|
req.release(); // ownership moved to evcon in above call
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
evhttp_connection_free(evcon);
|
|
||||||
event_base_free(base);
|
|
||||||
throw CConnectionFailed("send http request failed");
|
throw CConnectionFailed("send http request failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
event_base_dispatch(base);
|
event_base_dispatch(base.get());
|
||||||
evhttp_connection_free(evcon);
|
|
||||||
event_base_free(base);
|
|
||||||
|
|
||||||
if (response.status == 0)
|
if (response.status == 0)
|
||||||
throw CConnectionFailed(strprintf("couldn't connect to server: %s (code %d)\n(make sure server is running and you are connecting to the correct RPC port)", http_errorstring(response.error), response.error));
|
throw CConnectionFailed(strprintf("couldn't connect to server: %s (code %d)\n(make sure server is running and you are connecting to the correct RPC port)", http_errorstring(response.error), response.error));
|
||||||
|
|
Loading…
Reference in a new issue