|
DeterministicESPAsyncWebServer 1.2.0
Zero-allocation, bounded-execution async HTTP server for ESP32
|
WebSocket frame parser and connection pool implementation. More...
Go to the source code of this file.
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_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. | |
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
Definition in file websocket.cpp.
| 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_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().
| 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().
| 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().