Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocketpp::transport::asio::connection< config > Class Template Reference

Asio based connection transport component. More...

#include <connection.hpp>

Inheritance diagram for websocketpp::transport::asio::connection< config >:
Collaboration diagram for websocketpp::transport::asio::connection< config >:

Public Types

typedef connection< configtype
 Type of this connection transport component.
 
typedef lib::shared_ptr< typeptr
 Type of a shared pointer to this connection transport component.
 
typedef config::socket_type::socket_con_type socket_con_type
 Type of the socket connection component.
 
typedef socket_con_type::ptr socket_con_ptr
 Type of a shared pointer to the socket connection component.
 
typedef config::alog_type alog_type
 Type of this transport's access logging policy.
 
typedef config::elog_type elog_type
 Type of this transport's error logging policy.
 
typedef config::request_type request_type
 
typedef request_type::ptr request_ptr
 
typedef config::response_type response_type
 
typedef response_type::ptr response_ptr
 
typedef lib::asio::io_service * io_service_ptr
 Type of a pointer to the Asio io_service being used.
 
typedef lib::shared_ptr< lib::asio::io_service::strand > strand_ptr
 Type of a pointer to the Asio io_service::strand being used.
 
typedef lib::shared_ptr< lib::asio::steady_timertimer_ptr
 Type of a pointer to the Asio timer class.
 

Public Member Functions

 connection (bool is_server, alog_type &alog, elog_type &elog)
 
ptr get_shared ()
 Get a shared pointer to this component.
 
bool is_secure () const
 
void set_uri (uri_ptr u)
 Set uri hook.
 
void set_tcp_pre_init_handler (tcp_init_handler h)
 Sets the tcp pre init handler.
 
void set_tcp_init_handler (tcp_init_handler h)
 Sets the tcp pre init handler (deprecated)
 
void set_tcp_post_init_handler (tcp_init_handler h)
 Sets the tcp post init handler.
 
void set_proxy (std::string const &uri, lib::error_code &ec)
 Set the proxy to connect through (exception free)
 
void set_proxy (std::string const &uri)
 Set the proxy to connect through (exception)
 
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 set_proxy_basic_auth (std::string const &username, std::string const &password)
 Set the basic auth credentials to use (exception)
 
void set_proxy_timeout (long duration, lib::error_code &ec)
 Set the proxy timeout duration (exception free)
 
void set_proxy_timeout (long duration)
 Set the proxy timeout duration (exception)
 
std::string const & get_proxy () const
 
std::string get_remote_endpoint () const
 Get the remote endpoint address.
 
connection_hdl get_handle () const
 Get the connection handle.
 
timer_ptr set_timer (long duration, timer_handler callback)
 Call back a function after a period of time.
 
void handle_timer (timer_ptr, timer_handler callback, lib::asio::error_code const &ec)
 Timer callback.
 
strand_ptr get_strand ()
 Get a pointer to this connection's strand.
 
lib::asio::error_code get_transport_ec () const
 Get the internal transport error code for a closed/failed connection.
 

Protected Member Functions

void init (init_handler callback)
 Initialize transport for reading.
 
lib::error_code proxy_init (std::string const &authority)
 initialize the proxy buffers and http parsers
 
lib::error_code init_asio (io_service_ptr io_service)
 Finish constructing the transport.
 
void handle_pre_init (init_handler callback, lib::error_code const &ec)
 
void post_init (init_handler callback)
 
void handle_post_init_timeout (timer_ptr, init_handler callback, lib::error_code const &ec)
 Post init timeout callback.
 
void handle_post_init (timer_ptr post_timer, init_handler callback, lib::error_code const &ec)
 Post init timeout callback.
 
void proxy_write (init_handler callback)
 
void handle_proxy_timeout (init_handler callback, lib::error_code const &ec)
 
void handle_proxy_write (init_handler callback, lib::asio::error_code const &ec)
 
void proxy_read (init_handler callback)
 
void handle_proxy_read (init_handler callback, lib::asio::error_code const &ec, size_t)
 Proxy read callback.
 
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 handle_async_read (read_handler handler, lib::asio::error_code const &ec, size_t bytes_transferred)
 
void async_write (const char *buf, size_t len, write_handler handler)
 Initiate a potentially asyncronous write of the given buffer.
 
void async_write (std::vector< buffer > const &bufs, write_handler handler)
 Initiate a potentially asyncronous write of the given buffers.
 
void handle_async_write (write_handler handler, lib::asio::error_code const &ec, size_t)
 Async write callback.
 
void set_handle (connection_hdl hdl)
 Set Connection Handle.
 
lib::error_code interrupt (interrupt_handler handler)
 Trigger the on_interrupt handler.
 
lib::error_code dispatch (dispatch_handler handler)
 
void async_shutdown (shutdown_handler callback)
 close and clean up the underlying socket
 
void handle_async_shutdown_timeout (timer_ptr, init_handler callback, lib::error_code const &ec)
 Async shutdown timeout handler.
 
void handle_async_shutdown (timer_ptr shutdown_timer, shutdown_handler callback, lib::asio::error_code const &ec)
 
void cancel_socket_checked ()
 Cancel the underlying socket and log any errors.
 

Friends

class endpoint< config >
 
template<typename T >
class local_endpoint
 

Detailed Description

template<typename config>
class websocketpp::transport::asio::connection< config >

transport::asio::connection implements a connection transport component using Asio that works with the transport::asio::endpoint endpoint transport component.

Definition at line 67 of file connection.hpp.

Member Typedef Documentation

◆ alog_type

template<typename config >
config::alog_type websocketpp::transport::asio::connection< config >::alog_type

Definition at line 79 of file connection.hpp.

