28 #ifndef WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
29 #define WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
31 #include <websocketpp/transport/asio/base.hpp>
33 #include <websocketpp/transport/base/connection.hpp>
35 #include <websocketpp/logger/levels.hpp>
36 #include <websocketpp/http/constants.hpp>
38 #include <websocketpp/base64/base64.hpp>
39 #include <websocketpp/error.hpp>
41 #include <websocketpp/common/cpp11.hpp>
42 #include <websocketpp/common/memory.hpp>
43 #include <websocketpp/common/functional.hpp>
44 #include <websocketpp/common/connection_hdl.hpp>
46 #include <boost/asio.hpp>
47 #include <boost/system/error_code.hpp>
58 typedef lib::function<void(connection_hdl)> tcp_init_handler;
66 template <
typename config>
67 class connection :
public config::socket_type::socket_con_type {
72 typedef lib::shared_ptr<type>
ptr;
83 typedef typename config::request_type request_type;
84 typedef typename request_type::ptr request_ptr;
85 typedef typename config::response_type response_type;
86 typedef typename response_type::ptr response_ptr;
91 typedef lib::shared_ptr<boost::asio::io_service::strand>
strand_ptr;
93 typedef lib::shared_ptr<boost::asio::deadline_timer>
timer_ptr;
101 explicit connection(
bool is_server, alog_type& alog, elog_type& elog)
102 : m_is_server(is_server)
111 return lib::static_pointer_cast<type>(socket_con_type::get_shared());
114 bool is_secure()
const {
115 return socket_con_type::is_secure();
129 m_tcp_pre_init_handler = h;
158 m_tcp_post_init_handler = h;
177 m_proxy_data = lib::make_shared<proxy_data>();
178 ec = lib::error_code();
202 password, lib::error_code & ec)
210 std::string val =
"Basic "+
base64_encode(username +
":" + password);
211 m_proxy_data->req.replace_header(
"Proxy-Authorization",val);
212 ec = lib::error_code();
240 m_proxy_data->timeout_proxy = duration;
241 ec = lib::error_code();
251 std::string
const & get_proxy()
const {
268 std::string ret = socket_con_type::get_remote_endpoint(ec);
280 return m_connection_hdl;
298 timer_ptr new_timer = lib::make_shared<boost::asio::deadline_timer>(
299 lib::ref(*m_io_service),
300 boost::posix_time::milliseconds(duration)
303 if (config::enable_multithreading) {
304 new_timer->async_wait(m_strand->wrap(lib::bind(
308 lib::placeholders::_1
311 new_timer->async_wait(lib::bind(
315 lib::placeholders::_1
334 boost::system::error_code
const & ec)
337 if (ec == boost::asio::error::operation_aborted) {
344 callback(lib::error_code());
378 m_init_handler = callback;
380 socket_con_type::pre_init(
382 &type::handle_pre_init,
384 lib::placeholders::_1
399 return websocketpp::error::make_error_code(
402 m_proxy_data->req.set_version(
"HTTP/1.1");
403 m_proxy_data->req.set_method(
"CONNECT");
405 m_proxy_data->req.set_uri(authority);
406 m_proxy_data->req.replace_header(
"Host",authority);
408 return lib::error_code();
422 m_io_service = io_service;
424 if (config::enable_multithreading) {
425 m_strand = lib::make_shared<boost::asio::strand>(
426 lib::ref(*io_service));
428 m_async_read_handler = m_strand->wrap(lib::bind(
429 &type::handle_async_read,
get_shared(),lib::placeholders::_1,
430 lib::placeholders::_2));
432 m_async_write_handler = m_strand->wrap(lib::bind(
434 lib::placeholders::_2));
436 m_async_read_handler = lib::bind(&type::handle_async_read,
437 get_shared(), lib::placeholders::_1, lib::placeholders::_2);
440 get_shared(), lib::placeholders::_1, lib::placeholders::_2);
443 lib::error_code ec = socket_con_type::init_asio(io_service, m_strand,
449 lib::clear_function(m_async_read_handler);
450 lib::clear_function(m_async_write_handler);
456 void handle_pre_init(lib::error_code
const & ec) {
461 if (m_tcp_pre_init_handler) {
462 m_tcp_pre_init_handler(m_connection_hdl);
471 if (!m_proxy.empty()) {
483 timer_ptr post_timer;
485 if (config::timeout_socket_post_init > 0) {
487 config::timeout_socket_post_init,
493 lib::placeholders::_1
498 socket_con_type::post_init(
504 lib::placeholders::_1
519 lib::error_code
const & ec)
521 lib::error_code ret_ec;
526 "asio post init timer cancelled");
533 if (socket_con_type::get_ec()) {
534 ret_ec = socket_con_type::get_ec();
541 socket_con_type::cancel_socket();
555 lib::error_code
const & ec)
558 (post_timer && post_timer->expires_from_now().is_negative()))
565 post_timer->cancel();
572 if (m_tcp_post_init_handler) {
573 m_tcp_post_init_handler(m_connection_hdl);
586 "assertion failed: !m_proxy_data in asio::connection::proxy_write");
591 m_proxy_data->write_buf = m_proxy_data->req.raw();
593 m_bufs.push_back(boost::asio::buffer(m_proxy_data->write_buf.data(),
594 m_proxy_data->write_buf.size()));
600 m_proxy_data->timeout_proxy,
602 &type::handle_proxy_timeout,
605 lib::placeholders::_1
610 if (config::enable_multithreading) {
611 boost::asio::async_write(
612 socket_con_type::get_next_layer(),
614 m_strand->wrap(lib::bind(
617 lib::placeholders::_1
621 boost::asio::async_write(
622 socket_con_type::get_next_layer(),
627 lib::placeholders::_1
633 void handle_proxy_timeout(
init_handler callback, lib::error_code
const & ec)
637 "asio handle_proxy_write timer cancelled");
644 "asio handle_proxy_write timer expired");
645 socket_con_type::cancel_socket();
651 boost::system::error_code
const & ec)
655 "asio connection handle_proxy_write");
663 if (ec == boost::asio::error::operation_aborted ||
664 m_proxy_data->timer->expires_from_now().is_negative())
672 m_proxy_data->timer->cancel();
677 proxy_read(callback);
687 "assertion failed: !m_proxy_data in asio::connection::proxy_read");
688 m_proxy_data->timer->cancel();
693 if (config::enable_multithreading) {
694 boost::asio::async_read_until(
695 socket_con_type::get_next_layer(),
696 m_proxy_data->read_buf,
698 m_strand->wrap(lib::bind(
701 lib::placeholders::_1, lib::placeholders::_2
705 boost::asio::async_read_until(
706 socket_con_type::get_next_layer(),
707 m_proxy_data->read_buf,
712 lib::placeholders::_1, lib::placeholders::_2
725 boost::system::error_code
const & ec,
size_t)
729 "asio connection handle_proxy_read");
735 if (ec == boost::asio::error::operation_aborted ||
736 m_proxy_data->timer->expires_from_now().is_negative())
743 m_proxy_data->timer->cancel();
747 "asio handle_proxy_read error: "+ec.message());
752 "assertion failed: !m_proxy_data in asio::connection::handle_proxy_read");
757 std::istream input(&m_proxy_data->read_buf);
759 m_proxy_data->res.consume(input);
761 if (!m_proxy_data->res.headers_ready()) {
770 if (m_proxy_data->res.get_status_code() != http::status_code::ok) {
775 s <<
"Proxy connection error: "
776 << m_proxy_data->res.get_status_code()
778 << m_proxy_data->res.get_status_msg()
794 m_proxy_data.reset();
811 s <<
"asio async_read_at_least: " << num_bytes;
815 if (!m_async_read_handler) {
817 "async_read_at_least called after async_shutdown");
832 m_read_handler = handler;
834 if (!m_read_handler) {
836 "asio con async_read_at_least called with bad handler");
839 boost::asio::async_read(
840 socket_con_type::get_socket(),
841 boost::asio::buffer(buf,len),
842 boost::asio::transfer_at_least(num_bytes),
843 make_custom_alloc_handler(
844 m_read_handler_allocator,
850 void handle_async_read(boost::system::error_code
const & ec,
851 size_t bytes_transferred)
857 if (ec == boost::asio::error::eof) {
862 tec = socket_con_type::translate_ec(ec);
873 if (m_read_handler) {
874 m_read_handler(tec,bytes_transferred);
881 "handle_async_read called with null read handler");
885 void async_write(
const char* buf,
size_t len,
write_handler handler) {
886 if (!m_async_write_handler) {
888 "async_write (single) called after async_shutdown");
893 m_bufs.push_back(boost::asio::buffer(buf,len));
895 m_write_handler = handler;
897 boost::asio::async_write(
898 socket_con_type::get_socket(),
900 make_custom_alloc_handler(
901 m_write_handler_allocator,
902 m_async_write_handler
907 void async_write(std::vector<buffer>
const & bufs,
write_handler handler) {
908 if (!m_async_write_handler) {
910 "async_write (vector) called after async_shutdown");
914 std::vector<buffer>::const_iterator it;
916 for (it = bufs.begin(); it != bufs.end(); ++it) {
917 m_bufs.push_back(boost::asio::buffer((*it).buf,(*it).len));
920 m_write_handler = handler;
922 boost::asio::async_write(
923 socket_con_type::get_socket(),
925 make_custom_alloc_handler(
926 m_write_handler_allocator,
927 m_async_write_handler
944 if (m_write_handler) {
945 m_write_handler(tec);
952 "handle_async_write called with null write handler");
964 m_connection_hdl = hdl;
965 socket_con_type::set_handle(hdl);
973 if (config::enable_multithreading) {
974 m_io_service->post(m_strand->wrap(handler));
976 m_io_service->post(handler);
978 return lib::error_code();
982 if (config::enable_multithreading) {
983 m_io_service->post(m_strand->wrap(handler));
985 m_io_service->post(handler);
987 return lib::error_code();
1003 lib::clear_function(m_async_read_handler);
1004 lib::clear_function(m_async_write_handler);
1005 lib::clear_function(m_init_handler);
1007 lib::clear_function(m_read_handler);
1008 lib::clear_function(m_write_handler);
1010 timer_ptr shutdown_timer;
1012 config::timeout_socket_shutdown,
1018 lib::placeholders::_1
1022 socket_con_type::async_shutdown(
1024 &type::handle_async_shutdown,
1028 lib::placeholders::_1
1040 lib::error_code
const & ec)
1042 lib::error_code ret_ec;
1047 "asio socket shutdown timer cancelled");
1058 "Asio transport socket shutdown timed out");
1059 socket_con_type::cancel_socket();
1064 callback, boost::system::error_code
const & ec)
1066 if (ec == boost::asio::error::operation_aborted ||
1067 shutdown_timer->expires_from_now().is_negative())
1073 shutdown_timer->cancel();
1075 lib::error_code tec;
1077 if (ec == boost::asio::error::not_connected) {
1085 tec = socket_con_type::translate_ec(ec);
1102 "asio con handle_async_shutdown");
1109 template <
typename error_type>
1110 void log_err(log::level l,
const char * msg,
const error_type & ec) {
1111 std::stringstream s;
1112 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1113 m_elog.write(l,s.str());
1117 const bool m_is_server;
1122 proxy_data() : timeout_proxy(config::timeout_proxy) {}
1126 std::string write_buf;
1127 boost::asio::streambuf read_buf;
1132 std::string m_proxy;
1133 lib::shared_ptr<proxy_data> m_proxy_data;
1136 io_service_ptr m_io_service;
1137 strand_ptr m_strand;
1140 std::vector<boost::asio::const_buffer> m_bufs;
1143 tcp_init_handler m_tcp_pre_init_handler;
1144 tcp_init_handler m_tcp_post_init_handler;
1146 handler_allocator m_read_handler_allocator;
1147 handler_allocator m_write_handler_allocator;
1153 async_read_handler m_async_read_handler;
1154 async_write_handler m_async_write_handler;
1162 #endif // WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
config::elog_type elog_type
Type of this transport's error logging policy.
void set_proxy_basic_auth(std::string const &username, std::string const &password, lib::error_code &ec)
Set the basic auth credentials to use (exception free)
void handle_async_write(boost::system::error_code const &ec, size_t)
Async write callback.
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
strand_ptr get_strand()
Get a pointer to this connection's strand.
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
static level const devel
Low level debugging information (warning: very chatty)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
void handle_post_init_timeout(timer_ptr, init_handler callback, lib::error_code const &ec)
Post init timeout callback.
lib::function< void()> interrupt_handler
The type and signature of the callback passed to the interrupt method.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
lib::shared_ptr< boost::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service::strand being used.
std::string base64_encode(unsigned char const *input, size_t len)
Encode a char buffer into a base64 string.
underlying transport pass through
void init(init_handler callback)
Initialize transport for reading.
connection_hdl get_handle() const
Get the connection handle.
void handle_async_shutdown_timeout(timer_ptr, init_handler callback, lib::error_code const &ec)
Async shutdown timeout handler.
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
static level const devel
Development messages (warning: very chatty)
std::string get_remote_endpoint() const
Get the remote endpoint address.
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
void set_proxy(std::string const &uri, lib::error_code &ec)
Set the proxy to connect through (exception free)
config::alog_type alog_type
Type of this transport's access logging policy.
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
The connection was in the wrong state for this operation.
lib::error_code interrupt(interrupt_handler handler)
Trigger the on_interrupt handler.
void handle_proxy_read(init_handler callback, boost::system::error_code const &ec, size_t)
Proxy read callback.
void set_proxy_timeout(long duration, lib::error_code &ec)
Set the proxy timeout duration (exception free)
lib::shared_ptr< boost::asio::deadline_timer > timer_ptr
Type of a pointer to the ASIO timer class.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
there was an error in the underlying transport library
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
Namespace for the WebSocket++ project.
void set_proxy_timeout(long duration)
Set the proxy timeout duration (exception)
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
void handle_timer(timer_ptr, timer_handler callback, boost::system::error_code const &ec)
Timer callback.
Boost Asio based connection transport component.
Boost Asio based endpoint transport component.
The connection to the requested proxy server failed.
ptr get_shared()
Get a shared pointer to this component.
connection< config > type
Type of this connection transport component.
void handle_post_init(timer_ptr post_timer, init_handler callback, lib::error_code const &ec)
Post init timeout callback.
void async_shutdown(shutdown_handler callback)
close and clean up the underlying socket
lib::error_code proxy_init(std::string const &authority)
initialize the proxy buffers and http parsers
read or write after shutdown
lib::error_code init_asio(io_service_ptr io_service)
Finish constructing the transport.
config::socket_type::socket_con_type socket_con_type
Type of the socket connection component.
void set_proxy(std::string const &uri)
Set the proxy to connect through (exception)
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
read at least num_bytes bytes into buf and then call handler.
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
static level const library
void set_handle(connection_hdl hdl)
Set Connection Handle.
void set_proxy_basic_auth(std::string const &username, std::string const &password)
Set the basic auth credentials to use (exception)