diff --git a/esphome/components/socket/socket.cpp b/esphome/components/socket/socket.cpp index cc3f3ec417..fe65f35167 100644 --- a/esphome/components/socket/socket.cpp +++ b/esphome/components/socket/socket.cpp @@ -17,6 +17,7 @@ bool Socket::ready() const { return true; } +#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) // For loop-monitored sockets, check with the Application's select() results int fd = this->get_fd(); if (fd < 0) { @@ -25,6 +26,11 @@ bool Socket::ready() const { } return App.is_socket_ready(fd); +#else + // Without select() support, we can't monitor sockets in the loop + // Always return true (assume data may be available) + return true; +#endif } std::unique_ptr socket_ip(int type, int protocol) { diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 302bfa4880..b9ab02a0e1 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -247,19 +247,18 @@ void Application::calculate_looping_components_() { } } +#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) bool Application::register_socket_fd(int fd) { // WARNING: This function is NOT thread-safe and must only be called from the main loop // It modifies socket_fds_ and related variables without locking if (fd < 0) return false; -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) if (fd >= FD_SETSIZE) { ESP_LOGE(TAG, "Cannot monitor socket fd %d: exceeds FD_SETSIZE (%d)", fd, FD_SETSIZE); ESP_LOGE(TAG, "Socket will not be monitored for data - may cause performance issues!"); return false; } -#endif this->socket_fds_.insert(fd); this->socket_fds_changed_ = true; @@ -292,16 +291,12 @@ bool Application::is_socket_ready(int fd) const { // This function is thread-safe for reading the result of select() // However, it should only be called after select() has been executed in the main loop // The read_fds_ is only modified by select() in the main loop -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) if (fd < 0 || fd >= FD_SETSIZE) return false; return FD_ISSET(fd, &this->read_fds_); -#else - // If we don't have select support, assume socket is always ready - return true; -#endif } +#endif Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/esphome/core/application.h b/esphome/core/application.h index d1b61d2bde..91d252c6cd 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -469,6 +469,7 @@ class Application { Scheduler scheduler; /// Register/unregister a socket file descriptor to be monitored for read events. +#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) /// These functions update the fd_set used by select() in the main loop. /// WARNING: These functions are NOT thread-safe. They must only be called from the main loop. /// NOTE: File descriptors >= FD_SETSIZE (typically 10 on ESP) will be rejected with an error. @@ -478,6 +479,7 @@ class Application { /// Check if there's data available on a socket without blocking /// This function is thread-safe for reading, but should be called after select() has run bool is_socket_ready(int fd) const; +#endif protected: friend Component;