◆ elog_type

template<typename config >
config::elog_type websocketpp::transport::asio::connection< config >::elog_type

Definition at line 81 of file connection.hpp.

◆ io_service_ptr

template<typename config >
lib::asio::io_service* websocketpp::transport::asio::connection< config >::io_service_ptr

Definition at line 89 of file connection.hpp.

◆ ptr

template<typename config >
lib::shared_ptr<type> websocketpp::transport::asio::connection< config >::ptr

Definition at line 72 of file connection.hpp.

◆ request_ptr

template<typename config >
request_type::ptr websocketpp::transport::asio::connection< config >::request_ptr

Definition at line 84 of file connection.hpp.

◆ request_type

template<typename config >
config::request_type websocketpp::transport::asio::connection< config >::request_type

Definition at line 83 of file connection.hpp.

◆ response_ptr

template<typename config >
response_type::ptr websocketpp::transport::asio::connection< config >::response_ptr

Definition at line 86 of file connection.hpp.

◆ response_type

template<typename config >
config::response_type websocketpp::transport::asio::connection< config >::response_type

Definition at line 85 of file connection.hpp.

◆ socket_con_ptr

template<typename config >
socket_con_type::ptr websocketpp::transport::asio::connection< config >::socket_con_ptr

Definition at line 77 of file connection.hpp.

◆ socket_con_type

template<typename config >
config::socket_type::socket_con_type websocketpp::transport::asio::connection< config >::socket_con_type

Definition at line 75 of file connection.hpp.

◆ strand_ptr

template<typename config >
lib::shared_ptr<lib::asio::io_service::strand> websocketpp::transport::asio::connection< config >::strand_ptr

Definition at line 91 of file connection.hpp.

◆ timer_ptr

template<typename config >
lib::shared_ptr<lib::asio::steady_timer> websocketpp::transport::asio::connection< config >::timer_ptr

Definition at line 93 of file connection.hpp.

◆ type

Definition at line 70 of file connection.hpp.

Constructor & Destructor Documentation

◆ connection()

template<typename config >
websocketpp::transport::asio::connection< config >::connection ( bool is_server,
alog_type & alog,
elog_type & elog )
inlineexplicit

Definition at line 102 of file connection.hpp.

103 : m_is_server(is_server)
104 , m_alog(alog)
105 , m_elog(elog)
106 {
107 m_alog.write(log::alevel::devel,"asio con transport constructor");
108 }
#define elog(FORMAT,...)
Definition logger.hpp:130
static level const devel
Development messages (warning: very chatty)
Definition levels.hpp:141

Member Function Documentation

◆ async_read_at_least()

template<typename config >
void websocketpp::transport::asio::connection< config >::async_read_at_least ( size_t num_bytes,
char * buf,
size_t len,
read_handler handler )
inlineprotected

Definition at line 820 of file connection.hpp.

822 {
823 if (m_alog.static_test(log::alevel::devel)) {
824 std::stringstream s;
825 s << "asio async_read_at_least: " << num_bytes;
826 m_alog.write(log::alevel::devel,s.str());
827 }
828
829 // TODO: safety vs speed ?
830 // maybe move into an if devel block
831 /*if (num_bytes > len) {
832 m_elog.write(log::elevel::devel,
833 "asio async_read_at_least error::invalid_num_bytes");
834 handler(make_error_code(transport::error::invalid_num_bytes),
835 size_t(0));
836 return;
837 }*/
838
839 if (config::enable_multithreading) {
840 lib::asio::async_read(
841 socket_con_type::get_socket(),
842 lib::asio::buffer(buf,len),
843 lib::asio::transfer_at_least(num_bytes),
844 m_strand->wrap(make_custom_alloc_handler(
845 m_read_handler_allocator,
846 lib::bind(
848 handler,
849 lib::placeholders::_1, lib::placeholders::_2
850 )
851 ))
852 );
853 } else {
854 lib::asio::async_read(
855 socket_con_type::get_socket(),
856 lib::asio::buffer(buf,len),
857 lib::asio::transfer_at_least(num_bytes),
859 m_read_handler_allocator,
860 lib::bind(
862 handler,
863 lib::placeholders::_1, lib::placeholders::_2
864 )
865 )
866 );
867 }
868
869 }
void handle_async_read(read_handler handler, lib::asio::error_code const &ec, size_t bytes_transferred)
ptr get_shared()
Get a shared pointer to this component.
custom_alloc_handler< Handler > make_custom_alloc_handler(handler_allocator &a, Handler h)
Definition base.hpp:128
char * s
size_t len
uint8_t buf[2048]
Here is the call graph for this function:

◆ async_shutdown()

template<typename config >
void websocketpp::transport::asio::connection< config >::async_shutdown ( shutdown_handler callback)
inlineprotected

Definition at line 1036 of file connection.hpp.

1036 {
1037 if (m_alog.static_test(log::alevel::devel)) {
1038 m_alog.write(log::alevel::devel,"asio connection async_shutdown");
1039 }
1040
1041 timer_ptr shutdown_timer;
1042 shutdown_timer = set_timer(
1043 config::timeout_socket_shutdown,
1044 lib::bind(
1046 get_shared(),
1047 shutdown_timer,
1048 callback,
1049 lib::placeholders::_1
1050 )
1051 );
1052
1053 socket_con_type::async_shutdown(
1054 lib::bind(
1056 get_shared(),
1057 shutdown_timer,
1058 callback,
1059 lib::placeholders::_1
1060 )
1061 );
1062 }
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of a pointer to the Asio timer class.
void handle_async_shutdown_timeout(timer_ptr, init_handler callback, lib::error_code const &ec)
Async shutdown timeout handler.
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
void handle_async_shutdown(timer_ptr shutdown_timer, shutdown_handler callback, lib::asio::error_code const &ec)
Here is the call graph for this function:

