WebSocket++
0.8.3-dev
C++ websocket client/server library
|
28 #ifndef WEBSOCKETPP_CONNECTION_HPP
29 #define WEBSOCKETPP_CONNECTION_HPP
31 #include <websocketpp/close.hpp>
32 #include <websocketpp/error.hpp>
33 #include <websocketpp/frame.hpp>
35 #include <websocketpp/logger/levels.hpp>
36 #include <websocketpp/processors/processor.hpp>
37 #include <websocketpp/transport/base/connection.hpp>
38 #include <websocketpp/http/constants.hpp>
40 #include <websocketpp/common/connection_hdl.hpp>
41 #include <websocketpp/common/cpp11.hpp>
42 #include <websocketpp/common/functional.hpp>
154 typedef lib::function<
void(lib::error_code
const & ec, size_t bytes_transferred)> read_handler;
155 typedef lib::function<
void(lib::error_code
const & ec)> write_frame_handler;
158 #ifdef _WEBSOCKETPP_INITIALIZER_LISTS_
201 namespace internal_state {
209 READ_HTTP_REQUEST = 2,
210 WRITE_HTTP_REQUEST = 3,
211 READ_HTTP_RESPONSE = 4,
212 WRITE_HTTP_RESPONSE = 5,
213 PROCESS_HTTP_REQUEST = 6,
214 PROCESS_CONNECTION = 7
219 namespace http_state {
234 template <
typename config>
236 :
public config::transport_type::transport_con_type
237 ,
public config::connection_base
260 typedef lib::function<
void(ptr)> termination_handler;
265 typedef typename config::request_type request_type;
266 typedef typename config::response_type response_type;
268 typedef typename config::message_type message_type;
269 typedef typename message_type::ptr message_ptr;
271 typedef typename config::con_msg_manager_type con_msg_manager_type;
272 typedef typename con_msg_manager_type::ptr con_msg_manager_ptr;
278 typedef lib::shared_ptr<processor_type> processor_ptr;
281 typedef lib::function<
void(connection_hdl,message_ptr)> message_handler;
287 typedef session::internal_state::value istate_type;
290 enum terminate_status {
297 explicit connection(
bool p_is_server, std::string
const & ua,
const lib::shared_ptr<alog_type>& alog,
298 const lib::shared_ptr<elog_type>& elog, rng_type & rng)
299 : transport_con_type(p_is_server, alog, elog)
300 , m_handle_read_frame(lib::bind(
301 &type::handle_read_frame,
303 lib::placeholders::_1,
304 lib::placeholders::_2
306 , m_write_frame_handler(lib::bind(
307 &type::handle_write_frame,
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)
316 , m_state(session::state::connecting)
317 , m_internal_state(session::internal_state::USER_INIT)
318 , m_msg_manager(
new con_msg_manager_type())
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)
329 , m_http_state(session::http_state::init)
332 m_alog->write(log::alevel::devel,
"connection constructor");
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);
636 return get_buffered_amount();
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);
800 void close(close::status::value
const code, std::string
const & reason);
803 void close(close::status::value
const code, std::string
const & reason,
804 lib::error_code & ec);
898 void add_subprotocol(std::string
const & request, lib::error_code & ec);
982 return m_response.get_status_code();
994 return m_response.get_status_msg();
1015 void set_status(http::status_code::value code, lib::error_code & ec);
1032 void set_status(http::status_code::value code);
1051 void set_status(http::status_code::value code, std::string
const & msg,
1052 lib::error_code & ec);
1069 void set_status(http::status_code::value code, std::string
const & msg);
1088 void set_body(std::string
const & value, lib::error_code & ec);
1106 void set_body(std::string
const & value);
1129 void append_header(std::string
const & key, std::string
const & val,
1130 lib::error_code & ec);
1151 void append_header(std::string
const & key, std::string
const & val);
1172 lib::error_code & ec);
1192 void replace_header(std::string
const & key, std::string
const & val);
1321 return m_connection_hdl;
1347 session::state::value
get_state()
const;
1430 void handle_read_handshake(lib::error_code
const & ec,
1431 size_t bytes_transferred);
1432 void handle_read_http_response(lib::error_code
const & ec,
1433 size_t bytes_transferred);
1436 void handle_write_http_response(lib::error_code
const & ec);
1437 void handle_send_http_request(lib::error_code
const & ec);
1439 void handle_open_handshake_timeout(lib::error_code
const & ec);
1440 void handle_close_handshake_timeout(lib::error_code
const & ec);
1442 void handle_read_frame(lib::error_code
const & ec, size_t bytes_transferred);
1452 void terminate(lib::error_code
const & ec);
1453 void handle_terminate(terminate_status tstat, lib::error_code
const & ec);
1494 m_connection_hdl = hdl;
1495 transport_con_type::set_handle(hdl);
1498 void handle_transport_init(lib::error_code
const & ec);
1511 void write_http_response(lib::error_code
const & ec);
1514 void send_http_request();
1517 void write_http_response_error(lib::error_code
const & ec);
1523 void process_control_frame(message_ptr msg);
1536 lib::error_code send_close_ack(close::status::value code =
1537 close::status::blank, std::string
const & reason = std::string());
1554 lib::error_code send_close_frame(close::status::value code =
1555 close::status::blank, std::string
const & reason = std::string(),
bool ack =
false,
1556 bool terminal =
false);
1568 processor_ptr get_processor(
int version)
const;
1580 void write_push(message_ptr msg);
1593 message_ptr write_pop();
1601 void log_open_result();
1607 void log_close_result();
1613 void log_fail_result();
1619 void log_http_result();
1622 template <
typename error_type>
1623 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1624 std::stringstream s;
1625 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1626 m_elog->write(l, s.str());
1630 read_handler m_handle_read_frame;
1631 write_frame_handler m_write_frame_handler;
1634 std::string
const m_user_agent;
1637 connection_hdl m_connection_hdl;
1649 message_handler m_message_handler;
1652 long m_open_handshake_timeout_dur;
1653 long m_close_handshake_timeout_dur;
1654 long m_pong_timeout_dur;
1655 size_t m_max_message_size;
1661 session::state::value m_state;
1667 istate_type m_internal_state;
1669 mutable mutex_type m_connection_state_lock;
1676 mutex_type m_write_lock;
1679 char m_buf[config::connection_read_buffer_size];
1680 size_t m_buf_cursor;
1681 termination_handler m_termination_handler;
1682 con_msg_manager_ptr m_msg_manager;
1688 std::string m_handshake_buffer;
1699 processor_ptr m_processor;
1711 size_t m_send_buffer_size;
1733 request_type m_request;
1734 response_type m_response;
1736 std::string m_subprotocol;
1742 bool const m_is_server;
1753 std::string m_local_close_reason;
1759 std::string m_remote_close_reason;
1762 lib::error_code m_ec;
1770 session::http_state::value m_http_state;
1777 #include <websocketpp/impl/connection_impl.hpp>
void set_status(http::status_code::value code)
Set response status code and message (exception)
lib::error_code send(message_ptr msg)
Add a message to the outgoing send queue.
void handle_pause_reading()
Pause reading callback.
config::concurrency_type concurrency_type
Type of the concurrency component of this connection.
void set_validate_handler(validate_handler h)
Set validate handler.
lib::function< void(connection_hdl)> http_handler
The type and function signature of a http handler.
lib::error_code initialize_processor()
void set_ping_handler(ping_handler h)
Set ping handler.
lib::function< bool(connection_hdl)> validate_handler
The type and function signature of a validate handler.
void set_pong_timeout_handler(pong_timeout_handler h)
Set pong timeout handler.
void set_close_handshake_timeout(long dur)
Set close handshake timeout.
std::string const & get_resource() const
Returns the resource component of the connection URI.
void set_handle(connection_hdl hdl)
Set Connection Handle.
std::string const & get_request_header(std::string const &key) const
Retrieve a request header.
void handle_interrupt()
Transport inturrupt callback.
void set_status(http::status_code::value code, std::string const &msg, lib::error_code &ec)
Set response status code and message (exception free)
config::rng_type rng_type
Type of RNG.
void append_header(std::string const &key, std::string const &val)
Append a header (exception)
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.
config::transport_type::transport_con_type transport_con_type
Type of the transport component of this connection.
void remove_header(std::string const &key, lib::error_code &ec)
Remove a header (exception free)
bool is_server() const
Get whether or not this connection is part of a server or client.
void set_message_handler(message_handler h)
Set message handler.
void handle_write_frame(lib::error_code const &ec)
Process the results of a frame write operation and start the next write.
void set_max_http_body_size(size_t new_value)
Set maximum HTTP message body size.
lib::function< void(connection_hdl, std::string)> pong_timeout_handler
The type and function signature of a pong timeout handler.
void set_body(std::string const &value, lib::error_code &ec)
Set response body content (exception free)
std::vector< std::string > const & get_requested_subprotocols() const
Gets all of the subprotocols requested by the client.
lib::function< void(connection_hdl)> interrupt_handler
The type and function signature of an interrupt handler.
session::state::value get_state() const
Return the connection state.
size_t get_max_message_size() const
Get maximum message size.
std::vector< int > const & get_supported_versions() const
Get array of WebSocket protocol versions that this connection supports.
void set_status(http::status_code::value code, lib::error_code &ec)
Set response status code and message (exception free)
connection_hdl get_handle() const
Get Connection Handle.
std::string const & get_origin() const
Return the same origin policy origin value from the opening request.
bool get_secure() const
Returns the secure flag from the connection URI.
void send_http_response(lib::error_code &ec)
Send deferred HTTP Response (exception free)
lib::error_code interrupt()
Asyncronously invoke handler::on_inturrupt.
transport_con_type::timer_ptr timer_ptr
Type of a pointer to a transport timer handle.
lib::function< void(connection_hdl)> fail_handler
The type and function signature of a fail handler.
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
std::string const & get_response_msg() const
Get response HTTP status message.
void set_open_handler(open_handler h)
Set open handler.
void remove_header(std::string const &key)
Remove a header (exception)
uint16_t get_port() const
Returns the port component of the connection URI.
http::status_code::value get_response_code() const
Get response HTTP status code.
void set_body(std::string const &value)
Set response body content (exception)
std::string const & get_request_body() const
Retrieve a request body.
void add_subprotocol(std::string const &request, lib::error_code &ec)
Adds the given subprotocol string to the request list (exception free)
lib::error_code process_handshake_request()
void set_open_handshake_timeout(long dur)
Set open handshake timeout.
request_type const & get_request() const
Get request object.
void select_subprotocol(std::string const &value)
Select a subprotocol to use (exception)
void set_pong_timeout(long dur)
Set pong timeout.
ptr get_shared()
Get a shared pointer to this component.
void set_uri(uri_ptr uri)
Sets the connection URI.
response_type const & get_response() const
Get response object.
void select_subprotocol(std::string const &value, lib::error_code &ec)
Select a subprotocol to use (exception free)
void set_close_handler(close_handler h)
Set close handler.
std::string const & get_response_header(std::string const &key) const
Retrieve a response header.
lib::error_code send(void const *payload, size_t len, frame::opcode::value op=frame::opcode::binary)
Send a message (raw array overload)
lib::function< bool(connection_hdl, std::string)> ping_handler
The type and function signature of a ping handler.
config::elog_type elog_type
Type of the error logging policy.
lib::function< void(connection_hdl, std::string)> pong_handler
The type and function signature of a pong handler.
uri_ptr get_uri() const
Gets the connection URI.
lib::function< void(connection_hdl)> close_handler
The type and function signature of a close handler.
void pong(std::string const &payload, lib::error_code &ec)
exception free variant of pong
void add_subprotocol(std::string const &request)
Adds the given subprotocol string to the request list (exception)
void set_fail_handler(fail_handler h)
Set fail handler.
void set_termination_handler(termination_handler new_handler)
void read_frame()
Issue a new transport read unless reading is paused.
connection< config > type
Type of this connection.
void append_header(std::string const &key, std::string const &val, lib::error_code &ec)
Append a header (exception free)
std::string const & get_host() const
Returns the host component of the connection URI.
void set_status(http::status_code::value code, std::string const &msg)
Set response status code and message (exception)
void set_http_handler(http_handler h)
Set http handler.
size_t get_max_http_body_size() const
Get maximum HTTP message body size.
void replace_header(std::string const &key, std::string const &val, lib::error_code &ec)
Replace a header (exception free)
lib::error_code pause_reading()
Pause reading of new data.
void start()
Start the connection state machine.
void send_http_response()
Send deferred HTTP Response.
size_t buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
void pong(std::string const &payload)
Send a pong.
lib::function< void(connection_hdl)> open_handler
The type and function signature of an open handler.
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_resume_reading()
Resume reading callback.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the transport component of this connection.
void close(close::status::value const code, std::string const &reason, lib::error_code &ec)
exception free variant of close
config::alog_type alog_type
Type of the access logging policy.
void replace_header(std::string const &key, std::string const &val)
Replace a header (exception)
void ping(std::string const &payload)
Send a ping.
void write_frame()
Checks if there are frames in the send queue and if there are sends one.
std::string const & get_subprotocol() const
Gets the negotated subprotocol.
void ping(std::string const &payload, lib::error_code &ec)
exception free variant of ping
void set_pong_handler(pong_handler h)
Set pong handler.
void set_max_message_size(size_t new_value)
Set maximum message size.
size_t get_buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
static int const helper[]
Helper array to get around lack of initializer lists pre C++11.
lib::error_code resume_reading()
Resume reading of new data.
void set_interrupt_handler(interrupt_handler h)
Set interrupt handler.
void close(close::status::value const code, std::string const &reason)
Close the connection.
lib::error_code defer_http_response()
Defer HTTP Response until later (Exception free)