28 #ifndef WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
29 #define WEBSOCKETPP_TRANSPORT_ASIO_CON_HPP
31 #include <websocketpp/common/cpp11.hpp>
32 #include <websocketpp/common/memory.hpp>
33 #include <websocketpp/common/functional.hpp>
34 #include <websocketpp/common/connection_hdl.hpp>
35 #include <websocketpp/logger/levels.hpp>
36 #include <websocketpp/http/constants.hpp>
37 #include <websocketpp/transport/asio/base.hpp>
38 #include <websocketpp/transport/base/connection.hpp>
40 #include <websocketpp/base64/base64.hpp>
41 #include <websocketpp/error.hpp>
43 #include <boost/asio.hpp>
44 #include <boost/system/error_code.hpp>
53 typedef lib::function<void(connection_hdl)> tcp_init_handler;
61 template <
typename config>
62 class connection :
public config::socket_type::socket_con_type {
67 typedef lib::shared_ptr<type>
ptr;
78 typedef typename config::request_type request_type;
79 typedef typename request_type::ptr request_ptr;
80 typedef typename config::response_type response_type;
81 typedef typename response_type::ptr response_ptr;
86 typedef lib::shared_ptr<boost::asio::io_service::strand>
strand_ptr;
88 typedef lib::shared_ptr<boost::asio::deadline_timer>
timer_ptr;
96 explicit connection(
bool is_server, alog_type& alog, elog_type& elog)
97 : m_is_server(is_server)
106 return lib::static_pointer_cast<type>(socket_con_type::get_shared());
109 bool is_secure()
const {
110 return socket_con_type::is_secure();
124 m_tcp_pre_init_handler = h;
153 m_tcp_post_init_handler = h;
172 m_proxy_data = lib::make_shared<proxy_data>();
173 ec = lib::error_code();
197 password, lib::error_code & ec)
205 std::string val =
"Basic "+
base64_encode(username +
":" + password);
206 m_proxy_data->req.replace_header(
"Proxy-Authorization",val);
207 ec = lib::error_code();
235 m_proxy_data->timeout_proxy = duration;
236 ec = lib::error_code();
246 const std::string & get_proxy()
const {
263 std::string ret = socket_con_type::get_remote_endpoint(ec);
275 return m_connection_hdl;
293 timer_ptr new_timer = lib::make_shared<boost::asio::deadline_timer>(
294 lib::ref(*m_io_service),
295 boost::posix_time::milliseconds(duration)
298 if (config::enable_multithreading) {
299 new_timer->async_wait(m_strand->wrap(lib::bind(
303 lib::placeholders::_1
306 new_timer->async_wait(lib::bind(
310 lib::placeholders::_1
329 boost::system::error_code
const & ec)
332 if (ec == boost::asio::error::operation_aborted) {
339 callback(lib::error_code());
373 m_init_handler = callback;
375 socket_con_type::pre_init(
377 &type::handle_pre_init,
379 lib::placeholders::_1
394 return websocketpp::error::make_error_code(
397 m_proxy_data->req.set_version(
"HTTP/1.1");
398 m_proxy_data->req.set_method(
"CONNECT");
400 m_proxy_data->req.set_uri(authority);
401 m_proxy_data->req.replace_header(
"Host",authority);
403 return lib::error_code();
417 m_io_service = io_service;
419 if (config::enable_multithreading) {
420 m_strand = lib::make_shared<boost::asio::strand>(
421 lib::ref(*io_service));
423 m_async_read_handler = m_strand->wrap(lib::bind(
424 &type::handle_async_read,
get_shared(),lib::placeholders::_1,
425 lib::placeholders::_2));
427 m_async_write_handler = m_strand->wrap(lib::bind(
429 lib::placeholders::_2));
431 m_async_read_handler = lib::bind(&type::handle_async_read,
432 get_shared(), lib::placeholders::_1, lib::placeholders::_2);
435 get_shared(), lib::placeholders::_1, lib::placeholders::_2);
438 lib::error_code ec = socket_con_type::init_asio(io_service, m_strand,
444 lib::clear_function(m_async_read_handler);
445 lib::clear_function(m_async_write_handler);
451 void handle_pre_init(lib::error_code
const & ec) {
456 if (m_tcp_pre_init_handler) {
457 m_tcp_pre_init_handler(m_connection_hdl);
466 if (!m_proxy.empty()) {
478 timer_ptr post_timer;
480 if (config::timeout_socket_post_init > 0) {
482 config::timeout_socket_post_init,
488 lib::placeholders::_1
493 socket_con_type::post_init(
499 lib::placeholders::_1
514 lib::error_code
const & ec)
516 lib::error_code ret_ec;
521 "asio post init timer cancelled");
528 if (socket_con_type::get_ec()) {
529 ret_ec = socket_con_type::get_ec();
536 socket_con_type::cancel_socket();
550 lib::error_code
const & ec)
553 (post_timer && post_timer->expires_from_now().is_negative()))
560 post_timer->cancel();
567 if (m_tcp_post_init_handler) {
568 m_tcp_post_init_handler(m_connection_hdl);
581 "assertion failed: !m_proxy_data in asio::connection::proxy_write");
586 m_proxy_data->write_buf = m_proxy_data->req.raw();
588 m_bufs.push_back(boost::asio::buffer(m_proxy_data->write_buf.data(),
589 m_proxy_data->write_buf.size()));
595 m_proxy_data->timeout_proxy,
597 &type::handle_proxy_timeout,
600 lib::placeholders::_1
605 if (config::enable_multithreading) {
606 boost::asio::async_write(
607 socket_con_type::get_next_layer(),
609 m_strand->wrap(lib::bind(
612 lib::placeholders::_1
616 boost::asio::async_write(
617 socket_con_type::get_next_layer(),
622 lib::placeholders::_1
628 void handle_proxy_timeout(
init_handler callback, lib::error_code
const & ec)
632 "asio handle_proxy_write timer cancelled");
639 "asio handle_proxy_write timer expired");
640 socket_con_type::cancel_socket();
646 boost::system::error_code
const & ec)
650 "asio connection handle_proxy_write");
658 if (ec == boost::asio::error::operation_aborted ||
659 m_proxy_data->timer->expires_from_now().is_negative())
667 m_proxy_data->timer->cancel();
672 proxy_read(callback);
682 "assertion failed: !m_proxy_data in asio::connection::proxy_read");
683 m_proxy_data->timer->cancel();
688 if (config::enable_multithreading) {
689 boost::asio::async_read_until(
690 socket_con_type::get_next_layer(),
691 m_proxy_data->read_buf,
693 m_strand->wrap(lib::bind(
696 lib::placeholders::_1, lib::placeholders::_2
700 boost::asio::async_read_until(
701 socket_con_type::get_next_layer(),
702 m_proxy_data->read_buf,
707 lib::placeholders::_1, lib::placeholders::_2
720 boost::system::error_code
const & ec,
size_t)
724 "asio connection handle_proxy_read");
730 if (ec == boost::asio::error::operation_aborted ||
731 m_proxy_data->timer->expires_from_now().is_negative())
738 m_proxy_data->timer->cancel();
742 "asio handle_proxy_read error: "+ec.message());
747 "assertion failed: !m_proxy_data in asio::connection::handle_proxy_read");
752 std::istream input(&m_proxy_data->read_buf);
754 m_proxy_data->res.consume(input);
756 if (!m_proxy_data->res.headers_ready()) {
765 if (m_proxy_data->res.get_status_code() != http::status_code::ok) {
770 s <<
"Proxy connection error: "
771 << m_proxy_data->res.get_status_code()
773 << m_proxy_data->res.get_status_msg()
789 m_proxy_data.reset();
806 s <<
"asio async_read_at_least: " << num_bytes;
810 if (!m_async_read_handler) {
812 "async_read_at_least called after async_shutdown");
827 m_read_handler = handler;
829 if (!m_read_handler) {
831 "asio con async_read_at_least called with bad handler");
834 boost::asio::async_read(
835 socket_con_type::get_socket(),
836 boost::asio::buffer(buf,len),
837 boost::asio::transfer_at_least(num_bytes),
838 make_custom_alloc_handler(
839 m_read_handler_allocator,
845 void handle_async_read(boost::system::error_code
const & ec,
846 size_t bytes_transferred)
852 if (ec == boost::asio::error::eof) {
857 tec = socket_con_type::translate_ec(ec);
868 if (m_read_handler) {
869 m_read_handler(tec,bytes_transferred);
876 "handle_async_read called with null read handler");
880 void async_write(
const char* buf,
size_t len,
write_handler handler) {
881 if (!m_async_write_handler) {
883 "async_write (single) called after async_shutdown");
888 m_bufs.push_back(boost::asio::buffer(buf,len));
890 m_write_handler = handler;
892 boost::asio::async_write(
893 socket_con_type::get_socket(),
895 make_custom_alloc_handler(
896 m_write_handler_allocator,
897 m_async_write_handler
902 void async_write(
const std::vector<buffer>& bufs,
write_handler handler) {
903 if (!m_async_write_handler) {
905 "async_write (vector) called after async_shutdown");
909 std::vector<buffer>::const_iterator it;
911 for (it = bufs.begin(); it != bufs.end(); ++it) {
912 m_bufs.push_back(boost::asio::buffer((*it).buf,(*it).len));
915 m_write_handler = handler;
917 boost::asio::async_write(
918 socket_con_type::get_socket(),
920 make_custom_alloc_handler(
921 m_write_handler_allocator,
922 m_async_write_handler
939 if (m_write_handler) {
940 m_write_handler(tec);
947 "handle_async_write called with null write handler");
959 m_connection_hdl = hdl;
960 socket_con_type::set_handle(hdl);
968 if (config::enable_multithreading) {
969 m_io_service->post(m_strand->wrap(handler));
971 m_io_service->post(handler);
973 return lib::error_code();
977 if (config::enable_multithreading) {
978 m_io_service->post(m_strand->wrap(handler));
980 m_io_service->post(handler);
982 return lib::error_code();
998 lib::clear_function(m_async_read_handler);
999 lib::clear_function(m_async_write_handler);
1000 lib::clear_function(m_init_handler);
1002 lib::clear_function(m_read_handler);
1003 lib::clear_function(m_write_handler);
1005 timer_ptr shutdown_timer;
1007 config::timeout_socket_shutdown,
1013 lib::placeholders::_1
1017 socket_con_type::async_shutdown(
1019 &type::handle_async_shutdown,
1023 lib::placeholders::_1
1035 lib::error_code
const & ec)
1037 lib::error_code ret_ec;
1042 "asio socket shutdown timer cancelled");
1053 "Asio transport socket shutdown timed out");
1054 socket_con_type::cancel_socket();
1059 callback, boost::system::error_code
const & ec)
1061 if (ec == boost::asio::error::operation_aborted ||
1062 shutdown_timer->expires_from_now().is_negative())
1068 shutdown_timer->cancel();
1070 lib::error_code tec;
1072 if (ec == boost::asio::error::not_connected) {
1080 tec = socket_con_type::translate_ec(ec);
1097 "asio con handle_async_shutdown");
1104 template <
typename error_type>
1105 void log_err(log::level l,
const char * msg,
const error_type & ec) {
1106 std::stringstream s;
1107 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1108 m_elog.write(l,s.str());
1112 const bool m_is_server;
1117 proxy_data() : timeout_proxy(config::timeout_proxy) {}
1121 std::string write_buf;
1122 boost::asio::streambuf read_buf;
1127 std::string m_proxy;
1128 lib::shared_ptr<proxy_data> m_proxy_data;
1131 io_service_ptr m_io_service;
1132 strand_ptr m_strand;
1135 std::vector<boost::asio::const_buffer> m_bufs;
1138 tcp_init_handler m_tcp_pre_init_handler;
1139 tcp_init_handler m_tcp_post_init_handler;
1141 handler_allocator m_read_handler_allocator;
1142 handler_allocator m_write_handler_allocator;
1148 async_read_handler m_async_read_handler;
1149 async_write_handler m_async_write_handler;
1157 #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)