60#ifndef DETERMINISTICESPASYNCWEBSERVER_WEBSOCKET_H
61#define DETERMINISTICESPASYNCWEBSERVER_WEBSOCKET_H
150#if DETWS_ENABLE_WS_DEFLATE
User-facing configuration for DeterministicESPAsyncWebServer.
#define WS_FRAME_SIZE
Maximum WebSocket frame payload in bytes.
#define MAX_WS_CONNS
Maximum simultaneous WebSocket connections.
WebSocket connection state stored in ws_pool[].
bool active
True when this entry is in use.
uint8_t len64_count
Bytes consumed from 64-bit length.
uint8_t mask_key[4]
Client masking key.
uint32_t payload_len
Expected payload byte count (current frame).
uint32_t msg_len
Bytes assembled so far across all fragments.
uint8_t ws_id
Index into ws_pool[] (set at init).
bool fin
FIN bit of the frame being parsed.
bool masked
True if client sent a masking key.
uint8_t slot_id
Owning TCP slot in conn_pool[].
WsOpcode msg_opcode
Opcode of the data message being assembled.
uint32_t payload_idx
Bytes received so far (current frame).
WsParseState parse_state
Current frame parser state.
WsOpcode opcode
Opcode of the frame being parsed.
bool fragmenting
True between a non-FIN data frame and its FIN.
uint8_t buf[WS_FRAME_SIZE+1]
Reassembled message payload, null-terminated.
uint8_t ctl_buf[125+1]
Control-frame payload (ping/pong/close), null-terminated.
Layer 4 (Transport) - TCP connection pool, ring buffers, and lwIP integration.
WsCloseCode
WebSocket close status codes (RFC 6455 §7.4.1).
@ WS_CLOSE_INVALID_PAYLOAD
Text message that is not valid UTF-8 (RFC 6455 8.1).
@ WS_CLOSE_PROTOCOL
Protocol error.
@ WS_CLOSE_NORMAL
Normal closure.
@ WS_CLOSE_UNSUPPORTED
Received a data type the endpoint cannot accept (RFC 6455).
@ WS_CLOSE_GOING_AWAY
Endpoint going away.
@ WS_CLOSE_TOO_BIG
Payload too large for WS_FRAME_SIZE.
void ws_free(uint8_t slot_id)
Free the WsConn associated with a TCP slot.
bool ws_send_frame(WsConn *ws, WsOpcode opcode, const uint8_t *payload, uint16_t len)
Send a WebSocket frame to the client.
const char * ws_payload(uint8_t ws_id)
The NUL-terminated reassembled message payload for ws_id, or nullptr if the slot is out of range / in...
WsParseState
States of the WebSocket frame parser.
@ WS_MASK3
Reading masking key byte 3.
@ WS_PAYLOAD
Accumulating payload bytes.
@ WS_FRAME_READY
Complete frame ready for dispatch.
@ WS_LEN16_LO
Reading extended 16-bit length, low byte.
@ WS_ERROR
Protocol error; close frame has been queued.
@ WS_HEADER2
Awaiting second header byte (MASK, 7-bit length).
@ WS_MASK2
Reading masking key byte 2.
@ WS_LEN64
Consuming 8-byte 64-bit length (always rejected).
@ WS_LEN16_HI
Reading extended 16-bit length, high byte.
@ WS_MASK0
Reading masking key byte 0.
@ WS_CLOSED
Connection closed; slot may be recycled.
@ WS_MASK1
Reading masking key byte 1.
@ WS_HEADER1
Awaiting first header byte (FIN, RSV, opcode).
void ws_feed_byte(WsConn *ws, uint8_t byte)
Feed one already-plaintext byte through the WS frame state machine.
void ws_close(WsConn *ws, WsCloseCode code)
Send a Close frame and mark the slot WsParseState::WS_CLOSED.
WsOpcode
WebSocket frame opcodes.
@ WS_OP_CLOSE
Connection close.
@ WS_OP_PING
Ping (auto-ponged by the library).
@ WS_OP_CONTINUATION
Continuation frame (data-message fragment; reassembled into buf).
@ WS_OP_PONG
Pong (echoed ping; ignored by library).
@ WS_OP_TEXT
UTF-8 text payload.
@ WS_OP_BINARY
Binary payload.
void ws_parse(WsConn *ws)
Drain the ring buffer for slot_id and feed bytes to the WS parser.
WsConn * ws_find(uint8_t slot_id)
Find the WsConn for a given TCP slot, or nullptr if none.
void ws_reset_frame(WsConn *ws)
Reset the frame parser back to WsParseState::WS_HEADER1, ready for the next frame.
void ws_set_frag_size(uint16_t bytes)
Set the outbound fragmentation size (RFC 6455 sec 5.4), in payload bytes; 0 = off.
bool ws_active(uint8_t ws_id)
True if ws_id is a valid, in-use WebSocket slot. Use this instead of reaching into ws_pool[ws_id]....
WsConn * ws_alloc(uint8_t slot_id)
Allocate a WsConn slot and bind it to a TCP slot.
WsConn ws_pool[MAX_WS_CONNS]
Pool of WebSocket connection state, one per MAX_WS_CONNS.
void ws_init()
Initialize all WebSocket pool slots to inactive.