28 #ifndef WEBSOCKETPP_TRANSPORT_ASIO_HPP 29 #define WEBSOCKETPP_TRANSPORT_ASIO_HPP 31 #include <websocketpp/transport/base/endpoint.hpp> 32 #include <websocketpp/transport/asio/connection.hpp> 33 #include <websocketpp/transport/asio/security/none.hpp> 35 #include <websocketpp/uri.hpp> 36 #include <websocketpp/logger/levels.hpp> 38 #include <websocketpp/common/asio.hpp> 39 #include <websocketpp/common/functional.hpp> 53 template <
typename config>
54 class endpoint :
public config::socket_type {
83 typedef lib::shared_ptr<lib::asio::ip::tcp::acceptor>
acceptor_ptr;
85 typedef lib::shared_ptr<lib::asio::ip::tcp::resolver>
resolver_ptr;
87 typedef lib::shared_ptr<lib::asio::steady_timer>
timer_ptr;
89 typedef lib::shared_ptr<lib::asio::io_service::work>
work_ptr;
97 , m_external_io_service(false)
98 , m_listen_backlog(lib::asio::socket_base::max_connections)
100 , m_state(UNINITIALIZED)
112 if (m_state != UNINITIALIZED && !m_external_io_service) {
120 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_ 128 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_ 130 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_ 132 : config::socket_type(std::move(src))
133 , m_tcp_pre_init_handler(src.m_tcp_pre_init_handler)
134 , m_tcp_post_init_handler(src.m_tcp_post_init_handler)
135 , m_io_service(src.m_io_service)
136 , m_external_io_service(src.m_external_io_service)
137 , m_acceptor(src.m_acceptor)
138 , m_listen_backlog(lib::asio::socket_base::max_connections)
139 , m_reuse_addr(src.m_reuse_addr)
142 , m_state(src.m_state)
144 src.m_io_service = NULL;
145 src.m_external_io_service =
false;
146 src.m_acceptor = NULL;
147 src.m_state = UNINITIALIZED;
169 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_ 173 return socket_type::is_secure();
185 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
186 if (m_state != UNINITIALIZED) {
188 "asio::init_asio called from the wrong state");
189 using websocketpp::error::make_error_code;
197 m_external_io_service =
true;
198 m_acceptor = lib::make_shared<lib::asio::ip::tcp::acceptor>(
199 lib::ref(*m_io_service));
202 ec = lib::error_code();
233 #ifdef _WEBSOCKETPP_CPP11_MEMORY_ 234 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
236 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
239 if( !ec ) service.release();
240 m_external_io_service =
false;
255 #ifdef _WEBSOCKETPP_CPP11_MEMORY_ 256 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
258 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
263 m_external_io_service =
false;
276 m_tcp_pre_bind_handler = h;
290 m_tcp_pre_init_handler = h;
319 m_tcp_post_init_handler = h;
344 m_listen_backlog = backlog;
365 m_reuse_addr =
value;
380 return *m_io_service;
398 return m_acceptor->local_endpoint(ec);
400 ec = lib::asio::error::make_error_code(lib::asio::error::bad_descriptor);
401 return lib::asio::ip::tcp::endpoint();
413 void listen(lib::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
415 if (m_state != READY) {
417 "asio::listen called from the wrong state");
418 using websocketpp::error::make_error_code;
425 lib::asio::error_code bec;
427 m_acceptor->open(ep.protocol(),bec);
428 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
430 m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
431 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
434 if (m_tcp_pre_bind_handler) {
435 ec = m_tcp_pre_bind_handler(m_acceptor);
437 ec = clean_up_listen_after_error(ec);
442 m_acceptor->bind(ep,bec);
443 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
445 m_acceptor->listen(m_listen_backlog,bec);
446 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
450 ec = lib::error_code();
461 void listen(lib::asio::ip::tcp::endpoint
const & ep) {
481 template <
typename InternetProtocol>
482 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
483 lib::error_code & ec)
485 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
502 template <
typename InternetProtocol>
503 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
505 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
521 void listen(uint16_t port, lib::error_code & ec) {
522 listen(lib::asio::ip::tcp::v6(), port, ec);
538 listen(lib::asio::ip::tcp::v6(), port);
557 void listen(std::string
const & host, std::string
const & service,
558 lib::error_code & ec)
560 using lib::asio::ip::tcp;
561 tcp::resolver r(*m_io_service);
562 tcp::resolver::query query(host, service);
563 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
564 tcp::resolver::iterator end;
565 if (endpoint_iterator == end) {
567 "asio::listen could not resolve the supplied host or service");
571 listen(*endpoint_iterator,ec);
590 void listen(std::string
const & host, std::string
const & service)
606 if (m_state != LISTENING) {
608 "asio::listen called from the wrong state");
609 using websocketpp::error::make_error_code;
616 ec = lib::error_code();
637 return (m_state == LISTENING);
642 return m_io_service->run();
650 return m_io_service->run_one();
655 m_io_service->stop();
660 return m_io_service->poll();
665 return m_io_service->poll_one();
670 m_io_service->reset();
675 return m_io_service->stopped();
691 m_work = lib::make_shared<lib::asio::io_service::work>(
692 lib::ref(*m_io_service)
721 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
723 lib::asio::milliseconds(duration)
726 new_timer->async_wait(
732 lib::placeholders::_1
749 lib::asio::error_code
const & ec)
752 if (ec == lib::asio::error::operation_aborted) {
756 "asio handle_timer error: "+ec.message());
758 callback(socket_con_type::translate_ec(ec));
761 callback(lib::error_code());
772 lib::error_code & ec)
774 if (m_state != LISTENING || !m_acceptor) {
775 using websocketpp::error::make_error_code;
782 if (config::enable_multithreading) {
783 m_acceptor->async_accept(
784 tcon->get_raw_socket(),
785 tcon->get_strand()->wrap(lib::bind(
786 &type::handle_accept,
789 lib::placeholders::_1
793 m_acceptor->async_accept(
794 tcon->get_raw_socket(),
796 &type::handle_accept,
799 lib::placeholders::_1
826 void init_logging(
const lib::shared_ptr<alog_type>& a,
const lib::shared_ptr<elog_type>& e) {
831 void handle_accept(
accept_handler callback, lib::asio::error_code
const &
834 lib::error_code ret_ec;
839 if (asio_ec == lib::asio::errc::operation_canceled) {
843 ret_ec = socket_con_type::translate_ec(asio_ec);
853 using namespace lib::asio::ip;
857 m_resolver = lib::make_shared<lib::asio::ip::tcp::resolver>(
858 lib::ref(*m_io_service));
863 std::string proxy = tcon->get_proxy();
868 host = u->get_host();
869 port = u->get_port_str();
873 uri_ptr pu = lib::make_shared<uri>(proxy);
875 if (!pu->get_valid()) {
880 ec = tcon->proxy_init(u->get_authority());
886 host = pu->get_host();
887 port = pu->get_port_str();
890 tcp::resolver::query query(host,port);
894 "starting async DNS resolve for "+host+
":"+port);
899 dns_timer = tcon->set_timer(
900 config::timeout_dns_resolve,
906 lib::placeholders::_1
910 if (config::enable_multithreading) {
911 m_resolver->async_resolve(
913 tcon->get_strand()->wrap(lib::bind(
914 &type::handle_resolve,
919 lib::placeholders::_1,
920 lib::placeholders::_2
924 m_resolver->async_resolve(
927 &type::handle_resolve,
932 lib::placeholders::_1,
933 lib::placeholders::_2
949 lib::error_code
const & ec)
951 lib::error_code ret_ec;
956 "asio handle_resolve_timeout timer cancelled");
967 m_resolver->cancel();
971 void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer,
973 lib::asio::ip::tcp::resolver::iterator iterator)
975 if (ec == lib::asio::error::operation_aborted ||
976 lib::asio::is_neg(dns_timer->expires_from_now()))
986 callback(socket_con_type::translate_ec(ec));
992 s <<
"Async DNS resolve successful. Results: ";
994 lib::asio::ip::tcp::resolver::iterator it, end;
995 for (it = iterator; it != end; ++it) {
996 s << (*it).endpoint() <<
" ";
1004 timer_ptr con_timer;
1006 con_timer = tcon->set_timer(
1007 config::timeout_connect,
1014 lib::placeholders::_1
1018 if (config::enable_multithreading) {
1019 lib::asio::async_connect(
1020 tcon->get_raw_socket(),
1022 tcon->get_strand()->wrap(lib::bind(
1023 &type::handle_connect,
1028 lib::placeholders::_1
1032 lib::asio::async_connect(
1033 tcon->get_raw_socket(),
1036 &type::handle_connect,
1041 lib::placeholders::_1
1060 lib::error_code ret_ec;
1065 "asio handle_connect_timeout timer cancelled");
1076 tcon->cancel_socket_checked();
1080 void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
1083 if (ec == lib::asio::error::operation_aborted ||
1084 lib::asio::is_neg(con_timer->expires_from_now()))
1090 con_timer->cancel();
1094 callback(socket_con_type::translate_ec(ec));
1100 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1103 callback(lib::error_code());
1117 lib::error_code
init(transport_con_ptr tcon) {
1121 socket_type::init(lib::static_pointer_cast<socket_con_type,
1122 transport_con_type>(tcon));
1126 ec = tcon->init_asio(m_io_service);
1127 if (ec) {
return ec;}
1129 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1130 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1132 return lib::error_code();
1136 template <
typename error_type>
1137 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1138 std::stringstream s;
1139 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1140 m_elog->write(l,s.str());
1144 template <
typename error_type>
1145 lib::error_code clean_up_listen_after_error(error_type
const & ec) {
1146 if (m_acceptor->is_open()) {
1147 m_acceptor->close();
1150 return socket_con_type::translate_ec(ec);
1160 tcp_pre_bind_handler m_tcp_pre_bind_handler;
1161 tcp_init_handler m_tcp_pre_init_handler;
1162 tcp_init_handler m_tcp_post_init_handler;
1165 io_service_ptr m_io_service;
1166 bool m_external_io_service;
1167 acceptor_ptr m_acceptor;
1168 resolver_ptr m_resolver;
1172 int m_listen_backlog;
1175 lib::shared_ptr<elog_type> m_elog;
1176 lib::shared_ptr<alog_type> m_alog;
1186 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP lib::shared_ptr< lib::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
endpoint< config > type
Type of this endpoint transport component.
Asio based endpoint transport component.
uint16_t value
The type of a close code value.
lib::shared_ptr< lib::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
void init_asio()
Initialize asio transport with internal io_service.
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
void listen(lib::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
transport_con_type::ptr transport_con_ptr
asio::connection< config > transport_con_type
bool is_secure() const
Return whether or not the endpoint produces secure connections.
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
void listen(uint16_t port, lib::error_code &ec)
Set up endpoint for listening on a port (exception free)
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
bool stopped() const
wraps the stopped method of the internal io_service object
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
lib::function< lib::error_code(acceptor_ptr)> tcp_pre_bind_handler
Type of socket pre-bind handler.
void init_logging(const lib::shared_ptr< alog_type > &a, const lib::shared_ptr< elog_type > &e)
Initialize logging.
static level const devel
Low level debugging information (warning: very chatty)
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code &ec)
Get local TCP endpoint.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
void handle_resolve_timeout(timer_ptr, connect_handler callback, lib::error_code const &ec)
DNS resolution timeout handler.
config::socket_type socket_type
Type of the socket policy.
void listen(uint16_t port)
Set up endpoint for listening on a port.
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
lib::asio::io_service & get_io_service()
Retrieve a reference to the endpoint's io_service.
static level const devel
Development messages (warning: very chatty)
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
lib::shared_ptr< lib::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
std::size_t poll()
wraps the poll method of the internal io_service object
void stop_perpetual()
Clears the endpoint's perpetual flag, allowing it to exit when empty.
void stop_listening()
Stop listening.
config::alog_type alog_type
Type of the access logging policy.
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
The connection was in the wrong state for this operation.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
Namespace for the WebSocket++ project.
std::size_t run_one()
wraps the run_one method of the internal io_service object
The requested operation was canceled.
config::elog_type elog_type
Type of the error logging policy.
void reset()
wraps the reset method of the internal io_service object
void listen(std::string const &host, std::string const &service, lib::error_code &ec)
Set up endpoint for listening on a host and service (exception free)
void async_accept(transport_con_ptr tcon, accept_handler callback, lib::error_code &ec)
Accept the next connection attempt and assign it to con (exception free)
Asio based connection transport component.
bool is_listening() const
Check if the endpoint is listening.
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
void init_asio(io_service_ptr ptr)
initialize asio transport with external io_service
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
std::size_t poll_one()
wraps the poll_one method of the internal io_service object
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_service (exception free)
void stop()
wraps the stop method of the internal io_service object
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
std::size_t run()
wraps the run method of the internal io_service object
void listen(InternetProtocol const &internet_protocol, uint16_t port)
Set up endpoint for listening with protocol and port.
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
void listen(InternetProtocol const &internet_protocol, uint16_t port, lib::error_code &ec)
Set up endpoint for listening with protocol and port (exception free)
void set_tcp_pre_bind_handler(tcp_pre_bind_handler h)
Sets the tcp pre bind handler.
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
config::concurrency_type concurrency_type
Type of the concurrency policy.
static level const library
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
void handle_timer(timer_ptr, timer_handler callback, lib::asio::error_code const &ec)
Timer handler.
void start_perpetual()
Marks the endpoint as perpetual, stopping it from exiting when empty.