REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
message.h
Go to the documentation of this file.
1#pragma once
2
3#include "websockets/common.h"
5
6namespace websockets {
7 enum class MessageType {
8 Empty,
11 };
12
23 enum class MessageRole {
25 };
26
27 // The class the user will interact with as a message
28 // This message can be partial (so practically this is a Frame and not a message)
30 WebsocketsMessage(MessageType msgType, const std::string& msgData, MessageRole msgRole = MessageRole::Complete) : _type(msgType), _length(msgData.size()), _data(msgData), _role(msgRole) {}
32
34 auto type = overrideType;
37 }
38
39 // deduce role
41 if(frame.isNormalUnfragmentedMessage()) {
42 msgRole = MessageRole::Complete;
43 } else if(frame.isBeginningOfFragmentsStream()) {
44 msgRole = MessageRole::First;
45 } else if(frame.isContinuesFragment()) {
47 } else if(frame.isEndOfFragmentsStream()) {
48 msgRole = MessageRole::Last;
49 }
50
51 return WebsocketsMessage(type, std::move(frame.payload), msgRole);
52 }
53
54 // for validation
55 bool isEmpty() const { return this->_type == MessageType::Empty; }
56
57 // Type Helper Functions
58 MessageType type() const { return this->_type; }
59
60 bool isText() const { return this->_type == MessageType::Text; }
61 bool isBinary() const { return this->_type == MessageType::Binary; }
62
63 bool isPing() const { return this->_type == MessageType::Ping; }
64 bool isPong() const { return this->_type == MessageType::Pong; }
65
66 bool isClose() const { return this->_type == MessageType::Close; }
67
68
69 // Role Helper Function
70 MessageRole role() const { return this->_role; }
71
72 bool isComplete() const { return this->_role == MessageRole::Complete; }
73 bool isPartial() const { return this->_role != MessageRole::Complete; }
74 bool isFirst() const { return this->_role == MessageRole::First; }
75 bool isContinuation() const { return this->_role == MessageRole::Continuation; }
76 bool isLast() const { return this->_role == MessageRole::Last; }
77
78
79 std::string data() const { return internals::fromInternalString(this->_data); }
80 const std::string& rawData() const { return this->_data; }
81 const char* c_str() const { return this->_data.c_str(); }
82
83 uint32_t length() const { return this->_length; }
84
86 public:
87 StreamBuilder(bool dummyMode = false) : _dummyMode(dummyMode), _empty(true) {}
88
89 void first(const internals::WebsocketsFrame& frame) {
90 if(this->_empty == false) {
92 return;
93 }
94
95 this->_empty = false;
97 this->_isComplete = false;
98 this->_didErrored = false;
99
100 if(this->_dummyMode == false) {
101 this->_content = std::move(frame.payload);
102 }
103
104 this->_type = messageTypeFromOpcode(frame.opcode);
105 if(this->_type == MessageType::Empty) {
106 badFragment();
107 }
108 } else {
109 this->_didErrored = true;
110 }
111 }
112
114 if(isErrored()) return;
115 if(isEmpty() || isComplete()) {
116 badFragment();
117 return;
118 }
119
120 if(frame.isContinuesFragment()) {
121 if(this->_dummyMode == false) {
122 this->_content += std::move(frame.payload);
123 }
124 } else {
125 badFragment();
126 }
127 }
128
129 void end(const internals::WebsocketsFrame& frame) {
130 if(isErrored()) return;
131 if(isEmpty() || isComplete()) {
132 badFragment();
133 return;
134 }
135
136 if(frame.isEndOfFragmentsStream()) {
137 if(this->_dummyMode == false) {
138 this->_content += std::move(frame.payload);
139 }
140 this->_isComplete = true;
141 } else {
142 badFragment();
143 }
144 }
145
146 void badFragment() {
147 this->_didErrored = true;
148 this->_isComplete = false;
149 }
150
151 bool isErrored() {
152 return this->_didErrored;
153 }
154
155 bool isOk() {
156 return !this->_didErrored;
157 }
158
159 bool isComplete() {
160 return this->_isComplete;
161 }
162
163 bool isEmpty() {
164 return this->_empty;
165 }
166
168 return this->_type;
169 }
170
172 return WebsocketsMessage(
173 this->_type,
174 std::move(this->_content),
176 );
177 }
178
179 private:
180 bool _dummyMode;
181 bool _empty;
182 bool _isComplete = false;
183 std::string _content;
184 MessageType _type;
185 bool _didErrored;
186 };
187
188 private:
189 const MessageType _type;
190 const uint32_t _length;
191 const std::string _data;
192 const MessageRole _role;
193 };
194}
void end(const internals::WebsocketsFrame &frame)
Definition message.h:129
void first(const internals::WebsocketsFrame &frame)
Definition message.h:89
void append(const internals::WebsocketsFrame &frame)
Definition message.h:113
uint32_t
Definition flasher.cpp:195
uint32_t uint32_t size
Definition flasher.cpp:63
std::string fromInternalString(const std::string &str)
Definition common.h:21
MessageType messageTypeFromOpcode(uint8_t opcode)
Definition message.h:13
uint32_t length() const
Definition message.h:83
const char * c_str() const
Definition message.h:81
MessageRole role() const
Definition message.h:70
WebsocketsMessage(MessageType msgType, const std::string &msgData, MessageRole msgRole=MessageRole::Complete)
Definition message.h:30
bool isContinuation() const
Definition message.h:75
static WebsocketsMessage CreateFromFrame(internals::WebsocketsFrame frame, MessageType overrideType=MessageType::Empty)
Definition message.h:33
std::string data() const
Definition message.h:79
const std::string & rawData() const
Definition message.h:80
MessageType type() const
Definition message.h:58