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>
40 #include <boost/asio.hpp>
41 #include <boost/bind.hpp>
42 #include <boost/system/error_code.hpp>
56 template <
typename config>
57 class endpoint :
public config::socket_type {
86 typedef lib::shared_ptr<boost::asio::ip::tcp::acceptor>
acceptor_ptr;
88 typedef lib::shared_ptr<boost::asio::ip::tcp::resolver>
resolver_ptr;
90 typedef lib::shared_ptr<boost::asio::deadline_timer>
timer_ptr;
92 typedef lib::shared_ptr<boost::asio::io_service::work>
work_ptr;
97 , m_external_io_service(false)
100 , m_state(UNINITIALIZED)
108 if (m_state != UNINITIALIZED && !m_external_io_service) {
116 #ifdef _WEBSOCKETPP_DELETED_FUNCTIONS_
126 #ifdef _WEBSOCKETPP_RVALUE_REFERENCES_
128 : m_io_service(src.m_io_service)
129 , m_external_io_service(src.m_external_io_service)
130 , m_acceptor(src.m_acceptor)
131 , m_listen_backlog(boost::asio::socket_base::max_connections)
132 , m_reuse_addr(src.m_reuse_addr)
133 , m_state(src.m_state)
135 src.m_io_service = NULL;
136 src.m_external_io_service =
false;
137 src.m_acceptor = NULL;
138 src.m_state = UNINITIALIZED;
141 endpoint& operator= (
const endpoint && rhs) {
143 m_io_service = rhs.m_io_service;
144 m_external_io_service = rhs.m_external_io_service;
145 m_acceptor = rhs.m_acceptor;
146 m_listen_backlog = rhs.m_listen_backlog;
147 m_reuse_addr = rhs.m_reuse_addr;
148 m_state = rhs.m_state;
150 rhs.m_io_service = NULL;
151 rhs.m_external_io_service =
false;
152 rhs.m_acceptor = NULL;
153 rhs.m_listen_backlog = boost::asio::socket_base::max_connections;
154 rhs.m_state = UNINITIALIZED;
161 return socket_type::is_secure();
173 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
174 if (m_state != UNINITIALIZED) {
176 "asio::init_asio called from the wrong state");
177 using websocketpp::error::make_error_code;
185 m_external_io_service =
true;
186 m_acceptor = lib::make_shared<boost::asio::ip::tcp::acceptor>(
187 lib::ref(*m_io_service));
190 ec = lib::error_code();
217 init_asio(
new boost::asio::io_service(), ec);
218 m_external_io_service =
false;
229 init_asio(
new boost::asio::io_service());
230 m_external_io_service =
false;
244 m_tcp_pre_init_handler = h;
273 m_tcp_post_init_handler = h;
296 m_listen_backlog = backlog;
314 m_reuse_addr =
value;
329 return *m_io_service;
340 void listen(boost::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
342 if (m_state != READY) {
344 "asio::listen called from the wrong state");
345 using websocketpp::error::make_error_code;
352 boost::system::error_code bec;
354 m_acceptor->open(ep.protocol(),bec);
356 m_acceptor->set_option(boost::asio::socket_base::reuse_address(m_reuse_addr),bec);
359 m_acceptor->bind(ep,bec);
362 m_acceptor->listen(m_listen_backlog,bec);
365 if (m_acceptor->is_open()) {
372 ec = lib::error_code();
382 void listen(boost::asio::ip::tcp::endpoint
const & ep) {
402 template <
typename InternetProtocol>
403 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
404 lib::error_code & ec)
406 boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
423 template <
typename InternetProtocol>
424 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
426 boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
442 void listen(uint16_t port, lib::error_code & ec) {
443 listen(boost::asio::ip::tcp::v6(), port, ec);
459 listen(boost::asio::ip::tcp::v6(), port);
478 void listen(std::string
const & host, std::string
const & service,
479 lib::error_code & ec)
481 using boost::asio::ip::tcp;
482 tcp::resolver r(*m_io_service);
483 tcp::resolver::query query(host, service);
484 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
485 tcp::resolver::iterator end;
486 if (endpoint_iterator == end) {
488 "asio::listen could not resolve the supplied host or service");
492 listen(*endpoint_iterator,ec);
511 void listen(std::string
const & host, std::string
const & service)
527 if (m_state != LISTENING) {
529 "asio::listen called from the wrong state");
530 using websocketpp::error::make_error_code;
537 ec = lib::error_code();
558 return (m_state == LISTENING);
563 return m_io_service->run();
571 return m_io_service->run_one();
576 m_io_service->stop();
581 return m_io_service->poll();
586 return m_io_service->poll_one();
591 m_io_service->reset();
596 return m_io_service->stopped();
612 m_work = lib::make_shared<boost::asio::io_service::work>(
613 lib::ref(*m_io_service)
642 timer_ptr new_timer = lib::make_shared<boost::asio::deadline_timer>(
644 boost::posix_time::milliseconds(duration)
647 new_timer->async_wait(
653 lib::placeholders::_1
670 boost::system::error_code
const & ec)
673 if (ec == boost::asio::error::operation_aborted) {
677 "asio handle_timer error: "+ec.message());
682 callback(lib::error_code());
693 lib::error_code & ec)
695 if (m_state != LISTENING) {
696 using websocketpp::error::make_error_code;
703 if (config::enable_multithreading) {
704 m_acceptor->async_accept(
705 tcon->get_raw_socket(),
706 tcon->get_strand()->wrap(lib::bind(
707 &type::handle_accept,
710 lib::placeholders::_1
714 m_acceptor->async_accept(
715 tcon->get_raw_socket(),
717 &type::handle_accept,
720 lib::placeholders::_1
752 void handle_accept(
accept_handler callback, boost::system::error_code
const
755 lib::error_code ret_ec;
760 if (boost_ec == boost::system::errc::operation_canceled) {
774 using namespace boost::asio::ip;
778 m_resolver = lib::make_shared<boost::asio::ip::tcp::resolver>(
779 lib::ref(*m_io_service));
782 std::string proxy = tcon->get_proxy();
787 host = u->get_host();
788 port = u->get_port_str();
792 uri_ptr pu = lib::make_shared<uri>(proxy);
794 if (!pu->get_valid()) {
799 ec = tcon->proxy_init(u->get_authority());
805 host = pu->get_host();
806 port = pu->get_port_str();
809 tcp::resolver::query query(host,port);
813 "starting async DNS resolve for "+host+
":"+port);
818 dns_timer = tcon->set_timer(
819 config::timeout_dns_resolve,
825 lib::placeholders::_1
829 if (config::enable_multithreading) {
830 m_resolver->async_resolve(
832 tcon->get_strand()->wrap(lib::bind(
833 &type::handle_resolve,
838 lib::placeholders::_1,
839 lib::placeholders::_2
843 m_resolver->async_resolve(
846 &type::handle_resolve,
851 lib::placeholders::_1,
852 lib::placeholders::_2
868 lib::error_code
const & ec)
870 lib::error_code ret_ec;
875 "asio handle_resolve_timeout timer cancelled");
886 m_resolver->cancel();
890 void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer,
892 boost::asio::ip::tcp::resolver::iterator iterator)
894 if (ec == boost::asio::error::operation_aborted ||
895 dns_timer->expires_from_now().is_negative())
911 s <<
"Async DNS resolve successful. Results: ";
913 boost::asio::ip::tcp::resolver::iterator it, end;
914 for (it = iterator; it != end; ++it) {
915 s << (*it).endpoint() <<
" ";
925 con_timer = tcon->set_timer(
926 config::timeout_connect,
933 lib::placeholders::_1
937 if (config::enable_multithreading) {
938 boost::asio::async_connect(
939 tcon->get_raw_socket(),
941 tcon->get_strand()->wrap(lib::bind(
942 &type::handle_connect,
947 lib::placeholders::_1
951 boost::asio::async_connect(
952 tcon->get_raw_socket(),
955 &type::handle_connect,
960 lib::placeholders::_1
979 lib::error_code ret_ec;
984 "asio handle_connect_timeout timer cancelled");
995 tcon->cancel_socket();
999 void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
1002 if (ec == boost::asio::error::operation_aborted ||
1003 con_timer->expires_from_now().is_negative())
1009 con_timer->cancel();
1019 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1022 callback(lib::error_code());
1036 lib::error_code
init(transport_con_ptr tcon) {
1040 socket_type::init(lib::static_pointer_cast<socket_con_type,
1041 transport_con_type>(tcon));
1045 ec = tcon->init_asio(m_io_service);
1046 if (ec) {
return ec;}
1048 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1049 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1051 return lib::error_code();
1055 template <
typename error_type>
1056 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1057 std::stringstream s;
1058 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1059 m_elog->write(l,s.str());
1069 tcp_init_handler m_tcp_pre_init_handler;
1070 tcp_init_handler m_tcp_post_init_handler;
1073 io_service_ptr m_io_service;
1074 bool m_external_io_service;
1075 acceptor_ptr m_acceptor;
1076 resolver_ptr m_resolver;
1080 int m_listen_backlog;
1094 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
lib::shared_ptr< boost::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
transport_con_type::ptr transport_con_ptr
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
lib::shared_ptr< boost::asio::deadline_timer > timer_ptr
Type of timer handle.
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
uint16_t value
The type of a close code value.
lib::shared_ptr< boost::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
config::alog_type alog_type
Type of the access logging policy.
void listen(boost::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
void listen(InternetProtocol const &internet_protocol, uint16_t port)
Set up endpoint for listening with protocol and port.
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
void listen(uint16_t port)
Set up endpoint for listening on a port.
void stop_listening()
Stop listening.
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_service (exception free)
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
void start_perpetual()
Marks the endpoint as perpetual, stopping it from exiting when empty.
bool is_secure() const
Return whether or not the endpoint produces secure connections.
void init_asio()
Initialize asio transport with internal io_service.
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 init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
std::size_t run_one()
wraps the run_one method of the internal io_service object
static level const devel
Low level debugging information (warning: very chatty)
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.
boost::asio::io_service & get_io_service()
Retrieve a reference to the endpoint's io_service.
config::elog_type elog_type
Type of the error logging policy.
asio::connection< config > transport_con_type
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
static level const devel
Development messages (warning: very chatty)
void reset()
wraps the reset method of the internal io_service object
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
void handle_timer(timer_ptr, timer_handler callback, boost::system::error_code const &ec)
Timer handler.
void listen(InternetProtocol const &internet_protocol, uint16_t port, lib::error_code &ec)
Set up endpoint for listening with protocol and port (exception free)
lib::shared_ptr< boost::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
void stop_perpetual()
Clears the endpoint's perpetual flag, allowing it to exit when empty.
void listen(boost::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
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 poll()
wraps the poll method of the internal io_service object
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
The requested operation was canceled.
void stop()
wraps the stop method of the internal io_service object
Creates and manages connections associated with a WebSocket endpoint.
Boost Asio based connection transport component.
Boost Asio based endpoint transport component.
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
std::size_t run()
wraps the run method of the internal io_service object
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)
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
config::socket_type socket_type
Type of the socket policy.
bool stopped() const
wraps the stopped method of the internal io_service object
config::concurrency_type concurrency_type
Type of the concurrency policy.
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
void init_asio(io_service_ptr ptr)
initialize asio transport with external io_service
std::size_t poll_one()
wraps the poll_one method of the internal io_service object
void handle_resolve_timeout(timer_ptr, connect_handler callback, lib::error_code const &ec)
DNS resolution timeout handler.
endpoint< config > type
Type of this endpoint transport component.
static level const library
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
void init_logging(alog_type *a, elog_type *e)
Initialize logging.