RESTinio
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler > Class Template Referencefinal

Context for handling websocket connections. More...

#include <ws_connection.hpp>

Inheritance diagram for restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >:
restinio::websocket::basic::impl::ws_connection_base_t restinio::impl::executor_wrapper_t< Traits::strand_t > restinio::tcp_connection_ctx_base_t

Classes

class  one_shot_action_t
 A helper class for running exclusive action. Only a first action will run. More...
 

Public Types

using message_handler_t = WS_Message_Handler
 
using timer_manager_t = typename Traits::timer_manager_t
 
using timer_manager_handle_t = std::shared_ptr< timer_manager_t >
 
using timer_guard_t = typename timer_manager_t::timer_guard_t
 
using logger_t = typename Traits::logger_t
 
using strand_t = typename Traits::strand_t
 
using stream_socket_t = typename Traits::stream_socket_t
 
using lifetime_monitor_t = typename connection_count_limit_types< Traits >::lifetime_monitor_t
 
using ws_weak_handle_t = std::weak_ptr< ws_t >
 

Public Member Functions

 ws_connection_t (const ws_connection_t &)=delete
 
 ws_connection_t (ws_connection_t &&)=delete
 
ws_connection_toperator= (const ws_connection_t &)=delete
 
ws_connection_toperator= (ws_connection_t &&)=delete
 
 ~ws_connection_t () override
 
virtual void shutdown () override
 Shutdown websocket.
 
virtual void kill () override
 Kill websocket.
 
void init_read (ws_handle_t wsh) override
 Start reading ws-messages.
 
virtual void write_data (write_group_t wg, bool is_close_frame) override
 Write pieces of outgoing data.
 
- Public Member Functions inherited from restinio::websocket::basic::impl::ws_connection_base_t
 ws_connection_base_t (connection_id_t id)
 
- Public Member Functions inherited from restinio::tcp_connection_ctx_base_t
 tcp_connection_ctx_base_t (connection_id_t id)
 
virtual ~tcp_connection_ctx_base_t ()=default
 
connection_id_t connection_id () const noexcept
 Get connection id.
 
virtual void check_timeout (std::shared_ptr< tcp_connection_ctx_base_t > &self)=0
 Check timeouts for all activities.
 
- Public Member Functions inherited from restinio::impl::executor_wrapper_t< Traits::strand_t >
 executor_wrapper_t (Init_Executor &&init_executor)
 
virtual ~executor_wrapper_t ()=default
 
Traits::strand_t & get_executor () noexcept
 An executor for callbacks on async operations.
 

Private Types

enum class  write_state_t { write_enabled , write_disabled }
 Websocket output states. More...
 
enum class  read_state_t { read_any_frame , read_only_close_frame , read_nothing }
 Websocket input states. More...
 
using executor_wrapper_base_t = restinio::impl::executor_wrapper_t< typename Traits::strand_t >
 
using none_write_operation_t = ::restinio::impl::write_group_output_ctx_t::none_write_operation_t
 
using trivial_write_operation_t = ::restinio::impl::write_group_output_ctx_t::trivial_write_operation_t
 
using file_write_operation_t = ::restinio::impl::write_group_output_ctx_t::file_write_operation_t
 

Private Member Functions

void close_impl () noexcept
 Standard close routine.
 
void start_waiting_close_frame_only ()
 Start waiting for close-frame.
 
void graceful_close ()
 Close WebSocket connection in a graceful manner.
 
void send_close_frame_to_peer (std::string payload)
 Send close frame to peer.
 
void send_close_frame_to_peer (status_code_t code, std::string desc=std::string{})
 Send close frame to peer.
 
template<typename MSG_BUILDER >
void trigger_error_and_close (status_code_t status, MSG_BUILDER msg_builder) noexcept
 Trigger an error.
 
void start_read_header ()
 Start the process of reading ws messages from socket.
 
void consume_header_from_socket ()
 Initiate read operation on socket to receive bytes for header.
 
void handle_read_error (const char *desc, const asio_ns::error_code &ec)
 Handle read error (reading header or payload)
 
void after_read_header (const asio_ns::error_code &ec, std::size_t length)
 Handle read operation result, when reading header.
 
void consume_header_from_buffer (const char *data, std::size_t length)
 Parse header from internal buffer.
 
void handle_parsed_header (const message_details_t &md)
 Handle parsed header.
 
void handle_parsed_and_valid_header (const message_details_t &md)
 Handle parsed and valid header.
 
void start_read_payload (char *payload_data, std::size_t length_remaining, bool do_validate_payload_and_call_msg_handler=true)
 Start reading message payload.
 
