set not needed

This commit is contained in:
J. Nick Koston
2025-05-28 09:36:42 -05:00
parent 145378f6ec
commit cbafc19bec
2 changed files with 20 additions and 10 deletions

View File

@@ -2,6 +2,7 @@
#include "esphome/core/log.h"
#include "esphome/core/version.h"
#include "esphome/core/hal.h"
#include <algorithm>
#ifdef USE_STATUS_LED
#include "esphome/components/status_led/status_led.h"
@@ -256,7 +257,7 @@ bool Application::register_socket_fd(int fd) {
return false;
}
this->socket_fds_.insert(fd);
this->socket_fds_.push_back(fd);
this->socket_fds_changed_ = true;
if (fd > this->max_fd_) {
@@ -272,14 +273,24 @@ void Application::unregister_socket_fd(int fd) {
if (fd < 0)
return;
this->socket_fds_.erase(fd);
this->socket_fds_changed_ = true;
auto it = std::find(this->socket_fds_.begin(), this->socket_fds_.end(), fd);
if (it != this->socket_fds_.end()) {
// Swap with last element and pop - O(1) removal since order doesn't matter
if (it != this->socket_fds_.end() - 1) {
std::swap(*it, this->socket_fds_.back());
}
this->socket_fds_.pop_back();
this->socket_fds_changed_ = true;
// Recalculate max_fd if necessary
if (fd == this->max_fd_ && !this->socket_fds_.empty()) {
this->max_fd_ = *this->socket_fds_.rbegin();
} else if (this->socket_fds_.empty()) {
this->max_fd_ = -1;
// Only recalculate max_fd if we removed the current max
if (fd == this->max_fd_) {
if (this->socket_fds_.empty()) {
this->max_fd_ = -1;
} else {
// Find new max using std::max_element
this->max_fd_ = *std::max_element(this->socket_fds_.begin(), this->socket_fds_.end());
}
}
}
}

View File

@@ -2,7 +2,6 @@
#include <string>
#include <vector>
#include <set>
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
@@ -576,7 +575,7 @@ class Application {
#ifdef USE_SOCKET_SELECT_SUPPORT
// Socket select management
std::set<int> socket_fds_; // Set of all monitored socket file descriptors
std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
int max_fd_{-1}; // Highest file descriptor number for select()
fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes