Skip to content

Commit

Permalink
Add a flags argument to json_rpc_call()
Browse files Browse the repository at this point in the history
  • Loading branch information
pooler committed Apr 14, 2014
1 parent 6b2e46d commit 9aa776c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
9 changes: 4 additions & 5 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
str);

/* issue JSON-RPC request */
val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false, NULL);
val = json_rpc_call(curl, rpc_url, rpc_userpass, s, NULL, 0);
if (unlikely(!val)) {
applog(LOG_ERR, "submit_upstream_work json_rpc_call failed");
goto out;
Expand Down Expand Up @@ -422,8 +422,7 @@ static bool get_upstream_work(CURL *curl, struct work *work)
struct timeval tv_start, tv_end, diff;

gettimeofday(&tv_start, NULL);
val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
want_longpoll, false, NULL);
val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req, NULL, 0);
gettimeofday(&tv_end, NULL);

if (have_stratum) {
Expand Down Expand Up @@ -874,8 +873,8 @@ static void *longpoll_thread(void *userdata)
json_t *val, *soval;
int err;

val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
false, true, &err);
val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req, &err,
JSON_RPC_LONGPOLL);
if (have_stratum) {
if (val)
json_decref(val);
Expand Down
5 changes: 4 additions & 1 deletion miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,12 @@ extern int longpoll_thr_id;
extern int stratum_thr_id;
extern struct work_restart *work_restart;

#define JSON_RPC_LONGPOLL (1 << 0)
#define JSON_RPC_QUIET_404 (1 << 1)

extern void applog(int prio, const char *fmt, ...);
extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
const char *rpc_req, bool, bool, int *);
const char *rpc_req, int *curl_err, int flags);
extern char *bin2hex(const unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
extern int timeval_subtract(struct timeval *result, struct timeval *x,
Expand Down
14 changes: 8 additions & 6 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,19 @@ static int sockopt_keepalive_cb(void *userdata, curl_socket_t fd,

json_t *json_rpc_call(CURL *curl, const char *url,
const char *userpass, const char *rpc_req,
bool longpoll_scan, bool longpoll, int *curl_err)
int *curl_err, int flags)
{
json_t *val, *err_val, *res_val;
int rc;
long http_rc;
struct data_buffer all_data = {0};
struct upload_buffer upload_data;
json_error_t err;
struct curl_slist *headers = NULL;
char len_hdr[64];
char curl_err_str[CURL_ERROR_SIZE];
long timeout = longpoll ? opt_timeout : 30;
long timeout = (flags & JSON_RPC_LONGPOLL) ? opt_timeout : 30;
struct header_info hi = {0};
bool lp_scanning = longpoll_scan && !have_longpoll;

/* it is assumed that 'curl' is freshly [re]initialized at this pt */

Expand Down Expand Up @@ -343,7 +343,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
#if LIBCURL_VERSION_NUM >= 0x070f06
if (longpoll)
if (flags & JSON_RPC_LONGPOLL)
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
#endif
curl_easy_setopt(curl, CURLOPT_POST, 1);
Expand All @@ -370,7 +370,9 @@ json_t *json_rpc_call(CURL *curl, const char *url,
if (curl_err != NULL)
*curl_err = rc;
if (rc) {
if (!(longpoll && rc == CURLE_OPERATION_TIMEDOUT))
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_rc);
if (!((flags & JSON_RPC_LONGPOLL) && rc == CURLE_OPERATION_TIMEDOUT) &&
!((flags & JSON_RPC_QUIET_404) && http_rc == 404))
applog(LOG_ERR, "HTTP request failed: %s", curl_err_str);
goto err_out;
}
Expand All @@ -385,7 +387,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
}

/* If X-Long-Polling was found, activate long polling */
if (lp_scanning && hi.lp_path && !have_stratum) {
if (!have_longpoll && want_longpoll && hi.lp_path && !have_stratum) {
have_longpoll = true;
tq_push(thr_info[longpoll_thr_id].q, hi.lp_path);
hi.lp_path = NULL;
Expand Down

0 comments on commit 9aa776c

Please sign in to comment.