ESPHome 2026.9.0
Loading...
Searching...
No Matches
api_overflow_buffer.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cstddef>
4#include <cstdint>
5#include <sys/types.h>
6
8#ifdef USE_API
9
12#include "api_buffer.h"
13
14namespace esphome::api {
15
23 public:
25 bool empty() const { return this->count_ == 0; }
26
31
35 bool enqueue_iov(const struct iovec *iov, int iovcnt, size_t total_len, size_t skip);
36
38 void release() {
39 if (this->count_ == 0) {
40 this->buf_.release();
41 } else {
42 this->release_when_drained_ = true;
43 }
44 }
45
46 protected:
47 static constexpr size_t LEN_PREFIX = 2;
48 static constexpr size_t BYTES_PER_SLOT = 2048;
49 // Reserve in 256 byte steps so a creeping high-water mark settles quickly
50 static constexpr size_t GROW_QUANTUM = 256;
51 // Lone message ceiling, rounded down so reserve_for() never exceeds the buffer limit
52 static constexpr size_t MAX_LONE_BYTES = APIBuffer::MAX_SIZE & ~(GROW_QUANTUM - 1);
53 static constexpr size_t MAX_BYTES = std::min(API_MAX_SEND_QUEUE * BYTES_PER_SLOT, MAX_LONE_BYTES);
54 static constexpr size_t reserve_for(size_t want) { return (want + GROW_QUANTUM - 1) & ~(GROW_QUANTUM - 1); }
55
57 uint16_t head_{0}; // offset of the front message's length prefix; bytes before it are sent
58 uint8_t count_{0};
59 // socket->write() can re-enter the send path (log from an lwip callback):
60 // a nested drain makes no progress and a nested enqueue never moves storage
61 bool draining_ : 1 {false};
62 bool release_when_drained_ : 1 {false};
63};
64
65} // namespace esphome::api
66
67#endif // USE_API
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:26
void release()
Release all memory (equivalent to std::vector swap trick).
Definition api_buffer.h:61
static constexpr size_t MAX_SIZE
Definition api_buffer.h:28
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
void release()
Free the retained storage, now if empty, otherwise once it has drained.
bool empty() const
True when no backlogged data is waiting.
static constexpr size_t reserve_for(size_t want)
static constexpr size_t GROW_QUANTUM
static constexpr size_t BYTES_PER_SLOT
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