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>
52 template <
typename config>
53 class endpoint :
public config::socket_type {
82 typedef lib::shared_ptr<boost::asio::ip::tcp::acceptor>
acceptor_ptr;
84 typedef lib::shared_ptr<boost::asio::ip::tcp::resolver>
resolver_ptr;
86 typedef lib::shared_ptr<boost::asio::deadline_timer>
timer_ptr;
88 typedef lib::shared_ptr<boost::asio::io_service::work>
work_ptr;
92 : m_external_io_service(false)
95 , m_state(UNINITIALIZED)
103 if (m_state != UNINITIALIZED && !m_external_io_service) {
111 #ifdef _WEBSOCKETPP_DELETED_FUNCTIONS_
121 #ifdef _WEBSOCKETPP_RVALUE_REFERENCES_
123 : m_io_service(src.m_io_service)
124 , m_external_io_service(src.m_external_io_service)
125 , m_acceptor(src.m_acceptor)
126 , m_listen_backlog(boost::asio::socket_base::max_connections)
127 , m_reuse_addr(src.m_reuse_addr)
128 , m_state(src.m_state)
130 src.m_io_service = NULL;
131 src.m_external_io_service =
false;
132 src.m_acceptor = NULL;
133 src.m_state = UNINITIALIZED;
136 endpoint& operator= (
const endpoint && rhs) {
138 m_io_service = rhs.m_io_service;
139 m_external_io_service = rhs.m_external_io_service;
140 m_acceptor = rhs.m_acceptor;
141 m_listen_backlog = rhs.m_listen_backlog;
142 m_reuse_addr = rhs.m_reuse_addr;
143 m_state = rhs.m_state;
145 rhs.m_io_service = NULL;
146 rhs.m_external_io_service =
false;
147 rhs.m_acceptor = NULL;
148 rhs.m_listen_backlog = boost::asio::socket_base::max_connections;
149 rhs.m_state = UNINITIALIZED;
156 return socket_type::is_secure();
168 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
169 if (m_state != UNINITIALIZED) {
171 "asio::init_asio called from the wrong state");
172 using websocketpp::error::make_error_code;
180 m_external_io_service =
true;
181 m_acceptor.reset(
new boost::asio::ip::tcp::acceptor(*m_io_service));
183 ec = lib::error_code();
212 init_asio(
new boost::asio::io_service(),ec);
213 m_external_io_service =
false;
224 init_asio(
new boost::asio::io_service());
225 m_external_io_service =
false;
239 m_tcp_pre_init_handler = h;
268 m_tcp_post_init_handler = h;
291 m_listen_backlog = backlog;
309 m_reuse_addr =
value;
324 return *m_io_service;
335 void listen(boost::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
337 if (m_state != READY) {
339 "asio::listen called from the wrong state");
340 using websocketpp::error::make_error_code;
347 boost::system::error_code bec;
349 m_acceptor->open(ep.protocol(),bec);
351 m_acceptor->set_option(boost::asio::socket_base::reuse_address(m_reuse_addr),bec);
354 m_acceptor->bind(ep,bec);
357 m_acceptor->listen(m_listen_backlog,bec);
364 ec = lib::error_code();
374 void listen(boost::asio::ip::tcp::endpoint
const & ep) {
396 template <
typename InternetProtocol>
397 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
398 lib::error_code & ec)
400 boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
417 template <
typename InternetProtocol>
418 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
420 boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
436 void listen(uint16_t port, lib::error_code & ec) {
437 listen(boost::asio::ip::tcp::v6(), port, ec);
453 listen(boost::asio::ip::tcp::v6(), port);
472 void listen(std::string
const & host, std::string
const & service,
473 lib::error_code & ec)
475 using boost::asio::ip::tcp;
476 tcp::resolver r(*m_io_service);
477 tcp::resolver::query query(host, service);
478 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
479 tcp::resolver::iterator end;
480 if (endpoint_iterator == end) {
482 "asio::listen could not resolve the supplied host or service");
486 listen(*endpoint_iterator,ec);
505 void listen(std::string
const & host, std::string
const & service)
523 if (m_state != LISTENING) {
525 "asio::listen called from the wrong state");
526 using websocketpp::error::make_error_code;
533 ec = lib::error_code();
556 return (m_state == LISTENING);
561 return m_io_service->run();
569 return m_io_service->run_one();
574 m_io_service->stop();
579 return m_io_service->poll();
584 return m_io_service->poll_one();
589 m_io_service->reset();
594 return m_io_service->stopped();
610 m_work.reset(
new boost::asio::io_service::work(*m_io_service));
639 new boost::asio::deadline_timer(
641 boost::posix_time::milliseconds(duration)
645 new_timer->async_wait(
651 lib::placeholders::_1
668 boost::system::error_code
const & ec)
671 if (ec == boost::asio::error::operation_aborted) {
675 "asio handle_timer error: "+ec.message());
680 callback(lib::error_code());
691 lib::error_code & ec)
693 if (m_state != LISTENING) {
694 using websocketpp::error::make_error_code;
701 if (config::enable_multithreading) {
702 m_acceptor->async_accept(
703 tcon->get_raw_socket(),
704 tcon->get_strand()->wrap(lib::bind(
705 &type::handle_accept,
708 lib::placeholders::_1
712 m_acceptor->async_accept(
713 tcon->get_raw_socket(),
715 &type::handle_accept,
718 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.reset(
new boost::asio::ip::tcp::resolver(*m_io_service));
781 std::string proxy = tcon->get_proxy();
786 host = u->get_host();
787 port = u->get_port_str();
793 if (!pu->get_valid()) {
798 ec = tcon->proxy_init(u->get_authority());
804 host = pu->get_host();
805 port = pu->get_port_str();
808 tcp::resolver::query query(host,port);
812 "starting async DNS resolve for "+host+
":"+port);
817 dns_timer = tcon->set_timer(
818 config::timeout_dns_resolve,
820 &type::handle_resolve_timeout,
824 lib::placeholders::_1
828 if (config::enable_multithreading) {
829 m_resolver->async_resolve(
831 tcon->get_strand()->wrap(lib::bind(
832 &type::handle_resolve,
837 lib::placeholders::_1,
838 lib::placeholders::_2
842 m_resolver->async_resolve(
845 &type::handle_resolve,
850 lib::placeholders::_1,
851 lib::placeholders::_2
857 void handle_resolve_timeout(timer_ptr dns_timer,
connect_handler callback,
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,
918 &type::handle_connect_timeout,
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
956 void handle_connect_timeout(transport_con_ptr tcon, timer_ptr con_timer,
959 lib::error_code ret_ec;
964 "asio handle_connect_timeout timer cancelled");
975 tcon->cancel_socket();
979 void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
982 if (ec == boost::asio::error::operation_aborted ||
983 con_timer->expires_from_now().is_negative())
999 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1002 callback(lib::error_code());
1016 lib::error_code
init(transport_con_ptr tcon) {
1020 socket_type::init(lib::static_pointer_cast<socket_con_type,
1021 transport_con_type>(tcon));
1025 ec = tcon->init_asio(m_io_service);
1026 if (ec) {
return ec;}
1028 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1029 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1031 return lib::error_code();
1035 template <
typename error_type>
1036 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1037 std::stringstream s;
1038 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1039 m_elog->write(l,s.str());
1049 tcp_init_handler m_tcp_pre_init_handler;
1050 tcp_init_handler m_tcp_post_init_handler;
1053 io_service_ptr m_io_service;
1054 bool m_external_io_service;
1055 acceptor_ptr m_acceptor;
1056 resolver_ptr m_resolver;
1060 int m_listen_backlog;
1074 #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.
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 handle_timer(timer_ptr t, timer_handler callback, boost::system::error_code const &ec)
Timer callback.
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 or not to use the SO_REUSEADDR flag when opening a listening socket. ...
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 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
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.