void after_read_payload (char *payload_data, std::size_t length_remaining, const asio_ns::error_code &ec, std::size_t length, bool do_validate_payload_and_call_msg_handler=true)
 Handle read operation result, when reading payload.
 
void call_message_handler (message_handle_t close_frame)
 Call user message handler with current message.
 
bool validate_payload_part (char *payload_data, std::size_t length, std::size_t next_length_remaining)
 Validates a part of received payload.
 
void handle_invalid_payload (validation_state_t validation_result)
 Handle payload errors.
 
void call_handler_on_current_message ()
 
void call_close_handler_if_necessary (status_code_t status)
 
void write_data_impl (write_group_t wg, bool is_close_frame)
 Implementation of writing data performed on the asio_ns::io_context.
 
void init_write_if_necessary ()
 Checks if there is something to write, and if so starts write operation.
 
void init_write ()
 Initiate write operation.
 
void handle_current_write_ctx ()
 
void handle_trivial_write_operation (const trivial_write_operation_t &op)
 
void finish_handling_current_write_ctx ()
 Do post write actions for current write group.
 
void after_write (const asio_ns::error_code &ec)
 Handle write response finished.
 

Private Attributes

restinio::impl::connection_settings_handle_t< Traitsm_settings
 Common paramaters of a connection.
 
stream_socket_t m_socket
 Connection.
 
lifetime_monitor_t m_lifetime_monitor
 Monitor of the connection lifetime.
 
connection_input_t m_input
 Input routine.
 
ws_protocol_validator_t m_protocol_validator { true }
 Helper for validating protocol.
 
message_handler_t m_msg_handler
 Websocket message handler provided by user.
 
logger_tm_logger
 Logger for operation.
 
restinio::impl::write_group_output_ctx_t m_write_output_ctx
 Write to socket operation context.
 
ws_outgoing_data_t m_outgoing_data
 Output buffers queue.
 
ws_weak_handle_t m_websocket_weak_handle
 A waek handler for owning ws_t to use it when call message handler.
 
write_state_t m_write_state = write_state_t::write_enabled
 A state of a websocket output.
 
read_state_t m_read_state = read_state_t::read_nothing
 A state of a websocket input.
 
one_shot_action_t m_close_frame_to_user
 
one_shot_action_t m_close_frame_to_peer
 
one_shot_action_t m_close_impl
 
std::chrono::steady_clock::time_point m_write_operation_timeout_after
 
std::chrono::steady_clock::time_point m_close_frame_from_peer_timeout_after
 
tcp_connection_ctx_weak_handle_t m_prepared_weak_ctx
 
timer_guard_t m_timer_guard
 
virtual void check_timeout (tcp_connection_ctx_handle_t &self) override
 
void check_timeout_impl ()
 
void init_next_timeout_checking ()
 schedule next timeout checking.
 
void guard_write_operation ()
 Start guard write operation if necessary.
 
void guard_close_frame_from_peer_operation ()
 
static ws_connection_tcast_to_self (tcp_connection_ctx_base_t &base)
 Timers.
 

Additional Inherited Members

- Protected Member Functions inherited from restinio::tcp_connection_ctx_base_t
template<typename Derived >
std::shared_ptr< Derivedshared_from_concrete ()
 Cast self to derived class.
 

Detailed Description

template<typename Traits, typename WS_Message_Handler>
class restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >

Context for handling websocket connections.

Definition at line 122 of file ws_connection.hpp.

Member Typedef Documentation

◆ executor_wrapper_base_t

Definition at line 126 of file ws_connection.hpp.

◆ file_write_operation_t

Definition at line 1103 of file ws_connection.hpp.

◆ lifetime_monitor_t

Definition at line 137 of file ws_connection.hpp.

◆ logger_t

Definition at line 134 of file ws_connection.hpp.

◆ message_handler_t

Definition at line 129 of file ws_connection.hpp.

◆ none_write_operation_t

Definition at line 1101 of file ws_connection.hpp.

◆ strand_t

Definition at line 135 of file ws_connection.hpp.

◆ stream_socket_t

Definition at line 136 of file ws_connection.hpp.

◆ timer_guard_t

Definition at line 133 of file ws_connection.hpp.

◆ timer_manager_handle_t

Definition at line 132 of file ws_connection.hpp.

◆ timer_manager_t

Definition at line 131 of file ws_connection.hpp.

◆ trivial_write_operation_t

Definition at line 1102 of file ws_connection.hpp.

◆ ws_weak_handle_t

Definition at line 140 of file ws_connection.hpp.

Member Enumeration Documentation

◆ read_state_t

Websocket input states.

Enumerator
read_any_frame 