◆ async_write() [1/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::async_write ( const char * buf,
size_t len,
write_handler handler )
inlineprotected

Definition at line 906 of file connection.hpp.

906 {
907 m_bufs.push_back(lib::asio::buffer(buf,len));
908
909 if (config::enable_multithreading) {
910 lib::asio::async_write(
911 socket_con_type::get_socket(),
912 m_bufs,
913 m_strand->wrap(make_custom_alloc_handler(
914 m_write_handler_allocator,
915 lib::bind(
917 handler,
918 lib::placeholders::_1, lib::placeholders::_2
919 )
920 ))
921 );
922 } else {
923 lib::asio::async_write(
924 socket_con_type::get_socket(),
925 m_bufs,
927 m_write_handler_allocator,
928 lib::bind(
930 handler,
931 lib::placeholders::_1, lib::placeholders::_2
932 )
933 )
934 );
935 }
936 }
void handle_async_write(write_handler handler, lib::asio::error_code const &ec, size_t)
Async write callback.
Here is the call graph for this function:

◆ async_write() [2/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::async_write ( std::vector< buffer > const & bufs,
write_handler handler )
inlineprotected

Definition at line 939 of file connection.hpp.

939 {
940 std::vector<buffer>::const_iterator it;
941
942 for (it = bufs.begin(); it != bufs.end(); ++it) {
943 m_bufs.push_back(lib::asio::buffer((*it).buf,(*it).len));
944 }
945
946 if (config::enable_multithreading) {
947 lib::asio::async_write(
948 socket_con_type::get_socket(),
949 m_bufs,
950 m_strand->wrap(make_custom_alloc_handler(
951 m_write_handler_allocator,
952 lib::bind(
954 handler,
955 lib::placeholders::_1, lib::placeholders::_2
956 )
957 ))
958 );
959 } else {
960 lib::asio::async_write(
961 socket_con_type::get_socket(),
962 m_bufs,
964 m_write_handler_allocator,
965 lib::bind(
967 handler,
968 lib::placeholders::_1, lib::placeholders::_2
969 )
970 )
971 );
972 }
973 }
Here is the call graph for this function:

◆ cancel_socket_checked()

template<typename config >
void websocketpp::transport::asio::connection< config >::cancel_socket_checked ( )
inlineprotected

Definition at line 1141 of file connection.hpp.

1141 {
1142 lib::asio::error_code cec = socket_con_type::cancel_socket();
1143 if (cec) {
1144 if (cec == lib::asio::error::operation_not_supported) {
1145 // cancel not supported on this OS, ignore and log at dev level
1146 m_alog.write(log::alevel::devel, "socket cancel not supported");
1147 } else {
1148 log_err(log::elevel::warn, "socket cancel failed", cec);
1149 }
1150 }
1151 }
static level const warn
Definition levels.hpp:72
Here is the caller graph for this function:

◆ dispatch()

template<typename config >
lib::error_code websocketpp::transport::asio::connection< config >::dispatch ( dispatch_handler handler)
inlineprotected

Definition at line 1022 of file connection.hpp.

1022 {
1023 if (config::enable_multithreading) {
1024 m_io_service->post(m_strand->wrap(handler));
1025 } else {
1026 m_io_service->post(handler);
1027 }
1028 return lib::error_code();
1029 }

◆ get_handle()

template<typename config >
connection_hdl websocketpp::transport::asio::connection< config >::get_handle ( ) const
inline

Definition at line 296 of file connection.hpp.

296 {
297 return m_connection_hdl;
298 }

◆ get_proxy()

template<typename config >
std::string const & websocketpp::transport::asio::connection< config >::get_proxy ( ) const
inline

Definition at line 268 of file connection.hpp.

268 {
269 return m_proxy;
270 }

◆ get_remote_endpoint()

template<typename config >
std::string websocketpp::transport::asio::connection< config >::get_remote_endpoint ( ) const
inline

The iostream transport has no information about the ultimate remote endpoint. It will return the string "iostream transport". To indicate this.

TODO: allow user settable remote endpoint addresses if this seems useful

Returns
A string identifying the address of the remote endpoint

Definition at line 282 of file connection.hpp.

282 {
283 lib::error_code ec;
284
285 std::string ret = socket_con_type::get_remote_endpoint(ec);
286
287 if (ec) {
288 m_elog.write(log::elevel::info,ret);
289 return "Unknown";
290 } else {
291 return ret;
292 }
293 }
static level const info
Definition levels.hpp:69
CK_RV ret

◆ get_shared()

template<typename config >
ptr websocketpp::transport::asio::connection< config >::get_shared ( )
inline

Definition at line 111 of file connection.hpp.

111 {
112 return lib::static_pointer_cast<type>(socket_con_type::get_shared());
113 }
Here is the caller graph for this function:

◆ get_strand()

template<typename config >
strand_ptr websocketpp::transport::asio::connection< config >::get_strand ( )
inline

Definition at line 366 of file connection.hpp.

366 {
367 return m_strand;
368 }

◆ get_transport_ec()

template<typename config >
lib::asio::error_code websocketpp::transport::asio::connection< config >::get_transport_ec ( ) const
inline

Retrieves a machine readable detailed error code indicating the reason that the connection was closed or failed. Valid only after the close or fail handler is called.

Primarily used if you are using mismatched asio / system_error implementations such as boost::asio with std::system_error. In these cases the transport error type is different than the library error type and some WebSocket++ functions that return transport errors via the library error code type will be coerced into a catch all pass_through or tls_error error. This method will return the original machine readable transport error in the native type.

