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>
53template <
typename config>
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_
130#ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
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;
173 return socket_type::is_secure();
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.reset(
new lib::asio::ip::tcp::acceptor(*m_io_service));
201 ec = lib::error_code();
232#ifdef _WEBSOCKETPP_CPP11_MEMORY_
233 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
235 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
238 if( !ec ) service.release();
239 m_external_io_service =
false;
254#ifdef _WEBSOCKETPP_CPP11_MEMORY_
255 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
257 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
262 m_external_io_service =
false;
275 m_tcp_pre_bind_handler = h;
289 m_tcp_pre_init_handler = h;
318 m_tcp_post_init_handler = h;
343 m_listen_backlog = backlog;
364 m_reuse_addr = value;
379 return *m_io_service;
397 return m_acceptor->local_endpoint(ec);
399 ec = lib::asio::error::make_error_code(lib::asio::error::bad_descriptor);
400 return lib::asio::ip::tcp::endpoint();
412 void listen(lib::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
414 if (m_state != READY) {
416 "asio::listen called from the wrong state");
417 using websocketpp::error::make_error_code;
424 lib::asio::error_code bec;
426 m_acceptor->open(ep.protocol(),bec);
427 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
429 m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
430 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
433 if (m_tcp_pre_bind_handler) {
434 ec = m_tcp_pre_bind_handler(m_acceptor);
436 ec = clean_up_listen_after_error(ec);
441 m_acceptor->bind(ep,bec);
442 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
444 m_acceptor->listen(m_listen_backlog,bec);
445 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
449 ec = lib::error_code();
460 void listen(lib::asio::ip::tcp::endpoint
const & ep) {
480 template <
typename InternetProtocol>
481 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
482 lib::error_code & ec)
484 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
501 template <
typename InternetProtocol>
502 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
504 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
520 void listen(uint16_t port, lib::error_code & ec) {
521 listen(lib::asio::ip::tcp::v6(), port, ec);
537 listen(lib::asio::ip::tcp::v6(), port);
556 void listen(std::string
const & host, std::string
const & service,
557 lib::error_code & ec)
559 using lib::asio::ip::tcp;
560 tcp::resolver r(*m_io_service);
561 tcp::resolver::query query(host, service);
562 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
563 tcp::resolver::iterator end;
564 if (endpoint_iterator == end) {
566 "asio::listen could not resolve the supplied host or service");
570 listen(*endpoint_iterator,ec);
589 void listen(std::string
const & host, std::string
const & service)
605 if (m_state != LISTENING) {
607 "asio::listen called from the wrong state");
608 using websocketpp::error::make_error_code;
615 ec = lib::error_code();
636 return (m_state == LISTENING);
641 return m_io_service->run();
649 return m_io_service->run_one();
654 m_io_service->stop();
659 return m_io_service->poll();
664 return m_io_service->poll_one();
669 m_io_service->reset();
674 return m_io_service->stopped();
690 m_work.reset(
new lib::asio::io_service::work(*m_io_service));
718 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
720 lib::asio::milliseconds(duration)
723 new_timer->async_wait(
729 lib::placeholders::_1
746 lib::asio::error_code
const & ec)
749 if (ec == lib::asio::error::operation_aborted) {
753 "asio handle_timer error: "+ec.message());
755 callback(socket_con_type::translate_ec(ec));
758 callback(lib::error_code());
769 lib::error_code & ec)
771 if (m_state != LISTENING || !m_acceptor) {
772 using websocketpp::error::make_error_code;
779 if (config::enable_multithreading) {
780 m_acceptor->async_accept(
781 tcon->get_raw_socket(),
782 tcon->get_strand()->wrap(lib::bind(
783 &type::handle_accept,
786 lib::placeholders::_1
790 m_acceptor->async_accept(
791 tcon->get_raw_socket(),
793 &type::handle_accept,
796 lib::placeholders::_1
823 void init_logging(
const lib::shared_ptr<alog_type>& a,
const lib::shared_ptr<elog_type>& e) {
828 void handle_accept(
accept_handler callback, lib::asio::error_code
const &
831 lib::error_code ret_ec;
836 if (asio_ec == lib::asio::errc::operation_canceled) {
840 ret_ec = socket_con_type::translate_ec(asio_ec);
850 using namespace lib::asio::ip;
854 m_resolver.reset(
new lib::asio::ip::tcp::resolver(*m_io_service));
859 std::string proxy = tcon->get_proxy();
864 host = u->get_host();
865 port = u->get_port_str();
869 uri_ptr pu = lib::make_shared<uri>(proxy);
871 if (!pu->get_valid()) {
876 ec = tcon->proxy_init(u->get_authority());
882 host = pu->get_host();
883 port = pu->get_port_str();
886 tcp::resolver::query query(host,port);
890 "starting async DNS resolve for "+host+
":"+port);
895 dns_timer = tcon->set_timer(
896 config::timeout_dns_resolve,
902 lib::placeholders::_1
906 if (config::enable_multithreading) {
907 m_resolver->async_resolve(
909 tcon->get_strand()->wrap(lib::bind(
910 &type::handle_resolve,
915 lib::placeholders::_1,
916 lib::placeholders::_2
920 m_resolver->async_resolve(
923 &type::handle_resolve,
928 lib::placeholders::_1,
929 lib::placeholders::_2
945 lib::error_code
const & ec)
947 lib::error_code ret_ec;
952 "asio handle_resolve_timeout timer cancelled");
963 m_resolver->cancel();
969 lib::asio::ip::tcp::resolver::iterator iterator)
971 if (ec == lib::asio::error::operation_aborted ||
972 lib::asio::is_neg(dns_timer->expires_from_now()))
982 callback(socket_con_type::translate_ec(ec));
988 s <<
"Async DNS resolve successful. Results: ";
990 lib::asio::ip::tcp::resolver::iterator it, end;
991 for (it = iterator; it != end; ++it) {
992 s << (*it).endpoint() <<
" ";
1002 con_timer = tcon->set_timer(
1003 config::timeout_connect,
1010 lib::placeholders::_1
1014 if (config::enable_multithreading) {
1015 lib::asio::async_connect(
1016 tcon->get_raw_socket(),
1018 tcon->get_strand()->wrap(lib::bind(
1019 &type::handle_connect,
1024 lib::placeholders::_1
1028 lib::asio::async_connect(
1029 tcon->get_raw_socket(),
1032 &type::handle_connect,
1037 lib::placeholders::_1
1056 lib::error_code ret_ec;
1061 "asio handle_connect_timeout timer cancelled");
1072 tcon->cancel_socket_checked();
1079 if (ec == lib::asio::error::operation_aborted ||
1080 lib::asio::is_neg(con_timer->expires_from_now()))
1086 con_timer->cancel();
1090 callback(socket_con_type::translate_ec(ec));
1096 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1099 callback(lib::error_code());
1122 ec = tcon->init_asio(m_io_service);
1123 if (ec) {
return ec;}
1125 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1126 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1128 return lib::error_code();
1132 template <
typename error_type>
1133 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1134 std::stringstream s;
1135 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1136 m_elog->write(l,s.str());
1140 template <
typename error_type>
1141 lib::error_code clean_up_listen_after_error(error_type
const & ec) {
1142 if (m_acceptor->is_open()) {
1143 m_acceptor->close();
1146 return socket_con_type::translate_ec(ec);
1157 tcp_init_handler m_tcp_pre_init_handler;
1158 tcp_init_handler m_tcp_post_init_handler;
1162 bool m_external_io_service;
1168 int m_listen_backlog;
1171 lib::shared_ptr<elog_type> m_elog;
1172 lib::shared_ptr<alog_type> m_alog;
Creates and manages connections associated with a WebSocket endpoint.
Asio based connection transport component.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
Asio based endpoint transport component.
std::size_t run_one()
wraps the run_one method of the internal io_service object
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
config::socket_type socket_type
Type of the socket policy.
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
void init_asio()
Initialize asio transport with internal io_service.
config::elog_type elog_type
Type of the error logging policy.
void init_logging(const lib::shared_ptr< alog_type > &a, const lib::shared_ptr< elog_type > &e)
Initialize logging.
bool is_secure() const
Return whether or not the endpoint produces secure connections.
void init_asio(io_service_ptr ptr)
initialize asio transport with external io_service
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code &ec)
Get local TCP endpoint.
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
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.
void stop()
wraps the stop method of the internal io_service object
std::size_t run()
wraps the run method of the internal io_service object
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
bool stopped() const
wraps the stopped method of the internal io_service object
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
asio::connection< config > transport_con_type
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_service (exception free)
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
void listen(uint16_t port)
Set up endpoint for listening on a port.
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)
endpoint< config > type
Type of this endpoint transport component.
lib::asio::io_service & get_io_service()
Retrieve a reference to the endpoint's io_service.
config::concurrency_type concurrency_type
Type of the concurrency policy.
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.
lib::shared_ptr< lib::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
void listen(lib::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
void handle_resolve_timeout(timer_ptr, connect_handler callback, lib::error_code const &ec)
DNS resolution timeout handler.
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
transport_con_type::ptr transport_con_ptr
lib::shared_ptr< lib::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
void reset()
wraps the reset method of the internal io_service object
void set_tcp_pre_bind_handler(tcp_pre_bind_handler h)
Sets the tcp pre bind handler.
config::alog_type alog_type
Type of the access logging policy.
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
void listen(InternetProtocol const &internet_protocol, uint16_t port)
Set up endpoint for listening with protocol and port.
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
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)
std::size_t poll_one()
wraps the poll_one method of the internal io_service object
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 listen(uint16_t port, lib::error_code &ec)
Set up endpoint for listening on a port (exception free)
bool is_listening() const
Check if the endpoint is listening.
void stop_listening()
Stop listening.
lib::shared_ptr< lib::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
lib::function< lib::error_code(acceptor_ptr)> tcp_pre_bind_handler
Type of socket pre-bind handler.
@ async_accept_not_listening
@ operation_canceled
The requested operation was canceled.
@ invalid_state
The connection was in the wrong state for this operation.
@ proxy_invalid
Invalid Proxy URI.
@ invalid_host_service
Invalid host or service.
@ operation_aborted
Operation aborted.
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
Namespace for the WebSocket++ project.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
static level const devel
Development messages (warning: very chatty)
static level const devel
Low level debugging information (warning: very chatty)
static level const library