ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
api_overflow_buffer.cpp
Go to the documentation of this file.
2#ifdef USE_API
3#include <cstring>
5
6namespace esphome::api {
7
9 // Nested call from inside socket->write(); see draining_
10 if (this->draining_)
11 return 0;
12
13 struct DrainGuard {
14 APIOverflowBuffer &owner;
15 ~DrainGuard() { this->owner.draining_ = false; }
16 } guard{*this};
17 this->draining_ = true;
18
19 while (this->count_ > 0) {
20 uint8_t *msg = this->buf_.data() + this->head_;
21 size_t len = encode_value_le<uint16_t>(msg);
22
23 ssize_t sent = socket->write(msg + LEN_PREFIX, len);
24 if (sent <= 0)
25 return sent;
26 if (static_cast<size_t>(sent) < len) {
27 // Step past the sent bytes and rewrite the prefix there; it lands on bytes already sent
28 this->head_ += sent;
29 len -= sent;
30 msg += sent;
31 msg[0] = len;
32 msg[1] = len >> 8;
33 return sent;
34 }
35 this->head_ += LEN_PREFIX + len;
36 this->count_--;
37 }
38
39 this->head_ = 0;
40 if (this->release_when_drained_) {
41 this->release_when_drained_ = false;
42 this->buf_.release();
43 } else {
44 this->buf_.clear();
45 }
46 return 0;
47}
48
49bool APIOverflowBuffer::enqueue_iov(const struct iovec *iov, int iovcnt, size_t total_len, size_t skip) {
50 if (this->count_ >= API_MAX_SEND_QUEUE)
51 return false;
52
53 const size_t new_len = total_len - skip;
54 const size_t new_bytes = LEN_PREFIX + new_len;
55 const size_t live = this->buf_.size() - this->head_;
56 // A lone message is only bound by the buffer; refusing it would just drop the connection
57 if (live + new_bytes > (this->count_ > 0 ? MAX_BYTES : MAX_LONE_BYTES))
58 return false;
59
60 if (this->buf_.size() + new_bytes > this->buf_.capacity()) {
61 // Storage would move under an outer drain's write()
62 if (this->draining_)
63 return false;
64 if (this->head_ > 0) {
65 // Reclaim the sent prefix before growing
66 this->buf_.drop_front(this->head_);
67 this->head_ = 0;
68 }
69 if (!this->buf_.reserve(reserve_for(live + new_bytes)))
70 return false;
71 }
72
73 uint8_t *dst = this->buf_.append(new_bytes);
74 if (dst == nullptr)
75 return false;
76 dst[0] = new_len;
77 dst[1] = new_len >> 8;
78 dst += LEN_PREFIX;
79 for (const struct iovec *end = iov + iovcnt; iov != end; iov++) {
80 if (skip >= iov->iov_len) {
81 skip -= iov->iov_len;
82 } else {
83 const size_t len = iov->iov_len - skip;
84 std::memcpy(dst, static_cast<const uint8_t *>(iov->iov_base) + skip, len);
85 dst += len;
86 skip = 0;
87 }
88 }
89
90 this->count_++;
91 return true;
92}
93
94} // namespace esphome::api
95
96#endif // USE_API
void drop_front(size_t drop)
Drop the first drop bytes, sliding the rest down. Precondition: drop <= size().
Definition api_buffer.h:46
void release()
Release all memory (equivalent to std::vector swap trick).
Definition api_buffer.h:61
uint8_t * append(size_t n)
Grow by n bytes; returns the new bytes, or nullptr on allocation failure.
size_t size() const
Definition api_buffer.h:55
bool reserve(size_t n) ESPHOME_ALWAYS_INLINE
Returns false if allocation fails; the buffer is left unchanged.
Definition api_buffer.h:31
TCP send backlog, only used when the socket send buffer is full.
bool enqueue_iov(const struct iovec *iov, int iovcnt, size_t total_len, size_t skip)
Queue iov data from byte offset skip as one message.
static constexpr size_t LEN_PREFIX
static constexpr size_t MAX_BYTES
static constexpr size_t reserve_for(size_t want)
ssize_t try_drain(socket::Socket *socket)
Drain queued messages to the socket.
static constexpr size_t MAX_LONE_BYTES
__int64 ssize_t
Definition httplib.h:178
const void size_t len
Definition hal.h:68
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1268
void * iov_base
Definition headers.h:113
size_t iov_len
Definition headers.h:114
uint8_t end[39]
Definition sun_gtil2.cpp:17