Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
websocket_protocol.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
8
9#include <cstdint>
10#include <functional>
11#include <span>
12#include <string>
13#include <vector>
14
16{
23 enum class ws_message_type
24 {
25 text,
26 binary
27 };
28
37 {
39 std::vector<uint8_t> data;
40
45 auto as_text() const -> std::string;
46
51 auto as_binary() const -> const std::vector<uint8_t>&;
52 };
53
60 enum class ws_close_code : uint16_t
61 {
62 normal = 1000,
63 going_away = 1001,
64 protocol_error = 1002,
65 unsupported_data = 1003,
66 invalid_frame = 1007,
67 policy_violation = 1008,
68 message_too_big = 1009,
69 internal_error = 1011
70 };
71
83 {
84 public:
90 explicit websocket_protocol(bool is_client);
91
108 auto process_data(std::span<const uint8_t> data) -> void;
109
119 auto create_text_message(std::string&& text) -> std::vector<uint8_t>;
120
130 auto create_binary_message(std::vector<uint8_t>&& data)
131 -> std::vector<uint8_t>;
132
142 auto create_ping(std::vector<uint8_t>&& payload = {})
143 -> std::vector<uint8_t>;
144
153 auto create_pong(std::vector<uint8_t>&& payload = {})
154 -> std::vector<uint8_t>;
155
165 auto create_close(ws_close_code code, std::string&& reason = "")
166 -> std::vector<uint8_t>;
167
176 auto set_message_callback(std::function<void(const ws_message&)> callback)
177 -> void;
178
187 auto set_ping_callback(
188 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
189
198 auto set_pong_callback(
199 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
200
209 auto set_close_callback(
210 std::function<void(ws_close_code, const std::string&)> callback) -> void;
211
212 private:
214 std::vector<uint8_t> buffer_;
215 std::vector<uint8_t> fragmented_message_;
217
218 // Callbacks
219 std::function<void(const ws_message&)> message_callback_;
220 std::function<void(const std::vector<uint8_t>&)> ping_callback_;
221 std::function<void(const std::vector<uint8_t>&)> pong_callback_;
222 std::function<void(ws_close_code, const std::string&)> close_callback_;
223
230 auto process_frames() -> void;
231
240 auto handle_data_frame(const ws_frame_header& header,
241 const std::vector<uint8_t>& payload) -> void;
242
251 auto handle_control_frame(const ws_frame_header& header,
252 const std::vector<uint8_t>& payload) -> void;
253
261 auto handle_ping(const std::vector<uint8_t>& payload) -> void;
262
270 auto handle_pong(const std::vector<uint8_t>& payload) -> void;
271
279 auto handle_close(const std::vector<uint8_t>& payload) -> void;
280
289 static auto is_valid_utf8(const std::vector<uint8_t>& data) -> bool;
290 };
291
292} // namespace kcenon::network::internal
WebSocket protocol handler for message processing.
std::function< void(ws_close_code, const std::string &)> close_callback_
std::function< void(const std::vector< uint8_t > &)> ping_callback_
std::function< void(const ws_message &)> message_callback_
ws_opcode fragmented_type_
Type of fragmented message in progress.
bool is_client_
True if client (applies masking)
std::vector< uint8_t > buffer_
Incoming data buffer.
std::function< void(const std::vector< uint8_t > &)> pong_callback_
std::vector< uint8_t > fragmented_message_
Reassembly buffer for fragmented messages.
ws_message_type
Type of WebSocket message.
ws_opcode
WebSocket frame operation codes as defined in RFC 6455.
@ text
Text frame (UTF-8 encoded)
ws_close_code
WebSocket close status codes (RFC 6455 Section 7.4).
@ going_away
Endpoint is going away.
@ unsupported_data
Unsupported data type.
@ invalid_frame
Invalid frame payload data.
Represents a decoded WebSocket frame header.
Represents a complete WebSocket message.
auto as_binary() const -> const std::vector< uint8_t > &
Returns message data as binary (reference).
ws_message_type type
Message type.
std::vector< uint8_t > data
Message payload.
auto as_text() const -> std::string
Converts message data to string (for text messages).