Since
0.7.0
Returns
Error code indicating the reason the connection was closed or failed

Definition at line 389 of file connection.hpp.

389 {
390 return m_tec;
391 }

◆ handle_async_read()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_async_read ( read_handler handler,
lib::asio::error_code const & ec,
size_t bytes_transferred )
inlineprotected

Definition at line 871 of file connection.hpp.

873 {
874 m_alog.write(log::alevel::devel, "asio con handle_async_read");
875
876 // translate asio error codes into more lib::error_codes
877 lib::error_code tec;
878 if (ec == lib::asio::error::eof) {
879 tec = make_error_code(transport::error::eof);
880 } else if (ec) {
881 // We don't know much more about the error at this point. As our
882 // socket/security policy if it knows more:
883 tec = socket_con_type::translate_ec(ec);
884 m_tec = ec;
885
886 if (tec == transport::error::tls_error ||
888 {
889 // These are aggregate/catch all errors. Log some human readable
890 // information to the info channel to give library users some
891 // more details about why the upstream method may have failed.
892 log_err(log::elevel::info,"asio async_read_at_least",ec);
893 }
894 }
895 if (handler) {
896 handler(tec,bytes_transferred);
897 } else {
898 // This can happen in cases where the connection is terminated while
899 // the transport is waiting on a read.
900 m_alog.write(log::alevel::devel,
901 "handle_async_read called with null read handler");
902 }
903 }
@ pass_through
underlying transport pass through
Here is the caller graph for this function:

◆ handle_async_shutdown()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_async_shutdown ( timer_ptr shutdown_timer,
shutdown_handler callback,
lib::asio::error_code const & ec )
inlineprotected

Definition at line 1094 of file connection.hpp.

1096 {
1097 if (ec == lib::asio::error::operation_aborted ||
1098 lib::asio::is_neg(shutdown_timer->expires_from_now()))
1099 {
1100 m_alog.write(log::alevel::devel,"async_shutdown cancelled");
1101 return;
1102 }
1103
1104 shutdown_timer->cancel();
1105
1106 lib::error_code tec;
1107 if (ec) {
1108 if (ec == lib::asio::error::not_connected) {
1109 // The socket was already closed when we tried to close it. This
1110 // happens periodically (usually if a read or write fails
1111 // earlier and if it is a real error will be caught at another
1112 // level of the stack.
1113 } else {
1114 // We don't know anything more about this error, give our
1115 // socket/security policy a crack at it.
1116 tec = socket_con_type::translate_ec(ec);
1117 m_tec = ec;
1118
1120 // TLS short read at this point is somewhat expected if both
1121 // sides try and end the connection at the same time or if
1122 // SSLv2 is being used. In general there is nothing that can
1123 // be done here other than a low level development log.
1124 } else {
1125 // all other errors are effectively pass through errors of
1126 // some sort so print some detail on the info channel for
1127 // library users to look up if needed.
1128 log_err(log::elevel::info,"asio async_shutdown",ec);
1129 }
1130 }
1131 } else {
1132 if (m_alog.static_test(log::alevel::devel)) {
1133 m_alog.write(log::alevel::devel,
1134 "asio con handle_async_shutdown");
1135 }
1136 }
1137 callback(tec);
1138 }
bool is_neg(T duration)
Definition asio.hpp:114
@ tls_short_read
TLS short read.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_async_shutdown_timeout()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_async_shutdown_timeout ( timer_ptr ,
init_handler callback,
lib::error_code const & ec )
inlineprotected
Parameters
shutdown_timerA pointer to the timer to keep it in scope
callbackThe function to call back
ecThe status code

Definition at line 1070 of file connection.hpp.

1072 {
1073 lib::error_code ret_ec;
1074
1075 if (ec) {
1077 m_alog.write(log::alevel::devel,
1078 "asio socket shutdown timer cancelled");
1079 return;
1080 }
1081
1082 log_err(log::elevel::devel,"asio handle_async_shutdown_timeout",ec);
1083 ret_ec = ec;
1084 } else {
1085 ret_ec = make_error_code(transport::error::timeout);
1086 }
1087
1088 m_alog.write(log::alevel::devel,
1089 "Asio transport socket shutdown timed out");
1091 callback(ret_ec);
1092 }
void cancel_socket_checked()
Cancel the underlying socket and log any errors.
@ operation_aborted
Operation aborted.
static level const devel
Low level debugging information (warning: very chatty)
Definition levels.hpp:63
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_async_write()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_async_write ( write_handler handler,
lib::asio::error_code const & ec,
size_t  )
inlineprotected
Parameters
ecThe status code
bytes_transferredThe number of bytes read

Definition at line 980 of file connection.hpp.

980 {
981 m_bufs.clear();
982 lib::error_code tec;
983 if (ec) {
984 log_err(log::elevel::info,"asio async_write",ec);
985 tec = ec;
986 }
987 if (handler) {
988 handler(tec);
989 } else {
990 // This can happen in cases where the connection is terminated while
991 // the transport is waiting on a read.
992 m_alog.write(log::alevel::devel,
993 "handle_async_write called with null write handler");
994 }
995 }
Here is the caller graph for this function:

◆ handle_post_init()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_post_init ( timer_ptr post_timer,
init_handler callback,
lib::error_code const & ec )
inlineprotected

The timer pointer is included to ensure the timer isn't destroyed until after it has expired.

Parameters
post_timerPointer to the timer in question
callbackThe function to call back
ecThe status code

Definition at line 572 of file connection.hpp.

