28#ifndef WEBSOCKETPP_CONNECTION_HPP
29#define WEBSOCKETPP_CONNECTION_HPP
154typedef lib::function<void(lib::error_code
const & ec,
size_t bytes_transferred)>
read_handler;
158#ifdef _WEBSOCKETPP_INITIALIZER_LISTS_
164 static std::vector<int>
const versions_supported = {0,7,8,13};
167 static int const helper[] = {0,7,8,13};
173 static std::vector<int>
const versions_supported(helper,helper+4);
201namespace internal_state {
219namespace http_state {
234template <
typename config>
236 :
public config::transport_type::transport_con_type
237 ,
public config::connection_base
243 typedef lib::shared_ptr<type>
ptr;
255 typedef typename config::transport_type::transport_con_type
284 typedef typename transport_con_type::timer_ptr
timer_ptr;
290 enum terminate_status {
300 , m_handle_read_frame(lib::bind(
303 lib::placeholders::_1,
304 lib::placeholders::_2
306 , m_write_frame_handler(lib::bind(
309 lib::placeholders::_1
312 , m_open_handshake_timeout_dur(
config::timeout_open_handshake)
313 , m_close_handshake_timeout_dur(
config::timeout_close_handshake)
314 , m_pong_timeout_dur(
config::timeout_pong)
315 , m_max_message_size(
config::max_message_size)
317 , m_internal_state(
session::internal_state::USER_INIT)
319 , m_send_buffer_size(0)
320 , m_write_flag(false)
322 , m_is_server(p_is_server)
326 , m_local_close_code(
close::status::abnormal_close)
327 , m_remote_close_code(
close::status::abnormal_close)
337 return lib::static_pointer_cast<type>(transport_con_type::get_shared());
422 m_pong_timeout_handler = h;
433 m_interrupt_handler = h;
464 m_validate_handler = h;
474 m_message_handler = h;
502 m_open_handshake_timeout_dur = dur;
526 m_close_handshake_timeout_dur = dur;
547 m_pong_timeout_dur = dur;
560 return m_max_message_size;
576 m_max_message_size = new_value;
578 m_processor->set_max_message_size(new_value);
595 return m_request.get_max_body_size();
611 m_request.set_max_body_size(new_value);
757 void ping(std::string
const & payload);
760 void ping(std::string
const & payload, lib::error_code & ec);
775 void pong(std::string
const & payload);
778 void pong(std::string
const & payload, lib::error_code & ec);
804 lib::error_code & ec);
826 std::string
const &
get_host()
const;
899 void add_subprotocol(std::string
const & request, lib::error_code & ec);
984 return m_response.get_status_code();
996 return m_response.get_status_msg();
1060 void append_header(std::string
const & key, std::string
const & val);
1075 void replace_header(std::string
const & key, std::string
const & val);
1185 return m_connection_hdl;
1219 return m_local_close_code;
1227 return m_local_close_reason;
1235 return m_remote_close_code;
1243 return m_remote_close_reason;
1281 return m_msg_manager->get_message(
op, size);
1295 size_t bytes_transferred);
1297 size_t bytes_transferred);
1316 void terminate(lib::error_code
const & ec);
1358 m_connection_hdl = hdl;
1359 transport_con_type::set_handle(hdl);
1375 void write_http_response(lib::error_code
const & ec);
1378 void send_http_request();
1381 void write_http_response_error(lib::error_code
const & ec);
1401 close::status::blank, std::string
const & reason = std::string());
1419 close::status::blank, std::string
const & reason = std::string(),
bool ack =
false,
1420 bool terminal =
false);
1465 void log_open_result();
1471 void log_close_result();
1477 void log_fail_result();
1483 void log_http_result();
1486 template <
typename error_type>
1487 void log_err(
log::level l,
char const * msg, error_type
const & ec) {
1488 std::stringstream
s;
1489 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1490 m_elog.write(
l,
s.str());
1498 std::string
const m_user_agent;
1516 long m_open_handshake_timeout_dur;
1517 long m_close_handshake_timeout_dur;
1518 long m_pong_timeout_dur;
1519 size_t m_max_message_size;
1543 char m_buf[config::connection_read_buffer_size];
1544 size_t m_buf_cursor;
1552 std::string m_handshake_buffer;
1569 std::queue<message_ptr> m_send_queue;
1575 size_t m_send_buffer_size;
1581 std::vector<transport::buffer> m_send_buffer;
1585 std::vector<message_ptr> m_current_msgs;
1600 std::string m_subprotocol;
1604 std::vector<std::string> m_requested_subprotocols;
1606 bool const m_is_server;
1617 std::string m_local_close_reason;
1623 std::string m_remote_close_reason;
1626 lib::error_code m_ec;
1639 bool m_closed_by_me;
1642 bool m_failed_by_me;
1645 bool m_dropped_by_me;
Represents an individual WebSocket connection.
ptr get_shared()
Get a shared pointer to this component.
config::concurrency_type concurrency_type
Type of the concurrency component of this connection.
void handle_interrupt()
Transport inturrupt callback.
lib::function< void(ptr)> termination_handler
void set_message_handler(message_handler h)
Set message handler.
config::elog_type elog_type
Type of the error logging policy.
void handle_close_handshake_timeout(lib::error_code const &ec)
lib::error_code interrupt()
Asyncronously invoke handler::on_inturrupt.
void start()
Start the connection state machine.
std::string const & get_request_body() const
Retrieve a request body.
void ping(std::string const &payload)
Send a ping.
void set_close_handler(close_handler h)
Set close handler.
void handle_read_http_response(lib::error_code const &ec, size_t bytes_transferred)
void set_pong_handler(pong_handler h)
Set pong handler.
lib::error_code send(message_ptr msg)
Add a message to the outgoing send queue.
config::message_type message_type
config::response_type response_type
http::status_code::value get_response_code() const
Get response HTTP status code.
lib::error_code defer_http_response()
Defer HTTP Response until later (Exception free)
lib::error_code resume_reading()
Resume reading of new data.
void add_subprotocol(std::string const &request, lib::error_code &ec)
Adds the given subprotocol string to the request list (exception free)
bool get_secure() const
Returns the secure flag from the connection URI.
void set_close_handshake_timeout(long dur)
Set close handshake timeout.
config::alog_type alog_type
Type of the access logging policy.
lib::shared_ptr< processor_type > processor_ptr
void set_body(std::string const &value)
Set response body content.
size_t get_buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
std::string const & get_origin() const
Return the same origin policy origin value from the opening request.
message_type::ptr message_ptr
std::string const & get_host() const
Returns the host component of the connection URI.
transport_con_type::timer_ptr timer_ptr
Type of a pointer to a transport timer handle.
response_type const & get_response() const
Get response object.
void handle_terminate(terminate_status tstat, lib::error_code const &ec)
void set_max_message_size(size_t new_value)
Set maximum message size.
connection(bool p_is_server, std::string const &ua, alog_type &alog, elog_type &elog, rng_type &rng)
std::string const & get_resource() const
Returns the resource component of the connection URI.
void select_subprotocol(std::string const &value, lib::error_code &ec)
Select a subprotocol to use (exception free)
std::string const & get_request_header(std::string const &key) const
Retrieve a request header.
lib::error_code process_handshake_request()
void handle_pause_reading()
Pause reading callback.
std::vector< int > const & get_supported_versions() const
Get array of WebSocket protocol versions that this connection supports.
config::rng_type rng_type
Type of RNG.
void remove_header(std::string const &key)
Remove a header.
uri_ptr get_uri() const
Gets the connection URI.
con_msg_manager_type::ptr con_msg_manager_ptr
connection_hdl get_handle() const
Get Connection Handle.
void set_pong_timeout_handler(pong_timeout_handler h)
Set pong timeout handler.
void set_ping_handler(ping_handler h)
Set ping handler.
void handle_write_http_response(lib::error_code const &ec)
std::string const & get_response_header(std::string const &key) const
Retrieve a response header.
config::con_msg_manager_type con_msg_manager_type
lib::function< void(connection_hdl, message_ptr)> message_handler
void set_http_handler(http_handler h)
Set http handler.
std::string const & get_response_msg() const
Get response HTTP status message.
message_ptr get_message(websocketpp::frame::opcode::value op, size_t size) const
Get a message buffer.
void handle_write_frame(lib::error_code const &ec)
Process the results of a frame write operation and start the next write.
close::status::value get_local_close_code() const
Get the WebSocket close code sent by this endpoint.
lib::error_code get_ec() const
Get the internal error code for a closed/failed connection.
void set_fail_handler(fail_handler h)
Set fail handler.
void write_frame()
Checks if there are frames in the send queue and if there are sends one.
session::state::value get_state() const
Return the connection state.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the transport component of this connection.
lib::weak_ptr< type > weak_ptr
Type of a weak pointer to this connection.
void set_status(http::status_code::value code)
Set response status code and message.
void replace_header(std::string const &key, std::string const &val)
Replace a header.
concurrency_type::scoped_lock_type scoped_lock_type
size_t get_max_http_body_size() const
Get maximum HTTP message body size.
void handle_read_handshake(lib::error_code const &ec, size_t bytes_transferred)
void read_handshake(size_t num_bytes)
void set_validate_handler(validate_handler h)
Set validate handler.
void pong(std::string const &payload)
Send a pong.
void set_interrupt_handler(interrupt_handler h)
Set interrupt handler.
size_t buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
void send_http_response()
Send deferred HTTP Response.
close::status::value get_remote_close_code() const
Get the WebSocket close code sent by the remote endpoint.
void set_open_handler(open_handler h)
Set open handler.
bool is_server() const
Get whether or not this connection is part of a server or client.
std::string const & get_remote_close_reason() const
Get the WebSocket close reason sent by the remote endpoint.
void set_pong_timeout(long dur)
Set pong timeout.
void handle_read_frame(lib::error_code const &ec, size_t bytes_transferred)
lib::error_code initialize_processor()
void set_uri(uri_ptr uri)
Sets the connection URI.
void set_termination_handler(termination_handler new_handler)
request_type const & get_request() const
Get request object.
connection< config > type
Type of this connection.
session::internal_state::value istate_type
void set_max_http_body_size(size_t new_value)
Set maximum HTTP message body size.
void handle_transport_init(lib::error_code const &ec)
void set_open_handshake_timeout(long dur)
Set open handshake timeout.
lib::error_code pause_reading()
Pause reading of new data.
void handle_resume_reading()
Resume reading callback.
std::vector< std::string > const & get_requested_subprotocols() const
Gets all of the subprotocols requested by the client.
config::transport_type::transport_con_type transport_con_type
Type of the transport component of this connection.
void close(close::status::value const code, std::string const &reason)
Close the connection.
std::string const & get_subprotocol() const
Gets the negotated subprotocol.
void handle_open_handshake_timeout(lib::error_code const &ec)
void set_handle(connection_hdl hdl)
Set Connection Handle.
void handle_pong_timeout(std::string payload, lib::error_code const &ec)
Utility method that gets called back when the ping timer expires.
void handle_send_http_request(lib::error_code const &ec)
lib::error_code send(std::string const &payload, frame::opcode::value op=frame::opcode::text)
Create a message and then add it to the outgoing send queue.
void read_frame()
Issue a new transport read unless reading is paused.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
uint16_t get_port() const
Returns the port component of the connection URI.
std::string const & get_local_close_reason() const
Get the WebSocket close reason sent by this endpoint.
processor::processor< config > processor_type
size_t get_max_message_size() const
Get maximum message size.
void append_header(std::string const &key, std::string const &val)
Append a header.
concurrency_type::mutex_type mutex_type
config::request_type request_type
WebSocket protocol processor abstract base class.
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
CK_SESSION_HANDLE session
uint16_t value
The type of a close code value.
uint32_t level
Type of a channel package.
Namespace for the WebSocket++ project.
lib::function< void(connection_hdl, std::string)> pong_handler
The type and function signature of a pong handler.
lib::function< void(connection_hdl)> close_handler
The type and function signature of a close handler.
lib::function< void(connection_hdl, std::string)> pong_timeout_handler
The type and function signature of a pong timeout handler.
lib::function< void(connection_hdl)> http_handler
The type and function signature of a http handler.
lib::function< void(connection_hdl)> open_handler
The type and function signature of an open handler.
lib::function< void(connection_hdl)> interrupt_handler
The type and function signature of an interrupt handler.
lib::function< void(connection_hdl)> fail_handler
The type and function signature of a fail handler.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::function< void(lib::error_code const &ec, size_t bytes_transferred)> read_handler
lib::function< bool(connection_hdl, std::string)> ping_handler
The type and function signature of a ping handler.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
lib::function< bool(connection_hdl)> validate_handler
The type and function signature of a validate handler.
lib::function< void(lib::error_code const &ec)> write_frame_handler
static level const devel
Development messages (warning: very chatty)