-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketFrame
More file actions
105 lines (84 loc) · 2.51 KB
/
WebSocketFrame
File metadata and controls
105 lines (84 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (C) 2025 Frank Mertens.
*
* Distribution and use is allowed under the terms of the GNU General Public License version 3
* (see CoreComponents/LICENSE-gpl-3.0).
*
*/
#pragma once
#include <cc/ByteSource>
#include <cc/ByteSink>
#include <limits>
namespace cc {
/** WebSocket frame
*/
class WebSocketFrame final: public Object
{
public:
/** Type of frame
*/
enum class Type: uint8_t {
Continuation = 0,
Text = 1,
Binary = 2,
Close = 8,
Ping = 9,
Pong = 10,
Unknown = Continuation
};
/** Something went wrong processing a frame
*/
class Error{};
/** %Payload length exceeds limit
*/
class FrameToBig final: public Error{};
/** Create a null object
*/
WebSocketFrame() = default;
/** Read frame from \a source
* \exception FrameToBig
*/
explicit WebSocketFrame(ByteSource &source, long maxPayloadSize = std::numeric_limits<long>::max()):
Object{new State{source, maxPayloadSize}}
{}
/** Create a new WebSocket frame
*/
WebSocketFrame(Type type, const String &payload = String{}, bool fin = true, uint8_t rsv = 0):
Object{new State{type, payload, fin, rsv}}
{}
/** Write this frame to \a sink automatically applying \a mask in the process (if mask != 0)
*/
void writeTo(ByteSink &sink, uint32_t mask) { me().writeTo(sink, mask); }
/** Tell if this is the final frame of a message
*/
bool fin() const { return me().fin_; }
/** %Frame type
*/
Type type() const { return static_cast<Type>(me().type_); }
/** %Reserved bits
*/
uint8_t rsv() const { return me().rsv_; }
/** %Payload data
*/
const String &payload() const { return me().payload_; }
private:
struct State final: public Object::State
{
State(ByteSource &source, long maxPayloadSize);
State(Type type, const String &payload, bool fin, uint8_t rsv):
fin_{static_cast<uint8_t>(fin)},
rsv_{rsv},
type_{static_cast<uint8_t>(type)},
payload_{payload}
{}
void xorPayloadWithMask(uint32_t mask);
void writeTo(ByteSink &sink, uint32_t mask);
uint8_t fin_ : 1 { 0 };
uint8_t rsv_ : 3 { 0 };
uint8_t type_: 4 { 0 };
String payload_;
};
const State &me() const { return Object::me.as<State>(); }
State &me() { return Object::me.as<State>(); }
};
} // namespace cc