message.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <functional>
#include <restinio/utils/impl/bitops.hpp>
namespace restinio
{
namespace websocket
{
namespace basic
{
#define RESTINIO_WEBSOCKET_OPCODES_MAP( RESTINIO_GEN ) \
RESTINIO_GEN( continuation_frame, 0x00 ) \
RESTINIO_GEN( text_frame, 0x01 ) \
RESTINIO_GEN( binary_frame, 0x02 ) \
RESTINIO_GEN( connection_close_frame, 0x08 ) \
RESTINIO_GEN( ping_frame, 0x09 ) \
RESTINIO_GEN( pong_frame, 0x0A )
//
// opcode_t
//
enum class opcode_t : std::uint8_t
{
#define RESTINIO_WEBSOCKET_OPCODES_GEN( name, code ) name = code,
RESTINIO_WEBSOCKET_OPCODES_MAP( RESTINIO_WEBSOCKET_OPCODES_GEN )
#undef RESTINIO_WEBSOCKET_OPCODES_GEN
unknown_frame = 0x0F
};
inline const char *
opcode_to_string( opcode_t opcode )
{
const char * result = "unknown_frame";
switch( opcode )
{
#define RESTINIO_WEBSOCKET_OPCODES_GEN( name, code ) \
case opcode_t::name: result = #name; break;
RESTINIO_WEBSOCKET_OPCODES_MAP( RESTINIO_WEBSOCKET_OPCODES_GEN )
#undef RESTINIO_WEBSOCKET_OPCODES_GEN
default:; // Ignore.
}
return result;
}
inline bool
is_valid_opcode( opcode_t opcode )
{
bool result = true;
switch( opcode )
{
#define RESTINIO_WEBSOCKET_OPCODES_GEN( name, code ) \
case opcode_t::name: break;
RESTINIO_WEBSOCKET_OPCODES_MAP( RESTINIO_WEBSOCKET_OPCODES_GEN )
#undef RESTINIO_WEBSOCKET_OPCODES_GEN
default: result = false; // Ignore.
}
return result;
}
//
// status_code_t
//
enum class status_code_t : std::uint16_t
{
normal_closure = 1000,
going_away = 1001,
protocol_error = 1002,
cant_accept_data = 1003,
no_status_provided = 1005,
connection_lost = 1006,
invalid_message_data = 1007,
policy_violation = 1008,
too_big_message = 1009,
more_extensions_expected = 1010,
unexpected_condition = 1011
};
inline std::string
status_code_to_bin( status_code_t code )
{
using namespace ::restinio::utils::impl::bitops;
return {
n_bits_from<char, 8>( static_cast<std::uint16_t>(code) ),
n_bits_from<char, 0>( static_cast<std::uint16_t>(code) )
};
}
inline status_code_t
status_code_from_bin( string_view_t data )
{
using namespace ::restinio::utils::impl::bitops;
std::uint16_t result{ 0 };
if( 2 <= data.size() )
{
result |= static_cast< std::uint8_t >( data[ 0 ] );
result <<= 8;
result |= static_cast< std::uint8_t >( data[ 1 ] );
}
// TODO: make it ok.
return static_cast<status_code_t>(result);
}
//
// final_frame_flag_t
//
enum class final_frame_flag_t : std::uint8_t { final_frame, not_final_frame };
constexpr final_frame_flag_t final_frame = final_frame_flag_t::final_frame;
constexpr final_frame_flag_t not_final_frame = final_frame_flag_t::not_final_frame;
//
// message_t
//
class message_t final
: public std::enable_shared_from_this< message_t >
{
public:
message_t() = default;
message_t(
final_frame_flag_t final_flag,
opcode_t opcode )
: m_final_flag{ final_flag }
, m_opcode{ opcode }
{}
message_t(
final_frame_flag_t final_flag,
opcode_t opcode,
std::string payload )
: m_final_flag{ final_flag }
, m_opcode{ opcode }
, m_payload{ std::move( payload ) }
{}
final_frame_flag_t
final_flag() const noexcept
{
return m_final_flag;
}
void
set_final_flag( final_frame_flag_t final_flag ) noexcept
{
m_final_flag = final_flag;
}
bool
is_final() const noexcept
{
return final_frame == final_flag();
}
opcode_t
opcode() const noexcept
{
return m_opcode;
}
void
set_opcode( opcode_t opcode ) noexcept
{
m_opcode = opcode;
}
const std::string&
payload() const noexcept
{
return m_payload;
}
std::string&
payload() noexcept
{
return m_payload;
}
void
set_payload( std::string str )
{
m_payload = std::move( str );
}
private:
final_frame_flag_t m_final_flag{ final_frame };
opcode_t m_opcode = opcode_t::continuation_frame;
std::string m_payload;
};
using message_handle_t = std::shared_ptr< message_t >;
//
// default_request_handler_t
//
using default_message_handler_t =
std::function< void ( message_handle_t ) >;
} /* namespace basic */
} /* namespace websocket */
} /* namespace restinio */
bitops.hpp
restinio::websocket::basic::message_t
WebSocket message.
Definition: message.hpp:145
restinio::websocket::basic::message_t::message_t
message_t(final_frame_flag_t final_flag, opcode_t opcode)
Definition: message.hpp:150
restinio::websocket::basic::message_t::m_opcode
opcode_t m_opcode
Opcode.
Definition: message.hpp:220
restinio::websocket::basic::message_t::set_payload
void set_payload(std::string str)
Definition: message.hpp:210
restinio::websocket::basic::message_t::set_opcode
void set_opcode(opcode_t opcode) noexcept
Definition: message.hpp:192
restinio::websocket::basic::message_t::final_flag
final_frame_flag_t final_flag() const noexcept
Get final flag.
Definition: message.hpp:168
restinio::websocket::basic::message_t::payload
const std::string & payload() const noexcept
Definition: message.hpp:198
restinio::websocket::basic::message_t::opcode
opcode_t opcode() const noexcept
Definition: message.hpp:186
restinio::websocket::basic::message_t::set_final_flag
void set_final_flag(final_frame_flag_t final_flag) noexcept
Definition: message.hpp:174
restinio::websocket::basic::message_t::m_final_flag
final_frame_flag_t m_final_flag
Final flag.
Definition: message.hpp:217
restinio::websocket::basic::message_t::message_t
message_t(final_frame_flag_t final_flag, opcode_t opcode, std::string payload)
Definition: message.hpp:157
restinio::websocket::basic::message_t::is_final
bool is_final() const noexcept
Definition: message.hpp:180
restinio::websocket::basic::message_t::m_payload
std::string m_payload
Websocket message payload.
Definition: message.hpp:223
restinio::websocket::basic::message_t::message_t
message_t()=default
restinio::websocket::basic::message_t::payload
std::string & payload() noexcept
Definition: message.hpp:204
RESTINIO_WEBSOCKET_OPCODES_GEN
#define RESTINIO_WEBSOCKET_OPCODES_GEN(name, code)
Definition: message.hpp:38
RESTINIO_WEBSOCKET_OPCODES_MAP
#define RESTINIO_WEBSOCKET_OPCODES_MAP(RESTINIO_GEN)
Definition: message.hpp:24
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio::utils::impl::bitops
Definition: bitops.hpp:19
restinio::websocket::basic::status_code_t
status_code_t
Definition: message.hpp:86
restinio::websocket::basic::status_code_t::no_status_provided
@ no_status_provided
restinio::websocket::basic::status_code_t::going_away
@ going_away
restinio::websocket::basic::status_code_t::policy_violation
@ policy_violation
restinio::websocket::basic::status_code_t::unexpected_condition
@ unexpected_condition
restinio::websocket::basic::status_code_t::too_big_message
@ too_big_message
restinio::websocket::basic::status_code_t::connection_lost
@ connection_lost
restinio::websocket::basic::status_code_t::more_extensions_expected
@ more_extensions_expected
restinio::websocket::basic::status_code_t::invalid_message_data
@ invalid_message_data
restinio::websocket::basic::status_code_t::protocol_error
@ protocol_error
restinio::websocket::basic::status_code_t::cant_accept_data
@ cant_accept_data
restinio::websocket::basic::status_code_t::normal_closure
@ normal_closure
restinio::websocket::basic::opcode_t
opcode_t
Definition: message.hpp:37
restinio::websocket::basic::message_handle_t
std::shared_ptr< message_t > message_handle_t
Request handler, that is the type for calling request handlers.
Definition: message.hpp:227
restinio::websocket::basic::default_message_handler_t
std::function< void(message_handle_t) > default_message_handler_t
Definition: message.hpp:234
restinio::websocket::basic::final_frame
constexpr final_frame_flag_t final_frame
Definition: message.hpp:135
restinio::websocket::basic::is_valid_opcode
bool is_valid_opcode(opcode_t opcode)
Definition: message.hpp:64
restinio::websocket::basic::not_final_frame
constexpr final_frame_flag_t not_final_frame
Definition: message.hpp:136
restinio::websocket::basic::opcode_to_string
const char * opcode_to_string(opcode_t opcode)
Helper sunction to get method string name.
Definition: message.hpp:46
restinio::websocket::basic::status_code_from_bin
status_code_t status_code_from_bin(string_view_t data)
Definition: message.hpp:112
restinio::websocket::basic::final_frame_flag_t
final_frame_flag_t
WS frame (message) "final"/"not final" flag.
Definition: message.hpp:133
restinio::websocket::basic::final_frame_flag_t::final_frame
@ final_frame
restinio::websocket::basic::final_frame_flag_t::not_final_frame
@ not_final_frame
restinio::websocket::basic::status_code_to_bin
std::string status_code_to_bin(status_code_t code)
Definition: message.hpp:101
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
std
STL namespace.
code
Definition: inftrees.h:24
const
#define const
Definition: zconf.h:230