|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
One WebSocket frame/message (RFC 6455): parsing, generation and control frames. More...
#include <NetworkProtocols.h>
Public Types | |
| enum | DataType { NONE = 0 , TEXT = 1 , BINARY = 2 , CLOSE = 8 , PING = 9 , PONG = 10 } |
| Frame content type, mapping to the RFC 6455 opcode. More... | |
| enum | WSSTATUS { IDLE = 0 , INCOMPLETE = 1 , COMPLETE = 2 } |
| Parsing status of this frame: not started, awaiting more bytes, or fully received. More... | |
Public Member Functions | |
| WebsocketData (uint64 source=0, uint64 startRecTime=0) | |
| Construct an empty frame. | |
| WebsocketData (WebsocketData *req) | |
| Deep-copy constructor. | |
| virtual | ~WebsocketData () |
| bool | isTerminationRequest () |
| bool | isTerminationConfirmation () |
| bool | isComplete () |
| bool | isPing () |
| bool | isPong () |
| bool | setData (DataType dataType, bool maskData, const char *data=NULL, uint64 size=0) |
| Set the payload and build the serialised frame for sending. | |
| const char * | getRawData (uint64 &size) |
| Get the serialised on-the-wire frame bytes (header + masked payload). | |
| bool | processHeader (const char *buffer, uint64 size, bool &isInvalid) |
| Parse the frame header from raw bytes (handles 16/64-bit extended lengths). | |
| bool | processContent (const char *buffer, uint64 size) |
| Append (and unmask) payload bytes after the header has been parsed. | |
| const char * | getContent (uint64 &size) |
| Get the decoded (unmasked) payload. | |
| uint64 | getContentSize () |
| uint64 | getPayloadSize () |
Static Public Member Functions | |
| static WebsocketData * | CreateTerminationConfirmation () |
| static WebsocketData * | CreatePong () |
| static WebsocketData * | CreatePing () |
Public Attributes | |
| enum cmlabs::WebsocketData::DataType | dataType |
| enum cmlabs::WebsocketData::WSSTATUS | status |
| bool | isFinal |
| FIN bit: true when this is the last fragment of a message. | |
| uint64 | time |
| Timestamp when reception/creation started (ms epoch). | |
| uint64 | endReceiveTime |
| Timestamp when the frame was fully received (ms epoch). | |
| uint64 | source |
| Packed uint64 endpoint of the sender (0 if local). | |
| uint32 | headerLength |
| Parsed frame header length in bytes (2..14). | |
| uint64 | contentSize |
| Payload bytes accumulated so far. | |
| uint64 | payloadSize |
| Declared payload length from the header. | |
| uint8 | opcode |
| RFC 6455 opcode (see DataType). | |
| uint32 | maskingKey |
| 32-bit masking key (0 when unmasked, i.e. | |
| uint32 | packages |
| Number of fragments accumulated into this message. | |
| char * | data |
| Owned decoded payload bytes. | |
| char * | rawData |
| Owned serialised frame bytes (for sending). | |
Static Public Attributes | |
| static uint8 const | BHB0_OPCODE = 0x0F |
| Byte 0: 4-bit opcode (see DataType). | |
| static uint8 const | BHB0_RSV3 = 0x10 |
| Byte 0: reserved bit 3 (extensions; must be 0). | |
| static uint8 const | BHB0_RSV2 = 0x20 |
| Byte 0: reserved bit 2 (extensions; must be 0). | |
| static uint8 const | BHB0_RSV1 = 0x40 |
| Byte 0: reserved bit 1 (extensions; must be 0). | |
| static uint8 const | BHB0_FIN = 0x80 |
| Byte 0: FIN — set on the final fragment of a message. | |
| static uint8 const | BHB1_PAYLOAD = 0x7F |
| Byte 1: 7-bit basic payload length / extension code. | |
| static uint8 const | BHB1_MASK = 0x80 |
| Byte 1: MASK — payload is XOR-masked (required client-to-server). | |
| static uint8 const | payload_size_code_16bit = 0x7E |
| Basic length 126: a 16-bit big-endian length follows. | |
| static uint8 const | payload_size_code_64bit = 0x7F |
| Basic length 127: a 64-bit big-endian length follows. | |
| static uint8 const | basic_header_length = 2 |
| static uint8 const | max_header_length = 14 |
| Maximum length of a WebSocket header. | |
| static uint8 const | max_extended_header_length = 12 |
| Maximum length of the variable portion of the WebSocket header. | |
| static uint8 const | payload_size_basic = 125 |
| Maximum size of a basic WebSocket payload. | |
| static uint16 const | payload_size_extended = 0xFFFF |
| Maximum size of an extended WebSocket payload (basic payload = 126). | |
| static uint64 const | payload_size_jumbo = 0x7FFFFFFFFFFFFFFFLL |
| Maximum size of a jumbo WebSocket payload (basic payload = 127). | |
One WebSocket frame/message (RFC 6455): parsing, generation and control frames.
Represents a single WebSocket data unit, either parsed from socket bytes (processHeader() + processContent(), possibly across multiple fragments — see ::isFinal and ::packages) or generated for sending via setData() / the CreatePing()/CreatePong()/CreateTerminationConfirmation() factories. The BHB0_* and BHB1_* constants are the RFC 6455 basic-header bit masks (FIN/RSV/opcode in byte 0, mask flag and 7-bit payload length in byte 1); extended payload lengths (16-bit and 64-bit) are read in network byte order. Client-to-server frames are masked with ::maskingKey per the RFC.
Header layout parsed/produced (see also the file-level framing overview):
A header is thus 2..14 bytes (::basic_header_length .. ::max_header_length). Fragmented messages (FIN clear on all but the last frame) are reassembled by HTTPProtocol::ReceiveWebsocketData(), counting fragments in ::packages.
Definition at line 443 of file NetworkProtocols.h.
Frame content type, mapping to the RFC 6455 opcode.
| Enumerator | |
|---|---|
| NONE | |
| TEXT | |
| BINARY | |
| CLOSE | |
| PING | |
| PONG | |
Definition at line 446 of file NetworkProtocols.h.
Parsing status of this frame: not started, awaiting more bytes, or fully received.
| Enumerator | |
|---|---|
| IDLE | |
| INCOMPLETE | |
| COMPLETE | |
Definition at line 448 of file NetworkProtocols.h.
| cmlabs::WebsocketData::WebsocketData | ( | uint64 | source = 0, |
| uint64 | startRecTime = 0 ) |
Construct an empty frame.
| source | Packed uint64 endpoint of the sender (0 if local). |
| startRecTime | Receive start timestamp; defaults to now when 0. |
Definition at line 942 of file NetworkProtocols.cpp.
References contentSize, data, dataType, headerLength, IDLE, isFinal, maskingKey, NONE, opcode, packages, payloadSize, rawData, source, and status.
Referenced by CreatePing(), CreatePong(), CreateTerminationConfirmation(), and WebsocketData().
| cmlabs::WebsocketData::WebsocketData | ( | WebsocketData * | req | ) |
Deep-copy constructor.
| req | Frame to copy (not modified). |
Definition at line 956 of file NetworkProtocols.cpp.
References contentSize, data, dataType, headerLength, isFinal, maskingKey, opcode, packages, payloadSize, rawData, status, and WebsocketData().
|
virtual |
|
static |
Definition at line 994 of file NetworkProtocols.cpp.
References PING, setData(), and WebsocketData().
|
static |
Definition at line 1000 of file NetworkProtocols.cpp.
References PONG, setData(), and WebsocketData().
|
static |
Definition at line 988 of file NetworkProtocols.cpp.
References CLOSE, setData(), and WebsocketData().
Referenced by cmlabs::NetworkChannel::HTTPServerRun().
| const char * cmlabs::WebsocketData::getContent | ( | uint64 & | size | ) |
Get the decoded (unmasked) payload.
| size | Out: payload size. |
Definition at line 1301 of file NetworkProtocols.cpp.
References COMPLETE, contentSize, data, payloadSize, and status.
Referenced by cmlabs::WebsocketTestServer::receiveWebsocketData(), and cmlabs::TestWebsocketRoundTrip().
| uint64 cmlabs::WebsocketData::getContentSize | ( | ) |
Definition at line 1128 of file NetworkProtocols.cpp.
References contentSize.
| uint64 cmlabs::WebsocketData::getPayloadSize | ( | ) |
Definition at line 1132 of file NetworkProtocols.cpp.
References payloadSize.
Referenced by cmlabs::TestWebSocketRequestClient::run(), and cmlabs::TestWebsocketRoundTrip().
| const char * cmlabs::WebsocketData::getRawData | ( | uint64 & | size | ) |
Get the serialised on-the-wire frame bytes (header + masked payload).
| size | Out: total frame size. |
Definition at line 1118 of file NetworkProtocols.cpp.
References COMPLETE, headerLength, payloadSize, rawData, and status.
Referenced by cmlabs::HTTPProtocol::SendWebsocketData(), and cmlabs::TestWebsocketRoundTrip().
| bool cmlabs::WebsocketData::isComplete | ( | ) |
Definition at line 1137 of file NetworkProtocols.cpp.
References COMPLETE, and status.
Referenced by cmlabs::HTTPProtocol::ReceiveWebsocketData(), and cmlabs::TestWebsocketRoundTrip().
| bool cmlabs::WebsocketData::isPing | ( | ) |
Definition at line 1014 of file NetworkProtocols.cpp.
| bool cmlabs::WebsocketData::isPong | ( | ) |
Definition at line 1018 of file NetworkProtocols.cpp.
| bool cmlabs::WebsocketData::isTerminationConfirmation | ( | ) |
Definition at line 1010 of file NetworkProtocols.cpp.
| bool cmlabs::WebsocketData::isTerminationRequest | ( | ) |
Definition at line 1006 of file NetworkProtocols.cpp.
References CLOSE, and dataType.
Referenced by cmlabs::NetworkChannel::HTTPClientRun(), and cmlabs::NetworkChannel::HTTPServerRun().
| bool cmlabs::WebsocketData::processContent | ( | const char * | buffer, |
| uint64 | size ) |
Append (and unmask) payload bytes after the header has been parsed.
| buffer | Payload bytes. |
| size | Byte count. |
Definition at line 1232 of file NetworkProtocols.cpp.
References COMPLETE, contentSize, data, endReceiveTime, cmlabs::GetTimeNow(), IDLE, INCOMPLETE, isFinal, LOG_NETWORK, LogPrint, maskingKey, packages, payloadSize, and status.
Referenced by cmlabs::HTTPProtocol::ReceiveWebsocketData(), and cmlabs::TestWebsocketRoundTrip().
| bool cmlabs::WebsocketData::processHeader | ( | const char * | buffer, |
| uint64 | size, | ||
| bool & | isInvalid ) |
Parse the frame header from raw bytes (handles 16/64-bit extended lengths).
| buffer | Raw bytes. |
| size | Byte count. |
| isInvalid | Out: set true when the bytes cannot be a WebSocket frame. |
Definition at line 1141 of file NetworkProtocols.cpp.
References basic_header_length, BHB0_FIN, BHB0_OPCODE, BHB1_MASK, BHB1_PAYLOAD, BINARY, CLOSE, COMPLETE, dataType, cmlabs::GetTimeNow(), headerLength, IDLE, isFinal, maskingKey, cmlabs::utils::ntoh64(), opcode, payload_size_basic, payload_size_code_16bit, payloadSize, PING, PONG, status, TEXT, and time.
Referenced by cmlabs::HTTPProtocol::ReceiveWebsocketData(), and cmlabs::TestWebsocketRoundTrip().
| bool cmlabs::WebsocketData::setData | ( | DataType | dataType, |
| bool | maskData, | ||
| const char * | data = NULL, | ||
| uint64 | size = 0 ) |
Set the payload and build the serialised frame for sending.
| dataType | TEXT, BINARY or a control type. |
| maskData | Apply client-side masking (required for client-to-server frames). |
| data | Payload bytes (copied; may be NULL for empty control frames). |
| size | Payload size in bytes. |
Definition at line 1022 of file NetworkProtocols.cpp.
References basic_header_length, BHB0_FIN, BHB0_OPCODE, BHB1_MASK, COMPLETE, contentSize, data, dataType, headerLength, maskingKey, MAXVALUINT32, NONE, cmlabs::utils::ntoh64(), opcode, payload_size_basic, payload_size_code_16bit, payload_size_code_64bit, payload_size_extended, payloadSize, cmlabs::utils::RandomInt(), rawData, and status.
Referenced by CreatePing(), CreatePong(), CreateTerminationConfirmation(), cmlabs::WebsocketTestServer::receiveWebsocketData(), cmlabs::RequestGateway::replyToClient(), cmlabs::TestWebSocketRequestClient::run(), and cmlabs::TestWebsocketRoundTrip().
|
static |
Definition at line 461 of file NetworkProtocols.h.
Referenced by processHeader(), and setData().
|
static |
Byte 0: FIN — set on the final fragment of a message.
Definition at line 455 of file NetworkProtocols.h.
Referenced by processHeader(), setData(), and cmlabs::TestWebsocketRoundTrip().
|
static |
Byte 0: 4-bit opcode (see DataType).
Definition at line 451 of file NetworkProtocols.h.
Referenced by processHeader(), setData(), and cmlabs::TestWebsocketRoundTrip().
|
static |
Byte 0: reserved bit 1 (extensions; must be 0).
Definition at line 454 of file NetworkProtocols.h.
|
static |
Byte 0: reserved bit 2 (extensions; must be 0).
Definition at line 453 of file NetworkProtocols.h.
|
static |
Byte 0: reserved bit 3 (extensions; must be 0).
Definition at line 452 of file NetworkProtocols.h.
|
static |
Byte 1: MASK — payload is XOR-masked (required client-to-server).
Definition at line 457 of file NetworkProtocols.h.
Referenced by processHeader(), and setData().
|
static |
Byte 1: 7-bit basic payload length / extension code.
Definition at line 456 of file NetworkProtocols.h.
Referenced by processHeader().
| uint64 cmlabs::WebsocketData::contentSize |
Payload bytes accumulated so far.
Definition at line 563 of file NetworkProtocols.h.
Referenced by getContent(), getContentSize(), processContent(), cmlabs::RequestGateway::receiveWebsocketData(), setData(), WebsocketData(), and WebsocketData().
| char* cmlabs::WebsocketData::data |
Owned decoded payload bytes.
Definition at line 569 of file NetworkProtocols.h.
Referenced by getContent(), processContent(), cmlabs::RequestGateway::receiveWebsocketData(), cmlabs::TestWebSocketRequestClient::run(), setData(), WebsocketData(), WebsocketData(), and ~WebsocketData().
| enum cmlabs::WebsocketData::DataType cmlabs::WebsocketData::dataType |
| uint64 cmlabs::WebsocketData::endReceiveTime |
Timestamp when the frame was fully received (ms epoch).
Definition at line 558 of file NetworkProtocols.h.
Referenced by processContent().
| uint32 cmlabs::WebsocketData::headerLength |
Parsed frame header length in bytes (2..14).
Definition at line 562 of file NetworkProtocols.h.
Referenced by getRawData(), processHeader(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), setData(), cmlabs::TestWebsocketRoundTrip(), WebsocketData(), and WebsocketData().
| bool cmlabs::WebsocketData::isFinal |
FIN bit: true when this is the last fragment of a message.
Definition at line 556 of file NetworkProtocols.h.
Referenced by processContent(), processHeader(), WebsocketData(), and WebsocketData().
| uint32 cmlabs::WebsocketData::maskingKey |
32-bit masking key (0 when unmasked, i.e.
server-to-client).
Definition at line 566 of file NetworkProtocols.h.
Referenced by processContent(), processHeader(), setData(), WebsocketData(), and WebsocketData().
|
static |
Maximum length of the variable portion of the WebSocket header.
Definition at line 465 of file NetworkProtocols.h.
|
static |
Maximum length of a WebSocket header.
Definition at line 463 of file NetworkProtocols.h.
| uint8 cmlabs::WebsocketData::opcode |
RFC 6455 opcode (see DataType).
Definition at line 565 of file NetworkProtocols.h.
Referenced by processHeader(), setData(), WebsocketData(), and WebsocketData().
| uint32 cmlabs::WebsocketData::packages |
Number of fragments accumulated into this message.
Definition at line 567 of file NetworkProtocols.h.
Referenced by processContent(), WebsocketData(), and WebsocketData().
|
static |
Maximum size of a basic WebSocket payload.
Definition at line 467 of file NetworkProtocols.h.
Referenced by processHeader(), and setData().
|
static |
Basic length 126: a 16-bit big-endian length follows.
Definition at line 458 of file NetworkProtocols.h.
Referenced by processHeader(), and setData().
|
static |
Basic length 127: a 64-bit big-endian length follows.
Definition at line 459 of file NetworkProtocols.h.
Referenced by setData().
|
static |
Maximum size of an extended WebSocket payload (basic payload = 126).
Definition at line 469 of file NetworkProtocols.h.
Referenced by setData().
|
static |
Maximum size of a jumbo WebSocket payload (basic payload = 127).
Definition at line 471 of file NetworkProtocols.h.
| uint64 cmlabs::WebsocketData::payloadSize |
Declared payload length from the header.
Definition at line 564 of file NetworkProtocols.h.
Referenced by getContent(), getPayloadSize(), getRawData(), processContent(), processHeader(), cmlabs::HTTPProtocol::ReceiveWebsocketData(), setData(), WebsocketData(), and WebsocketData().
| char* cmlabs::WebsocketData::rawData |
Owned serialised frame bytes (for sending).
Definition at line 570 of file NetworkProtocols.h.
Referenced by getRawData(), setData(), WebsocketData(), WebsocketData(), and ~WebsocketData().
| uint64 cmlabs::WebsocketData::source |
Packed uint64 endpoint of the sender (0 if local).
Definition at line 559 of file NetworkProtocols.h.
Referenced by cmlabs::RequestGateway::receiveWebsocketData(), and WebsocketData().
| enum cmlabs::WebsocketData::WSSTATUS cmlabs::WebsocketData::status |
Referenced by getContent(), getRawData(), isComplete(), processContent(), processHeader(), setData(), WebsocketData(), WebsocketData(), and ~WebsocketData().
| uint64 cmlabs::WebsocketData::time |
Timestamp when reception/creation started (ms epoch).
Definition at line 557 of file NetworkProtocols.h.
Referenced by processHeader().