This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
data_frame.hpp
134 lines (112 loc) · 3.55 KB
/
data_frame.hpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************************************************************************************************
data_frame.hpp
For WebSockets2_Generic Library
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, SAM DUE, Teensy, RP2040 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
Version: 1.10.2
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 14/07/2020 Initial coding/porting to support nRF52 and SAMD21/SAMD51 boards. Add SINRIC/Alexa support
...
1.9.0 K Hoang 30/11/2021 Auto detect ESP32 core version. Fix bug in examples
1.9.1 K Hoang 17/12/2021 Fix QNEthernet TCP interface
1.10.0 K Hoang 18/12/2021 Supporting case-insensitive headers, according to RFC2616
1.10.1 K Hoang 26/02/2022 Reduce QNEthernet latency
1.10.2 K Hoang 14/03/2022 Fix bug when using QNEthernet staticIP. Add staticIP option to NativeEthernet
*****************************************************************************************************************************/
#pragma once
#include <Tiny_Websockets_Generic/internals/ws_common.hpp>
namespace websockets2_generic
{
namespace internals2_generic
{
enum ContentType
{
// None as error value
None = -1,
// Default value for empty messages
Continuation = 0x0,
// Data opcdoes
Text = 0x1,
Binary = 0x2,
// Control opcodes
Close = 0x8,
Ping = 0x9,
Pong = 0xA
};
struct WebsocketsFrame
{
uint8_t fin : 1;
uint8_t opcode : 4;
uint8_t mask : 1;
uint8_t mask_buf[4];
uint64_t payload_length;
WSString payload;
bool isControlFrame()
{
return fin && (opcode == 0x8 || opcode == 0x9 || opcode == 0xA);
}
bool isEmpty()
{
return (fin == 0) && (opcode == 0) && (payload_length == 0);
}
bool isBeginningOfFragmentsStream() const
{
return (fin == 0) && (opcode != 0);
}
bool isContinuesFragment() const
{
return (fin == 0) && (opcode == 0);
}
bool isEndOfFragmentsStream() const
{
return (fin == 1) && (opcode == 0);
}
bool isNormalUnfragmentedMessage() const
{
return (fin == 1) && (opcode != 0);
}
};
template <class HeaderTy> HeaderTy MakeHeader(size_t len, uint8_t opcode, bool fin, bool mask)
{
HeaderTy header;
header.fin = fin;
header.flags = 0;
header.opcode = opcode;
header.mask = mask ? 1 : 0;
// set payload
if (len < 126)
{
header.payload = len;
}
else if (len < 65536)
{
header.payload = 126;
}
else
{
header.payload = 127;
}
return header;
}
struct Header
{
uint8_t opcode : 4;
uint8_t flags : 3;
uint8_t fin : 1;
uint8_t payload : 7;
uint8_t mask : 1;
};
struct HeaderWithExtended16 : Header
{
uint16_t extendedPayload;
};
struct HeaderWithExtended64 : Header
{
uint64_t extendedPayload;
};
} // namespace internals2_generic
} // namespace websockets2_generic