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 {
93 , m_external_io_service(
false)
96 , m_state(UNINITIALIZED)
108 if (m_state != UNINITIALIZED && !m_external_io_service) {
116 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_ 126 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_ 182 if (m_state != UNINITIALIZED) {
183 m_elog->write(log::elevel::library,
184 "asio::init_asio called from the wrong state");
185 using websocketpp::error::make_error_code;
186 ec = make_error_code(websocketpp::error::invalid_state);
190 m_alog->write(log::alevel::devel,
"asio::init_asio");
193 m_external_io_service =
true;
194 m_acceptor = lib::make_shared<lib::asio::ip::tcp::acceptor>(
195 lib::ref(*m_io_service));
198 ec = lib::error_code();
212 if (ec) {
throw exception(ec); }
229 #ifdef _WEBSOCKETPP_CPP11_MEMORY_ 230 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
232 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
234 init_asio(service.get(), ec);
235 if( !ec ) service.release();
236 m_external_io_service =
false;
251 #ifdef _WEBSOCKETPP_CPP11_MEMORY_ 252 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
254 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
256 init_asio( service.get() );
259 m_external_io_service =
false;
273 m_tcp_pre_init_handler = h;
287 set_tcp_pre_init_handler(h);
302 m_tcp_post_init_handler = h;
325 m_listen_backlog = backlog;
343 m_reuse_addr = value;
358 return *m_io_service;
376 return m_acceptor->local_endpoint(ec);
378 ec = lib::asio::error::make_error_code(lib::asio::error::bad_descriptor);
379 return lib::asio::ip::tcp::endpoint();
393 if (m_state != READY) {
394 m_elog->write(log::elevel::library,
395 "asio::listen called from the wrong state");
396 using websocketpp::error::make_error_code;
397 ec = make_error_code(websocketpp::error::invalid_state);
401 m_alog->write(log::alevel::devel,
"asio::listen");
403 lib::asio::error_code bec;
405 m_acceptor->open(ep.protocol(),bec);
407 m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
410 m_acceptor->bind(ep,bec);
413 m_acceptor->listen(m_listen_backlog,bec);
416 if (m_acceptor->is_open()) {
419 log_err(log::elevel::info,
"asio listen",bec);
420 ec = make_error_code(error::pass_through);
423 ec = lib::error_code();
436 if (ec) {
throw exception(ec); }
453 template <
typename InternetProtocol>
454 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
455 lib::error_code & ec)
457 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
474 template <
typename InternetProtocol>
475 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
477 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
493 void listen(uint16_t port, lib::error_code & ec) {
494 listen(lib::asio::ip::tcp::v6(), port, ec);
510 listen(lib::asio::ip::tcp::v6(), port);
529 void listen(std::string
const & host, std::string
const & service,
530 lib::error_code & ec)
532 using lib::asio::ip::tcp;
533 tcp::resolver r(*m_io_service);
534 tcp::resolver::query query(host, service);
535 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
536 tcp::resolver::iterator end;
537 if (endpoint_iterator == end) {
538 m_elog->write(log::elevel::library,
539 "asio::listen could not resolve the supplied host or service");
540 ec = make_error_code(error::invalid_host_service);
543 listen(*endpoint_iterator,ec);
562 void listen(std::string
const & host, std::string
const & service)
565 listen(host,service,ec);
566 if (ec) {
throw exception(ec); }
578 if (m_state != LISTENING) {
579 m_elog->write(log::elevel::library,
580 "asio::listen called from the wrong state");
581 using websocketpp::error::make_error_code;
582 ec = make_error_code(websocketpp::error::invalid_state);
588 ec = lib::error_code();
601 if (ec) {
throw exception(ec); }
609 return (m_state == LISTENING);
614 return m_io_service->run();
622 return m_io_service->run_one();
627 m_io_service->stop();
632 return m_io_service->poll();
637 return m_io_service->poll_one();
642 m_io_service->reset();
647 return m_io_service->stopped();
663 m_work = lib::make_shared<lib::asio::io_service::work>(
664 lib::ref(*m_io_service)
693 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
695 lib::asio::milliseconds(duration)
698 new_timer->async_wait(
704 lib::placeholders::_1
724 if (ec == lib::asio::error::operation_aborted) {
725 callback(make_error_code(transport::error::operation_aborted));
727 m_elog->write(log::elevel::info,
728 "asio handle_timer error: "+ec.message());
729 log_err(log::elevel::info,
"asio handle_timer",ec);
730 callback(make_error_code(error::pass_through));
733 callback(lib::error_code());
744 lib::error_code & ec)
746 if (m_state != LISTENING) {
747 using websocketpp::error::make_error_code;
748 ec = make_error_code(websocketpp::error::async_accept_not_listening);
752 m_alog->write(log::alevel::devel,
"asio::async_accept");
754 if (config::enable_multithreading) {
755 m_acceptor->async_accept(
756 tcon->get_raw_socket(),
757 tcon->get_strand()->wrap(lib::bind(
758 &type::handle_accept,
761 lib::placeholders::_1
765 m_acceptor->async_accept(
766 tcon->get_raw_socket(),
768 &type::handle_accept,
771 lib::placeholders::_1
784 async_accept(tcon,callback,ec);
785 if (ec) {
throw exception(ec); }
803 void handle_accept(accept_handler callback, lib::asio::error_code
const &
806 lib::error_code ret_ec;
808 m_alog->write(log::alevel::devel,
"asio::handle_accept");
811 if (asio_ec == lib::asio::errc::operation_canceled) {
812 ret_ec = make_error_code(websocketpp::error::operation_canceled);
814 log_err(log::elevel::info,
"asio handle_accept",asio_ec);
815 ret_ec = make_error_code(error::pass_through);
825 using namespace lib::asio::ip;
829 m_resolver = lib::make_shared<lib::asio::ip::tcp::resolver>(
830 lib::ref(*m_io_service));
835 std::string proxy = tcon->get_proxy();
840 host = u->get_host();
841 port = u->get_port_str();
845 uri_ptr pu = lib::make_shared<uri>(proxy);
847 if (!pu->get_valid()) {
848 cb(make_error_code(error::proxy_invalid));
852 ec = tcon->proxy_init(u->get_authority());
858 host = pu->get_host();
859 port = pu->get_port_str();
862 tcp::resolver::query query(host,port);
864 if (m_alog->static_test(log::alevel::devel)) {
865 m_alog->write(log::alevel::devel,
866 "starting async DNS resolve for "+host+
":"+port);
871 dns_timer = tcon->set_timer(
872 config::timeout_dns_resolve,
874 &type::handle_resolve_timeout,
878 lib::placeholders::_1
882 if (config::enable_multithreading) {
883 m_resolver->async_resolve(
885 tcon->get_strand()->wrap(lib::bind(
886 &type::handle_resolve,
891 lib::placeholders::_1,
892 lib::placeholders::_2
896 m_resolver->async_resolve(
899 &type::handle_resolve,
904 lib::placeholders::_1,
905 lib::placeholders::_2
923 lib::error_code ret_ec;
926 if (ec == transport::error::operation_aborted) {
927 m_alog->write(log::alevel::devel,
928 "asio handle_resolve_timeout timer cancelled");
932 log_err(log::elevel::devel,
"asio handle_resolve_timeout",ec);
935 ret_ec = make_error_code(transport::error::timeout);
938 m_alog->write(log::alevel::devel,
"DNS resolution timed out");
939 m_resolver->cancel();
943 void handle_resolve(transport_con_ptr tcon,
timer_ptr dns_timer,
944 connect_handler callback, lib::asio::error_code
const & ec,
945 lib::asio::ip::tcp::resolver::iterator iterator)
947 if (ec == lib::asio::error::operation_aborted ||
948 lib::asio::is_neg(dns_timer->expires_from_now()))
950 m_alog->write(log::alevel::devel,
"async_resolve cancelled");
957 log_err(log::elevel::info,
"asio async_resolve",ec);
958 callback(make_error_code(error::pass_through));
962 if (m_alog->static_test(log::alevel::devel)) {
964 s <<
"Async DNS resolve successful. Results: ";
966 lib::asio::ip::tcp::resolver::iterator it, end;
967 for (it = iterator; it != end; ++it) {
968 s << (*it).endpoint() <<
" ";
971 m_alog->write(log::alevel::devel,s.str());
974 m_alog->write(log::alevel::devel,
"Starting async connect");
978 con_timer = tcon->set_timer(
979 config::timeout_connect,
981 &type::handle_connect_timeout,
986 lib::placeholders::_1
990 if (config::enable_multithreading) {
991 lib::asio::async_connect(
992 tcon->get_raw_socket(),
994 tcon->get_strand()->wrap(lib::bind(
995 &type::handle_connect,
1000 lib::placeholders::_1
1004 lib::asio::async_connect(
1005 tcon->get_raw_socket(),
1008 &type::handle_connect,
1013 lib::placeholders::_1
1032 lib::error_code ret_ec;
1035 if (ec == transport::error::operation_aborted) {
1036 m_alog->write(log::alevel::devel,
1037 "asio handle_connect_timeout timer cancelled");
1041 log_err(log::elevel::devel,
"asio handle_connect_timeout",ec);
1044 ret_ec = make_error_code(transport::error::timeout);
1047 m_alog->write(log::alevel::devel,
"TCP connect timed out");
1048 tcon->cancel_socket_checked();
1052 void handle_connect(transport_con_ptr tcon,
timer_ptr con_timer,
1053 connect_handler callback, lib::asio::error_code
const & ec)
1055 if (ec == lib::asio::error::operation_aborted ||
1056 lib::asio::is_neg(con_timer->expires_from_now()))
1058 m_alog->write(log::alevel::devel,
"async_connect cancelled");
1062 con_timer->cancel();
1065 log_err(log::elevel::info,
"asio async_connect",ec);
1066 callback(make_error_code(error::pass_through));
1070 if (m_alog->static_test(log::alevel::devel)) {
1071 m_alog->write(log::alevel::devel,
1072 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1075 callback(lib::error_code());
1090 m_alog->write(log::alevel::devel,
"transport::asio::init");
1093 socket_type::init(lib::static_pointer_cast<socket_con_type,
1094 transport_con_type>(tcon));
1098 ec = tcon->init_asio(m_io_service);
1099 if (ec) {
return ec;}
1101 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1102 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1104 return lib::error_code();
1108 template <
typename error_type>
1109 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1110 std::stringstream s;
1111 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1112 m_elog->write(l,s.str());
1122 tcp_init_handler m_tcp_pre_init_handler;
1123 tcp_init_handler m_tcp_post_init_handler;
1127 bool m_external_io_service;
1133 int m_listen_backlog;
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.
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.
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 is_secure() const
Return whether or not the endpoint produces secure connections.
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.
bool stopped() const
wraps the stopped method of the internal io_service object
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.
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code &ec)
Get local TCP endpoint.
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.
Transport policies provide network connectivity and timers.
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.
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
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.
Transport policy that uses asio.
void stop_listening()
Stop 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.
std::size_t run_one()
wraps the run_one method of the internal io_service object
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)
bool is_listening() const
Check if the endpoint is listening.
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
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(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.
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.