diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index b4646a2d7d..ca615a6d98 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -135,31 +135,35 @@ void APIConnection::loop() { api_error_to_str(err), errno); return; } - ReadPacketBuffer buffer; - err = this->helper_->read_packet(&buffer); - if (err == APIError::WOULD_BLOCK) { - // pass - } else if (err != APIError::OK) { - on_fatal_error(); - if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) { - ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str()); - } else if (err == APIError::CONNECTION_CLOSED) { - ESP_LOGW(TAG, "%s: Connection closed", this->client_combined_info_.c_str()); - } else { - ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err), - errno); - } - return; - } else { - this->last_traffic_ = App.get_loop_component_start_time(); - // read a packet - if (buffer.data_len > 0) { - this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]); - } else { - this->read_message(0, buffer.type, nullptr); - } - if (this->remove_) + + // Check if socket has data ready before attempting to read + if (this->helper_->is_socket_ready()) { + ReadPacketBuffer buffer; + err = this->helper_->read_packet(&buffer); + if (err == APIError::WOULD_BLOCK) { + // pass + } else if (err != APIError::OK) { + on_fatal_error(); + if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) { + ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str()); + } else if (err == APIError::CONNECTION_CLOSED) { + ESP_LOGW(TAG, "%s: Connection closed", this->client_combined_info_.c_str()); + } else { + ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err), + errno); + } return; + } else { + this->last_traffic_ = App.get_loop_component_start_time(); + // read a packet + if (buffer.data_len > 0) { + this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]); + } else { + this->read_message(0, buffer.type, nullptr); + } + if (this->remove_) + return; + } } if (!this->deferred_message_queue_.empty() && this->helper_->can_write_without_blocking()) { diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index 8b591c3eeb..19983e3456 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -307,11 +307,6 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) { return APIError::BAD_ARG; } - // Check if socket has data available before attempting to read - if (!socket_->ready()) { - return APIError::WOULD_BLOCK; - } - // read header if (rx_header_buf_len_ < 3) { // no header information yet @@ -834,11 +829,6 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) { return APIError::BAD_ARG; } - // Check if socket has data available before attempting to read - if (!socket_->ready()) { - return APIError::WOULD_BLOCK; - } - // read header while (!rx_header_parsed_) { uint8_t data; diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index 16ce1edf12..90bd0164e3 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -91,6 +91,8 @@ class APIFrameHelper { virtual uint8_t frame_header_padding() = 0; // Get the frame footer size required by this protocol virtual uint8_t frame_footer_size() = 0; + // Check if socket has data ready to read + bool is_socket_ready() const { return socket_ != nullptr && socket_->ready(); } protected: // Struct for holding parsed frame data