28 #ifndef WEBSOCKETPP_TRANSPORT_ASIO_HPP
29 #define WEBSOCKETPP_TRANSPORT_ASIO_HPP
31 #include <websocketpp/common/functional.hpp>
32 #include <websocketpp/logger/levels.hpp>
33 #include <websocketpp/transport/base/endpoint.hpp>
34 #include <websocketpp/transport/asio/connection.hpp>
35 #include <websocketpp/transport/asio/security/none.hpp>
37 #include <boost/asio.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/system/error_code.hpp>
50 template <
typename config>
51 class endpoint :
public config::socket_type {
80 typedef lib::shared_ptr<boost::asio::ip::tcp::acceptor>
acceptor_ptr;
82 typedef lib::shared_ptr<boost::asio::ip::tcp::resolver>
resolver_ptr;
84 typedef lib::shared_ptr<boost::asio::deadline_timer>
timer_ptr;
86 typedef lib::shared_ptr<boost::asio::io_service::work>
work_ptr;
90 : m_external_io_service(false)
93 , m_state(UNINITIALIZED)
101 if (m_state != UNINITIALIZED && !m_external_io_service) {
109 #ifdef _WEBSOCKETPP_DELETED_FUNCTIONS_
119 #ifdef _WEBSOCKETPP_RVALUE_REFERENCES_
121 : m_io_service(src.m_io_service)
122 , m_external_io_service(src.m_external_io_service)
123 , m_acceptor(src.m_acceptor)
124 , m_listen_backlog(boost::asio::socket_base::max_connections)
125 , m_reuse_addr(src.m_reuse_addr)
126 , m_state(src.m_state)
128 src.m_io_service = NULL;
129 src.m_external_io_service =
false;
130 src.m_acceptor = NULL;
131 src.m_state = UNINITIALIZED;
134 endpoint& operator= (
const endpoint && rhs) {
136 m_io_service = rhs.m_io_service;
137 m_external_io_service = rhs.m_external_io_service;
138 m_acceptor = rhs.m_acceptor;
139 m_listen_backlog = rhs.m_listen_backlog;
140 m_reuse_addr = rhs.m_reuse_addr;
141 m_state = rhs.m_state;
143 rhs.m_io_service = NULL;
144 rhs.m_external_io_service =
false;
145 rhs.m_acceptor = NULL;
146 rhs.m_listen_backlog = boost::asio::socket_base::max_connections;
147 rhs.m_state = UNINITIALIZED;
154 return socket_type::is_secure();
166 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
167 if (m_state != UNINITIALIZED) {
169 "asio::init_asio called from the wrong state");
170 using websocketpp::error::make_error_code;
178 m_external_io_service =
true;
179 m_acceptor = lib::make_shared<boost::asio::ip::tcp::acceptor>(
180 lib::ref(*m_io_service));
183 ec = lib::error_code();
210 init_asio(
new boost::asio::io_service(), ec);
211 m_external_io_service =
false;
222 init_asio(
new boost::asio::io_service());
223 m_external_io_service =
false;
237 m_tcp_pre_init_handler = h;
266 m_tcp_post_init_handler = h;
289 m_listen_backlog = backlog;
307 m_reuse_addr =
value;
322 return *m_io_service;
333 void listen(boost::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
335 if (m_state != READY) {
337 "asio::listen called from the wrong state");
338 using websocketpp::error::make_error_code;
345 boost::system::error_code bec;
347 m_acceptor->open(ep.protocol(),bec);
349 m_acceptor->set_option(boost::asio::socket_base::reuse_address(m_reuse_addr),bec);
352 m_acceptor->bind(ep,bec);
355 m_acceptor->listen(m_listen_backlog,bec);
362 ec = lib::error_code();
372 void listen(boost::asio::ip::tcp::endpoint
const & ep) {
392 template <
typename InternetProtocol>
393 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
394 lib::error_code & ec)
396 boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
413 template <
typename InternetProtocol>
414 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
416 boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
432 void listen(uint16_t port, lib::error_code & ec) {
433 listen(boost::asio::ip::tcp::v6(), port, ec);
449 listen(boost::asio::ip::tcp::v6(), port);
468 void listen(std::string
const & host, std::string
const & service,
469 lib::error_code & ec)
471 using boost::asio::ip::tcp;
472 tcp::resolver r(*m_io_service);
473 tcp::resolver::query query(host, service);
474 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
475 tcp::resolver::iterator end;
476 if (endpoint_iterator == end) {
478 "asio::listen could not resolve the supplied host or service");
482 listen(*endpoint_iterator,ec);
501 void listen(std::string
const & host, std::string
const & service)
517 if (m_state != LISTENING) {
519 "asio::listen called from the wrong state");
520 using websocketpp::error::make_error_code;
527 ec = lib::error_code();
548 return (m_state == LISTENING);
553 return m_io_service->run();
561 return m_io_service->run_one();
566 m_io_service->stop();
571 return m_io_service->poll();
576 return m_io_service->poll_one();
581 m_io_service->reset();
586 return m_io_service->stopped();
602 m_work = lib::make_shared<boost::asio::io_service::work>(
603 lib::ref(*m_io_service)
632 timer_ptr new_timer = lib::make_shared<boost::asio::deadline_timer>(
634 boost::posix_time::milliseconds(duration)
637 new_timer->async_wait(
643 lib::placeholders::_1
660 boost::system::error_code
const & ec)
663 if (ec == boost::asio::error::operation_aborted) {
667 "asio handle_timer error: "+ec.message());
672 callback(lib::error_code());
683 lib::error_code & ec)
685 if (m_state != LISTENING) {
686 using websocketpp::error::make_error_code;
693 if (config::enable_multithreading) {
694 m_acceptor->async_accept(
695 tcon->get_raw_socket(),
696 tcon->get_strand()->wrap(lib::bind(
697 &type::handle_accept,
700 lib::placeholders::_1
704 m_acceptor->async_accept(
705 tcon->get_raw_socket(),
707 &type::handle_accept,
710 lib::placeholders::_1
742 void handle_accept(
accept_handler callback, boost::system::error_code
const
745 lib::error_code ret_ec;
750 if (boost_ec == boost::system::errc::operation_canceled) {
764 using namespace boost::asio::ip;
768 m_resolver = lib::make_shared<boost::asio::ip::tcp::resolver>(
769 lib::ref(*m_io_service));
772 std::string proxy = tcon->get_proxy();
777 host = u->get_host();
778 port = u->get_port_str();
782 uri_ptr pu = lib::make_shared<uri>(proxy);
784 if (!pu->get_valid()) {
789 ec = tcon->proxy_init(u->get_authority());
795 host = pu->get_host();
796 port = pu->get_port_str();
799 tcp::resolver::query query(host,port);
803 "starting async DNS resolve for "+host+
":"+port);
808 dns_timer = tcon->set_timer(
809 config::timeout_dns_resolve,
815 lib::placeholders::_1
819 if (config::enable_multithreading) {
820 m_resolver->async_resolve(
822 tcon->get_strand()->wrap(lib::bind(
823 &type::handle_resolve,
828 lib::placeholders::_1,
829 lib::placeholders::_2
833 m_resolver->async_resolve(
836 &type::handle_resolve,
841 lib::placeholders::_1,
842 lib::placeholders::_2
858 lib::error_code
const & ec)
860 lib::error_code ret_ec;
865 "asio handle_resolve_timeout timer cancelled");
876 m_resolver->cancel();
880 void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer,
882 boost::asio::ip::tcp::resolver::iterator iterator)
884 if (ec == boost::asio::error::operation_aborted ||
885 dns_timer->expires_from_now().is_negative())
901 s <<
"Async DNS resolve successful. Results: ";
903 boost::asio::ip::tcp::resolver::iterator it, end;
904 for (it = iterator; it != end; ++it) {
905 s << (*it).endpoint() <<
" ";
915 con_timer = tcon->set_timer(
916 config::timeout_connect,
923 lib::placeholders::_1
927 if (config::enable_multithreading) {
928 boost::asio::async_connect(
929 tcon->get_raw_socket(),
931 tcon->get_strand()->wrap(lib::bind(
932 &type::handle_connect,
937 lib::placeholders::_1
941 boost::asio::async_connect(
942 tcon->get_raw_socket(),
945 &type::handle_connect,
950 lib::placeholders::_1
969 lib::error_code ret_ec;
974 "asio handle_connect_timeout timer cancelled");
985 tcon->cancel_socket();
989 void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
992 if (ec == boost::asio::error::operation_aborted ||
993 con_timer->expires_from_now().is_negative())
1009 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1012 callback(lib::error_code());
1026 lib::error_code
init(transport_con_ptr tcon) {
1030 socket_type::init(lib::static_pointer_cast<socket_con_type,
1031 transport_con_type>(tcon));
1035 ec = tcon->init_asio(m_io_service);
1036 if (ec) {
return ec;}
1038 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1039 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1041 return lib::error_code();
1045 template <
typename error_type>
1046 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1047 std::stringstream s;
1048 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1049 m_elog->write(l,s.str());
1059 tcp_init_handler m_tcp_pre_init_handler;
1060 tcp_init_handler m_tcp_post_init_handler;
1063 io_service_ptr m_io_service;
1064 bool m_external_io_service;
1065 acceptor_ptr m_acceptor;
1066 resolver_ptr m_resolver;
1070 int m_listen_backlog;
1084 #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.