RESTinio
message.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <functional>
12
14
15namespace restinio
16{
17
18namespace websocket
19{
20
21namespace basic
22{
23
24#define RESTINIO_WEBSOCKET_OPCODES_MAP( RESTINIO_GEN ) \
25 RESTINIO_GEN( continuation_frame, 0x00 ) \
26 RESTINIO_GEN( text_frame, 0x01 ) \
27 RESTINIO_GEN( binary_frame, 0x02 ) \
28 RESTINIO_GEN( connection_close_frame, 0x08 ) \
29 RESTINIO_GEN( ping_frame, 0x09 ) \
30 RESTINIO_GEN( pong_frame, 0x0A )
31
32//
33// opcode_t
34//
35
36enum class opcode_t : std::uint8_t
37{
38#define RESTINIO_WEBSOCKET_OPCODES_GEN( name, code ) name = code,
39 RESTINIO_WEBSOCKET_OPCODES_MAP( RESTINIO_WEBSOCKET_OPCODES_GEN )
40#undef RESTINIO_WEBSOCKET_OPCODES_GEN
41 unknown_frame = 0x0F
42};
43
45inline const char *
47{
48 const char * result = "unknown_frame";
49 switch( opcode )
50 {
51 #define RESTINIO_WEBSOCKET_OPCODES_GEN( name, code ) \
52 case opcode_t::name: result = #name; break;
53
55 #undef RESTINIO_WEBSOCKET_OPCODES_GEN
56
57 default:; // Ignore.
58 }
59
60 return result;
61}
62
63inline bool
65{
66 bool result = true;
67 switch( opcode )
68 {
69 #define RESTINIO_WEBSOCKET_OPCODES_GEN( name, code ) \
70 case opcode_t::name: break;
71
73 #undef RESTINIO_WEBSOCKET_OPCODES_GEN
74
75 default: result = false; // Ignore.
76 }
77
78 return result;
79}
80
81//
82// status_code_t
83//
84
85enum class status_code_t : std::uint16_t
86{
87 normal_closure = 1000,
88 going_away = 1001,
89 protocol_error = 1002,
90 cant_accept_data = 1003,
91 no_status_provided = 1005,
92 connection_lost = 1006,
94 policy_violation = 1008,
95 too_big_message = 1009,
98};
99
100inline std::string
102{
103 using namespace ::restinio::utils::impl::bitops;
104
105 return {
106 n_bits_from<char, 8>( static_cast<std::uint16_t>(code) ),
107 n_bits_from<char, 0>( static_cast<std::uint16_t>(code) )
108 };
109}
110
111inline status_code_t
113{
114 using namespace ::restinio::utils::impl::bitops;
115
116 std::uint16_t result{ 0 };
117 if( 2 <= data.size() )
118 {
119 result |= static_cast< std::uint8_t >( data[ 0 ] );
120 result <<= 8;
121 result |= static_cast< std::uint8_t >( data[ 1 ] );
122 }
123
124 // TODO: make it ok.
125 return static_cast<status_code_t>(result);
126}
127
128//
129// final_frame_flag_t
130//
131
133enum class final_frame_flag_t : std::uint8_t { final_frame, not_final_frame };
134
137
138//
139// message_t
140//
141
143class message_t final
144 : public std::enable_shared_from_this< message_t >
145{
146 public:
147
148 message_t() = default;
149
154 , m_opcode{ opcode }
155 {}
156
160 std::string payload )
162 , m_opcode{ opcode }
163 , m_payload{ std::move( payload ) }
164 {}
165
168 final_flag() const noexcept
169 {
170 return m_final_flag;
171 }
172
173 void
175 {
177 }
178
179 bool
180 is_final() const noexcept
181 {
182 return final_frame == final_flag();
183 }
184
186 opcode() const noexcept
187 {
188 return m_opcode;
189 }
190
191 void
193 {
195 }
196
197 const std::string&
198 payload() const noexcept
199 {
200 return m_payload;
201 }
202
203 std::string&
204 payload() noexcept
205 {
206 return m_payload;
207 }
208
209 void
210 set_payload( std::string str )
211 {
212 m_payload = std::move( str );
213 }
214
215 private:
218
220 opcode_t m_opcode = opcode_t::continuation_frame;
221
223 std::string m_payload;
224};
225
227using message_handle_t = std::shared_ptr< message_t >;
228
229//
230// default_request_handler_t
231//
232
234 std::function< void ( message_handle_t ) >;
235
236} /* namespace basic */
237
238} /* namespace websocket */
239
240} /* namespace restinio */
message_t(final_frame_flag_t final_flag, opcode_t opcode)
Definition: message.hpp:150
void set_payload(std::string str)
Definition: message.hpp:210
void set_opcode(opcode_t opcode) noexcept
Definition: message.hpp:192
final_frame_flag_t final_flag() const noexcept
Get final flag.
Definition: message.hpp:168
const std::string & payload() const noexcept
Definition: message.hpp:198
opcode_t opcode() const noexcept
Definition: message.hpp:186
void set_final_flag(final_frame_flag_t final_flag) noexcept
Definition: message.hpp:174
final_frame_flag_t m_final_flag
Final flag.
Definition: message.hpp:217
message_t(final_frame_flag_t final_flag, opcode_t opcode, std::string payload)
Definition: message.hpp:157
bool is_final() const noexcept
Definition: message.hpp:180
std::string m_payload
Websocket message payload.
Definition: message.hpp:223
std::string & payload() noexcept
Definition: message.hpp:204
#define RESTINIO_WEBSOCKET_OPCODES_GEN(name, code)
Definition: message.hpp:38
#define RESTINIO_WEBSOCKET_OPCODES_MAP(RESTINIO_GEN)
Definition: message.hpp:24
std::shared_ptr< message_t > message_handle_t
Request handler, that is the type for calling request handlers.
Definition: message.hpp:227
std::function< void(message_handle_t) > default_message_handler_t
Definition: message.hpp:234
constexpr final_frame_flag_t final_frame
Definition: message.hpp:135
bool is_valid_opcode(opcode_t opcode)
Definition: message.hpp:64
constexpr final_frame_flag_t not_final_frame
Definition: message.hpp:136
const char * opcode_to_string(opcode_t opcode)
Helper sunction to get method string name.
Definition: message.hpp:46
status_code_t status_code_from_bin(string_view_t data)
Definition: message.hpp:112
final_frame_flag_t
WS frame (message) "final"/"not final" flag.
Definition: message.hpp:133
std::string status_code_to_bin(status_code_t code)
Definition: message.hpp:101
nonstd::string_view string_view_t
Definition: string_view.hpp:19
STL namespace.
Definition: inftrees.h:24
#define const
Definition: zconf.h:230