From e621b474383939963566b3242933180766699254 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Thu, 23 Oct 2025 23:07:08 -0400 Subject: [PATCH] Shutdown/startup TCP server with leader status --- components/rpc/CommunicationRouter.cpp | 4 +++ components/rpc/include/IConnectionManager.h | 2 +- components/rpc/include/IRPCServer.h | 2 ++ components/rpc/include/wireless/TCPServer.h | 2 ++ components/rpc/wireless/TCPServer.cpp | 31 ++++++++++++++++++--- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/components/rpc/CommunicationRouter.cpp b/components/rpc/CommunicationRouter.cpp index 7d60024..89e1d95 100644 --- a/components/rpc/CommunicationRouter.cpp +++ b/components/rpc/CommunicationRouter.cpp @@ -84,8 +84,12 @@ void CommunicationRouter::update_leader() { if (this->m_leader != max) { if (max == m_module_id) { m_pc_connection->connect(); + m_lossless_server->startup(); + // todo: BTS-22 add lossy server } else if (this->m_leader == m_module_id) { m_pc_connection->disconnect(); + m_lossless_server->shutdown(); + // todo: BTS-22 add lossy server } } diff --git a/components/rpc/include/IConnectionManager.h b/components/rpc/include/IConnectionManager.h index 1065612..de359d4 100644 --- a/components/rpc/include/IConnectionManager.h +++ b/components/rpc/include/IConnectionManager.h @@ -12,4 +12,4 @@ public: virtual int disconnect() = 0; }; -#endif //IWIFIMANAGER_H +#endif //ICONNECTIONMANAGER_H diff --git a/components/rpc/include/IRPCServer.h b/components/rpc/include/IRPCServer.h index ed91386..5efe3b0 100644 --- a/components/rpc/include/IRPCServer.h +++ b/components/rpc/include/IRPCServer.h @@ -8,6 +8,8 @@ class IRPCServer { public: virtual ~IRPCServer() = default; + virtual void startup() = 0; + virtual void shutdown() = 0; virtual int send_msg(char* buffer, uint32_t length) const = 0; }; diff --git a/components/rpc/include/wireless/TCPServer.h b/components/rpc/include/wireless/TCPServer.h index 299fb81..cd63aee 100644 --- a/components/rpc/include/wireless/TCPServer.h +++ b/components/rpc/include/wireless/TCPServer.h @@ -17,6 +17,8 @@ class TCPServer final : public IRPCServer { public: TCPServer(int port, const std::shared_ptr>>& rx_queue); ~TCPServer() override; + void startup() override; + void shutdown() override; int send_msg(char* buffer, uint32_t length) const override; private: diff --git a/components/rpc/wireless/TCPServer.cpp b/components/rpc/wireless/TCPServer.cpp index 2577b6a..5b74e32 100644 --- a/components/rpc/wireless/TCPServer.cpp +++ b/components/rpc/wireless/TCPServer.cpp @@ -30,15 +30,37 @@ TCPServer::TCPServer(const int port, const std::shared_ptrm_rx_task = nullptr; this->m_rx_queue = rx_queue; this->m_server_sock = 0; +} + +TCPServer::~TCPServer() { + this->shutdown(); + vSemaphoreDelete(this->m_mutex); +} + +void TCPServer::startup() { + ESP_LOGI(TAG, "Starting TCP server on port %d", this->m_port); + if (nullptr != this->m_task || nullptr != this->m_rx_task) { + ESP_LOGW(TAG, "Attempted to start TCP server when already started, ignoring start request"); + return; + } xTaskCreate(tcp_server_task, "tcp_accept_server", 3072, this, 5, &this->m_task); xTaskCreate(socket_monitor_thread, "tcp_rx", 4096, this, 5, &this->m_rx_task); } -TCPServer::~TCPServer() { - vTaskDelete(this->m_task); - vTaskDelete(this->m_rx_task); - vSemaphoreDelete(this->m_mutex); +void TCPServer::shutdown() { + ESP_LOGI(TAG, "Shutting down TCP server"); + if (nullptr != this->m_task) { + vTaskDelete(this->m_task); + close(this->m_server_sock); + } + + if (nullptr != this->m_rx_task) { + vTaskDelete(this->m_rx_task); + for (const auto sock : this->m_clients) { + close(sock); + } + } } [[noreturn]] void TCPServer::tcp_server_task(void *args) { @@ -179,6 +201,7 @@ TCPServer::~TCPServer() { for (const auto r : to_remove) { that->m_clients.erase(r); + close(r); } xSemaphoreGive(that->m_mutex);