Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for client.reconnect method in Stratum V1 protocol #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/stratum/include/stratum_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ typedef struct
uint32_t version_mask;
// result
bool response_success;

// client.reconnect
char *new_host;
uint16_t new_port;
uint16_t wait_time;
} StratumApiV1Message;

void STRATUM_V1_reset_uid();
Expand Down
29 changes: 29 additions & 0 deletions components/stratum/stratum_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,35 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
cJSON * params = cJSON_GetObjectItem(json, "params");
uint32_t version_mask = strtoul(cJSON_GetArrayItem(params, 0)->valuestring, NULL, 16);
message->version_mask = version_mask;
} else if (message->method == CLIENT_RECONNECT) {
cJSON * params = cJSON_GetObjectItem(json, "params");
if (params != NULL && cJSON_IsArray(params)) {
cJSON * host = cJSON_GetArrayItem(params, 0);
cJSON * port = cJSON_GetArrayItem(params, 1);
cJSON * wait = cJSON_GetArrayItem(params, 2);

if (host && cJSON_IsString(host)) {
message->new_host = strdup(host->valuestring);
} else {
message->new_host = NULL;
}

if (port && cJSON_IsNumber(port)) {
message->new_port = (uint16_t)port->valueint;
} else {
message->new_port = 0;
}

if (wait && cJSON_IsNumber(wait)) {
message->wait_time = (uint16_t)wait->valueint;
} else {
message->wait_time = 0;
}

ESP_LOGI(TAG, "Received client.reconnect: host=%s, port=%u, wait=%u",
message->new_host ? message->new_host : "NULL",
message->new_port, message->wait_time);
}
}
done:
cJSON_Delete(json);
Expand Down
35 changes: 33 additions & 2 deletions main/tasks/stratum_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,39 @@ void stratum_task(void * pvParameters)
GLOBAL_STATE->extranonce_str = stratum_api_v1_message.extranonce_str;
GLOBAL_STATE->extranonce_2_len = stratum_api_v1_message.extranonce_2_len;
} else if (stratum_api_v1_message.method == CLIENT_RECONNECT) {
ESP_LOGE(TAG, "Pool requested client reconnect...");
ESP_LOGI(TAG, "Pool requested client reconnect to %s:%d (wait: %d seconds)",
stratum_api_v1_message.new_host ? stratum_api_v1_message.new_host : "same host",
stratum_api_v1_message.new_port ? stratum_api_v1_message.new_port : port,
stratum_api_v1_message.wait_time);


// Update the stratum_url and port if provided
if (stratum_api_v1_message.new_host) {
bears-fan marked this conversation as resolved.
Show resolved Hide resolved
free(stratum_url);
stratum_url = strdup(stratum_api_v1_message.new_host);
free(stratum_api_v1_message.new_host);
stratum_api_v1_message.new_host = NULL;
}

if (stratum_api_v1_message.new_port > 0) {
port = stratum_api_v1_message.new_port;
}

// close the current socket, and mark as invalid
shutdown(GLOBAL_STATE->sock, SHUT_RDWR);
close(GLOBAL_STATE->sock);
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay before attempting to reconnect
GLOBAL_STATE->sock = -1;

// Wait before reconnecting. Default to 1000 ms
int wait_time_ms = (stratum_api_v1_message.wait_time > 0)
? stratum_api_v1_message.wait_time * 1000
: 1000;
vTaskDelay(wait_time_ms / portTICK_PERIOD_MS);

// reset DNS lookup flags
bDNSFound = false;
bDNSInvalid = false;

break;
} else if (stratum_api_v1_message.method == STRATUM_RESULT) {
if (stratum_api_v1_message.response_success) {
Expand All @@ -236,6 +265,8 @@ void stratum_task(void * pvParameters)
ESP_LOGE(TAG, "Shutting down socket and restarting...");
shutdown(GLOBAL_STATE->sock, 0);
close(GLOBAL_STATE->sock);
// mark the socket as invalid
GLOBAL_STATE->sock = -1;
}
}
}
Expand Down
Loading