Shutdown/startup TCP server with leader status

This commit is contained in:
2025-10-23 23:07:08 -04:00
parent a667d8d9f7
commit f769ce3ca0
5 changed files with 36 additions and 5 deletions

View File

@@ -84,8 +84,12 @@ void CommunicationRouter::update_leader() {
if (this->m_leader != max) { if (this->m_leader != max) {
if (max == m_module_id) { if (max == m_module_id) {
m_pc_connection->connect(); m_pc_connection->connect();
m_lossless_server->startup();
// todo: BTS-22 add lossy server
} else if (this->m_leader == m_module_id) { } else if (this->m_leader == m_module_id) {
m_pc_connection->disconnect(); m_pc_connection->disconnect();
m_lossless_server->shutdown();
// todo: BTS-22 add lossy server
} }
} }

View File

@@ -12,4 +12,4 @@ public:
virtual int disconnect() = 0; virtual int disconnect() = 0;
}; };
#endif //IWIFIMANAGER_H #endif //ICONNECTIONMANAGER_H

View File

@@ -8,6 +8,8 @@
class IRPCServer { class IRPCServer {
public: public:
virtual ~IRPCServer() = default; virtual ~IRPCServer() = default;
virtual void startup() = 0;
virtual void shutdown() = 0;
virtual int send_msg(char* buffer, uint32_t length) const = 0; virtual int send_msg(char* buffer, uint32_t length) const = 0;
}; };

View File

@@ -17,6 +17,8 @@ class TCPServer final : public IRPCServer {
public: public:
TCPServer(int port, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue); TCPServer(int port, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue);
~TCPServer() override; ~TCPServer() override;
void startup() override;
void shutdown() override;
int send_msg(char* buffer, uint32_t length) const override; int send_msg(char* buffer, uint32_t length) const override;
private: private:

View File

@@ -30,15 +30,37 @@ TCPServer::TCPServer(const int port, const std::shared_ptr<PtrQueue<std::vector<
this->m_rx_task = nullptr; this->m_rx_task = nullptr;
this->m_rx_queue = rx_queue; this->m_rx_queue = rx_queue;
this->m_server_sock = 0; 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(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); xTaskCreate(socket_monitor_thread, "tcp_rx", 4096, this, 5, &this->m_rx_task);
} }
TCPServer::~TCPServer() { void TCPServer::shutdown() {
ESP_LOGI(TAG, "Shutting down TCP server");
if (nullptr != this->m_task) {
vTaskDelete(this->m_task); vTaskDelete(this->m_task);
close(this->m_server_sock);
}
if (nullptr != this->m_rx_task) {
vTaskDelete(this->m_rx_task); vTaskDelete(this->m_rx_task);
vSemaphoreDelete(this->m_mutex); for (const auto sock : this->m_clients) {
close(sock);
}
}
} }
[[noreturn]] void TCPServer::tcp_server_task(void *args) { [[noreturn]] void TCPServer::tcp_server_task(void *args) {
@@ -179,6 +201,7 @@ TCPServer::~TCPServer() {
for (const auto r : to_remove) { for (const auto r : to_remove) {
that->m_clients.erase(r); that->m_clients.erase(r);
close(r);
} }
xSemaphoreGive(that->m_mutex); xSemaphoreGive(that->m_mutex);