ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
async_stream_response.h
Go to the documentation of this file.
1#pragma once
2
4#if !defined(USE_ESP32) && defined(USE_ARDUINO)
5
6#include <ESPAsyncWebServer.h>
7
8#include <atomic>
9#include <cstddef>
10#include <cstdint>
11#include <memory>
12#include <span>
13#include <sys/types.h>
14
16#include "esphome/core/log.h"
17#include "stream_head.h"
18
20
23 void *iov_base;
24 size_t iov_len;
25};
26
30enum class StreamState : uint8_t {
31 STREAM_STATE_PENDING, // begin() ran; the request has not handed over the connection yet
32 STREAM_STATE_ATTACHED, // the connection is ours and the head is queued
33 STREAM_STATE_DISCONNECTED, // gone; no network callback references the stream any more
34};
35
37 public:
43
49 void begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
50 std::span<const HttpHeader> headers, const char *body = nullptr);
51
56 ssize_t write(StreamIovec *iov, int iovcnt);
57 ssize_t write(const void *data, size_t len);
59 void close();
61 void loop();
62
64 bool is_attached() const {
65 return this->state_.load(std::memory_order_acquire) == StreamState::STREAM_STATE_ATTACHED;
66 }
68 bool is_connected() const {
69 return this->state_.load(std::memory_order_acquire) != StreamState::STREAM_STATE_DISCONNECTED;
70 }
71 bool is_closing() const { return this->close_requested_; }
73 int log_id() const { return static_cast<int>(reinterpret_cast<uintptr_t>(this) & 0xFFFF); }
75 bool safe_to_delete() const {
76 return this->state_.load(std::memory_order_acquire) == StreamState::STREAM_STATE_DISCONNECTED;
77 }
78
79 static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
80 // While stalled, retry at most this often: on RP2 each try releases the lwIP lock, which postpones a polled
81 // Ethernet chip's 20 ms receive poll
82 static constexpr uint32_t STALL_RETRY_MS = 100;
83
84 protected:
85 friend class StreamHandoffResponse;
86
87 // Network context: take the connection and the part of the head not queued yet, as a C string; head_kept false
88 // (no memory for that part) closes it
89 void attach_(AsyncClient *client, RAMUniquePtr<char[]> head_rest, bool head_kept);
90 // Starts the stall clock; once it has run SEND_STALL_TIMEOUT_MS, logs reason and aborts. True when it did.
91 bool stall_or_close_(const LogString *reason);
92 // Graceful FIN, or with abort a RST that frees the pcb now (a stuck stream's FIN would queue behind its data)
93 void close_client_(bool abort = false);
94 void abort_();
95 // Logs how long the stream made no progress and why, then aborts it
96 void __attribute__((cold)) close_stuck_(uint32_t elapsed_ms, const LogString *reason);
97
98 // Set before the state turns attached; the main loop owns it and frees it once disconnected
99 AsyncClient *client_{nullptr};
100 // The part of the head the send buffer could not take in the network context; sent before anything else
102 // Runs while sends make no progress
105 // Send buffer free after the last write that queued bytes, and when an ACK was last seen (from attach_() on);
106 // main loop only once attached
109 // Set from the network callbacks with plain stores (no read-modify-write: RP2040 has none)
110 // Only moves forward (pending, attached, disconnected), set with plain stores from the network context, the
111 // request's teardown and abort_now(); one byte keeps the three main loop flags below in the same word
112 std::atomic<StreamState> state_{StreamState::STREAM_STATE_PENDING};
113 std::atomic<bool> ack_timed_out_{false};
114 // Main loop only, apart from attach_() setting close_requested_ when the head was lost
115 bool close_requested_{false};
116 bool close_sent_{false};
117};
118
119} // namespace esphome::web_server_base
120
121#endif // !USE_ESP32 && USE_ARDUINO
bool safe_to_delete() const
The connection is gone; no network callback still references this object.
void loop()
Main loop, every pass. Aborts (RST) a stream whose last send went unacknowledged for SEND_STALL_TIMEO...
void close()
Main loop. Closes the connection; safe to call more than once.
AsyncStreamResponse(const AsyncStreamResponse &)=delete
void __attribute__((cold)) close_stuck_(uint32_t elapsed_ms
void begin(AsyncWebServerRequest *request, const char *status, const char *content_type, std::span< const HttpHeader > headers, const char *body=nullptr)
Network context, from a request handler, once.
ssize_t write(StreamIovec *iov, int iovcnt)
Main loop.
void attach_(AsyncClient *client, RAMUniquePtr< char[]> head_rest, bool head_kept)
AsyncStreamResponse & operator=(const AsyncStreamResponse &)=delete
bool is_connected() const
The client is still connected.
int log_id() const
An id for log lines only.
bool is_attached() const
The connection is taken from the request and the head queued; write() needs it.
~AsyncStreamResponse()
Main loop, once safe_to_delete(): frees the connection.
How long sends have made no progress.
__int64 ssize_t
Definition httplib.h:178
StreamState
A long lived response the main loop writes to without blocking, on ESPAsyncWebServer; the same interf...
std::unique_ptr< T, RAMDeleter< T > > RAMUniquePtr
unique_ptr over RAMAllocator storage
Definition helpers.h:2659
const void size_t len
Definition hal.h:68
static void uint32_t
One entry of the gather list AsyncStreamResponse::write() takes; the TCP libraries take plain buffers...