From d1082199477bbd8ac2f5cac5b71d2dfad414cf0f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 May 2025 03:49:51 -0500 Subject: [PATCH] dry --- esphome/components/api/api_frame_helper.cpp | 35 ++++++++++----------- esphome/components/api/api_frame_helper.h | 3 ++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index 71b3409c78..53b8c8f01f 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -65,6 +65,20 @@ const char *api_error_to_str(APIError err) { return "UNKNOWN"; } +// Helper method to buffer data from IOVs +void APIFrameHelper::buffer_data_from_iov_(const struct iovec *iov, int iovcnt, size_t total_write_len) { + // Add new data to the buffer queue + 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); + } + + this->tx_buf_.push_back(std::move(buffer)); +} + // Common implementation for writing raw data to socket APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) { // This method writes data to socket or buffers it @@ -98,16 +112,7 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) { // If there is still data in the buffer, we can't send, buffer // the new data and return if (!this->tx_buf_.empty()) { - // Add new data to the buffer queue - 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); - } - - this->tx_buf_.push_back(std::move(buffer)); + this->buffer_data_from_iov_(iov, iovcnt, total_write_len); return APIError::OK; // Success, data buffered } } @@ -118,15 +123,7 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) { 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); - } - - this->tx_buf_.push_back(std::move(buffer)); + this->buffer_data_from_iov_(iov, iovcnt, total_write_len); return APIError::OK; // Success, data buffered } // Socket error diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index f711b091cb..081e10e90a 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -165,6 +165,9 @@ class APIFrameHelper { // Try to send data from the tx buffer APIError try_send_tx_buf_(); + + // Helper method to buffer data from IOVs + void buffer_data_from_iov_(const struct iovec *iov, int iovcnt, size_t total_write_len); }; #ifdef USE_API_NOISE