diff --git a/esphome/components/socket/lwip_raw_tcp_impl.cpp b/esphome/components/socket/lwip_raw_tcp_impl.cpp index a9c2eda4e8..aa37386d70 100644 --- a/esphome/components/socket/lwip_raw_tcp_impl.cpp +++ b/esphome/components/socket/lwip_raw_tcp_impl.cpp @@ -452,6 +452,8 @@ class LWIPRawImpl : public Socket { errno = ENOSYS; return -1; } + bool ready() const override { return this->rx_buf_ != nullptr || this->rx_closed_ || this->pcb_ == nullptr; } + int setblocking(bool blocking) final { if (pcb_ == nullptr) { errno = ECONNRESET; @@ -576,6 +578,8 @@ class LWIPRawListenImpl final : public LWIPRawImpl { tcp_err(pcb_, LWIPRawImpl::s_err_fn); // Use base class error handler } + bool ready() const override { return this->accepted_socket_count_ > 0; } + std::unique_ptr accept(struct sockaddr *addr, socklen_t *addrlen) override { if (pcb_ == nullptr) { errno = EBADF; diff --git a/esphome/components/socket/socket.cpp b/esphome/components/socket/socket.cpp index e4dfe91cc5..2fcc162ead 100644 --- a/esphome/components/socket/socket.cpp +++ b/esphome/components/socket/socket.cpp @@ -10,13 +10,9 @@ namespace esphome::socket { Socket::~Socket() {} -bool Socket::ready() const { #ifdef USE_SOCKET_SELECT_SUPPORT - return !this->loop_monitored_ || App.is_socket_ready_(this->fd_); -#else - return true; +bool Socket::ready() const { return !this->loop_monitored_ || App.is_socket_ready_(this->fd_); } #endif -} // Platform-specific inet_ntop wrappers #if defined(USE_SOCKET_IMPL_LWIP_TCP) diff --git a/esphome/components/socket/socket.h b/esphome/components/socket/socket.h index abb2ddb589..c0098d689a 100644 --- a/esphome/components/socket/socket.h +++ b/esphome/components/socket/socket.h @@ -71,10 +71,14 @@ class Socket { int get_fd() const { return -1; } #endif - /// Check if socket has data ready to read (non-virtual for direct call) - /// For loop-monitored sockets, checks the Application's select() results - /// For non-monitored sockets, always returns true (assumes data may be available) + /// Check if socket has data ready to read + /// For select()-based sockets: non-virtual, checks Application's select() results + /// For LWIP raw TCP sockets: virtual, checks internal buffer state +#ifdef USE_SOCKET_SELECT_SUPPORT bool ready() const; +#else + virtual bool ready() const { return true; } +#endif protected: #ifdef USE_SOCKET_SELECT_SUPPORT