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;
164 static std::vector<
int>
const versions_supported = {0,7,8,13};
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
245 typedef lib::weak_ptr<
type> weak_ptr;
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,
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)
329 , m_http_state(session::http_state::init)
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();
655 lib::error_code
send(std::string
const & payload,
frame::
opcode::value op =
672 lib::error_code
send(
void const * payload, size_t len,
frame::
opcode::value
688 lib::error_code
send(message_ptr msg);
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();
1043 void set_body(std::string
const & value);
1059 void append_header(std::string
const & key, std::string
const & val);
1074 void replace_header(std::string
const & key, std::string
const & val);
1184 return m_connection_hdl;
1210 session::state::value
get_state()
const;
1218 return m_local_close_code;
1226 return m_local_close_reason;
1234 return m_remote_close_code;
1242 return m_remote_close_reason;
1280 return m_msg_manager->get_message(op, size);
1291 void read_handshake(size_t num_bytes);
1293 void handle_read_handshake(lib::error_code
const & ec,
1294 size_t bytes_transferred);
1295 void handle_read_http_response(lib::error_code
const & ec,
1296 size_t bytes_transferred);
1299 void handle_write_http_response(lib::error_code
const & ec);
1300 void handle_send_http_request(lib::error_code
const & ec);
1302 void handle_open_handshake_timeout(lib::error_code
const & ec);
1303 void handle_close_handshake_timeout(lib::error_code
const & ec);
1305 void handle_read_frame(lib::error_code
const & ec, size_t bytes_transferred);
1315 void terminate(lib::error_code
const & ec);
1316 void handle_terminate(terminate_status tstat, lib::error_code
const & ec);
1357 m_connection_hdl = hdl;
1358 transport_con_type::set_handle(hdl);
1361 void handle_transport_init(lib::error_code
const & ec);
1374 void write_http_response(lib::error_code
const & ec);
1377 void send_http_request();
1380 void write_http_response_error(lib::error_code
const & ec);
1386 void process_control_frame(message_ptr msg);
1419 bool terminal =
false);
1431 processor_ptr get_processor(
int version)
const;
1443 void write_push(message_ptr msg);
1456 message_ptr write_pop();
1464 void log_open_result();
1470 void log_close_result();
1476 void log_fail_result();
1482 void log_http_result();
1485 template <
typename error_type>
1486 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1487 std::stringstream s;
1488 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1489 m_elog->write(l, s.str());
1493 read_handler m_handle_read_frame;
1494 write_frame_handler m_write_frame_handler;
1497 std::string
const m_user_agent;
1512 message_handler m_message_handler;
1515 long m_open_handshake_timeout_dur;
1516 long m_close_handshake_timeout_dur;
1517 long m_pong_timeout_dur;
1518 size_t m_max_message_size;
1524 session::state::value m_state;
1530 istate_type m_internal_state;
1532 mutable mutex_type m_connection_state_lock;
1539 mutex_type m_write_lock;
1542 char m_buf[config::connection_read_buffer_size];
1543 size_t m_buf_cursor;
1544 termination_handler m_termination_handler;
1545 con_msg_manager_ptr m_msg_manager;
1551 std::string m_handshake_buffer;
1562 processor_ptr m_processor;
1568 std::queue<message_ptr> m_send_queue;
1574 size_t m_send_buffer_size;
1580 std::vector<transport::buffer> m_send_buffer;
1584 std::vector<message_ptr> m_current_msgs;
1596 request_type m_request;
1597 response_type m_response;
1599 std::string m_subprotocol;
1603 std::vector<std::string> m_requested_subprotocols;
1605 bool const m_is_server;
1606 const lib::shared_ptr<
alog_type> m_alog;
1607 const lib::shared_ptr<
elog_type> m_elog;
1616 std::string m_local_close_reason;
1622 std::string m_remote_close_reason;
1625 lib::error_code m_ec;
1633 session::http_state::value m_http_state;
1640 #include <websocketpp/impl/connection_impl.hpp>