save some bytes

This commit is contained in:
J. Nick Koston
2025-05-15 03:24:47 -05:00
parent bafc57f02e
commit 2646ec166b
2 changed files with 6 additions and 4 deletions

View File

@@ -185,9 +185,10 @@ APIError APIFrameHelper::try_send_tx_buf_() {
} else if (sent == 0) {
// Nothing sent but not an error
return APIError::WOULD_BLOCK;
} else if (static_cast<size_t>(sent) < front_buffer.remaining()) {
} else if (static_cast<uint16_t>(sent) < front_buffer.remaining()) {
// Partially sent, update offset
front_buffer.offset += sent;
// Cast to ensure no overflow issues with uint16_t
front_buffer.offset += static_cast<uint16_t>(sent);
return APIError::WOULD_BLOCK; // Stop processing more buffers if we couldn't send a complete buffer
} else {
// Buffer completely sent, remove it from the queue

View File

@@ -121,9 +121,10 @@ class APIFrameHelper {
// Buffer containing data to be sent
struct SendBuffer {
std::vector<uint8_t> data;
size_t offset{0}; // Current offset within the buffer
uint16_t offset{0}; // Current offset within the buffer (uint16_t to reduce memory usage)
size_t remaining() const { return data.size() - offset; }
// Using uint16_t reduces memory usage since ESPHome API messages are limited to 64KB max
uint16_t remaining() const { return static_cast<uint16_t>(data.size()) - offset; }
const uint8_t *current_data() const { return data.data() + offset; }
};