574 {
576 (post_timer && lib::asio::is_neg(post_timer->expires_from_now())))
577 {
578 m_alog.write(log::alevel::devel,"post_init cancelled");
579 return;
580 }
581
582 if (post_timer) {
583 post_timer->cancel();
584 }
585
586 if (m_alog.static_test(log::alevel::devel)) {
587 m_alog.write(log::alevel::devel,"asio connection handle_post_init");
588 }
589
590 if (m_tcp_post_init_handler) {
591 m_tcp_post_init_handler(m_connection_hdl);
592 }
593
594 callback(ec);
595 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_post_init_timeout()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_post_init_timeout ( timer_ptr ,
init_handler callback,
lib::error_code const & ec )
inlineprotected

The timer pointer is included to ensure the timer isn't destroyed until after it has expired.

Parameters
post_timerPointer to the timer in question
callbackThe function to call back
ecThe status code

Definition at line 536 of file connection.hpp.

538 {
539 lib::error_code ret_ec;
540
541 if (ec) {
543 m_alog.write(log::alevel::devel,
544 "asio post init timer cancelled");
545 return;
546 }
547
548 log_err(log::elevel::devel,"asio handle_post_init_timeout",ec);
549 ret_ec = ec;
550 } else {
551 if (socket_con_type::get_ec()) {
552 ret_ec = socket_con_type::get_ec();
553 } else {
554 ret_ec = make_error_code(transport::error::timeout);
555 }
556 }
557
558 m_alog.write(log::alevel::devel, "Asio transport post-init timed out");
560 callback(ret_ec);
561 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_pre_init()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_pre_init ( init_handler callback,
lib::error_code const & ec )
inlineprotected

Definition at line 474 of file connection.hpp.

474 {
475 if (m_alog.static_test(log::alevel::devel)) {
476 m_alog.write(log::alevel::devel,"asio connection handle pre_init");
477 }
478
479 if (m_tcp_pre_init_handler) {
480 m_tcp_pre_init_handler(m_connection_hdl);
481 }
482
483 if (ec) {
484 callback(ec);
485 }
486
487 // If we have a proxy set issue a proxy connect, otherwise skip to
488 // post_init
489 if (!m_proxy.empty()) {
490 proxy_write(callback);
491 } else {
492 post_init(callback);
493 }
494 }
void proxy_write(init_handler callback)
void post_init(init_handler callback)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_proxy_read()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_proxy_read ( init_handler callback,
lib::asio::error_code const & ec,
size_t  )
inlineprotected
Parameters
init_handlerThe function to call back
ecThe status code
bytes_transferredThe number of bytes read

Definition at line 742 of file connection.hpp.

744 {
745 if (m_alog.static_test(log::alevel::devel)) {
746 m_alog.write(log::alevel::devel,
747 "asio connection handle_proxy_read");
748 }
749
750 // Timer expired or the operation was aborted for some reason.
751 // Whatever aborted it will be issuing the callback so we are safe to
752 // return
753 if (ec == lib::asio::error::operation_aborted ||
754 lib::asio::is_neg(m_proxy_data->timer->expires_from_now()))
755 {
756 m_elog.write(log::elevel::devel,"read operation aborted");
757 return;
758 }
759
760 // At this point there is no need to wait for the timer anymore
761 m_proxy_data->timer->cancel();
762
763 if (ec) {
764 m_elog.write(log::elevel::info,
765 "asio handle_proxy_read error: "+ec.message());
766 callback(ec);
767 } else {
768 if (!m_proxy_data) {
769 m_elog.write(log::elevel::library,
770 "assertion failed: !m_proxy_data in asio::connection::handle_proxy_read");
771 callback(make_error_code(error::general));
772 return;
773 }
774
775 std::istream input(&m_proxy_data->read_buf);
776
777 m_proxy_data->res.consume(input);
778
779 if (!m_proxy_data->res.headers_ready()) {
780 // we read until the headers were done in theory but apparently
781 // they aren't. Internal endpoint error.
782 callback(make_error_code(error::general));
783 return;
784 }
785
786 m_alog.write(log::alevel::devel,m_proxy_data->res.raw());
787
788 if (m_proxy_data->res.get_status_code() != http::status_code::ok) {
789 // got an error response back
790 // TODO: expose this error in a programmatically accessible way?
791 // if so, see below for an option on how to do this.
792 std::stringstream s;
793 s << "Proxy connection error: "
794 << m_proxy_data->res.get_status_code()
795 << " ("
796 << m_proxy_data->res.get_status_msg()
797 << ")";
798 m_elog.write(log::elevel::info,s.str());
799 callback(make_error_code(error::proxy_failed));
800 return;
801 }
802
803 // we have successfully established a connection to the proxy, now
804 // we can continue and the proxy will transparently forward the
805 // WebSocket connection.
806
807 // TODO: decide if we want an on_proxy callback that would allow
808 // access to the proxy response.
809
810 // free the proxy buffers and req/res objects as they aren't needed
811 // anymore
812 m_proxy_data.reset();
813
814 // Continue with post proxy initialization
815 post_init(callback);
816 }
817 }
@ proxy_failed
The connection to the requested proxy server failed.
Definition base.hpp:174
static level const library
Definition levels.hpp:66
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_proxy_timeout()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_proxy_timeout ( init_handler callback,
lib::error_code const & ec )
inlineprotected

Definition at line 651 of file connection.hpp.

652 {
654 m_alog.write(log::alevel::devel,
655 "asio handle_proxy_write timer cancelled");
656 return;
657 } else if (ec) {
658 log_err(log::elevel::devel,"asio handle_proxy_write",ec);
659 callback(ec);
660 } else {
661 m_alog.write(log::alevel::devel,
662 "asio handle_proxy_write timer expired");
664 callback(make_error_code(transport::error::timeout));
665 }
666 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_proxy_write()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_proxy_write ( init_handler callback,
lib::asio::error_code const & ec )
inlineprotected

