|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
WebSocket framing layer built on top of tcp_socket. More...
#include <websocket_socket.h>


Public Member Functions | |
| websocket_socket (std::shared_ptr< tcp_socket > socket, bool is_client) | |
| Constructs a websocket_socket wrapping an existing tcp_socket. | |
| ~websocket_socket () | |
| Destructor. | |
| auto | async_handshake (const std::string &host, const std::string &path, uint16_t port, std::function< void(std::error_code)> handler) -> void |
| Performs WebSocket client handshake (RFC 6455 Section 4.1). | |
| auto | async_accept (std::function< void(std::error_code)> handler) -> void |
| Accepts WebSocket server handshake (RFC 6455 Section 4.2). | |
| auto | start_read () -> void |
| Starts reading WebSocket frames from the underlying socket. | |
| auto | async_send_text (std::string &&message, std::function< void(std::error_code, std::size_t)> handler) -> VoidResult |
| Sends a text message (UTF-8 encoded). | |
| auto | async_send_binary (std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> VoidResult |
| Sends a binary message. | |
| auto | async_send_ping (std::vector< uint8_t > payload, std::function< void(std::error_code)> handler) -> void |
| Sends a ping control frame. | |
| auto | async_close (ws_close_code code, const std::string &reason, std::function< void(std::error_code)> handler) -> void |
| Initiates the WebSocket closing handshake. | |
| auto | state () const -> ws_state |
| Gets the current connection state. | |
| auto | is_open () const -> bool |
| Checks if the connection is open and ready. | |
| auto | set_message_callback (std::function< void(const ws_message &)> callback) -> void |
| Sets the callback for received messages. | |
| auto | set_ping_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets the callback for received ping frames. | |
| auto | set_pong_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets the callback for received pong frames. | |
| auto | set_close_callback (std::function< void(ws_close_code, const std::string &)> callback) -> void |
| Sets the callback for received close frames. | |
| auto | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets the callback for errors. | |
Private Member Functions | |
| auto | on_tcp_receive (std::span< const uint8_t > data) -> void |
| Called by tcp_socket when data is received. | |
| auto | on_tcp_error (std::error_code ec) -> void |
| Called by tcp_socket when an error occurs. | |
| auto | handle_protocol_message (const ws_message &msg) -> void |
| Handles a decoded message from the protocol layer. | |
| auto | handle_protocol_ping (const std::vector< uint8_t > &payload) -> void |
| Handles a ping frame from the protocol layer. | |
| auto | handle_protocol_pong (const std::vector< uint8_t > &payload) -> void |
| Handles a pong frame from the protocol layer. | |
| auto | handle_protocol_close (ws_close_code code, const std::string &reason) -> void |
| Handles a close frame from the protocol layer. | |
Private Attributes | |
| std::shared_ptr< tcp_socket > | tcp_socket_ |
| websocket_protocol | protocol_ |
| std::atomic< ws_state > | state_ {ws_state::connecting} |
| std::mutex | callback_mutex_ |
| 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_ |
| std::function< void(std::error_code)> | error_callback_ |
WebSocket framing layer built on top of tcp_socket.
This class wraps an existing tcp_socket and provides WebSocket protocol framing. The underlying tcp_socket handles all transport I/O operations, while websocket_socket handles:
Design rationale:
Definition at line 59 of file websocket_socket.h.
| kcenon::network::internal::websocket_socket::websocket_socket | ( | std::shared_ptr< tcp_socket > | socket, |
| bool | is_client ) |
Constructs a websocket_socket wrapping an existing tcp_socket.
| socket | The underlying TCP socket (must be connected) |
| is_client | True if this is a client endpoint (applies masking) |
Definition at line 16 of file websocket_socket.cpp.
References kcenon::network::internal::websocket_protocol::create_pong(), handle_protocol_close(), handle_protocol_message(), handle_protocol_ping(), handle_protocol_pong(), on_tcp_error(), on_tcp_receive(), protocol_, kcenon::network::internal::websocket_protocol::set_close_callback(), kcenon::network::internal::websocket_protocol::set_message_callback(), kcenon::network::internal::websocket_protocol::set_ping_callback(), kcenon::network::internal::websocket_protocol::set_pong_callback(), and tcp_socket_.

| kcenon::network::internal::websocket_socket::~websocket_socket | ( | ) |
Destructor.
Stops reading and cleans up resources.
Definition at line 52 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::async_accept | ( | std::function< void(std::error_code)> | handler | ) | -> void |
Accepts WebSocket server handshake (RFC 6455 Section 4.2).
Reads the client's HTTP/1.1 Upgrade request and sends the acceptance response. The handler is called with success/failure status.
| handler | Callback invoked with handshake result |
Definition at line 124 of file websocket_socket.cpp.
References kcenon::network::internal::websocket_handshake::create_server_response(), kcenon::network::internal::open, and kcenon::network::internal::websocket_handshake::parse_client_request().

| auto kcenon::network::internal::websocket_socket::async_close | ( | ws_close_code | code, |
| const std::string & | reason, | ||
| std::function< void(std::error_code)> | handler ) -> void |
Initiates the WebSocket closing handshake.
Sends a close frame with the specified status code and reason. The connection state transitions to closing.
| code | The close status code (RFC 6455 Section 7.4) |
| reason | Optional human-readable reason (UTF-8, max ~123 bytes) |
| handler | Callback invoked when close frame is sent |
Definition at line 249 of file websocket_socket.cpp.
References kcenon::network::internal::closed, and kcenon::network::internal::closing.
| auto kcenon::network::internal::websocket_socket::async_handshake | ( | const std::string & | host, |
| const std::string & | path, | ||
| uint16_t | port, | ||
| std::function< void(std::error_code)> | handler ) -> void |
Performs WebSocket client handshake (RFC 6455 Section 4.1).
Sends an HTTP/1.1 Upgrade request and validates the server response. The handler is called with success/failure status.
| host | The server hostname (for Host header) |
| path | The request path (e.g., "/chat") |
| port | The server port (for Host header) |
| handler | Callback invoked with handshake result |
Definition at line 57 of file websocket_socket.cpp.
References kcenon::network::internal::websocket_handshake::create_client_handshake(), kcenon::network::internal::open, and kcenon::network::internal::websocket_handshake::validate_server_response().

| auto kcenon::network::internal::websocket_socket::async_send_binary | ( | std::vector< uint8_t > && | data, |
| std::function< void(std::error_code, std::size_t)> | handler ) -> VoidResult |
Sends a binary message.
Uses move semantics for zero-copy operation. The data is encoded as a WebSocket binary frame.
| data | The binary data to send |
| handler | Callback invoked with send result |
Definition at line 212 of file websocket_socket.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error_void(), kcenon::network::ok(), and kcenon::network::internal::open.

| auto kcenon::network::internal::websocket_socket::async_send_ping | ( | std::vector< uint8_t > | payload, |
| std::function< void(std::error_code)> | handler ) -> void |
Sends a ping control frame.
Ping frames are used to check connection liveness. The peer should respond with a pong frame.
| payload | Optional payload data (max 125 bytes) |
| handler | Callback invoked with send result |
Definition at line 231 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::async_send_text | ( | std::string && | message, |
| std::function< void(std::error_code, std::size_t)> | handler ) -> VoidResult |
Sends a text message (UTF-8 encoded).
Uses move semantics for zero-copy operation. The message is validated for UTF-8 encoding and encoded as a WebSocket text frame.
| message | The text message to send |
| handler | Callback invoked with send result |
Definition at line 187 of file websocket_socket.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error_void(), kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::message, kcenon::network::ok(), and kcenon::network::internal::open.

|
private |
Handles a close frame from the protocol layer.
Updates connection state and invokes the close callback.
| code | The close status code |
| reason | The close reason string |
Definition at line 371 of file websocket_socket.cpp.
References kcenon::network::internal::closed.
Referenced by websocket_socket().

|
private |
Handles a decoded message from the protocol layer.
Invokes the message callback.
| msg | The decoded message |
Definition at line 342 of file websocket_socket.cpp.
Referenced by websocket_socket().

|
private |
Handles a ping frame from the protocol layer.
Invokes the ping callback (application may send pong).
| payload | The ping payload |
Definition at line 351 of file websocket_socket.cpp.
Referenced by websocket_socket().

|
private |
Handles a pong frame from the protocol layer.
Invokes the pong callback.
| payload | The pong payload |
Definition at line 361 of file websocket_socket.cpp.
Referenced by websocket_socket().

| auto kcenon::network::internal::websocket_socket::is_open | ( | ) | const -> bool |
Checks if the connection is open and ready.
Definition at line 283 of file websocket_socket.cpp.
References kcenon::network::internal::open, and state_.
|
private |
Called by tcp_socket when an error occurs.
Propagates the error to the application.
| ec | The error code |
Definition at line 329 of file websocket_socket.cpp.
References kcenon::network::internal::closed.
Referenced by websocket_socket().

|
private |
Called by tcp_socket when data is received.
Feeds the data into the WebSocket protocol decoder. Uses zero-copy span view from tcp_socket's read buffer.
| data | The received raw bytes as a view |
Definition at line 323 of file websocket_socket.cpp.
Referenced by websocket_socket().

| auto kcenon::network::internal::websocket_socket::set_close_callback | ( | std::function< void(ws_close_code, const std::string &)> | callback | ) | -> void |
Sets the callback for received 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 309 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets the callback for errors.
This callback is invoked when an error occurs on the underlying socket.
| callback | The callback function |
Definition at line 316 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::set_message_callback | ( | std::function< void(const ws_message &)> | callback | ) | -> void |
Sets the callback for received messages.
This callback is invoked when a complete text or binary message has been received and reassembled.
| callback | The callback function |
Definition at line 288 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::set_ping_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets the callback for received 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 295 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::set_pong_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets the callback for received 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 302 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::start_read | ( | ) | -> void |
Starts reading WebSocket frames from the underlying socket.
Must be called after successful handshake to begin receiving messages.
Definition at line 185 of file websocket_socket.cpp.
| auto kcenon::network::internal::websocket_socket::state | ( | ) | const -> ws_state |
Gets the current connection state.
Definition at line 281 of file websocket_socket.cpp.
References state_.
|
private |
Definition at line 238 of file websocket_socket.h.
|
private |
Definition at line 242 of file websocket_socket.h.
|
private |
Definition at line 243 of file websocket_socket.h.
|
private |
Definition at line 239 of file websocket_socket.h.
|
private |
Definition at line 240 of file websocket_socket.h.
|
private |
Definition at line 241 of file websocket_socket.h.
|
private |
Definition at line 234 of file websocket_socket.h.
Referenced by websocket_socket().
|
private |
Definition at line 235 of file websocket_socket.h.
|
private |
Definition at line 231 of file websocket_socket.h.
Referenced by websocket_socket().