mirror of
https://github.com/esphome/esphome.git
synced 2026-09-17 18:18:43 +00:00
Inline enqueue_or_fail_ at single call site, add branch hints
Only one call site remains after tail-call restructure, so inline it. Add [[unlikely]]/[[likely]] hints for overflow and error paths.
This commit is contained in:
@@ -114,7 +114,7 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt, uint16_
|
||||
uint16_t skip = 0;
|
||||
|
||||
// If there is already backlogged data, try to drain it first
|
||||
if (!this->overflow_buf_.empty()) {
|
||||
if (!this->overflow_buf_.empty()) [[unlikely]] {
|
||||
if (this->overflow_buf_.try_drain(this->socket_.get()) == -1) {
|
||||
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||
if (this->check_socket_write_err_(errno) != APIError::WOULD_BLOCK)
|
||||
@@ -129,18 +129,23 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt, uint16_
|
||||
ssize_t sent =
|
||||
(iovcnt == 1) ? this->socket_->write(iov[0].iov_base, iov[0].iov_len) : this->socket_->writev(iov, iovcnt);
|
||||
|
||||
if (sent == -1) {
|
||||
if (sent == -1) [[unlikely]] { // Socket write failed
|
||||
HELPER_LOG("Socket write failed with errno %d", errno);
|
||||
if (this->check_socket_write_err_(errno) != APIError::WOULD_BLOCK)
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
} else if (static_cast<uint16_t>(sent) >= total_write_len) {
|
||||
return APIError::OK; // All data sent successfully
|
||||
} else if (static_cast<uint16_t>(sent) >= total_write_len) [[likely]] { // All data sent successfully
|
||||
return APIError::OK; // All data sent successfully
|
||||
} else {
|
||||
skip = static_cast<uint16_t>(sent); // Partially sent — queue the remainder
|
||||
}
|
||||
}
|
||||
|
||||
return this->enqueue_or_fail_(iov, iovcnt, total_write_len, skip);
|
||||
// Queue data into overflow buffer, or fail the connection if full
|
||||
if (!this->overflow_buf_.enqueue_iov(iov, iovcnt, total_write_len, skip)) {
|
||||
this->state_ = State::FAILED;
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
}
|
||||
return APIError::OK;
|
||||
}
|
||||
|
||||
const char *APIFrameHelper::get_peername_to(std::span<char, socket::SOCKADDR_STR_LEN> buf) const {
|
||||
|
||||
@@ -212,15 +212,6 @@ class APIFrameHelper {
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
}
|
||||
|
||||
// Enqueue IOV data into the overflow buffer, or fail the connection if full
|
||||
APIError enqueue_or_fail_(const struct iovec *iov, int iovcnt, uint16_t total_len, uint16_t skip) {
|
||||
if (!this->overflow_buf_.enqueue_iov(iov, iovcnt, total_len, skip)) {
|
||||
this->state_ = State::FAILED;
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
}
|
||||
return APIError::OK;
|
||||
}
|
||||
|
||||
// Socket ownership (4 bytes on 32-bit, 8 bytes on 64-bit)
|
||||
std::unique_ptr<socket::Socket> socket_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user