Definition at line 668 of file connection.hpp.

670 {
671 if (m_alog.static_test(log::alevel::devel)) {
672 m_alog.write(log::alevel::devel,
673 "asio connection handle_proxy_write");
674 }
675
676 m_bufs.clear();
677
678 // Timer expired or the operation was aborted for some reason.
679 // Whatever aborted it will be issuing the callback so we are safe to
680 // return
681 if (ec == lib::asio::error::operation_aborted ||
682 lib::asio::is_neg(m_proxy_data->timer->expires_from_now()))
683 {
684 m_elog.write(log::elevel::devel,"write operation aborted");
685 return;
686 }
687
688 if (ec) {
689 log_err(log::elevel::info,"asio handle_proxy_write",ec);
690 m_proxy_data->timer->cancel();
691 callback(ec);
692 return;
693 }
694
695 proxy_read(callback);
696 }
void proxy_read(init_handler callback)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_timer()

template<typename config >
void websocketpp::transport::asio::connection< config >::handle_timer ( timer_ptr ,
timer_handler callback,
lib::asio::error_code const & ec )
inline

The timer pointer is included to ensure the timer isn't destroyed until after it has expired.

TODO: candidate for protected status

Parameters
post_timerPointer to the timer in question
callbackThe function to call back
ecThe status code

Definition at line 350 of file connection.hpp.

352 {
353 if (ec) {
354 if (ec == lib::asio::error::operation_aborted) {
355 callback(make_error_code(transport::error::operation_aborted));
356 } else {
357 log_err(log::elevel::info,"asio handle_timer",ec);
358 callback(ec);
359 }
360 } else {
361 callback(lib::error_code());
362 }
363 }
Here is the caller graph for this function:

◆ init()

template<typename config >
void websocketpp::transport::asio::connection< config >::init ( init_handler callback)
inlineprotected

init_asio is called once immediately after construction to initialize Asio components to the io_service

The transport initialization sequence consists of the following steps:

  • Pre-init: the underlying socket is initialized to the point where bytes may be written. No bytes are actually written in this stage
  • Proxy negotiation: if a proxy is set, a request is made to it to start a tunnel to the final destination. This stage ends when the proxy is ready to forward the next byte to the remote endpoint.
  • Post-init: Perform any i/o with the remote endpoint, such as setting up tunnels for encryption. This stage ends when the connection is ready to read or write the WebSocket handshakes. At this point the original callback function is called.

Definition at line 411 of file connection.hpp.

411 {
412 if (m_alog.static_test(log::alevel::devel)) {
413 m_alog.write(log::alevel::devel,"asio connection init");
414 }
415
416 // TODO: pre-init timeout. Right now no implemented socket policies
417 // actually have an asyncronous pre-init
418
419 socket_con_type::pre_init(
420 lib::bind(
422 get_shared(),
423 callback,
424 lib::placeholders::_1
425 )
426 );
427 }
void handle_pre_init(init_handler callback, lib::error_code const &ec)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_asio()

template<typename config >
lib::error_code websocketpp::transport::asio::connection< config >::init_asio ( io_service_ptr io_service)
inlineprotected

init_asio is called once immediately after construction to initialize Asio components to the io_service.

Parameters
io_serviceA pointer to the io_service to register with this connection
Returns
Status code for the success or failure of the initialization

Definition at line 461 of file connection.hpp.

461 {
462 m_io_service = io_service;
463
464 if (config::enable_multithreading) {
465 m_strand = lib::make_shared<lib::asio::io_service::strand>(*io_service);
466 }
467
468 lib::error_code ec = socket_con_type::init_asio(io_service, m_strand,
469 m_is_server);
470
471 return ec;
472 }

◆ interrupt()

template<typename config >
lib::error_code websocketpp::transport::asio::connection< config >::interrupt ( interrupt_handler handler)
inlineprotected

This needs to be thread safe

Definition at line 1013 of file connection.hpp.

1013 {
1014 if (config::enable_multithreading) {
1015 m_io_service->post(m_strand->wrap(handler));
1016 } else {
1017 m_io_service->post(handler);
1018 }
1019 return lib::error_code();
1020 }

◆ is_secure()

template<typename config >
bool websocketpp::transport::asio::connection< config >::is_secure ( ) const
inline

Definition at line 115 of file connection.hpp.

115 {
116 return socket_con_type::is_secure();
117 }

◆ post_init()

template<typename config >
void websocketpp::transport::asio::connection< config >::post_init ( init_handler callback)
inlineprotected

Definition at line 496 of file connection.hpp.

496 {
497 if (m_alog.static_test(log::alevel::devel)) {
498 m_alog.write(log::alevel::devel,"asio connection post_init");
499 }
500
501 timer_ptr post_timer;
502
503 if (config::timeout_socket_post_init > 0) {
504 post_timer = set_timer(
505 config::timeout_socket_post_init,
506 lib::bind(
508 get_shared(),
509 post_timer,
510 callback,
511 lib::placeholders::_1
512 )
513 );
514 }
515
516 socket_con_type::post_init(
517 lib::bind(
519 get_shared(),
520 post_timer,
521 callback,
522 lib::placeholders::_1
523 )
524 );
525 }
void handle_post_init(timer_ptr post_timer, init_handler callback, lib::error_code const &ec)
Post init timeout callback.
void handle_post_init_timeout(timer_ptr, init_handler callback, lib::error_code const &ec)
Post init timeout callback.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ proxy_init()

template<typename config >
lib::error_code websocketpp::transport::asio::connection< config >::proxy_init ( std::string const & authority)
inlineprotected
Parameters
authorityThe address of the server we want the proxy to tunnel to in the format of a URI authority (host:port)
Returns
Status code indicating what errors occurred, if any

