RESTinio
Toggle main menu visibility
Loading...
Searching...
No Matches
restinio
websocket
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
16
namespace
restinio
17
{
18
19
namespace
websocket
20
{
21
22
namespace
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
37
enum
class
opcode_t
:
std
::
uint8_t
38
{
39
#
define
RESTINIO_WEBSOCKET_OPCODES_GEN
(
name
,
code
)
name
=
code
,
40
RESTINIO_WEBSOCKET_OPCODES_MAP
(
RESTINIO_WEBSOCKET_OPCODES_GEN
)
41
#
undef
RESTINIO_WEBSOCKET_OPCODES_GEN
42
unknown_frame
= 0x0F
43
};
44
45
//! Helper sunction to get method string name.
46
inline
const
char
*
47
opcode_to_string
(
opcode_t
opcode )
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
64
inline
bool
65
is_valid_opcode
(
opcode_t
opcode )
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
86
enum
class
status_code_t
:
std
::
uint16_t
87
{
88
normal_closure
= 1000,
89
going_away
= 1001,
90
protocol_error
= 1002,
91
cant_accept_data
= 1003,
92
no_status_provided
= 1005,
93
connection_lost
= 1006,
94
invalid_message_data
= 1007,
95
policy_violation
= 1008,
96
too_big_message
= 1009,
97
more_extensions_expected
= 1010,
98
unexpected_condition
= 1011
99
};
100
101
inline
std::string
102
status_code_to_bin
(
status_code_t
code )
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
112
inline
status_code_t
113
status_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.
134
enum
class
final_frame_flag_t
:
std
::
uint8_t
{
final_frame
,
not_final_frame
};
135
136
constexpr
final_frame_flag_t
final_frame
=
final_frame_flag_t
::
final_frame
;
137
constexpr
final_frame_flag_t
not_final_frame
=
final_frame_flag_t
::
not_final_frame
;
138
139
//
140
// message_t
141
//
142
143
//! WebSocket message.
144
class
message_t
final
145
:
public
std::enable_shared_from_this< message_t >
146
{
147
public
:
148
149
message_t
() =
default
;
150
151
message_t
(
152
final_frame_flag_t
final_flag,
153
opcode_t
opcode )
154
:
m_final_flag
{ final_flag }
155
,
m_opcode
{ opcode }
156
{}
157
158
message_t
(
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.
168
final_frame_flag_t
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
186
opcode_t
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.
218
final_frame_flag_t
m_final_flag
{
final_frame
};
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.
228
using
message_handle_t
=
std
::
shared_ptr
<
message_t
>;
229
230
//
231
// default_request_handler_t
232
//
233
234
using
default_message_handler_t
=
235
std
::
function
<
void
(
message_handle_t
) >;
236
237
}
/* namespace basic */
238
239
}
/* namespace websocket */
240
241
}
/* namespace restinio */
restinio::websocket::basic::message_t::message_t
message_t(final_frame_flag_t final_flag, opcode_t opcode)
Definition
message.hpp:151
restinio::websocket::basic::message_t::m_opcode
opcode_t m_opcode
Opcode.
Definition
message.hpp:221
restinio::websocket::basic::message_t::set_payload
void set_payload(std::string str)
Definition
message.hpp:211
restinio::websocket::basic::message_t::set_opcode
void set_opcode(opcode_t opcode) noexcept
Definition
message.hpp:193
restinio::websocket::basic::message_t::final_flag
final_frame_flag_t final_flag() const noexcept
Get final flag.
Definition
message.hpp:169
restinio::websocket::basic::message_t::payload
const std::string & payload() const noexcept
Definition
message.hpp:199
restinio::websocket::basic::message_t::opcode
opcode_t opcode() const noexcept
Definition
message.hpp:187
restinio::websocket::basic::message_t::set_final_flag
void set_final_flag(final_frame_flag_t final_flag) noexcept
Definition
message.hpp:175
restinio::websocket::basic::message_t::m_final_flag
final_frame_flag_t m_final_flag
Final flag.
Definition
message.hpp:218
restinio::websocket::basic::message_t::message_t
message_t(final_frame_flag_t final_flag, opcode_t opcode, std::string payload)
Definition
message.hpp:158
restinio::websocket::basic::message_t::is_final
bool is_final() const noexcept
Definition
message.hpp:181
restinio::websocket::basic::message_t::m_payload
std::string m_payload
Websocket message payload.
Definition
message.hpp:224
restinio::websocket::basic::message_t::message_t
message_t()=default
restinio::websocket::basic::message_t::payload
std::string & payload() noexcept
Definition
message.hpp:205
RESTINIO_WEBSOCKET_OPCODES_MAP
#define RESTINIO_WEBSOCKET_OPCODES_MAP(RESTINIO_GEN)
Definition
message.hpp:25
restinio::utils::impl::bitops
Definition
bitops.hpp:19
restinio::utils::impl
Definition
bitops.hpp:17
restinio::utils
Definition
from_string_details.ipp:5
restinio::websocket::basic
Definition
utf8.hpp:19
restinio::websocket::basic::status_code_t
status_code_t
Definition
message.hpp:87
restinio::websocket::basic::status_code_t::no_status_provided
@ no_status_provided
Definition
message.hpp:92
restinio::websocket::basic::status_code_t::going_away
@ going_away
Definition
message.hpp:89
restinio::websocket::basic::status_code_t::policy_violation
@ policy_violation
Definition
message.hpp:95
restinio::websocket::basic::status_code_t::unexpected_condition
@ unexpected_condition
Definition
message.hpp:98
restinio::websocket::basic::status_code_t::too_big_message
@ too_big_message
Definition
message.hpp:96
restinio::websocket::basic::status_code_t::connection_lost
@ connection_lost
Definition
message.hpp:93
restinio::websocket::basic::status_code_t::more_extensions_expected
@ more_extensions_expected
Definition
message.hpp:97
restinio::websocket::basic::status_code_t::invalid_message_data
@ invalid_message_data
Definition
message.hpp:94
restinio::websocket::basic::status_code_t::protocol_error
@ protocol_error
Definition
message.hpp:90
restinio::websocket::basic::status_code_t::cant_accept_data
@ cant_accept_data
Definition
message.hpp:91
restinio::websocket::basic::status_code_t::normal_closure
@ normal_closure
Definition
message.hpp:88
restinio::websocket::basic::opcode_t
opcode_t
Definition
message.hpp:38
restinio::websocket::basic::opcode_t::unknown_frame
@ unknown_frame
Definition
message.hpp:42
restinio::websocket::basic::final_frame
constexpr final_frame_flag_t final_frame
Definition
message.hpp:136
restinio::websocket::basic::is_valid_opcode
bool is_valid_opcode(opcode_t opcode)
Definition
message.hpp:65
restinio::websocket::basic::not_final_frame
constexpr final_frame_flag_t not_final_frame
Definition
message.hpp:137
restinio::websocket::basic::opcode_to_string
const char * opcode_to_string(opcode_t opcode)
Helper sunction to get method string name.
Definition
message.hpp:47
restinio::websocket::basic::status_code_from_bin
status_code_t status_code_from_bin(string_view_t data)
Definition
message.hpp:113
restinio::websocket::basic::final_frame_flag_t
final_frame_flag_t
WS frame (message) "final"/"not final" flag.
Definition
message.hpp:134
restinio::websocket::basic::final_frame_flag_t::final_frame
@ final_frame
Definition
message.hpp:134
restinio::websocket::basic::final_frame_flag_t::not_final_frame
@ not_final_frame
Definition
message.hpp:134
restinio::websocket::basic::status_code_to_bin
std::string status_code_to_bin(status_code_t code)
Definition
message.hpp:102
restinio::websocket
Definition
utf8.hpp:16
restinio
Definition
sendfile_operation_default.ipp:12
Generated by
1.17.0