Reads any type of frame and serve it to user.

read_only_close_frame 

Reads only close frame: skip all frames until close-frame.

read_nothing 

Do not read anything (before activation).

Definition at line 1393 of file ws_connection.hpp.

◆ write_state_t

Websocket output states.

Enumerator
write_enabled 

Able to append outgoing data.

write_disabled 

No more outgoing data can be added (e.g. close-frame was sent).

Definition at line 1381 of file ws_connection.hpp.

Constructor & Destructor Documentation

◆ ws_connection_t() [1/2]

◆ ws_connection_t() [2/2]

◆ ~ws_connection_t()

Definition at line 192 of file ws_connection.hpp.

Member Function Documentation

◆ after_read_header()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::after_read_header ( const asio_ns::error_code &  ec,
std::size_t  length 
)
inlineprivate

Handle read operation result, when reading header.

Definition at line 562 of file ws_connection.hpp.

◆ after_read_payload()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::after_read_payload ( char payload_data,
std::size_t  length_remaining,
const asio_ns::error_code &  ec,
std::size_t  length,
bool  do_validate_payload_and_call_msg_handler = true 
)
inlineprivate

Handle read operation result, when reading payload.

Definition at line 748 of file ws_connection.hpp.

◆ after_write()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::after_write ( const asio_ns::error_code &  ec)
inlineprivate

Handle write response finished.

Definition at line 1216 of file ws_connection.hpp.

◆ call_close_handler_if_necessary()

Definition at line 1003 of file ws_connection.hpp.

◆ call_handler_on_current_message()

Definition at line 935 of file ws_connection.hpp.

◆ call_message_handler()

Call user message handler with current message.

Definition at line 816 of file ws_connection.hpp.

◆ cast_to_self()

Timers.

Definition at line 1265 of file ws_connection.hpp.

◆ check_timeout()

Definition at line 1271 of file ws_connection.hpp.

◆ check_timeout_impl()

Definition at line 1307 of file ws_connection.hpp.

◆ close_impl()

Standard close routine.

Note
This method is noexcept since v.0.6.0.

Definition at line 371 of file ws_connection.hpp.

◆ consume_header_from_buffer()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::consume_header_from_buffer ( const char data,
std::size_t  length 
)
inlineprivate

Parse header from internal buffer.

Definition at line 586 of file ws_connection.hpp.

◆ consume_header_from_socket()

Initiate read operation on socket to receive bytes for header.

Definition at line 510 of file ws_connection.hpp.

◆ finish_handling_current_write_ctx()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::finish_handling_current_write_ctx ( )
inlineprivate

Do post write actions for current write group.

Definition at line 1197 of file ws_connection.hpp.

◆ graceful_close()

Close WebSocket connection in a graceful manner.

Definition at line 413 of file ws_connection.hpp.

◆ guard_close_frame_from_peer_operation()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::guard_close_frame_from_peer_operation ( )
inlineprivate

Definition at line 1352 of file ws_connection.hpp.

◆ guard_write_operation()

Start guard write operation if necessary.

Definition at line 1345 of file ws_connection.hpp.

◆ handle_current_write_ctx()

Definition at line 1106 of file ws_connection.hpp.

◆ handle_invalid_payload()

Handle payload errors.

Definition at line 883 of file ws_connection.hpp.

◆ handle_parsed_and_valid_header()

Handle parsed and valid header.

Definition at line 651 of file ws_connection.hpp.

◆ handle_parsed_header()

Handle parsed header.

Definition at line 605 of file ws_connection.hpp.

◆ handle_read_error()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::handle_read_error ( const char desc,
const asio_ns::error_code &  ec 
)
inlineprivate

Handle read error (reading header or payload)

Definition at line 546 of file ws_connection.hpp.

◆ handle_trivial_write_operation()

Definition at line 1140 of file ws_connection.hpp.

◆ init_next_timeout_checking()

schedule next timeout checking.

Definition at line 1338 of file ws_connection.hpp.

◆ init_read()

Start reading ws-messages.

Implements restinio::websocket::basic::impl::ws_connection_base_t.

Definition at line 284 of file ws_connection.hpp.

◆ init_write()

Initiate write operation.

Definition at line 1075 of file ws_connection.hpp.

◆ init_write_if_necessary()

Checks if there is something to write, and if so starts write operation.

Definition at line 1065 of file ws_connection.hpp.

◆ kill()

Kill websocket.

Implements restinio::websocket::basic::impl::ws_connection_base_t.

Definition at line 245 of file ws_connection.hpp.

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ send_close_frame_to_peer() [1/2]

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::send_close_frame_to_peer ( status_code_t  code,
std::string  desc = std::string{} 
)
inlineprivate

