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/functional.hpp>
52 template <
typename config>
53 class endpoint :
public config::socket_type {
82 typedef lib::shared_ptr<lib::asio::ip::tcp::acceptor>
acceptor_ptr;
84 typedef lib::shared_ptr<lib::asio::ip::tcp::resolver>
resolver_ptr;
86 typedef lib::shared_ptr<lib::asio::steady_timer>
timer_ptr;
88 typedef lib::shared_ptr<lib::asio::io_service::work>
work_ptr;
93 , m_external_io_service(false)
96 , m_state(UNINITIALIZED)
104 if (m_state != UNINITIALIZED && !m_external_io_service) {
112 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
120 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
122 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
124 : config::socket_type(
std::move(src))
125 , m_tcp_pre_init_handler(src.m_tcp_pre_init_handler)
126 , m_tcp_post_init_handler(src.m_tcp_post_init_handler)
127 , m_io_service(src.m_io_service)
128 , m_external_io_service(src.m_external_io_service)
129 , m_acceptor(src.m_acceptor)
130 , m_listen_backlog(lib::asio::socket_base::max_connections)
131 , m_reuse_addr(src.m_reuse_addr)
134 , m_state(src.m_state)
136 src.m_io_service = NULL;
137 src.m_external_io_service =
false;
138 src.m_acceptor = NULL;
139 src.m_state = UNINITIALIZED;
161 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
165 return socket_type::is_secure();
177 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
178 if (m_state != UNINITIALIZED) {
180 "asio::init_asio called from the wrong state");
181 using websocketpp::error::make_error_code;
189 m_external_io_service =
true;
190 m_acceptor = lib::make_shared<lib::asio::ip::tcp::acceptor>(
191 lib::ref(*m_io_service));
194 ec = lib::error_code();
221 init_asio(
new lib::asio::io_service(), ec);
222 m_external_io_service =
false;
234 m_external_io_service =
false;
248 m_tcp_pre_init_handler = h;
277 m_tcp_post_init_handler = h;
300 m_listen_backlog = backlog;
318 m_reuse_addr =
value;
333 return *m_io_service;
344 void listen(lib::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
346 if (m_state != READY) {
348 "asio::listen called from the wrong state");
349 using websocketpp::error::make_error_code;
356 lib::asio::error_code bec;
358 m_acceptor->open(ep.protocol(),bec);
360 m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
363 m_acceptor->bind(ep,bec);
366 m_acceptor->listen(m_listen_backlog,bec);
369 if (m_acceptor->is_open()) {
376 ec = lib::error_code();
386 void listen(lib::asio::ip::tcp::endpoint
const & ep) {
406 template <
typename InternetProtocol>
407 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
408 lib::error_code & ec)
410 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
427 template <
typename InternetProtocol>
428 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
430 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
446 void listen(uint16_t port, lib::error_code & ec) {
447 listen(lib::asio::ip::tcp::v6(), port, ec);
463 listen(lib::asio::ip::tcp::v6(), port);
482 void listen(std::string
const & host, std::string
const & service,
483 lib::error_code & ec)
485 using lib::asio::ip::tcp;
486 tcp::resolver r(*m_io_service);
487 tcp::resolver::query query(host, service);
488 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
489 tcp::resolver::iterator end;
490 if (endpoint_iterator == end) {
492 "asio::listen could not resolve the supplied host or service");
496 listen(*endpoint_iterator,ec);
515 void listen(std::string
const & host, std::string
const & service)
531 if (m_state != LISTENING) {
533 "asio::listen called from the wrong state");
534 using websocketpp::error::make_error_code;
541 ec = lib::error_code();
562 return (m_state == LISTENING);
567 return m_io_service->run();
575 return m_io_service->run_one();
580 m_io_service->stop();
585 return m_io_service->poll();
590 return m_io_service->poll_one();
595 m_io_service->reset();
600 return m_io_service->stopped();
616 m_work = lib::make_shared<lib::asio::io_service::work>(
617 lib::ref(*m_io_service)
646 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
648 lib::asio::milliseconds(duration)
651 new_timer->async_wait(
657 lib::placeholders::_1
674 lib::asio::error_code
const & ec)
677 if (ec == lib::asio::error::operation_aborted) {
681 "asio handle_timer error: "+ec.message());
686 callback(lib::error_code());
697 lib::error_code & ec)
699 if (m_state != LISTENING) {
700 using websocketpp::error::make_error_code;
707 if (config::enable_multithreading) {
708 m_acceptor->async_accept(
709 tcon->get_raw_socket(),
710 tcon->get_strand()->wrap(lib::bind(
711 &type::handle_accept,
714 lib::placeholders::_1
718 m_acceptor->async_accept(
719 tcon->get_raw_socket(),
721 &type::handle_accept,
724 lib::placeholders::_1
756 void handle_accept(
accept_handler callback, lib::asio::error_code
const &
759 lib::error_code ret_ec;
764 if (asio_ec == lib::asio::errc::operation_canceled) {
778 using namespace lib::asio::ip;
782 m_resolver = lib::make_shared<lib::asio::ip::tcp::resolver>(
783 lib::ref(*m_io_service));
788 std::string proxy = tcon->get_proxy();
793 host = u->get_host();
794 port = u->get_port_str();
798 uri_ptr pu = lib::make_shared<uri>(proxy);
800 if (!pu->get_valid()) {
805 ec = tcon->proxy_init(u->get_authority());
811 host = pu->get_host();
812 port = pu->get_port_str();
815 tcp::resolver::query query(host,port);
819 "starting async DNS resolve for "+host+
":"+port);
824 dns_timer = tcon->set_timer(
825 config::timeout_dns_resolve,
831 lib::placeholders::_1
835 if (config::enable_multithreading) {
836 m_resolver->async_resolve(
838 tcon->get_strand()->wrap(lib::bind(
839 &type::handle_resolve,
844 lib::placeholders::_1,
845 lib::placeholders::_2
849 m_resolver->async_resolve(
852 &type::handle_resolve,
857 lib::placeholders::_1,
858 lib::placeholders::_2
874 lib::error_code
const & ec)
876 lib::error_code ret_ec;
881 "asio handle_resolve_timeout timer cancelled");
892 m_resolver->cancel();
896 void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer,
898 lib::asio::ip::tcp::resolver::iterator iterator)
900 if (ec == lib::asio::error::operation_aborted ||
901 lib::asio::is_neg(dns_timer->expires_from_now()))
917 s <<
"Async DNS resolve successful. Results: ";
919 lib::asio::ip::tcp::resolver::iterator it, end;
920 for (it = iterator; it != end; ++it) {
921 s << (*it).endpoint() <<
" ";
931 con_timer = tcon->set_timer(
932 config::timeout_connect,
939 lib::placeholders::_1
943 if (config::enable_multithreading) {
944 lib::asio::async_connect(
945 tcon->get_raw_socket(),
947 tcon->get_strand()->wrap(lib::bind(
948 &type::handle_connect,
953 lib::placeholders::_1
957 lib::asio::async_connect(
958 tcon->get_raw_socket(),
961 &type::handle_connect,
966 lib::placeholders::_1
985 lib::error_code ret_ec;
990 "asio handle_connect_timeout timer cancelled");
1001 tcon->cancel_socket();
1005 void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
1008 if (ec == lib::asio::error::operation_aborted ||
1009 lib::asio::is_neg(con_timer->expires_from_now()))
1015 con_timer->cancel();
1025 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1028 callback(lib::error_code());
1042 lib::error_code
init(transport_con_ptr tcon) {
1046 socket_type::init(lib::static_pointer_cast<socket_con_type,
1047 transport_con_type>(tcon));
1051 ec = tcon->init_asio(m_io_service);
1052 if (ec) {
return ec;}
1054 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1055 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1057 return lib::error_code();
1061 template <
typename error_type>
1062 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1063 std::stringstream s;
1064 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1065 m_elog->write(l,s.str());
1075 tcp_init_handler m_tcp_pre_init_handler;
1076 tcp_init_handler m_tcp_post_init_handler;
1079 io_service_ptr m_io_service;
1080 bool m_external_io_service;
1081 acceptor_ptr m_acceptor;
1082 resolver_ptr m_resolver;
1086 int m_listen_backlog;
1100 #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 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 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.
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.
static level const devel
Low level debugging information (warning: very chatty)
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.
bool is_secure() const
Return whether or not the endpoint produces secure connections.
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.
bool is_listening() const
Check if the endpoint is 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.
there was an error in the underlying transport library
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)
Creates and manages connections associated with a WebSocket endpoint.
Asio based connection transport component.
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_logging(alog_type *a, elog_type *e)
Initialize logging.
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 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.