Skip to content

Commit 0c8aedb

Browse files
author
administrator
committed
Add function keepalive
1 parent 8393e03 commit 0c8aedb

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

cpu-miner.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static const char *algo_names[] = {
137137

138138
bool opt_debug = false;
139139
bool opt_protocol = false;
140+
static bool opt_keepalive = false ;
140141
static bool opt_benchmark = false;
141142
bool opt_redirect = true;
142143
bool want_longpoll = true;
@@ -207,6 +208,7 @@ Options:\n\
207208
--cert=FILE certificate for mining server using SSL\n\
208209
-x, --proxy=[PROTOCOL://]HOST[:PORT] connect through a proxy\n\
209210
-t, --threads=N number of miner threads (default: number of processors)\n\
211+
-k, --keepalive Send keepalived for prevent timeout (need pool support features)\n\
210212
-r, --retries=N number of times to retry if a network call fails\n\
211213
(default: retry indefinitely)\n\
212214
-R, --retry-pause=N time to pause between retries, in seconds (default: 30)\n\
@@ -241,7 +243,7 @@ static char const short_options[] =
241243
#ifdef HAVE_SYSLOG_H
242244
"S"
243245
#endif
244-
"a:c:Dhp:Px:qr:R:s:t:T:o:u:O:V";
246+
"a:c:Dhp:Px:kqr:R:s:t:T:o:u:O:V";
245247

246248
static struct option const options[] = {
247249
{ "algo", 1, NULL, 'a' },
@@ -253,6 +255,7 @@ static struct option const options[] = {
253255
{ "config", 1, NULL, 'c' },
254256
{ "debug", 0, NULL, 'D' },
255257
{ "help", 0, NULL, 'h' },
258+
{ "keepalive" , 0 , NULL ,'k'},
256259
{ "no-longpoll", 0, NULL, 1003 },
257260
{ "no-redirect", 0, NULL, 1009 },
258261
{ "no-stratum", 0, NULL, 1007 },
@@ -1047,7 +1050,6 @@ static void *miner_thread(void *userdata) {
10471050
thr_id % num_processors);
10481051
affine_to_cpu(thr_id, thr_id % num_processors);
10491052
}*/
1050-
10511053
persistentctx = persistentctxs[thr_id];
10521054
if(!persistentctx && opt_algo == ALGO_CRYPTONIGHT)
10531055
{
@@ -1328,8 +1330,12 @@ static bool stratum_handle_response(char *buf) {
13281330

13291331
if(jsonrpc_2) {
13301332
json_t *status = json_object_get(res_val, "status");
1333+
const char *s = json_string_value(status);
1334+
if ( !strcmp(s, "KEEPALIVED") ) {
1335+
applog(LOG_INFO, "Keepalived receveid");
1336+
goto out;
1337+
}
13311338
if(status) {
1332-
const char *s = json_string_value(status);
13331339
valid = !strcmp(s, "OK") && json_is_null(err_val);
13341340
} else {
13351341
valid = json_is_null(err_val);
@@ -1405,11 +1411,14 @@ static void *stratum_thread(void *userdata) {
14051411
}
14061412
}
14071413
}
1408-
1409-
if (!stratum_socket_full(&stratum, 600)) {
1410-
applog(LOG_ERR, "Stratum connection timed out");
1411-
s = NULL;
1412-
} else
1414+
if ( opt_keepalive && !stratum_socket_full(&stratum, 90)) {
1415+
applog(LOG_INFO, "Keepalived send....");
1416+
stratum_keepalived(&stratum,rpc2_id);
1417+
}
1418+
if (!stratum_socket_full(&stratum, 300)) {
1419+
applog(LOG_ERR, "Stratum connection timed out");
1420+
s = NULL;
1421+
} else
14131422
s = stratum_recv_line(&stratum);
14141423
if (!s) {
14151424
stratum_disconnect(&stratum);

miner.h

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ struct stratum_ctx {
277277
pthread_mutex_t work_lock;
278278
};
279279

280+
bool stratum_keepalived(struct stratum_ctx *sctx , const char *rpc2_id);
280281
bool stratum_socket_full(struct stratum_ctx *sctx, int timeout);
281282
bool stratum_send_line(struct stratum_ctx *sctx, char *s);
282283
char *stratum_recv_line(struct stratum_ctx *sctx);

util.c

+27
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,33 @@ bool stratum_subscribe(struct stratum_ctx *sctx)
925925
}
926926

927927
return ret;
928+
}
929+
930+
bool stratum_keepalived(struct stratum_ctx *sctx, const char *rpc2_id) {
931+
932+
933+
json_t *val = NULL, *res_val, *err_val;
934+
char *s, *sret;
935+
json_error_t err;
936+
bool ret = false;
937+
938+
if(jsonrpc_2) {
939+
s = malloc(300 + strlen(rpc2_id));
940+
snprintf(s, 128, "{\"method\": \"keepalived\", \"params\": {\"id\": \"%s\"}, \"id\":1}\r\n", rpc2_id);
941+
} else {
942+
return true ;
943+
944+
}
945+
if (!stratum_send_line(sctx, s))
946+
goto out;
947+
ret = true;
948+
out:
949+
free(s);
950+
return ret;
951+
952+
953+
954+
928955
}
929956

930957
bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass)

0 commit comments

Comments
 (0)