DeterministicESPAsyncWebServer 1.2.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sse.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sse.h
6 * @brief Layer 6 (Presentation) -- Server-Sent Events connection pool.
7 *
8 * SSE (RFC 8895 / W3C EventSource) is a long-lived HTTP GET response with
9 * Content-Type: text/event-stream. After the initial headers the connection
10 * stays open indefinitely; the server pushes newline-delimited event records
11 * at any time.
12 *
13 * **Event record format (RFC 8895 §9.2.6)**
14 * ```
15 * [event: <name>\n]
16 * [id: <id>\n]
17 * data: <payload>\n
18 * \n
19 * ```
20 *
21 * Each SseConn occupies one TCP slot from conn_pool[] for the lifetime of
22 * the subscription. The total number of simultaneous SSE connections is
23 * capped at MAX_SSE_CONNS.
24 *
25 * @author Douglas Quigg (dstroy0)
26 * @date 2026
27 */
28
29#ifndef DETERMINISTICESPASYNCWEBSERVER_SSE_H
30#define DETERMINISTICESPASYNCWEBSERVER_SSE_H
31
32#include "DetWebServerConfig.h"
33#include "transport.h"
34
35// ---------------------------------------------------------------------------
36// Per-connection SSE state
37// ---------------------------------------------------------------------------
38
39/**
40 * @brief SSE connection state stored in sse_pool[].
41 *
42 * Allocated when the SSE handshake (200 + headers) is sent. slot_id ties
43 * this entry back to conn_pool[] and the underlying TCP PCB.
44 */
45struct SseConn
46{
47 uint8_t sse_id; ///< Index into sse_pool[] (set at init).
48 uint8_t slot_id; ///< Owning TCP slot in conn_pool[].
49 bool active; ///< True when this entry is in use.
50
51 /** Path this client subscribed to (for sse_broadcast() matching). */
53};
54
55/** @brief Pool of SSE connection state, one per MAX_SSE_CONNS. */
57
58// ---------------------------------------------------------------------------
59// SSE pool API
60// ---------------------------------------------------------------------------
61
62/**
63 * @brief Initialise all SSE pool slots to inactive.
64 *
65 * Called once from DetWebServer::begin().
66 */
67void sse_init();
68
69/**
70 * @brief Allocate an SseConn and bind it to a TCP slot.
71 *
72 * @param slot_id TCP slot that just received the SSE subscription request.
73 * @param path URL path the client subscribed to (stored for broadcast).
74 * @return Pointer to the allocated SseConn, or nullptr if the pool is full.
75 */
76SseConn *sse_alloc(uint8_t slot_id, const char *path);
77
78/**
79 * @brief Find the SseConn for a given TCP slot, or nullptr.
80 *
81 * @param slot_id TCP connection slot index.
82 */
83SseConn *sse_find(uint8_t slot_id);
84
85/**
86 * @brief Free the SseConn associated with a TCP slot.
87 *
88 * @param slot_id TCP connection slot index.
89 */
90void sse_free(uint8_t slot_id);
91
92/**
93 * @brief Write one SSE event record to a client.
94 *
95 * Formats and sends `event: ...\nid: ...\ndata: ...\n\n`. Any optional
96 * field may be nullptr to omit it. data must not be nullptr.
97 *
98 * The caller must call tcp_output() on the underlying PCB afterwards if
99 * immediate delivery is needed.
100 *
101 * @param sse SSE connection.
102 * @param data Event data (required).
103 * @param event Event name (optional).
104 * @param id Event ID (optional).
105 * @return true on success, false if the TCP slot is not active.
106 */
107bool sse_write(SseConn *sse, const char *data,
108 const char *event, const char *id);
109
110#endif
User-facing configuration for DeterministicESPAsyncWebServer.
#define MAX_PATH_LEN
Maximum URL path length (including leading /).
#define MAX_SSE_CONNS
Maximum simultaneous SSE connections.
void sse_init()
Initialise all SSE pool slots to inactive.
Definition sse.cpp:16
bool sse_write(SseConn *sse, const char *data, const char *event, const char *id)
Write one SSE event record to a client.
Definition sse.cpp:66
SseConn sse_pool[MAX_SSE_CONNS]
Pool of SSE connection state, one per MAX_SSE_CONNS.
Definition sse.cpp:14
SseConn * sse_find(uint8_t slot_id)
Find the SseConn for a given TCP slot, or nullptr.
Definition sse.cpp:43
SseConn * sse_alloc(uint8_t slot_id, const char *path)
Allocate an SseConn and bind it to a TCP slot.
Definition sse.cpp:25
void sse_free(uint8_t slot_id)
Free the SseConn associated with a TCP slot.
Definition sse.cpp:53
SSE connection state stored in sse_pool[].
Definition sse.h:46
char path[MAX_PATH_LEN]
Definition sse.h:52
uint8_t sse_id
Index into sse_pool[] (set at init).
Definition sse.h:47
bool active
True when this entry is in use.
Definition sse.h:49
uint8_t slot_id
Owning TCP slot in conn_pool[].
Definition sse.h:48
Layer 4 (Transport) — TCP connection pool, ring buffers, and lwIP integration.