ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sse.h File Reference

Layer 6 (Presentation) – Server-Sent Events connection pool. More...

Go to the source code of this file.

Classes

struct  SseConn
 SSE connection state stored in pc_sse_pool[]. More...
 

Functions

void pc_sse_init ()
 Initialize all SSE pool slots to inactive.
 
SseConnpc_sse_alloc (uint8_t slot_id, const char *path)
 Allocate an SseConn and bind it to a TCP slot.
 
SseConnpc_sse_find (uint8_t slot_id)
 Find the SseConn for a given TCP slot, or nullptr.
 
void pc_sse_free (uint8_t slot_id)
 Free the SseConn associated with a TCP slot.
 
int pc_sse_format (char *buf, size_t n, const char *data, const char *event, const char *id)
 Format one SSE event record into a caller buffer (no transport).
 
bool pc_sse_write (SseConn *sse, const char *data, const char *event, const char *id)
 Write one SSE event record to a client.
 

Variables

SseConn pc_sse_pool [MAX_SSE_CONNS]
 Pool of SSE connection state, one per MAX_SSE_CONNS.
 

Detailed Description

Layer 6 (Presentation) – Server-Sent Events connection pool.

SSE (WHATWG HTML Living Standard, Server-sent events; the W3C EventSource API) is a long-lived HTTP GET response with Content-Type: text/event-stream. After the initial headers the connection stays open indefinitely; the server pushes newline-delimited event records at any time.

Event record format (WHATWG HTML, Server-sent events)

[event: <name>\n]
[id: <id>\n]
data: <payload>\n
\n

Each SseConn occupies one TCP slot from conn_pool[] for the lifetime of the subscription. The total number of simultaneous SSE connections is capped at MAX_SSE_CONNS.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file sse.h.

Function Documentation

◆ pc_sse_init()

void pc_sse_init ( )

Initialize all SSE pool slots to inactive.

Called once from PC::begin().

Definition at line 16 of file sse.cpp.

References MAX_SSE_CONNS, SseConn::pc_sse_id, and pc_sse_pool.

Referenced by PC::begin(), and PC::stop().

◆ pc_sse_alloc()

SseConn * pc_sse_alloc ( uint8_t  slot_id,
const char *  path 
)

Allocate an SseConn and bind it to a TCP slot.

Parameters
slot_idTCP slot that just received the SSE subscription request.
pathURL path the client subscribed to (stored for broadcast).
Returns
Pointer to the allocated SseConn, or nullptr if the pool is full.

Definition at line 25 of file sse.cpp.

References SseConn::active, MAX_PATH_LEN, MAX_SSE_CONNS, SseConn::path, SseConn::pc_sse_id, pc_sse_pool, and SseConn::slot_id.

◆ pc_sse_find()

SseConn * pc_sse_find ( uint8_t  slot_id)

Find the SseConn for a given TCP slot, or nullptr.

Parameters
slot_idTCP connection slot index.

Definition at line 43 of file sse.cpp.

References MAX_SSE_CONNS, and pc_sse_pool.

Referenced by PC::http_poll_slot().

◆ pc_sse_free()

void pc_sse_free ( uint8_t  slot_id)

Free the SseConn associated with a TCP slot.

Parameters
slot_idTCP connection slot index.

Definition at line 55 of file sse.cpp.

References MAX_SSE_CONNS, SseConn::pc_sse_id, and pc_sse_pool.

◆ pc_sse_format()

int pc_sse_format ( char *  buf,
size_t  n,
const char *  data,
const char *  event,
const char *  id 
)

Format one SSE event record into a caller buffer (no transport).

Emits event: <event>\n (if event), id: <id>\n (if id), then data: <data>\n\n per the WHATWG event-stream format. data must not be nullptr. Pure: no connection state, so it is unit-testable and benchable on its own; pc_sse_write() wraps it with the pc_conn_send() I/O.

Parameters
bufDestination buffer.
nSize of buf.
dataEvent data (required).
eventEvent name (optional).
idEvent ID (optional).
Returns
Bytes written (excluding the terminator), or 0 on empty/overflow.

Definition at line 82 of file sse.cpp.

Referenced by pc_sse_write().

◆ pc_sse_write()

bool pc_sse_write ( SseConn sse,
const char *  data,
const char *  event,
const char *  id 
)

Write one SSE event record to a client.

Formats and sends event: ...\nid: ...\ndata: ...\n\n. Any optional field may be nullptr to omit it. data must not be nullptr.

The caller must flush the connection afterwards (pc_conn_flush()) if immediate delivery is needed.

Parameters
sseSSE connection.
dataEvent data (required).
eventEvent name (optional).
idEvent ID (optional).
Returns
true on success, false if the TCP slot is not active.

Definition at line 116 of file sse.cpp.

References pc_conn_send(), pc_sse_format(), SseConn::slot_id, and SSE_BUF_SIZE.

Variable Documentation

◆ pc_sse_pool

SseConn pc_sse_pool[MAX_SSE_CONNS]
extern

Pool of SSE connection state, one per MAX_SSE_CONNS.

Definition at line 14 of file sse.cpp.

Referenced by pc_sse_alloc(), pc_sse_find(), pc_sse_free(), and pc_sse_init().