Definition at line 437 of file connection.hpp.

437 {
438 if (!m_proxy_data) {
441 }
442 m_proxy_data->req.set_version("HTTP/1.1");
443 m_proxy_data->req.set_method("CONNECT");
444
445 m_proxy_data->req.set_uri(authority);
446 m_proxy_data->req.replace_header("Host",authority);
447
448 return lib::error_code();
449 }
@ invalid_state
The connection was in the wrong state for this operation.
Definition error.hpp:74
lib::error_code make_error_code(error::value e)
Definition error.hpp:235
Here is the call graph for this function:

◆ proxy_read()

template<typename config >
void websocketpp::transport::asio::connection< config >::proxy_read ( init_handler callback)
inlineprotected

Definition at line 698 of file connection.hpp.

698 {
699 if (m_alog.static_test(log::alevel::devel)) {
700 m_alog.write(log::alevel::devel,"asio connection proxy_read");
701 }
702
703 if (!m_proxy_data) {
704 m_elog.write(log::elevel::library,
705 "assertion failed: !m_proxy_data in asio::connection::proxy_read");
706 m_proxy_data->timer->cancel();
707 callback(make_error_code(error::general));
708 return;
709 }
710
711 if (config::enable_multithreading) {
712 lib::asio::async_read_until(
713 socket_con_type::get_next_layer(),
714 m_proxy_data->read_buf,
715 "\r\n\r\n",
716 m_strand->wrap(lib::bind(
718 callback,
719 lib::placeholders::_1, lib::placeholders::_2
720 ))
721 );
722 } else {
723 lib::asio::async_read_until(
724 socket_con_type::get_next_layer(),
725 m_proxy_data->read_buf,
726 "\r\n\r\n",
727 lib::bind(
729 callback,
730 lib::placeholders::_1, lib::placeholders::_2
731 )
732 );
733 }
734 }
void handle_proxy_read(init_handler callback, lib::asio::error_code const &ec, size_t)
Proxy read callback.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ proxy_write()

template<typename config >
void websocketpp::transport::asio::connection< config >::proxy_write ( init_handler callback)
inlineprotected

Definition at line 597 of file connection.hpp.

597 {
598 if (m_alog.static_test(log::alevel::devel)) {
599 m_alog.write(log::alevel::devel,"asio connection proxy_write");
600 }
601
602 if (!m_proxy_data) {
603 m_elog.write(log::elevel::library,
604 "assertion failed: !m_proxy_data in asio::connection::proxy_write");
605 callback(make_error_code(error::general));
606 return;
607 }
608
609 m_proxy_data->write_buf = m_proxy_data->req.raw();
610
611 m_bufs.push_back(lib::asio::buffer(m_proxy_data->write_buf.data(),
612 m_proxy_data->write_buf.size()));
613
614 m_alog.write(log::alevel::devel,m_proxy_data->write_buf);
615
616 // Set a timer so we don't wait forever for the proxy to respond
617 m_proxy_data->timer = this->set_timer(
618 m_proxy_data->timeout_proxy,
619 lib::bind(
621 get_shared(),
622 callback,
623 lib::placeholders::_1
624 )
625 );
626
627 // Send proxy request
628 if (config::enable_multithreading) {
629 lib::asio::async_write(
630 socket_con_type::get_next_layer(),
631 m_bufs,
632 m_strand->wrap(lib::bind(
634 callback,
635 lib::placeholders::_1
636 ))
637 );
638 } else {
639 lib::asio::async_write(
640 socket_con_type::get_next_layer(),
641 m_bufs,
642 lib::bind(
644 callback,
645 lib::placeholders::_1
646 )
647 );
648 }
649 }
void handle_proxy_write(init_handler callback, lib::asio::error_code const &ec)
void handle_proxy_timeout(init_handler callback, lib::error_code const &ec)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_handle()

template<typename config >
void websocketpp::transport::asio::connection< config >::set_handle ( connection_hdl hdl)
inlineprotected

See common/connection_hdl.hpp for information

Parameters
hdlA connection_hdl that the transport will use to refer to itself

Definition at line 1004 of file connection.hpp.

1004 {
1005 m_connection_hdl = hdl;
1006 socket_con_type::set_handle(hdl);
1007 }

