|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
WebSocket protocol handler for message processing. More...
#include <websocket_protocol.h>

Public Member Functions | |
| websocket_protocol (bool is_client) | |
| Constructs a WebSocket protocol handler. | |
| auto | process_data (std::span< const uint8_t > data) -> void |
| Processes incoming WebSocket data. | |
| auto | create_text_message (std::string &&text) -> std::vector< uint8_t > |
| Creates a text message frame. | |
| auto | create_binary_message (std::vector< uint8_t > &&data) -> std::vector< uint8_t > |
| Creates a binary message frame. | |
| auto | create_ping (std::vector< uint8_t > &&payload={}) -> std::vector< uint8_t > |
| Creates a ping control frame. | |
| auto | create_pong (std::vector< uint8_t > &&payload={}) -> std::vector< uint8_t > |
| Creates a pong control frame. | |
| auto | create_close (ws_close_code code, std::string &&reason="") -> std::vector< uint8_t > |
| Creates a close control frame. | |
| auto | set_message_callback (std::function< void(const ws_message &)> callback) -> void |
| Sets the callback for data messages. | |
| auto | set_ping_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets the callback for ping frames. | |
| auto | set_pong_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets the callback for pong frames. | |
| auto | set_close_callback (std::function< void(ws_close_code, const std::string &)> callback) -> void |
| Sets the callback for close frames. | |
Private Member Functions | |
| auto | process_frames () -> void |
| Processes incoming frames from the buffer. | |
| auto | handle_data_frame (const ws_frame_header &header, const std::vector< uint8_t > &payload) -> void |
| Handles a data frame (text or binary). | |
| auto | handle_control_frame (const ws_frame_header &header, const std::vector< uint8_t > &payload) -> void |
| Handles a control frame (ping, pong, close). | |
| auto | handle_ping (const std::vector< uint8_t > &payload) -> void |
| Handles a ping frame. | |
| auto | handle_pong (const std::vector< uint8_t > &payload) -> void |
| Handles a pong frame. | |
| auto | handle_close (const std::vector< uint8_t > &payload) -> void |
| Handles a close frame. | |
Static Private Member Functions | |
| static auto | is_valid_utf8 (const std::vector< uint8_t > &data) -> bool |
| Validates UTF-8 encoding. | |
Private Attributes | |
| bool | is_client_ |
| True if client (applies masking) | |
| std::vector< uint8_t > | buffer_ |
| Incoming data buffer. | |
| std::vector< uint8_t > | fragmented_message_ |
| Reassembly buffer for fragmented messages. | |
| ws_opcode | fragmented_type_ |
| Type of fragmented message in progress. | |
| std::function< void(const ws_message &)> | message_callback_ |
| std::function< void(const std::vector< uint8_t > &)> | ping_callback_ |
| std::function< void(const std::vector< uint8_t > &)> | pong_callback_ |
| std::function< void(ws_close_code, const std::string &)> | close_callback_ |
WebSocket protocol handler for message processing.
This class handles the WebSocket protocol state machine including:
Definition at line 82 of file websocket_protocol.h.
|
explicit |
Constructs a WebSocket protocol handler.
| is_client | True if this is a client endpoint (applies masking) |
Definition at line 30 of file websocket_protocol.cpp.
| auto kcenon::network::internal::websocket_protocol::create_binary_message | ( | std::vector< uint8_t > && | data | ) | -> std::vector<uint8_t> |
Creates a binary message frame.
Encodes the binary data as a WebSocket binary frame. Uses move semantics for zero-copy operation.
| data | The binary data to send |
Definition at line 269 of file websocket_protocol.cpp.
References kcenon::network::internal::binary, and kcenon::network::internal::websocket_frame::encode_frame().

| auto kcenon::network::internal::websocket_protocol::create_close | ( | ws_close_code | code, |
| std::string && | reason = "" ) -> std::vector<uint8_t> |
Creates a close control frame.
Initiates the WebSocket closing handshake.
| code | The close status code |
| reason | Optional human-readable reason (UTF-8) |
Definition at line 302 of file websocket_protocol.cpp.
References kcenon::network::internal::close, and kcenon::network::internal::websocket_frame::encode_frame().

| auto kcenon::network::internal::websocket_protocol::create_ping | ( | std::vector< uint8_t > && | payload = {} | ) | -> std::vector<uint8_t> |
Creates a ping control frame.
Ping frames are used to check connection liveness. The peer should respond with a pong frame.
| payload | Optional payload data (typically empty) |
Definition at line 276 of file websocket_protocol.cpp.
References kcenon::network::internal::websocket_frame::encode_frame(), and kcenon::network::internal::ping.

| auto kcenon::network::internal::websocket_protocol::create_pong | ( | std::vector< uint8_t > && | payload = {} | ) | -> std::vector<uint8_t> |
Creates a pong control frame.
Pong frames are sent in response to ping frames.
| payload | Payload data (should match the ping payload) |
Definition at line 289 of file websocket_protocol.cpp.
References kcenon::network::internal::websocket_frame::encode_frame(), and kcenon::network::internal::pong.
Referenced by kcenon::network::internal::websocket_socket::websocket_socket().


