2#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
19#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
20#pragma GCC diagnostic error "-Wstack-usage=2048"
50 this->adopt_pending_sessions_main_loop_();
56 if (ses->safe_to_delete()) {
68void AsyncEventSource::adopt_pending_sessions_main_loop_() {
76 while (incoming !=
nullptr) {
78 incoming = rsp->
next_;
80 if (rsp->safe_to_delete()) {
85 if (!rsp->is_attached()) {
100 for (
auto *ses = this->
sessions_; ses !=
nullptr; ses = ses->
next_) {
101 if (ses->is_connected()) {
102 ses->try_send_nodefer(
message, message_len, event,
id, reconnect);
108 for (
auto *ses = this->
sessions_; ses !=
nullptr; ses = ses->
next_) {
109 if (ses->is_connected()) {
110 ses->deferrable_send_state(source,
false, message_generator);
118 web_server_base::STREAM_HEAD_MAX_IOV,
119 "the event stream head must go out in one write");
131 ESP_LOGW(TAG,
"Config not sent to stream %d", this->
log_id());
137#ifdef USE_WEBSERVER_SORTING
140 JsonObject root = builder.
root();
142 root[ESPHOME_F(
"sorting_weight")] = group.weight;
151 ESP_LOGW(TAG,
"Sorting groups not sent to stream %d", this->
log_id());
172 this->deferred_queue_.push_back(item);
208 const ssize_t sent = this->
write(this->
tail_.get() + this->tail_sent_, remaining);
213 this->tail_sent_ += sent;
215 ESP_LOGV(TAG,
"Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
238 size_t message_len,
size_t total,
size_t sent) {
242 "the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
246 ESP_LOGW(TAG,
"Cannot buffer a %zu byte chunk, closing", total);
260 size_t message_len,
size_t total) {
274 uint8_t *dst = grown ? grown.get() :
src;
278 this->
tail_ = std::move(grown);
285 AsyncStreamResponse::loop();
301#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
302#pragma GCC diagnostic push
303#pragma GCC diagnostic ignored "-Wstack-usage="
307 char *
buffer,
size_t *size_out) {
310 SendGuard guard{*
this};
324 const size_t prefix_len =
build_chunk_prefix(prefix,
sizeof(prefix),
"state", 0, 0,
true);
335 json_len = builder.
serialize_to(
reinterpret_cast<char *
>(this->
tail_.get()) + prefix_len, room);
336 if (json_len < room) {
347 write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
348 uint8_t *dst = this->
tail_.get();
349 std::memcpy(dst, prefix, prefix_len);
355#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
356#pragma GCC diagnostic pop
379 ESP_LOGW(TAG,
"State event does not fit %zu bytes, dropped",
size);
395 ESP_LOGW(TAG,
"No memory for a %zu byte chunk", cap);
418 if (
message ==
nullptr && prefix_len == CHUNK_HDR_LEN) {
423 ESP_LOGW(TAG,
"Event name too long, dropped");
434 g.total = prefix_len;
437 [](
void *ctx,
const char *piece,
size_t len) {
438 auto &g = *
static_cast<Gather *
>(ctx);
447 g.iov[g.iovcnt++] = {
const_cast<char *
>(piece),
len};
451 write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
452 g.iov[0] = {prefix, prefix_len};
459 const ssize_t sent = g.fits ? this->
write(g.iov, g.iovcnt) : 0;
466 if (
static_cast<size_t>(sent) == g.total) {
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void begin(bool include_internal=false)
ESPHOME_ALWAYS_INLINE void try_advance(size_t max_steps)
Run up to max_steps iteration steps; stops early when iteration completes or a callback refuses (that...
An STL allocator that uses SPI or internal RAM.
RAMUniquePtr< T[]> make_unique_array_for_overwrite(size_t n)
n elements left uninitialized, as std::make_unique_for_overwrite does; empty on exhaustion,...
Bump allocator over a fixed buffer for a document built and serialized in one scope.
Builder class for creating JSON documents without lambdas.
size_t serialize_to(char *buf, size_t cap)
Serialize into a caller owned buffer.
SerializationBuffer serialize()
Serialize the JSON document to a SerializationBuffer (stack-first allocation) Uses 512-byte stack buf...
void handle_request(AsyncWebServerRequest *request, WebServer *ws)
void push_pending_(AsyncEventSourceResponse *rsp)
std::atomic< AsyncEventSourceResponse * > pending_sessions_
AsyncEventSourceResponse * sessions_
bool loop()
Returns true if there are sessions remaining (including pending cleanup).
void deferrable_send_state(void *source, message_generator_t *message_generator)
void try_send_nodefer(const char *message, size_t message_len, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
static constexpr size_t TAIL_QUEUE_SIZE
void process_deferred_queue_()
AsyncEventSourceResponse * next_
static constexpr size_t PREFIX_BUF_SIZE
static constexpr size_t JSON_BUF_SIZE
RAMUniquePtr< uint8_t[]> tail_
std::vector< DeferredEvent > deferred_queue_
ListEntitiesIterator entities_iterator_
static constexpr size_t MAX_SEND_IOV
void start_session_main_loop_()
bool try_send_nodefer(const char *message, size_t message_len, const char *event=nullptr, uint32_t id=0, uint32_t reconnect=0)
bool reserve_tail_(size_t len)
JsonPrepareResult message_generator_t * generator
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator)
bool append_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total)
JsonPrepareResult message_generator_t char * buffer
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator)
AsyncEventSourceResponse(WebServer *ws)
@ JSON_PREPARE_RESULT_NO_MEMORY
@ JSON_PREPARE_RESULT_TAIL
@ JSON_PREPARE_RESULT_BUFFER
@ JSON_PREPARE_RESULT_TOO_LARGE
static constexpr size_t TAIL_MAX_SIZE
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total, size_t sent)
JsonPrepareResult message_generator_t char size_t * size_out
bool send_json_(void *source, message_generator_t *generator)
void tail_alloc_failed_(size_t cap)
This class allows users to create a web server with their ESP nodes.
json::SerializationBuffer get_config_json()
Return the webserver configuration as JSON.
void close()
Main loop. Closes the connection; safe to call more than once.
ssize_t write(StreamIovec *iov, int iovcnt)
Main loop.
bool stall_or_close_(const LogString *reason)
bool is_connected() const
The client is still connected.
int log_id() const
An id for log lines only.
const LogString * message
JsonString linked(const char *s)
LockGuard StreamIntakeGuard
constexpr size_t stream_head_iov_count(size_t header_count)
Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body.
size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len, const char *message, size_t message_len)
const web_server_base::HttpHeader SSE_HEADERS[SSE_HEADERS_COUNT]
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx)
size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len)
constexpr size_t SSE_SEP_LEN
ESPHOME_LOG_TAG(TAG, "web_server")
const std::array< SortingGroup, WEBSERVER_SORTING_GROUP_COUNT > SORTING_GROUPS
size_t tail_queue_cap(size_t need, size_t cap, size_t max)
const char SSE_CONTENT_TYPE[]
constexpr size_t LOG_EVENT_MAX_LEN
constexpr size_t SSE_HEADERS_COUNT
void(WebServer *, void *, json::JsonBuilder &) message_generator_t
const char SSE_FIRST_CHUNK[]
size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data)
constexpr size_t SSE_SUFFIX_LEN
std::unique_ptr< T, RAMDeleter< T > > RAMUniquePtr
unique_ptr over RAMAllocator storage
void progmem_memcpy(void *dst, const void *src, size_t len)
uint32_t IRAM_ATTR HOT millis()
message_generator_t * message_generator_
One entry of the gather list AsyncStreamResponse::write() takes; the TCP libraries take plain buffers...