28 #ifndef WEBSOCKETPP_TRANSPORT_ASIO_HPP 29 #define WEBSOCKETPP_TRANSPORT_ASIO_HPP 53 template <
typename config>
83 typedef lib::shared_ptr<lib::asio::ip::tcp::acceptor>
acceptor_ptr;
85 typedef lib::shared_ptr<lib::asio::ip::tcp::resolver>
resolver_ptr;
87 typedef lib::shared_ptr<lib::asio::steady_timer>
timer_ptr;
89 typedef lib::shared_ptr<lib::asio::io_service::work>
work_ptr;
97 , m_external_io_service(false)
98 , m_listen_backlog(lib::
asio::socket_base::max_connections)
100 , m_state(UNINITIALIZED)
112 if (m_state != UNINITIALIZED && !m_external_io_service) {
120 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_ 128 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_ 130 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_ 133 , m_tcp_pre_init_handler(src.m_tcp_pre_init_handler)
134 , m_tcp_post_init_handler(src.m_tcp_post_init_handler)
135 , m_io_service(src.m_io_service)
136 , m_external_io_service(src.m_external_io_service)
137 , m_acceptor(src.m_acceptor)
138 , m_listen_backlog(lib::asio::socket_base::max_connections)
139 , m_reuse_addr(src.m_reuse_addr)
142 , m_state(src.m_state)
144 src.m_io_service = NULL;
145 src.m_external_io_service =
false;
146 src.m_acceptor = NULL;
147 src.m_state = UNINITIALIZED;
169 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_ 185 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
186 if (m_state != UNINITIALIZED) {
188 "asio::init_asio called from the wrong state");
197 m_external_io_service =
true;
198 m_acceptor = lib::make_shared<lib::asio::ip::tcp::acceptor>(
202 ec = lib::error_code();
233 #ifdef _WEBSOCKETPP_CPP11_MEMORY_ 234 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
236 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
239 if( !ec ) service.release();
240 m_external_io_service =
false;
255 #ifdef _WEBSOCKETPP_CPP11_MEMORY_ 256 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
258 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
263 m_external_io_service =
false;
276 m_tcp_pre_bind_handler = h;
290 m_tcp_pre_init_handler = h;
319 m_tcp_post_init_handler = h;
344 m_listen_backlog = backlog;
365 m_reuse_addr =
value;
380 return *m_io_service;
398 return m_acceptor->local_endpoint(ec);
401 return lib::asio::ip::tcp::endpoint();
413 void listen(lib::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
415 if (m_state != READY) {
417 "asio::listen called from the wrong state");
425 lib::asio::error_code bec;
427 m_acceptor->open(ep.protocol(),bec);
428 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
430 m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
431 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
434 if (m_tcp_pre_bind_handler) {
435 ec = m_tcp_pre_bind_handler(m_acceptor);
437 ec = clean_up_listen_after_error(ec);
442 m_acceptor->bind(ep,bec);
443 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
445 m_acceptor->listen(m_listen_backlog,bec);
446 if (bec) {ec = clean_up_listen_after_error(bec);
return;}
450 ec = lib::error_code();
461 void listen(lib::asio::ip::tcp::endpoint
const & ep) {
481 template <
typename InternetProtocol>
482 void listen(InternetProtocol
const & internet_protocol, uint16_t port,
483 lib::error_code & ec)
485 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
502 template <
typename InternetProtocol>
503 void listen(InternetProtocol
const & internet_protocol, uint16_t port)
505 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
521 void listen(uint16_t port, lib::error_code & ec) {
522 listen(lib::asio::ip::tcp::v6(), port, ec);
538 listen(lib::asio::ip::tcp::v6(), port);
557 void listen(std::string
const & host, std::string
const & service,
558 lib::error_code & ec)
560 using lib::asio::ip::tcp;
561 tcp::resolver r(*m_io_service);
562 tcp::resolver::query query(host, service);
563 tcp::resolver::iterator endpoint_iterator = r.resolve(query);
564 tcp::resolver::iterator end;
565 if (endpoint_iterator == end) {
567 "asio::listen could not resolve the supplied host or service");
571 listen(*endpoint_iterator,ec);
590 void listen(std::string
const & host, std::string
const & service)
606 if (m_state != LISTENING) {
608 "asio::listen called from the wrong state");
616 ec = lib::error_code();
637 return (m_state == LISTENING);
642 return m_io_service->run();
650 return m_io_service->run_one();
655 m_io_service->stop();
660 return m_io_service->poll();
665 return m_io_service->poll_one();
670 m_io_service->reset();
675 return m_io_service->stopped();
691 m_work = lib::make_shared<lib::asio::io_service::work>(
721 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
726 new_timer->async_wait(
732 lib::placeholders::_1
749 lib::asio::error_code
const & ec)
756 "asio handle_timer error: "+ec.message());
761 callback(lib::error_code());
772 lib::error_code & ec)
774 if (m_state != LISTENING || !m_acceptor) {
783 m_acceptor->async_accept(
784 tcon->get_raw_socket(),
785 tcon->get_strand()->wrap(lib::bind(
789 lib::placeholders::_1
793 m_acceptor->async_accept(
794 tcon->get_raw_socket(),
799 lib::placeholders::_1
826 void init_logging(
const lib::shared_ptr<alog_type>& a,
const lib::shared_ptr<elog_type>& e) {
834 lib::error_code ret_ec;
853 using namespace lib::asio::ip;
857 m_resolver = lib::make_shared<lib::asio::ip::tcp::resolver>(
863 std::string proxy = tcon->get_proxy();
868 host = u->get_host();
869 port = u->get_port_str();
873 uri_ptr pu = lib::make_shared<uri>(proxy);
875 if (!pu->get_valid()) {
880 ec = tcon->proxy_init(u->get_authority());
886 host = pu->get_host();
887 port = pu->get_port_str();
890 tcp::resolver::query query(host,port);
894 "starting async DNS resolve for "+host+
":"+port);
899 dns_timer = tcon->set_timer(
906 lib::placeholders::_1
911 m_resolver->async_resolve(
913 tcon->get_strand()->wrap(lib::bind(
919 lib::placeholders::_1,
920 lib::placeholders::_2
924 m_resolver->async_resolve(
932 lib::placeholders::_1,
933 lib::placeholders::_2
949 lib::error_code
const & ec)
951 lib::error_code ret_ec;
956 "asio handle_resolve_timeout timer cancelled");
967 m_resolver->cancel();
973 lib::asio::ip::tcp::resolver::iterator iterator)
992 s <<
"Async DNS resolve successful. Results: ";
994 lib::asio::ip::tcp::resolver::iterator it, end;
995 for (it = iterator; it != end; ++it) {
996 s << (*it).endpoint() <<
" ";
1004 timer_ptr con_timer;
1006 con_timer = tcon->set_timer(
1014 lib::placeholders::_1
1019 lib::asio::async_connect(
1020 tcon->get_raw_socket(),
1022 tcon->get_strand()->wrap(lib::bind(
1028 lib::placeholders::_1
1032 lib::asio::async_connect(
1033 tcon->get_raw_socket(),
1041 lib::placeholders::_1
1060 lib::error_code ret_ec;
1065 "asio handle_connect_timeout timer cancelled");
1076 tcon->cancel_socket_checked();
1090 con_timer->cancel();
1100 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1103 callback(lib::error_code());
1117 lib::error_code
init(transport_con_ptr tcon) {
1122 transport_con_type>(tcon));
1126 ec = tcon->init_asio(m_io_service);
1127 if (ec) {
return ec;}
1129 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1130 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1132 return lib::error_code();
1136 template <
typename error_type>
1137 void log_err(
log::level l,
char const * msg, error_type
const & ec) {
1138 std::stringstream s;
1139 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1140 m_elog->write(l,s.str());
1144 template <
typename error_type>
1145 lib::error_code clean_up_listen_after_error(error_type
const & ec) {
1146 if (m_acceptor->is_open()) {
1147 m_acceptor->close();
1160 tcp_pre_bind_handler m_tcp_pre_bind_handler;
1165 io_service_ptr m_io_service;
1166 bool m_external_io_service;
1167 acceptor_ptr m_acceptor;
1168 resolver_ptr m_resolver;
1172 int m_listen_backlog;
1175 lib::shared_ptr<elog_type> m_elog;
1176 lib::shared_ptr<alog_type> m_alog;
1186 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP static const bool enable_multithreading
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.
lib::shared_ptr< lib::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
lib::error_code make_error_code(error::value e)
TLS enabled Asio connection socket component.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
static lib::error_code translate_ec(ErrorCodeType ec)
Translate any security policy specific information about an error code.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
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.
span_CONFIG_SIZE_TYPE size_t
void listen(lib::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the connection transport component associated with this endpoint transpor...
asio::connection< config > transport_con_type
Type of the connection transport component associated with this endpoint transport component...
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.
boost::posix_time::time_duration milliseconds(long duration)
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
websocketpp::transport::asio::tls_socket::endpoint socket_type
lib::function< lib::error_code(acceptor_ptr)> tcp_pre_bind_handler
Type of socket pre-bind handler.
void init_logging(const lib::shared_ptr< alog_type > &a, const lib::shared_ptr< elog_type > &e)
Initialize logging.
static level const devel
Low level debugging information (warning: very chatty)
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code &ec)
Get local TCP endpoint.
boost::chrono::duration< Rep, Period > duration
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.
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
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.
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.
static const long timeout_connect
void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer, connect_handler callback, lib::asio::error_code const &ec, lib::asio::ip::tcp::resolver::iterator iterator)
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.
static const long timeout_dns_resolve
The connection was in the wrong state for this operation.
static level const info
Information about minor configuration problems or additional information about other warnings...
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
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.
An async accept operation failed because the underlying transport has been requested to not listen fo...
config::elog_type elog_type
Type of the error logging policy.
Stub concurrency policy that implements the interface using no-ops.
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 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.
lib::function< void(connection_hdl)> tcp_init_handler
TLS enabled Asio endpoint socket component.
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.
bool is_secure() const
Checks whether the endpoint creates secure connections.
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)
uint32_t level
Type of a channel package.
void set_tcp_pre_bind_handler(tcp_pre_bind_handler h)
Sets the tcp pre bind handler.
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
void handle_connect(transport_con_ptr tcon, timer_ptr con_timer, connect_handler callback, lib::asio::error_code const &ec)
Stub logger that ignores all input.
void handle_accept(accept_handler callback, lib::asio::error_code const &asio_ec)
config::concurrency_type concurrency_type
Type of the concurrency policy.
static level const library
Information about unusual system states or other minor internal library problems, less chatty than de...
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.