◆ set_proxy() [1/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::set_proxy ( std::string const & uri)
inline

Definition at line 199 of file connection.hpp.

199 {
200 lib::error_code ec;
201 set_proxy(uri,ec);
202 if (ec) { throw exception(ec); }
203 }
void set_proxy(std::string const &uri, lib::error_code &ec)
Set the proxy to connect through (exception free)
Here is the call graph for this function:

◆ set_proxy() [2/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::set_proxy ( std::string const & uri,
lib::error_code & ec )
inline

The URI passed should be a complete URI including scheme. For example: http://proxy.example.com:8080/

The proxy must be set up as an explicit (CONNECT) proxy allowed to connect to the port you specify. Traffic to the proxy is not encrypted.

Parameters
uriThe full URI of the proxy to connect to.
ecA status value

Definition at line 190 of file connection.hpp.

190 {
191 // TODO: return errors for illegal URIs here?
192 // TODO: should https urls be illegal for the moment?
193 m_proxy = uri;
194 m_proxy_data = lib::make_shared<proxy_data>();
195 ec = lib::error_code();
196 }
Here is the caller graph for this function:

◆ set_proxy_basic_auth() [1/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::set_proxy_basic_auth ( std::string const & username,
std::string const & password )
inline

Definition at line 233 of file connection.hpp.

235 {
236 lib::error_code ec;
237 set_proxy_basic_auth(username,password,ec);
238 if (ec) { throw exception(ec); }
239 }
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)
Here is the call graph for this function:

◆ set_proxy_basic_auth() [2/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::set_proxy_basic_auth ( std::string const & username,
std::string const & password,
lib::error_code & ec )
inline

The URI passed should be a complete URI including scheme. For example: http://proxy.example.com:8080/

The proxy must be set up as an explicit proxy

Parameters
usernameThe username to send
passwordThe password to send
ecA status value

Definition at line 218 of file connection.hpp.

220 {
221 if (!m_proxy_data) {
222 ec = make_error_code(websocketpp::error::invalid_state);
223 return;
224 }
225
226 // TODO: username can't contain ':'
227 std::string val = "Basic "+base64_encode(username + ":" + password);
228 m_proxy_data->req.replace_header("Proxy-Authorization",val);
229 ec = lib::error_code();
230 }
std::string base64_encode(unsigned char const *input, size_t len)
Encode a char buffer into a base64 string.
Definition base64.hpp:66
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_proxy_timeout() [1/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::set_proxy_timeout ( long duration)
inline

Definition at line 262 of file connection.hpp.

262 {
263 lib::error_code ec;
264 set_proxy_timeout(duration,ec);
265 if (ec) { throw exception(ec); }
266 }
void set_proxy_timeout(long duration, lib::error_code &ec)
Set the proxy timeout duration (exception free)
Here is the call graph for this function:

◆ set_proxy_timeout() [2/2]

template<typename config >
void websocketpp::transport::asio::connection< config >::set_proxy_timeout ( long duration,
lib::error_code & ec )
inline

Duration is in milliseconds. Default value is based on the transport config

Parameters
durationThe number of milliseconds to wait before aborting the proxy connection.
ecA status value

Definition at line 251 of file connection.hpp.

251 {
252 if (!m_proxy_data) {
253 ec = make_error_code(websocketpp::error::invalid_state);
254 return;
255 }
256
257 m_proxy_data->timeout_proxy = duration;
258 ec = lib::error_code();
259 }
Here is the caller graph for this function:

◆ set_tcp_init_handler()

template<typename config >
void websocketpp::transport::asio::connection< config >::set_tcp_init_handler ( tcp_init_handler h)
inline

The tcp pre init handler is called after the raw tcp connection has been established but before any additional wrappers (proxy connects, TLS handshakes, etc) have been performed.

Deprecated
Use set_tcp_pre_init_handler instead
Parameters
hThe handler to call on tcp pre init.

Definition at line 159 of file connection.hpp.

159 {
161 }
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
Here is the call graph for this function:

◆ set_tcp_post_init_handler()

template<typename config >
void websocketpp::transport::asio::connection< config >::set_tcp_post_init_handler ( tcp_init_handler h)
inline

The tcp post init handler is called after the tcp connection has been established and all additional wrappers (proxy connects, TLS handshakes, etc have been performed. This is fired before any bytes are read or any WebSocket specific handshake logic has been performed.

Since
0.3.0
Parameters
hThe handler to call on tcp post init.

Definition at line 174 of file connection.hpp.

174 {
175 m_tcp_post_init_handler = h;
176 }

◆ set_tcp_pre_init_handler()

template<typename config >
void websocketpp::transport::asio::connection< config >::set_tcp_pre_init_handler ( tcp_init_handler h)
inline

The tcp pre init handler is called after the raw tcp connection has been established but before any additional wrappers (proxy connects, TLS handshakes, etc) have been performed.

Since
0.3.0
Parameters
hThe handler to call on tcp pre init.

Definition at line 145 of file connection.hpp.

145 {
146 m_tcp_pre_init_handler = h;
147 }
Here is the caller graph for this function:

◆ set_timer()

template<typename config >
timer_ptr websocketpp::transport::asio::connection< config >::set_timer ( long duration,
timer_handler callback )
inline

Sets a timer that calls back a function after the specified period of milliseconds. Returns a handle that can be used to cancel the timer. A cancelled timer will return the error code error::operation_aborted A timer that expired will return no error.

Parameters
durationLength of time to wait in milliseconds
callbackThe function to call back when the timer has expired
Returns
A handle that can be used to cancel the timer if it is no longer needed.

Definition at line 314 of file connection.hpp.

314 {
315 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
316 *m_io_service,
318 );
319
320 if (config::enable_multithreading) {
321 new_timer->async_wait(m_strand->wrap(lib::bind(
323 new_timer,
324 callback,
325 lib::placeholders::_1
326 )));
327 } else {
328 new_timer->async_wait(lib::bind(
330 new_timer,
331 callback,
332 lib::placeholders::_1
333 ));
334 }
335
336 return new_timer;
337 }
void handle_timer(timer_ptr, timer_handler callback, lib::asio::error_code const &ec)
Timer callback.
boost::posix_time::time_duration milliseconds(long duration)
Definition asio.hpp:117
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_uri()

template<typename config >
void websocketpp::transport::asio::connection< config >::set_uri ( uri_ptr u)
inline

Called by the endpoint as a connection is being established to provide the uri being connected to to the transport layer.

This transport policy doesn't use the uri except to forward it to the socket layer.

Since
0.6.0
Parameters
uThe uri to set

Definition at line 131 of file connection.hpp.

131 {
132 socket_con_type::set_uri(u);
133 }

Friends And Related Symbol Documentation

◆ endpoint< config >

template<typename config >
friend class endpoint< config >
friend

Definition at line 93 of file connection.hpp.

◆ local_endpoint

template<typename config >
template<typename T >
friend class local_endpoint
friend

Definition at line 99 of file connection.hpp.


The documentation for this class was generated from the following file: