Add a flags argument to json_rpc_call()

This commit is contained in:
pooler 2014-04-14 14:15:02 +02:00
parent 6b2e46d4ac
commit 9aa776cf94
3 changed files with 16 additions and 12 deletions

View file

@ -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;
@ -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) {
@ -874,8 +873,8 @@ start:
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);

View file

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

14
util.c
View file

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