ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::web_server_base::AsyncStreamResponse Class Reference

#include <async_stream_response.h>

Inheritance diagram for esphome::web_server_base::AsyncStreamResponse:
esphome::web_server::AsyncEventSourceResponse

Public Member Functions

 AsyncStreamResponse ()=default
 
 AsyncStreamResponse (const AsyncStreamResponse &)=delete
 
AsyncStreamResponse & operator= (const AsyncStreamResponse &)=delete
 
 ~AsyncStreamResponse ()
 Main loop, once safe_to_delete(): frees the connection.
 
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.
 
ssize_t write (const void *data, size_t len)
 
void close ()
 Main loop. Closes the connection; safe to call more than once.
 
void loop ()
 Main loop, every pass. Aborts (RST) a stream whose last send went unacknowledged for SEND_STALL_TIMEOUT_MS.
 
bool is_attached () const
 The connection is taken from the request and the head queued; write() needs it.
 
bool is_connected () const
 The client is still connected.
 
bool is_closing () const
 
int log_id () const
 An id for log lines only.
 
bool safe_to_delete () const
 The connection is gone; no network callback still references this object.
 

Static Public Attributes

static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000
 
static constexpr uint32_t STALL_RETRY_MS = 100
 

Protected Member Functions

void attach_ (AsyncClient *client, RAMUniquePtr< char[]> head_rest, bool head_kept)
 
bool stall_or_close_ (const LogString *reason)
 
void close_client_ (bool abort=false)
 
void abort_ ()
 
