From 7b4e7108c0c3d80a1d02d5d20be492568b13c3cf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 May 2025 01:45:24 -0500 Subject: [PATCH] cleanup --- esphome/components/api/api_frame_helper.cpp | 46 +++++++++++---------- esphome/components/api/api_frame_helper.h | 8 ---- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index 669be8052c..23eccecaef 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -91,12 +91,14 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) { // Try to send the remaining data in this buffer ssize_t sent = this->socket_->write(front_buffer.current_data(), front_buffer.remaining()); - if (this->is_would_block(sent)) { - // Socket would block, we'll try again later - } else if (sent == -1) { - ESP_LOGVV(TAG, "%s: Socket write failed with errno %d", this->info_.c_str(), errno); - this->state_ = State::FAILED; - return APIError::SOCKET_WRITE_FAILED; // Socket write failed + if (sent == -1) { + if (errno == EWOULDBLOCK || errno == EAGAIN) { + // Socket would block, we'll try again later + } else { + ESP_LOGVV(TAG, "%s: Socket write failed with errno %d", this->info_.c_str(), errno); + this->state_ = State::FAILED; + return APIError::SOCKET_WRITE_FAILED; // Socket write failed + } } else if (sent > 0 && static_cast(sent) < front_buffer.remaining()) { // Partially sent, update offset front_buffer.offset += sent; @@ -129,19 +131,20 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) { // Try to send directly if no buffered data ssize_t sent = this->socket_->writev(iov, iovcnt); - if (this->is_would_block(sent)) { - // Socket would block, buffer the data - SendBuffer buffer; - buffer.data.reserve(total_write_len); + if (sent == -1) { + if (errno == EWOULDBLOCK || errno == EAGAIN) { + // Socket would block, buffer the data + SendBuffer buffer; + buffer.data.reserve(total_write_len); - for (int i = 0; i < iovcnt; i++) { - const uint8_t *data = reinterpret_cast(iov[i].iov_base); - buffer.data.insert(buffer.data.end(), data, data + iov[i].iov_len); + for (int i = 0; i < iovcnt; i++) { + const uint8_t *data = reinterpret_cast(iov[i].iov_base); + buffer.data.insert(buffer.data.end(), data, data + iov[i].iov_len); + } + + this->tx_buf_.push_back(std::move(buffer)); + return APIError::OK; // Success, data buffered } - - this->tx_buf_.push_back(std::move(buffer)); - return APIError::OK; // Success, data buffered - } else if (sent == -1) { // Socket error ESP_LOGVV(TAG, "%s: Socket write failed with errno %d", this->info_.c_str(), errno); this->state_ = State::FAILED; @@ -184,10 +187,11 @@ APIError APIFrameHelper::try_send_tx_buf_() { // Try to send the remaining data in this buffer ssize_t sent = this->socket_->write(front_buffer.current_data(), front_buffer.remaining()); - if (this->is_would_block(sent)) { - // Socket would block, try again later - break; - } else if (sent == -1) { + if (sent == -1) { + if (errno == EWOULDBLOCK || errno == EAGAIN) { + // Socket would block, try again later + break; + } // Socket error state_ = State::FAILED; ESP_LOGVV(TAG, "%s: Socket write failed with errno %d", this->info_.c_str(), errno); diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index c4e7072666..15d9d8664d 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -164,14 +164,6 @@ class APIFrameHelper { // Try to send data from the tx buffer APIError try_send_tx_buf_(); - - // Check if the socket would block based on return value - inline bool is_would_block(ssize_t ret) const { - if (ret == -1) { - return errno == EWOULDBLOCK || errno == EAGAIN; - } - return ret == 0; - } }; #ifdef USE_API_NOISE