Send close frame to peer.

Definition at line 446 of file ws_connection.hpp.

◆ send_close_frame_to_peer() [2/2]

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::send_close_frame_to_peer ( std::string  payload)
inlineprivate

Send close frame to peer.

Definition at line 424 of file ws_connection.hpp.

◆ shutdown()

Shutdown websocket.

Implements restinio::websocket::basic::impl::ws_connection_base_t.

Definition at line 209 of file ws_connection.hpp.

◆ start_read_header()

Start the process of reading ws messages from socket.

Definition at line 485 of file ws_connection.hpp.

◆ start_read_payload()

void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::start_read_payload ( char payload_data,
std::size_t  length_remaining,
bool  do_validate_payload_and_call_msg_handler = true 
)
inlineprivate

Start reading message payload.

Parameters
payload_dataA pointer to the remainder of unfetched payload.
length_remainingThe size of the remainder of unfetched payload.
do_validate_payload_and_call_msg_handlerValidate payload and call handler.

Definition at line 703 of file ws_connection.hpp.

◆ start_waiting_close_frame_only()

Start waiting for close-frame.

Definition at line 405 of file ws_connection.hpp.

◆ trigger_error_and_close()

template<typename MSG_BUILDER >
void restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::trigger_error_and_close ( status_code_t  status,
MSG_BUILDER  msg_builder 
)
inlineprivatenoexcept

Trigger an error.

Writes error message to log, closes socket, and sends close frame to user if necessary.

Note
This method is noexcept since v.0.6.0

Definition at line 464 of file ws_connection.hpp.

◆ validate_payload_part()

bool restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::validate_payload_part ( char payload_data,
std::size_t  length,
std::size_t  next_length_remaining 
)
inlineprivate

Validates a part of received payload.

Definition at line 840 of file ws_connection.hpp.

◆ write_data()

Write pieces of outgoing data.

Run write message on io_context loop if possible.

Implements restinio::websocket::basic::impl::ws_connection_base_t.

Definition at line 321 of file ws_connection.hpp.

◆ write_data_impl()

Implementation of writing data performed on the asio_ns::io_context.

Definition at line 1017 of file ws_connection.hpp.

Member Data Documentation

◆ m_close_frame_from_peer_timeout_after

std::chrono::steady_clock::time_point restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::m_close_frame_from_peer_timeout_after
private
Initial value:
=
std::chrono::steady_clock::time_point::max()

Definition at line 1301 of file ws_connection.hpp.

◆ m_close_frame_to_peer

Definition at line 1434 of file ws_connection.hpp.

◆ m_close_frame_to_user

Definition at line 1433 of file ws_connection.hpp.

◆ m_close_impl

Definition at line 1435 of file ws_connection.hpp.

◆ m_input

Input routine.

Definition at line 1360 of file ws_connection.hpp.

◆ m_lifetime_monitor

Monitor of the connection lifetime.

Since
v.0.6.12

Definition at line 1260 of file ws_connection.hpp.

◆ m_logger

Logger for operation.

Definition at line 1369 of file ws_connection.hpp.

◆ m_msg_handler

Websocket message handler provided by user.

Definition at line 1366 of file ws_connection.hpp.

◆ m_outgoing_data

Output buffers queue.

Definition at line 1375 of file ws_connection.hpp.

◆ m_prepared_weak_ctx

Definition at line 1303 of file ws_connection.hpp.

◆ m_protocol_validator

Helper for validating protocol.

Definition at line 1363 of file ws_connection.hpp.

◆ m_read_state

A state of a websocket input.

Definition at line 1404 of file ws_connection.hpp.

◆ m_settings

Common paramaters of a connection.

Definition at line 1250 of file ws_connection.hpp.

◆ m_socket

Connection.

Definition at line 1253 of file ws_connection.hpp.

◆ m_timer_guard

Definition at line 1304 of file ws_connection.hpp.

◆ m_websocket_weak_handle

A waek handler for owning ws_t to use it when call message handler.

Definition at line 1378 of file ws_connection.hpp.

◆ m_write_operation_timeout_after

std::chrono::steady_clock::time_point restinio::websocket::basic::impl::ws_connection_t< Traits, WS_Message_Handler >::m_write_operation_timeout_after
private

Definition at line 1300 of file ws_connection.hpp.

◆ m_write_output_ctx

Write to socket operation context.

Definition at line 1372 of file ws_connection.hpp.

◆ m_write_state

A state of a websocket output.

Definition at line 1390 of file ws_connection.hpp.


The documentation for this class was generated from the following file: