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