From 93245a24b57a9c0e399487cc59bf097a38c8a72a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 23 Jun 2025 11:36:54 +0200 Subject: [PATCH] preen --- esphome/components/api/api_server.cpp | 66 ++++++++++++++------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index ab1568c80b..156cf7cc6e 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -158,43 +158,45 @@ void APIServer::loop() { } // Process clients and remove disconnected ones in a single pass - if (!this->clients_.empty()) { - // Check network connectivity once for all clients - if (!network::is_connected()) { - // Network is down - disconnect all clients - for (auto &client : this->clients_) { - client->on_fatal_error(); - ESP_LOGW(TAG, "%s: Network unavailable; disconnecting", client->get_client_combined_info().c_str()); - } - // Continue to process and clean up the clients below + if (this->clients_.empty()) { + return; + } + + // Check network connectivity once for all clients + if (!network::is_connected()) { + // Network is down - disconnect all clients + for (auto &client : this->clients_) { + client->on_fatal_error(); + ESP_LOGW(TAG, "%s: Network unavailable; disconnecting", client->get_client_combined_info().c_str()); } + // Continue to process and clean up the clients below + } - size_t client_index = 0; - while (client_index < this->clients_.size()) { - auto &client = this->clients_[client_index]; + size_t client_index = 0; + while (client_index < this->clients_.size()) { + auto &client = this->clients_[client_index]; - if (client->remove_) { - // Handle disconnection - this->client_disconnected_trigger_->trigger(client->client_info_, client->client_peername_); - ESP_LOGV(TAG, "Removing connection to %s", client->client_info_.c_str()); + if (client->remove_) { + // Handle disconnection + this->client_disconnected_trigger_->trigger(client->client_info_, client->client_peername_); + ESP_LOGV(TAG, "Removing connection to %s", client->client_info_.c_str()); - // Swap with the last element and pop (avoids expensive vector shifts) - if (client_index < this->clients_.size() - 1) { - std::swap(this->clients_[client_index], this->clients_.back()); - } - this->clients_.pop_back(); - - // Schedule reboot when last client disconnects - if (this->clients_.empty() && this->reboot_timeout_ != 0) { - this->schedule_reboot_timeout_(); - } - - // Don't increment client_index since we need to process the swapped element - } else { - // Network is connected, process the client - client->loop(); - client_index++; // Move to next client + // Swap with the last element and pop (avoids expensive vector shifts) + if (client_index < this->clients_.size() - 1) { + std::swap(this->clients_[client_index], this->clients_.back()); } + this->clients_.pop_back(); + + // Schedule reboot when last client disconnects + if (this->clients_.empty() && this->reboot_timeout_ != 0) { + this->schedule_reboot_timeout_(); + } + + // Don't increment client_index since we need to process the swapped element + } else { + // Network is connected, process the client + client->loop(); + client_index++; // Move to next client } } }