66 std::optional< write_group_t >
69 std::optional< write_group_t >
result;
93 :
m_buf{ buffer_size }
166 "[connection:{}] move socket to [ws_connection:{}]" ),
174 "[ws_connection:{}] start connection with {}" ),
180 m_settings->call_state_listener( [
this]()
noexcept {
202 "[ws_connection:{}] destructor called" ),
227 "[ws_connection:{}] shutdown" ),
234 catch(
const std::exception &
ex )
240 "[ws_connection:{}] shutdown operation error: {}" ),
266 "[ws_connection:{}] kill" ),
275 catch(
const std::exception &
ex )
281 "[ws_connection:{}] kill operation error: {}" ),
312 catch(
const std::exception &
ex )
319 "[ws_connection:{}] unable to init read: {}" ),
354 "[ws_connection:{}] cannot write to websocket: "
355 "write operations disabled" ),
360 catch(
const std::exception &
ex )
367 "[ws_connection:{}] unable to write data: {}" ),
389 "[ws_connection:{}] close socket" ),
397 "ws_connection.close_impl.socket.shutdown",
401 asio_ns::ip::tcp::socket::shutdown_both,
407 "ws_connection.close_impl.socket.close",
443 opcode_t::connection_close_frame,
446 bufs.emplace_back( std::move( payload ) );
459 std::string
desc = std::string{} )
473 template<
typename MSG_BUILDER >
485 m_logger,
"ws_connection.call_close_handler_if_necessary",
501 "[ws_connection:{}] start reading header" ),
527 "[ws_connection:{}] continue reading message" ),
533 asio_ns::bind_executor(
537 (
const asio_ns::error_code &
ec, std::size_t length )
noexcept
543 catch(
const std::exception &
ex )
550 "[ws_connection:{}] after read header "
551 "callback error: {}" ),
578 const asio_ns::error_code &
ec,
586 "[ws_connection:{}] received {} bytes" ),
626 "[ws_connection:{}] start handling {} ({:#x})" ),
629 static_cast<std::uint16_t
>(
md.m_opcode) );
640 "[ws_connection:{}] invalid header" ),
731 asio_ns::bind_executor(
739 (
const asio_ns::error_code &
ec, std::size_t length )
noexcept
750 catch(
const std::exception &
ex )
757 "[ws_connection:{}] after read payload "
758 "callback error: {}" ),
771 const asio_ns::error_code &
ec,
780 "[ws_connection:{}] received {} bytes" ),
847 catch(
const std::exception &
ex )
852 "[ws_connection:{}] execute handler error: {}" ),
910 "[ws_connection:{}] invalid paload" ),
967 if( opcode_t::connection_close_frame ==
md.m_opcode )
972 "[ws_connection:{}] got close frame from "
973 "peer, status: {}" ),
975 static_cast<std::uint16_t
>(
989 std::make_shared< message_t >(
1001 if( opcode_t::connection_close_frame ==
md.m_opcode )
1011 "[ws_connection:{}] expected close frame came" ),
1034 std::make_shared< message_t >(
1036 opcode_t::connection_close_frame,
1052 "[ws_connection:{}] user sends close frame" ),
1075 "[ws_connection:{}] try to write while "
1076 "socket is closed" ),
1082 wg.invoke_after_write_notificator_if_exists(
1115 "[ws_connection:{}] start next write group, "
1142 if( std::holds_alternative< trivial_write_operation_t >(
wo ) )
1146 else if( std::holds_alternative< none_write_operation_t >(
wo ) )
1152 assert( std::holds_alternative< file_write_operation_t >(
wo ) );
1153 throw exception_t{
"sendfile write operation not implemented" };
1156 catch(
const std::exception &
ex )
1163 "[ws_connection:{}] handle_current_write_ctx failed: {}" ),
1174 auto &
bufs =
op.get_trivial_bufs();
1179 "[ws_connection:{}] sending data with "
1189 asio_ns::async_write(
1192 asio_ns::bind_executor(
1197 (
const asio_ns::error_code &
ec, std::size_t
written )
noexcept
1206 "[ws_connection:{}] outgoing data was "
1215 catch(
const std::exception &
ex )
1222 "[ws_connection:{}] after write "
1223 "callback error: {}" ),
1239 "[ws_connection:{}] finishing current write group" ),
1266 "[ws_connection:{}] unable to write: {}" ),
1275 catch(
const std::exception &
ex )
1280 "[ws_connection:{}] notificator error: {}" ),
1325 catch(
const std::exception &
x )
1332 "[connection: {}] unexpected "
1333 "error during timeout handling: {}" ),
1343 std::chrono::steady_clock::time_point::max();
1350 const auto now = std::chrono::steady_clock::now();
1356 "[wd_connection:{}] write operation timed out" ),
1368 "[wd_connection:{}] waiting for close-frame "
1369 "from peer timed out" ),
1392 std::chrono::steady_clock::now() +
m_settings->m_write_http_response_timelimit;
1399 std::chrono::steady_clock::now() +
m_settings->m_read_next_http_message_timelimit;
1455 template <
typename Action >
Helper type for controlling the lifetime of the connection.
An object with info about connection to be passed to state listener.
endpoint_t remote_endpoint() const noexcept
Get the remote endpoint for the connection.
Type of object that tells that the connection has been upgraded to WebSocket.
Exception class for all exceptions thrown by RESTinio.
Wrapper for an executor (strand) used by connections.
Traits::strand_t & get_executor() noexcept
An executor for callbacks on async operations.
Helper class for reading bytes and feeding them to parser.
std::size_t length() const noexcept
How many unconsumed bytes are there in buffer.
void consumed_bytes(std::size_t length) noexcept
Mark how many bytes were obtained.
void obtained_bytes(std::size_t length) noexcept
Mark how many bytes were obtained.
auto make_asio_buffer() noexcept
Make asio buffer for reading bytes from socket.
const char * bytes() const noexcept
Get pointer to unconsumed bytes.
Write operaton using sendfile.
Helper class for writting response data.
void fail_write_group(const asio_ns::error_code &ec)
Handle current group write process failed.
solid_write_operation_variant_t extract_next_write_operation()
et an object with next write operation to perform.
void start_next_write_group(std::optional< write_group_t > next_wg) noexcept
Start handlong next write group.
void finish_write_group()
Finish writing group normally.
bool transmitting() const noexcept
Check if data is trunsmitting now.
connection_id_t connection_id() const noexcept
Get connection id.
Websocket message class with more detailed protocol information.
WebSocket connection base.
A helper class for running exclusive action. Only a first action will run.
void run_if_first(Action &&action) noexcept(noexcept(action()))
void disable()
Disable ation: action will not be executed even on a first shot.
Context for handling websocket connections.
timer_guard_t m_timer_guard
void finish_handling_current_write_ctx()
Do post write actions for current write group.
void after_read_header(const asio_ns::error_code &ec, std::size_t length)
Handle read operation result, when reading header.
restinio::impl::connection_settings_handle_t< Traits > m_settings
Common paramaters of a connection.
void consume_header_from_socket()
Initiate read operation on socket to receive bytes for header.
message_handler_t m_msg_handler
Websocket message handler provided by user.
void handle_parsed_header(const message_details_t &md)
Handle parsed header.
WS_Message_Handler message_handler_t
bool validate_payload_part(char *payload_data, std::size_t length, std::size_t next_length_remaining)
Validates a part of received payload.
ws_weak_handle_t m_websocket_weak_handle
A waek handler for owning ws_t to use it when call message handler.
virtual void shutdown() override
Shutdown websocket.
void send_close_frame_to_peer(std::string payload)
Send close frame to peer.
void write_data_impl(write_group_t wg, bool is_close_frame)
Implementation of writing data performed on the asio_ns::io_context.
read_state_t
Websocket input states.
@ read_only_close_frame
Reads only close frame: skip all frames until close-frame.
@ read_nothing
Do not read anything (before activation).
@ read_any_frame
Reads any type of frame and serve it to user.
std::weak_ptr< ws_t > ws_weak_handle_t
void check_timeout_impl()
typename Traits::logger_t logger_t
write_state_t
Websocket output states.
@ write_enabled
Able to append outgoing data.
@ write_disabled
No more outgoing data can be added (e.g. close-frame was sent).
void guard_close_frame_from_peer_operation()
std::chrono::steady_clock::time_point m_close_frame_from_peer_timeout_after
typename timer_manager_t::timer_guard_t timer_guard_t
one_shot_action_t m_close_impl
void init_write_if_necessary()
Checks if there is something to write, and if so starts write operation.
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 start_read_payload(char *payload_data, std::size_t length_remaining, bool do_validate_payload_and_call_msg_handler=true)
Start reading message payload.
logger_t & m_logger
Logger for operation.
one_shot_action_t m_close_frame_to_peer
one_shot_action_t m_close_frame_to_user
tcp_connection_ctx_weak_handle_t m_prepared_weak_ctx
void handle_trivial_write_operation(const trivial_write_operation_t &op)
void init_next_timeout_checking()
schedule next timeout checking.
void handle_invalid_payload(validation_state_t validation_result)
Handle payload errors.
void handle_current_write_ctx()
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_close_handler_if_necessary(status_code_t status)
void consume_header_from_buffer(const char *data, std::size_t length)
Parse header from internal buffer.
void send_close_frame_to_peer(status_code_t code, std::string desc=std::string{})
Send close frame to peer.
ws_connection_t(const ws_connection_t &)=delete
virtual void check_timeout(tcp_connection_ctx_handle_t &self) override
connection_input_t m_input
Input routine.
void handle_read_error(const char *desc, const asio_ns::error_code &ec)
Handle read error (reading header or payload)
void call_handler_on_current_message()
write_state_t m_write_state
A state of a websocket output.
ws_protocol_validator_t m_protocol_validator
Helper for validating protocol.
void handle_parsed_and_valid_header(const message_details_t &md)
Handle parsed and valid header.
ws_connection_t & operator=(const ws_connection_t &)=delete
ws_connection_t(ws_connection_t &&)=delete
void start_waiting_close_frame_only()
Start waiting for close-frame.
std::chrono::steady_clock::time_point m_write_operation_timeout_after
typename Traits::strand_t strand_t
virtual void kill() override
Kill websocket.
void guard_write_operation()
Start guard write operation if necessary.
~ws_connection_t() override
ws_outgoing_data_t m_outgoing_data
Output buffers queue.
void init_read(ws_handle_t wsh) override
Start reading ws-messages.
std::shared_ptr< timer_manager_t > timer_manager_handle_t
void call_message_handler(message_handle_t close_frame)
Call user message handler with current message.
static ws_connection_t & cast_to_self(tcp_connection_ctx_base_t &base)
Timers.
void init_write()
Initiate write operation.
typename connection_count_limit_types< Traits >::lifetime_monitor_t lifetime_monitor_t
lifetime_monitor_t m_lifetime_monitor
Monitor of the connection lifetime.
void after_write(const asio_ns::error_code &ec)
Handle write response finished.
restinio::impl::write_group_output_ctx_t m_write_output_ctx
Write to socket operation context.
void close_impl() noexcept
Standard close routine.
typename Traits::stream_socket_t stream_socket_t
virtual void write_data(write_group_t wg, bool is_close_frame) override
Write pieces of outgoing data.
stream_socket_t m_socket
Connection.
void graceful_close()
Close WebSocket connection in a graceful manner.
read_state_t m_read_state
A state of a websocket input.
typename Traits::timer_manager_t timer_manager_t
A queue for outgoing buffers.
write_groups_queue_t m_awaiting_write_groups
A queue of buffers.
void append(write_group_t wg)
Add buffers to queue.
std::optional< write_group_t > pop_ready_buffers()
void reset()
Reset internal state.
const message_details_t & current_message() const
Get current mesasge details.
size_t parser_execute(const char *data, size_t size)
Parse piece of data from buffer.
bool header_parsed() const
Check header of current websocket message is parsed.
Class for websocket protocol validations.
validation_state_t process_and_unmask_next_payload_part(char *data, size_t size)
Validate next part of current frame and reset source part to unmasked data.
validation_state_t process_new_frame(const message_details_t &frame)
Start work with new frame.
validation_state_t finish_frame()
Make final checks of payload if it is necessary and reset state.
void reset()
Reset to initial state.
Group of writable items transported to the context of underlying connection as one solid piece.
Detection of compiler version and absence of various features.
#define RESTINIO_ENSURE_NOEXCEPT_CALL(expr)
A wrapper around static_assert for checking that an expression is noexcept and execution of that expr...
Include all core header files in one.
A special wrapper around fmtlib include files.
#define RESTINIO_FMT_FORMAT_STRING(s)
std::shared_ptr< connection_settings_t< Traits > > connection_settings_handle_t
std::size_t uint64_to_size_t(std::uint64_t v)
Helper function for truncating uint64 to std::size_t with exception if that truncation will lead to d...
void suppress_exceptions(Logger &&logger, const char *block_description, Lambda &&lambda) noexcept
Helper function for execution a block of code with suppression of any exceptions raised inside that b...
void log_error_noexcept(Logger &&logger, Message_Builder &&builder) noexcept
void log_trace_noexcept(Logger &&logger, Message_Builder &&builder) noexcept
raw_data_t write_message_details(const message_details_t &message)
Serialize websocket message details into bytes buffer.
constexpr size_t websocket_header_max_size()
Max possible size of websocket frame header (a part before payload).
std::queue< write_group_t > write_groups_queue_t
validation_state_t
States of validated frame.
std::shared_ptr< ws_t > ws_handle_t
Alias for ws_t handle.
std::shared_ptr< message_t > message_handle_t
Request handler, that is the type for calling request handlers.
constexpr final_frame_flag_t final_frame
constexpr final_frame_flag_t not_final_frame
const char * opcode_to_string(opcode_t opcode)
Helper sunction to get method string name.
status_code_t status_code_from_bin(string_view_t data)
std::string status_code_to_bin(status_code_t code)
run_on_this_thread_settings_t< Traits > on_this_thread()
A special marker for the case when http_server must be run on the context of the current thread.
std::weak_ptr< tcp_connection_ctx_base_t > tcp_connection_ctx_weak_handle_t
Alias for http connection weak handle.
@ write_was_not_executed
After write notificator error: data was not sent, connection closed (or aborted) before a given piece...
std::uint64_t connection_id_t
Type for ID of connection.
asio_ns::error_code make_asio_compaible_error(asio_convertible_error_t err) noexcept
Make restinio error_code compatible with asio_ns::error_code.
std::vector< writable_item_t > writable_items_container_t
std::shared_ptr< tcp_connection_ctx_base_t > tcp_connection_ctx_handle_t
Alias for http connection handle.
Helpers for safe truncation of unsigned integers.