diff options
author | Jeff Garzik <jeff@garzik.org> | 2011-02-10 00:41:44 -0500 |
---|---|---|
committer | Jeff Garzik <jgarzik@redhat.com> | 2011-02-10 00:41:44 -0500 |
commit | c0935a94899bc7261bc98b17a52d7c11b005fde4 (patch) | |
tree | b69fdac650a0c428e7f55babc9bc557f8feaa1ef | |
parent | 82772022216c1d8b4dca2d0772d5810afc2e4ad8 (diff) |
Re-use CURL object, thereby caching DNS and HTTP connections where possible.
-rw-r--r-- | cpu-miner.c | 18 | ||||
-rw-r--r-- | miner.h | 3 | ||||
-rw-r--r-- | util.c | 14 |
3 files changed, 21 insertions, 14 deletions
diff --git a/cpu-miner.c b/cpu-miner.c index f288471..e78c48f 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -23,6 +23,7 @@ #include <pthread.h> #include <getopt.h> #include <jansson.h> +#include <curl/curl.h> #include "compat.h" #include "miner.h" @@ -199,7 +200,7 @@ err_out: return false; } -static void submit_work(struct work *work) +static void submit_work(CURL *curl, struct work *work) { char *hexstr = NULL; json_t *val, *res; @@ -221,7 +222,7 @@ static void submit_work(struct work *work) fprintf(stderr, "DBG: sending RPC call:\n%s", s); /* issue JSON-RPC request */ - val = json_rpc_call(rpc_url, userpass, s); + val = json_rpc_call(curl, rpc_url, userpass, s); if (!val) { fprintf(stderr, "submit_work json_rpc_call failed\n"); goto out; @@ -259,6 +260,13 @@ static void *miner_thread(void *thr_id_int) static const char *rpc_req = "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n"; uint32_t max_nonce = 0xffffff; + CURL *curl; + + curl = curl_easy_init(); + if (!curl) { + fprintf(stderr, "CURL initialization failed\n"); + return NULL; + } while (1) { struct work work __attribute__((aligned(128))); @@ -268,7 +276,7 @@ static void *miner_thread(void *thr_id_int) bool rc; /* obtain new work from bitcoin */ - val = json_rpc_call(rpc_url, userpass, rpc_req); + val = json_rpc_call(curl, rpc_url, userpass, rpc_req); if (!val) { fprintf(stderr, "json_rpc_call failed, "); @@ -369,11 +377,13 @@ static void *miner_thread(void *thr_id_int) /* if nonce found, submit work */ if (rc) - submit_work(&work); + submit_work(curl, &work); failures = 0; } + curl_easy_cleanup(curl); + return NULL; } @@ -5,6 +5,7 @@ #include <stdint.h> #include <sys/time.h> #include <jansson.h> +#include <curl/curl.h> #ifdef __SSE2__ #define WANT_SSE2_4WAY 1 @@ -45,7 +46,7 @@ static inline void swap256(void *dest_p, const void *src_p) extern bool opt_debug; extern bool opt_protocol; extern const uint32_t sha256_init_state[]; -extern json_t *json_rpc_call(const char *url, const char *userpass, +extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, const char *rpc_req); extern char *bin2hex(unsigned char *p, size_t len); extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len); @@ -80,9 +80,9 @@ static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb, return len; } -json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req) +json_t *json_rpc_call(CURL *curl, const char *url, + const char *userpass, const char *rpc_req) { - CURL *curl; json_t *val, *err_val, *res_val; int rc; struct data_buffer all_data = { }; @@ -92,11 +92,7 @@ json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req char len_hdr[64]; char curl_err_str[CURL_ERROR_SIZE]; - curl = curl_easy_init(); - if (!curl) { - fprintf(stderr, "CURL initialization failed, aborting JSON-RPC call\n"); - return NULL; - } + /* it is assumed that 'curl' is freshly [re]initialized at this pt */ if (opt_protocol) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); @@ -172,13 +168,13 @@ json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req databuf_free(&all_data); curl_slist_free_all(headers); - curl_easy_cleanup(curl); + curl_easy_reset(curl); return val; err_out: databuf_free(&all_data); curl_slist_free_all(headers); - curl_easy_cleanup(curl); + curl_easy_reset(curl); return NULL; } |