ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
websocket.h File Reference

Layer 6 (Presentation) – WebSocket frame parser and connection pool. More...

#include "protocore_config.h"

Go to the source code of this file.

Detailed Description

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)

0 1 2 3
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| payload len | extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| +-------------------------------+
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - -+
| extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - -+-------------------------------+
| | masking key, if MASK set |
+-------------------------------+-------------------------------+
| masking key (continued) | payload data |
+-------------------------------- - - - - - - - - - - - - - - -+
: payload data continued :
+---------------------------------------------------------------+

State machine

WS_HEADER1 -- read FIN + opcode byte
WS_HEADER2 -- read MASK + 7-bit payload length
WS_LEN16_HI -- read extended 16-bit length high byte
WS_LEN16_LO -- read extended 16-bit length low byte
WS_LEN64 -- consume 8-byte 64-bit length (reject; too large)
WS_MASK0..3 -- read 4-byte masking key
WS_PAYLOAD -- accumulate payload bytes (unmasked)
WS_FRAME_READY -- complete frame waiting for dispatch
WS_CLOSED -- connection closed; slot may be recycled
WS_ERROR -- protocol error; close frame sent

Limitations

  • A reassembled message must fit in WS_FRAME_SIZE bytes; larger closes 1009.
  • RSV bits must be zero (no extensions supported).

Fragmentation (RFC 6455 ยง5.4) Fragmented data messages are reassembled into buf across continuation frames; the message is delivered only when the FIN frame arrives. Control frames (ping/pong/close) may be interleaved between fragments and are handled immediately without disturbing the partial message.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file websocket.h.