|
DeterministicESPAsyncWebServer 1.2.0
Zero-allocation, bounded-execution async HTTP server for ESP32
|
Layer 6 (Presentation) – WebSocket frame parser and connection pool. More...
Go to the source code of this file.
Classes | |
| struct | WsConn |
| WebSocket connection state stored in ws_pool[]. More... | |
Enumerations | |
| enum | WsOpcode { WS_OP_CONTINUATION = 0x0 , WS_OP_TEXT = 0x1 , WS_OP_BINARY = 0x2 , WS_OP_CLOSE = 0x8 , WS_OP_PING = 0x9 , WS_OP_PONG = 0xA } |
| WebSocket frame opcodes. More... | |
| enum | WsCloseCode { WS_CLOSE_NORMAL = 1000 , WS_CLOSE_GOING_AWAY = 1001 , WS_CLOSE_PROTOCOL = 1002 , WS_CLOSE_UNSUPPORTED =1003 , WS_CLOSE_TOO_BIG = 1009 } |
| WebSocket close status codes (RFC 6455 §7.4.1). More... | |
| enum | WsParseState { WS_HEADER1 , WS_HEADER2 , WS_LEN16_HI , WS_LEN16_LO , WS_LEN64 , WS_MASK0 , WS_MASK1 , WS_MASK2 , WS_MASK3 , WS_PAYLOAD , WS_FRAME_READY , WS_CLOSED , WS_ERROR } |
| States of the WebSocket frame parser. More... | |
Functions | |
| void | ws_init () |
| Initialise all WebSocket pool slots to inactive. | |
| WsConn * | ws_alloc (uint8_t slot_id) |
| Allocate a WsConn slot and bind it to a TCP slot. | |
| WsConn * | ws_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_parse (WsConn *ws) |
| Drain the ring buffer for slot_id and feed bytes to the WS parser. | |
| 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. | |
Variables | |
| WsConn | ws_pool [MAX_WS_CONNS] |
| Pool of WebSocket connection state, one per MAX_WS_CONNS. | |
Layer 6 (Presentation) – WebSocket frame parser and connection pool.
Implements RFC 6455 framing with a fixed-size payload buffer per slot. Connections are tracked in ws_pool[MAX_WS_CONNS]; each entry maps to one TCP slot in conn_pool[] via slot_id.
Frame format (client to server)
State machine
Limitations
Definition in file websocket.h.
| enum WsOpcode |
WebSocket frame opcodes.
Definition at line 66 of file websocket.h.
| enum WsCloseCode |
WebSocket close status codes (RFC 6455 §7.4.1).
Definition at line 77 of file websocket.h.
| enum WsParseState |
States of the WebSocket frame parser.
Definition at line 91 of file websocket.h.
| 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().
| WsConn * ws_alloc | ( | uint8_t | slot_id | ) |
Allocate a WsConn slot and bind it to a TCP slot.
| slot_id | TCP connection slot that just completed an upgrade. |
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.
| WsConn * ws_find | ( | uint8_t | slot_id | ) |
Find the WsConn for a given TCP slot, or nullptr if none.
| slot_id | TCP connection slot index. |
Definition at line 50 of file websocket.cpp.
References MAX_WS_CONNS, and ws_pool.
Referenced by DetWebServer::handle().
| void ws_free | ( | uint8_t | slot_id | ) |
Free the WsConn associated with a TCP slot.
| slot_id | TCP 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().
| void ws_parse | ( | WsConn * | ws | ) |
Drain the ring buffer for slot_id and feed bytes to the WS parser.
Stops when the ring buffer is empty or the parser reaches a terminal state (WS_FRAME_READY, WS_CLOSED, WS_ERROR).
| ws | WebSocket connection to drain into. |
Definition at line 140 of file websocket.cpp.
References WsConn::buf, CONN_ACTIVE, conn_pool, WsConn::fin, WsConn::len64_count, WsConn::mask_key, WsConn::masked, WsConn::opcode, WsConn::parse_state, WsConn::payload_idx, WsConn::payload_len, TcpConn::pcb, RX_BUF_SIZE, TcpConn::rx_buffer, TcpConn::rx_head, TcpConn::rx_tail, WsConn::slot_id, TcpConn::state, ws_close(), WS_CLOSE_NORMAL, WS_CLOSE_PROTOCOL, WS_CLOSE_TOO_BIG, WS_CLOSE_UNSUPPORTED, WS_CLOSED, WS_ERROR, WS_FRAME_READY, WS_FRAME_SIZE, WS_HEADER1, WS_HEADER2, WS_LEN16_HI, WS_LEN16_LO, WS_LEN64, WS_MASK0, WS_MASK1, WS_MASK2, WS_MASK3, WS_OP_CLOSE, WS_OP_CONTINUATION, WS_OP_PING, WS_OP_PONG, WS_PAYLOAD, ws_reset_frame(), and ws_send_frame().
Referenced by DetWebServer::handle().
| 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.
| ws | WebSocket 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().
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.
| ws | WebSocket connection. |
| opcode | Frame opcode (WS_OP_TEXT, WS_OP_BINARY, WS_OP_PONG, etc.). |
| payload | Payload bytes (may be nullptr for zero-length frames). |
| len | Payload length in bytes. |
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().
| void ws_close | ( | WsConn * | ws, |
| WsCloseCode | code | ||
| ) |
Send a Close frame and mark the slot WS_CLOSED.
| ws | WebSocket connection. |
| code | Close 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().
|
extern |
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().