DeterministicESPAsyncWebServer 1.2.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
websocket.cpp File Reference

WebSocket frame parser and connection pool implementation. More...

#include "websocket.h"
#include "lwip/tcp.h"
#include <string.h>

Go to the source code of this file.

Functions

void ws_init ()
 Initialise all WebSocket pool slots to inactive.
 
WsConnws_alloc (uint8_t slot_id)
 Allocate a WsConn slot and bind it to a TCP slot.
 
WsConnws_find (uint8_t slot_id)
 Find the WsConn for a given TCP slot, or nullptr if none.
 
void ws_free (uint8_t slot_id)
 Free the WsConn associated with a TCP slot.
 
void ws_reset_frame (WsConn *ws)
 Reset the frame parser back to WS_HEADER1, ready for the next frame.
 
bool ws_send_frame (WsConn *ws, WsOpcode opcode, const uint8_t *payload, uint16_t len)
 Send a WebSocket frame to the client.
 
void ws_close (WsConn *ws, WsCloseCode code)
 Send a Close frame and mark the slot WS_CLOSED.
 
void ws_parse (WsConn *ws)
 Drain the ring buffer for slot_id and feed bytes to the WS parser.
 

Variables

WsConn ws_pool [MAX_WS_CONNS]
 Pool of WebSocket connection state, one per MAX_WS_CONNS.
 

Detailed Description

WebSocket frame parser and connection pool implementation.

Handles RFC 6455 framing. Control frames (ping/pong/close) are handled automatically here; data frames (text/binary) are surfaced to the application layer via WS_FRAME_READY.

Automatic control frame handling

  • Ping -> sends Pong with the same payload immediately.
  • Close -> sends echoed Close frame, marks slot WS_CLOSED.
  • Pong -> silently discarded (keepalive response, no action needed).

Definition in file websocket.cpp.

Function Documentation

◆ ws_init()

void ws_init ( )

Initialise all WebSocket pool slots to inactive.

Called once from DetWebServer::begin().

Definition at line 24 of file websocket.cpp.

References MAX_WS_CONNS, WsConn::ws_id, and ws_pool.

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

◆ ws_alloc()

WsConn * ws_alloc ( uint8_t  slot_id)

Allocate a WsConn slot and bind it to a TCP slot.

Parameters
slot_idTCP connection slot that just completed an upgrade.
Returns
Pointer to the allocated WsConn, or nullptr if the pool is full.

Definition at line 33 of file websocket.cpp.

References WsConn::active, MAX_WS_CONNS, WsConn::parse_state, WsConn::slot_id, WS_HEADER1, WsConn::ws_id, and ws_pool.

◆ ws_find()

WsConn * ws_find ( uint8_t  slot_id)

Find the WsConn for a given TCP slot, or nullptr if none.

Parameters
slot_idTCP connection slot index.

Definition at line 50 of file websocket.cpp.

References MAX_WS_CONNS, and ws_pool.

Referenced by DetWebServer::handle().

◆ ws_free()

void ws_free ( uint8_t  slot_id)

Free the WsConn associated with a TCP slot.

Parameters
slot_idTCP connection slot index.

Definition at line 60 of file websocket.cpp.

References MAX_WS_CONNS, WsConn::ws_id, and ws_pool.

Referenced by DetWebServer::handle().

◆ ws_reset_frame()

void ws_reset_frame ( WsConn ws)

Reset the frame parser back to WS_HEADER1, ready for the next frame.

Does not change ws->active or ws->slot_id.

Parameters
wsWebSocket connection to reset.

Definition at line 73 of file websocket.cpp.

References WsConn::buf, WsConn::fin, WsConn::len64_count, WsConn::mask_key, WsConn::masked, WsConn::opcode, WsConn::parse_state, WsConn::payload_idx, WsConn::payload_len, WS_HEADER1, and WS_OP_TEXT.

Referenced by DetWebServer::handle(), and ws_parse().

◆ ws_send_frame()

bool ws_send_frame ( WsConn ws,
WsOpcode  opcode,
const uint8_t *  payload,
uint16_t  len 
)

Send a WebSocket frame to the client.

Builds the header (no masking – server-to-client frames are never masked) and hands both to tcp_write(). The caller is responsible for calling tcp_output() afterwards.

Parameters
wsWebSocket connection.
opcodeFrame opcode (WS_OP_TEXT, WS_OP_BINARY, WS_OP_PONG, etc.).
payloadPayload bytes (may be nullptr for zero-length frames).
lenPayload length in bytes.
Returns
true on success, false if the TCP slot is not active.

Definition at line 90 of file websocket.cpp.

References CONN_ACTIVE, conn_pool, TcpConn::pcb, WsConn::slot_id, and TcpConn::state.

Referenced by ws_close(), and ws_parse().

◆ ws_close()

void ws_close ( WsConn ws,
WsCloseCode  code 
)

Send a Close frame and mark the slot WS_CLOSED.

Parameters
wsWebSocket connection.
codeClose status code (e.g. WS_CLOSE_NORMAL).

Definition at line 123 of file websocket.cpp.

References conn_pool, WsConn::parse_state, TcpConn::pcb, WsConn::slot_id, WS_CLOSED, WS_OP_CLOSE, and ws_send_frame().

Referenced by DetWebServer::handle(), and ws_parse().

◆ ws_parse()

void ws_parse ( WsConn ws)

Variable Documentation

◆ ws_pool

WsConn ws_pool[MAX_WS_CONNS]

Pool of WebSocket connection state, one per MAX_WS_CONNS.

Definition at line 22 of file websocket.cpp.

Referenced by ws_alloc(), ws_find(), ws_free(), and ws_init().