void __attribute__ ((cold)) close_stuck_(uint32_t elapsed_ms
 

Protected Attributes

void const LogString * reason
 
AsyncClient * client_ {nullptr}
 
RAMUniquePtr< char[]> head_rest_
 
StallClock stall_
 
uint32_t last_try_ms_ {0}
 
size_t space_after_write_ {0}
 
uint32_t last_ack_ms_ {0}
 
std::atomic< StreamState > state_ {StreamState::STREAM_STATE_PENDING}
 
std::atomic< bool > ack_timed_out_ {false}
 
bool close_requested_ {false}
 
bool close_sent_ {false}
 

Detailed Description

Definition at line 36 of file async_stream_response.h.

Constructor & Destructor Documentation

◆ AsyncStreamResponse() [1/2]

esphome::web_server_base::AsyncStreamResponse::AsyncStreamResponse ( )
default

◆ AsyncStreamResponse() [2/2]

esphome::web_server_base::AsyncStreamResponse::AsyncStreamResponse ( const AsyncStreamResponse & )
delete

◆ ~AsyncStreamResponse()

esphome::web_server_base::AsyncStreamResponse::~AsyncStreamResponse ( )

Main loop, once safe_to_delete(): frees the connection.

Definition at line 172 of file async_stream_response.cpp.

Member Function Documentation

◆ __attribute__()

void esphome::web_server_base::AsyncStreamResponse::__attribute__ ( (cold) )
protected

◆ abort_()

void esphome::web_server_base::AsyncStreamResponse::abort_ ( )
protected

Definition at line 278 of file async_stream_response.cpp.

◆ attach_()

void esphome::web_server_base::AsyncStreamResponse::attach_ ( AsyncClient * client,
RAMUniquePtr< char[]> head_rest,
bool head_kept )
protected

Definition at line 140 of file async_stream_response.cpp.

◆ begin()

void esphome::web_server_base::AsyncStreamResponse::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.

Queues the library's head for status (for example "200 OK": status line, default headers, Content-Type) with headers and body (a C string or null; all may be in flash), then takes the connection from the request. A chunked response passes {"Transfer-Encoding", "chunked"} in headers and frames each chunk itself. The head is built after the handler returns, so headers, their strings and body must outlive the request (static data in practice); the ESP-IDF begin() sends it at once.

Definition at line 134 of file async_stream_response.cpp.

◆ close()

void esphome::web_server_base::AsyncStreamResponse::close ( )

Main loop. Closes the connection; safe to call more than once.

Definition at line 283 of file async_stream_response.cpp.

◆ close_client_()

void esphome::web_server_base::AsyncStreamResponse::close_client_ ( bool abort = false)
protected

Definition at line 288 of file async_stream_response.cpp.

◆ is_attached()

bool esphome::web_server_base::AsyncStreamResponse::is_attached ( ) const
inline

The connection is taken from the request and the head queued; write() needs it.

Definition at line 64 of file async_stream_response.h.

◆ is_closing()

bool esphome::web_server_base::AsyncStreamResponse::is_closing ( ) const
inline

Definition at line 71 of file async_stream_response.h.

◆ is_connected()

bool esphome::web_server_base::AsyncStreamResponse::is_connected ( ) const
inline

The client is still connected.

Definition at line 68 of file async_stream_response.h.

◆ log_id()

int esphome::web_server_base::AsyncStreamResponse::log_id ( ) const
inline

An id for log lines only.

Definition at line 73 of file async_stream_response.h.

◆ loop()

void esphome::web_server_base::AsyncStreamResponse::loop ( )

Main loop, every pass. Aborts (RST) a stream whose last send went unacknowledged for SEND_STALL_TIMEOUT_MS.

Definition at line 305 of file async_stream_response.cpp.

◆ operator=()

AsyncStreamResponse & esphome::web_server_base::AsyncStreamResponse::operator= ( const AsyncStreamResponse & )
delete

◆ safe_to_delete()

bool esphome::web_server_base::AsyncStreamResponse::safe_to_delete ( ) const
inline

The connection is gone; no network callback still references this object.

Definition at line 75 of file async_stream_response.h.

◆ stall_or_close_()

bool esphome::web_server_base::AsyncStreamResponse::stall_or_close_ ( const LogString * reason)
protected

Definition at line 264 of file async_stream_response.cpp.

◆ write() [1/2]

ssize_t esphome::web_server_base::AsyncStreamResponse::write ( const void * data,
size_t len )

Definition at line 259 of file async_stream_response.cpp.

◆ write() [2/2]

ssize_t esphome::web_server_base::AsyncStreamResponse::write ( StreamIovec * iov,
int iovcnt )

Main loop.

Non-blocking gather write. Returns the bytes accepted, which may be fewer than offered: the caller keeps the rest and continues from there. 0 means the send buffer is full, the last send is not acked yet (ESP8266 only) or the connection is not ours yet; -1 means the stream is closing. SEND_STALL_TIMEOUT_MS of refused writes, or of writes with no ACK in between, also aborts it (RST).

Definition at line 184 of file async_stream_response.cpp.

Field Documentation

◆ ack_timed_out_

std::atomic<bool> esphome::web_server_base::AsyncStreamResponse::ack_timed_out_ {false}
protected

Definition at line 113 of file async_stream_response.h.

◆ client_

AsyncClient* esphome::web_server_base::AsyncStreamResponse::client_ {nullptr}
protected

Definition at line 99 of file async_stream_response.h.

◆ close_requested_

bool esphome::web_server_base::AsyncStreamResponse::close_requested_ {false}
protected

Definition at line 115 of file async_stream_response.h.

◆ close_sent_

bool esphome::web_server_base::AsyncStreamResponse::close_sent_ {false}
protected

Definition at line 116 of file async_stream_response.h.

◆ head_rest_

RAMUniquePtr<char[]> esphome::web_server_base::AsyncStreamResponse::head_rest_
protected

Definition at line 101 of file async_stream_response.h.

◆ last_ack_ms_

uint32_t esphome::web_server_base::AsyncStreamResponse::last_ack_ms_ {0}
protected

Definition at line 108 of file async_stream_response.h.

◆ last_try_ms_

uint32_t esphome::web_server_base::AsyncStreamResponse::last_try_ms_ {0}
protected

Definition at line 104 of file async_stream_response.h.

◆ reason

void const LogString* esphome::web_server_base::AsyncStreamResponse::reason
protected

Definition at line 96 of file async_stream_response.h.

◆ SEND_STALL_TIMEOUT_MS

uint32_t esphome::web_server_base::AsyncStreamResponse::SEND_STALL_TIMEOUT_MS = 20000
staticconstexpr

Definition at line 79 of file async_stream_response.h.

◆ space_after_write_

size_t esphome::web_server_base::AsyncStreamResponse::space_after_write_ {0}
protected

Definition at line 107 of file async_stream_response.h.

◆ stall_

StallClock esphome::web_server_base::AsyncStreamResponse::stall_
protected

Definition at line 103 of file async_stream_response.h.

◆ STALL_RETRY_MS

uint32_t esphome::web_server_base::AsyncStreamResponse::STALL_RETRY_MS = 100
staticconstexpr

Definition at line 82 of file async_stream_response.h.

◆ state_

std::atomic<StreamState> esphome::web_server_base::AsyncStreamResponse::state_ {StreamState::STREAM_STATE_PENDING}
protected

Definition at line 112 of file async_stream_response.h.


The documentation for this class was generated from the following files: