diff --git a/components/stratum/include/stratum_api.h b/components/stratum/include/stratum_api.h index 7ce73ff2..ac2a0a54 100644 --- a/components/stratum/include/stratum_api.h +++ b/components/stratum/include/stratum_api.h @@ -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(); diff --git a/components/stratum/stratum_api.c b/components/stratum/stratum_api.c index 673564aa..1e48435d 100644 --- a/components/stratum/stratum_api.c +++ b/components/stratum/stratum_api.c @@ -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); diff --git a/main/tasks/stratum_task.c b/main/tasks/stratum_task.c index 8c88d4e7..a86fe5fd 100644 --- a/main/tasks/stratum_task.c +++ b/main/tasks/stratum_task.c @@ -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) { + 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) { @@ -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; } } }