diff --git a/components/constants/include/constants/tcp.h b/components/constants/include/constants/tcp.h index bd4ec8f..a061786 100644 --- a/components/constants/include/constants/tcp.h +++ b/components/constants/include/constants/tcp.h @@ -13,6 +13,8 @@ #define SLEEP_AFTER_FAIL_MS 5000 +#define NO_CLIENT_SLEEP_MS 400 + #define TCP_PORT 3001 #endif //TCP_H diff --git a/components/rpc/MessagingInterface.cpp b/components/rpc/MessagingInterface.cpp index 99bef0e..97b11b9 100644 --- a/components/rpc/MessagingInterface.cpp +++ b/components/rpc/MessagingInterface.cpp @@ -2,6 +2,8 @@ // Created by Johnathon Slightham on 2025-05-25. // +#include + #include "MessagingInterface.h" #include "AngleControlMessageBuilder.h" #include "ConfigManager.h" diff --git a/components/rpc/wireless/TCPServer.cpp b/components/rpc/wireless/TCPServer.cpp index 5b74e32..85ca928 100644 --- a/components/rpc/wireless/TCPServer.cpp +++ b/components/rpc/wireless/TCPServer.cpp @@ -1,4 +1,3 @@ -#include #include #include "esp_log.h" @@ -18,6 +17,8 @@ #define TAG "TCPServer" +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) + // todo: - add message routing to correct client // - authenticate (don't just return true from the auth function) // - tx from board @@ -153,15 +154,16 @@ void TCPServer::shutdown() { int max_fd = -1; xSemaphoreTake(that->m_mutex, portMAX_DELAY); + if (that->m_clients.size() < 1) { + vTaskDelay(NO_CLIENT_SLEEP_MS / portTICK_PERIOD_MS); + } for (const auto sock : that->m_clients) { FD_SET(sock, &readfds); if (sock > max_fd) max_fd = sock; } xSemaphoreGive(that->m_mutex); - // todo: Select seems to be timing out the watchdog, even though we have a 50ms timeout. - // Potentially select is not respecting our timeout? - timeval timeout = {0, 50000}; // 50 ms timeout + timeval timeout = {.tv_sec = 1, .tv_usec = 0}; // 1s timeout int ret = select(max_fd + 1, &readfds, nullptr, nullptr, &timeout); vTaskDelay(0); // Avoid starving other threads @@ -172,17 +174,25 @@ void TCPServer::shutdown() { for (int sock : that->m_clients) { vTaskDelay(0); // Avoid starving other threads if (FD_ISSET(sock, &readfds)) { - // Handle socket - auto buffer = std::make_unique>(); - buffer->resize(MAX_RX_BUFFER_SIZE); uint32_t msg_size = 0; - recv(sock, &msg_size, 4, MSG_WAITALL); - if (msg_size < 1 || msg_size > 512) { + if (int len = recv(sock, &msg_size, 4, MSG_WAITALL); len < 0) { + ESP_LOGE(TAG, "Error occurred during receiving msg length: errno %d\n", errno); + to_remove.emplace_back(sock); + continue; + } else if (0 == len) { + ESP_LOGI(TAG, "TCP Connection closed when receiving msg length\n"); + close(sock); + to_remove.emplace_back(sock); continue; } - ESP_LOGD(TAG, "Message size: %ld\n", msg_size); + if (msg_size < 1 || msg_size > MAX_RX_BUFFER_SIZE) { + continue; + } + + auto buffer = std::make_unique>(); + buffer->resize(MIN(MAX_RX_BUFFER_SIZE, msg_size)); if (int len = recv(sock, buffer->data(), msg_size, MSG_WAITALL); len < 0) { ESP_LOGE(TAG, "Error occurred during receiving: errno %d\n", errno); @@ -251,4 +261,3 @@ int TCPServer::send_msg(char *buffer, const uint32_t length) const { return 0; } -