Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
websocket_socket.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
9
10#include <atomic>
11#include <cstdint>
12#include <functional>
13#include <memory>
14#include <mutex>
15#include <span>
16#include <string>
17#include <system_error>
18#include <vector>
19
21{
22 // Forward declaration
23 class tcp_socket;
24
32 enum class ws_state
33 {
35 open,
36 closing,
37 closed
38 };
39
59 class websocket_socket : public std::enable_shared_from_this<websocket_socket>
60 {
61 public:
68 websocket_socket(std::shared_ptr<tcp_socket> socket, bool is_client);
69
76
88 auto async_handshake(const std::string& host, const std::string& path,
89 uint16_t port,
90 std::function<void(std::error_code)> handler) -> void;
91
100 auto async_accept(std::function<void(std::error_code)> handler) -> void;
101
107 auto start_read() -> void;
108
119 auto async_send_text(std::string&& message,
120 std::function<void(std::error_code, std::size_t)> handler)
121 -> VoidResult;
122
133 auto async_send_binary(std::vector<uint8_t>&& data,
134 std::function<void(std::error_code, std::size_t)> handler)
135 -> VoidResult;
136
146 auto async_send_ping(std::vector<uint8_t> payload,
147 std::function<void(std::error_code)> handler) -> void;
148
159 auto async_close(ws_close_code code, const std::string& reason,
160 std::function<void(std::error_code)> handler) -> void;
161
167 auto state() const -> ws_state;
168
174 auto is_open() const -> bool;
175
184 auto set_message_callback(std::function<void(const ws_message&)> callback)
185 -> void;
186
196 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
197
207 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
208
218 std::function<void(ws_close_code, const std::string&)> callback) -> void;
219
227 auto set_error_callback(std::function<void(std::error_code)> callback) -> void;
228
229 private:
230 // Underlying TCP socket (reuses existing infrastructure)
231 std::shared_ptr<tcp_socket> tcp_socket_;
232
233 // WebSocket protocol state machine
235 std::atomic<ws_state> state_{ws_state::connecting};
236
237 // Callbacks
238 std::mutex callback_mutex_;
239 std::function<void(const ws_message&)> message_callback_;
240 std::function<void(const std::vector<uint8_t>&)> ping_callback_;
241 std::function<void(const std::vector<uint8_t>&)> pong_callback_;
242 std::function<void(ws_close_code, const std::string&)> close_callback_;
243 std::function<void(std::error_code)> error_callback_;
244
253 auto on_tcp_receive(std::span<const uint8_t> data) -> void;
254
262 auto on_tcp_error(std::error_code ec) -> void;
263
271 auto handle_protocol_message(const ws_message& msg) -> void;
272
280 auto handle_protocol_ping(const std::vector<uint8_t>& payload) -> void;
281
289 auto handle_protocol_pong(const std::vector<uint8_t>& payload) -> void;
290
299 auto handle_protocol_close(ws_close_code code, const std::string& reason)
300 -> void;
301 };
302
303} // namespace kcenon::network::internal
WebSocket protocol handler for message processing.
WebSocket framing layer built on top of tcp_socket.
std::function< void(ws_close_code, const std::string &)> close_callback_
auto is_open() const -> bool
Checks if the connection is open and ready.
std::function< void(const ws_message &)> message_callback_
auto state() const -> ws_state
Gets the current connection state.
auto handle_protocol_ping(const std::vector< uint8_t > &payload) -> void
Handles a ping frame from the protocol layer.
std::shared_ptr< tcp_socket > tcp_socket_
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets the callback for errors.
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_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 handle_protocol_message(const ws_message &msg) -> void
Handles a decoded message from the protocol layer.
auto on_tcp_receive(std::span< const uint8_t > data) -> void
Called by tcp_socket when data is received.
auto set_close_callback(std::function< void(ws_close_code, const std::string &)> callback) -> void
Sets the callback for received close frames.
websocket_socket(std::shared_ptr< tcp_socket > socket, bool is_client)
Constructs a websocket_socket wrapping an existing tcp_socket.
auto start_read() -> void
Starts reading WebSocket frames from the underlying socket.
auto async_accept(std::function< void(std::error_code)> handler) -> void
Accepts WebSocket server handshake (RFC 6455 Section 4.2).
std::function< void(const std::vector< uint8_t > &)> pong_callback_
auto handle_protocol_close(ws_close_code code, const std::string &reason) -> void
Handles a close frame from the protocol layer.
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_ping(std::vector< uint8_t > payload, std::function< void(std::error_code)> handler) -> void
Sends a ping control frame.
auto set_message_callback(std::function< void(const ws_message &)> callback) -> void
Sets the callback for received messages.
std::function< void(const std::vector< uint8_t > &)> ping_callback_
auto async_close(ws_close_code code, const std::string &reason, std::function< void(std::error_code)> handler) -> void
Initiates the WebSocket closing handshake.
std::function< void(std::error_code)> error_callback_
auto on_tcp_error(std::error_code ec) -> void
Called by tcp_socket when an error occurs.
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 handle_protocol_pong(const std::vector< uint8_t > &payload) -> void
Handles a pong frame from the protocol layer.
::kcenon::network::VoidResult VoidResult
ws_state
WebSocket connection state (RFC 6455 Section 7).
@ open
Connection established and ready.
@ connecting
Handshake in progress.
ws_close_code
WebSocket close status codes (RFC 6455 Section 7.4).
@ closing
CONNECTION_CLOSE sent, waiting for timeout.
Network-specific error and result type definitions.
Represents a complete WebSocket message.