mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Shutdown/startup TCP server with leader status
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,4 +12,4 @@ public:
|
||||
virtual int disconnect() = 0;
|
||||
};
|
||||
|
||||
#endif //IWIFIMANAGER_H
|
||||
#endif //ICONNECTIONMANAGER_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;
|
||||
};
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ class TCPServer final : public IRPCServer {
|
||||
public:
|
||||
TCPServer(int port, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue);
|
||||
~TCPServer() override;
|
||||
void startup() override;
|
||||
void shutdown() override;
|
||||
int send_msg(char* buffer, uint32_t length) const override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -30,15 +30,37 @@ TCPServer::TCPServer(const int port, const std::shared_ptr<PtrQueue<std::vector<
|
||||
this->m_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);
|
||||
|
||||
Reference in New Issue
Block a user