| auto kcenon::network::internal::websocket_protocol::create_text_message | ( | std::string && | text | ) | -> std::vector<uint8_t> |
Creates a text message frame.
Encodes the text as a WebSocket text frame with UTF-8 validation. Uses move semantics for zero-copy operation.
| text | The text message to send |
Definition at line 251 of file websocket_protocol.cpp.
References kcenon::network::internal::websocket_frame::encode_frame(), and kcenon::network::internal::text.

|
private |
Handles a close frame.
Parses the close code and reason, then invokes the close callback.
| payload | The close frame payload |
Definition at line 219 of file websocket_protocol.cpp.
References kcenon::network::internal::normal, and kcenon::network::internal::protocol_error.
|
private |
Handles a control frame (ping, pong, close).
Control frames must not be fragmented.
| header | The frame header |
| payload | The frame payload |
Definition at line 168 of file websocket_protocol.cpp.
References kcenon::network::internal::close, kcenon::network::internal::ping, and kcenon::network::internal::pong.
|
private |
Handles a data frame (text or binary).
Manages fragmentation and reassembly.
| header | The frame header |
| payload | The frame payload |
Definition at line 90 of file websocket_protocol.cpp.
References kcenon::network::internal::binary, kcenon::network::internal::continuation, kcenon::network::internal::ws_message::data, kcenon::network::internal::text, and kcenon::network::internal::ws_message::type.
|
private |
Handles a ping frame.
Invokes the ping callback if set.
| payload | The ping payload |
Definition at line 203 of file websocket_protocol.cpp.
|
private |
Handles a pong frame.
Invokes the pong callback if set.
| payload | The pong payload |
Definition at line 211 of file websocket_protocol.cpp.
|
staticprivate |
Validates UTF-8 encoding.
Checks if the data is valid UTF-8 (required for text messages).
| data | The data to validate |
Definition at line 356 of file websocket_protocol.cpp.
| auto kcenon::network::internal::websocket_protocol::process_data | ( | std::span< const uint8_t > | data | ) | -> void |
Processes incoming WebSocket data.
Parses frames, handles fragmentation, and invokes appropriate callbacks. This method should be called when data is received from the network.
Accepts data as a non-owning span view, avoiding unnecessary copies. The data is copied once into the internal buffer for frame processing.
| data | The incoming data buffer as a view |
Definition at line 36 of file websocket_protocol.cpp.
|
private |
Processes incoming frames from the buffer.
Attempts to parse and handle frames from the accumulated buffer. May invoke callbacks for complete messages.
Definition at line 45 of file websocket_protocol.cpp.
References kcenon::network::internal::binary, kcenon::network::internal::websocket_frame::calculate_header_size(), kcenon::network::internal::continuation, kcenon::network::internal::websocket_frame::decode_header(), kcenon::network::internal::websocket_frame::decode_payload(), and kcenon::network::internal::text.

| auto kcenon::network::internal::websocket_protocol::set_close_callback | ( | std::function< void(ws_close_code, const std::string &)> | callback | ) | -> void |
Sets the callback for close frames.
This callback is invoked when a close frame is received. The application should respond with a close frame if it hasn't already.
| callback | The callback function |
Definition at line 350 of file websocket_protocol.cpp.
Referenced by kcenon::network::internal::websocket_socket::websocket_socket().

| auto kcenon::network::internal::websocket_protocol::set_message_callback | ( | std::function< void(const ws_message &)> | callback | ) | -> void |
Sets the callback for data messages.
This callback is invoked when a complete text or binary message has been received and reassembled.
| callback | The callback function |
Definition at line 332 of file websocket_protocol.cpp.
Referenced by kcenon::network::internal::websocket_socket::websocket_socket().

| auto kcenon::network::internal::websocket_protocol::set_ping_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets the callback for ping frames.
This callback is invoked when a ping frame is received. The application may choose to send a pong response.
| callback | The callback function |
Definition at line 338 of file websocket_protocol.cpp.
Referenced by kcenon::network::internal::websocket_socket::websocket_socket().

| auto kcenon::network::internal::websocket_protocol::set_pong_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets the callback for pong frames.
This callback is invoked when a pong frame is received (typically in response to a ping).
| callback | The callback function |
Definition at line 344 of file websocket_protocol.cpp.
Referenced by kcenon::network::internal::websocket_socket::websocket_socket().

|
private |
Incoming data buffer.
Definition at line 214 of file websocket_protocol.h.
|
private |
Definition at line 222 of file websocket_protocol.h.
|
private |
Reassembly buffer for fragmented messages.
Definition at line 215 of file websocket_protocol.h.
|
private |
Type of fragmented message in progress.
Definition at line 216 of file websocket_protocol.h.
|
private |
True if client (applies masking)
Definition at line 213 of file websocket_protocol.h.
|
private |
Definition at line 219 of file websocket_protocol.h.
|
private |
Definition at line 220 of file websocket_protocol.h.
|
private |
Definition at line 221 of file websocket_protocol.h.