Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
websocket_frame.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
7#include <array>
8#include <cstdint>
9#include <optional>
10#include <vector>
11
13{
21 enum class ws_opcode : uint8_t
22 {
23 continuation = 0x0,
24 text = 0x1,
25 binary = 0x2,
26 close = 0x8,
27 ping = 0x9,
28 pong = 0xA
29 };
30
39 {
40 bool fin;
41 bool rsv1;
42 bool rsv2;
43 bool rsv3;
45 bool mask;
46 uint64_t payload_len;
47 std::array<uint8_t, 4> masking_key;
48 };
49
58 {
59 public:
72 static auto encode_frame(ws_opcode opcode, std::vector<uint8_t>&& payload,
73 bool fin = true, bool mask = false)
74 -> std::vector<uint8_t>;
75
85 static auto decode_header(const std::vector<uint8_t>& data)
86 -> std::optional<ws_frame_header>;
87
98 static auto decode_payload(const ws_frame_header& header,
99 const std::vector<uint8_t>& data)
100 -> std::vector<uint8_t>;
101
111 static auto apply_mask(std::vector<uint8_t>& data,
112 const std::array<uint8_t, 4>& mask) -> void;
113
122 static auto generate_mask() -> std::array<uint8_t, 4>;
123
134 static auto calculate_header_size(uint64_t payload_len, bool mask) -> size_t;
135 };
136
137} // namespace kcenon::network::internal
Provides WebSocket frame encoding and decoding functionality.
static auto generate_mask() -> std::array< uint8_t, 4 >
Generates a random 4-byte masking key.
static auto encode_frame(ws_opcode opcode, std::vector< uint8_t > &&payload, bool fin=true, bool mask=false) -> std::vector< uint8_t >
Encodes data into a WebSocket frame.
static auto decode_payload(const ws_frame_header &header, const std::vector< uint8_t > &data) -> std::vector< uint8_t >
Decodes the payload from a WebSocket frame.
static auto calculate_header_size(uint64_t payload_len, bool mask) -> size_t
Calculates the size of the frame header.
static auto apply_mask(std::vector< uint8_t > &data, const std::array< uint8_t, 4 > &mask) -> void
Applies or removes XOR masking on data.
static auto decode_header(const std::vector< uint8_t > &data) -> std::optional< ws_frame_header >
Decodes a WebSocket frame header from raw data.
ws_opcode
WebSocket frame operation codes as defined in RFC 6455.
@ text
Text frame (UTF-8 encoded)
@ close
Connection close frame.
Represents a decoded WebSocket frame header.
std::array< uint8_t, 4 > masking_key
Masking key (if mask == true)