28 #ifndef WEBSOCKETPP_TRANSPORT_ASIO_HPP 29 #define WEBSOCKETPP_TRANSPORT_ASIO_HPP 52 template <
typename config>
82 typedef lib::shared_ptr<lib::asio::ip::tcp::acceptor>
acceptor_ptr;
84 typedef lib::shared_ptr<lib::asio::ip::tcp::resolver>
resolver_ptr;
86 typedef lib::shared_ptr<lib::asio::steady_timer>
timer_ptr;
88 typedef lib::shared_ptr<lib::asio::io_service::work>
work_ptr;
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_ 124 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_ 126 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_ 129 , m_tcp_pre_init_handler(src.m_tcp_pre_init_handler)
130 , m_tcp_post_init_handler(src.m_tcp_post_init_handler)
131 , m_io_service(src.m_io_service)
132 , m_external_io_service(src.m_external_io_service)
133 , m_acceptor(src.m_acceptor)
134 , m_listen_backlog(lib::asio::socket_base::max_connections)
135 , m_reuse_addr(src.m_reuse_addr)
138 , m_state(src.m_state)
140 src.m_io_service = NULL;
141 src.m_external_io_service =
false;
142 src.m_acceptor = NULL;
143 src.m_state = UNINITIALIZED;
165 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_ 181 void init_asio(io_service_ptr ptr, lib::error_code & ec) {
182 if (m_state != UNINITIALIZED) {
184 "asio::init_asio called from the wrong state");
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();
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());
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());
259 m_external_io_service =
false;
273 m_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);
379 return lib::asio::ip::tcp::endpoint();
391 void listen(lib::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
393 if (m_state != READY) {
395 "asio::listen called from the wrong state");
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()) {
423 ec = lib::error_code();
433 void listen(lib::asio::ip::tcp::endpoint
const & ep) {
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);
537 if (endpoint_iterator == end) {
539 "asio::listen could not resolve the supplied host or service");
543 listen(*endpoint_iterator,ec);
562 void listen(std::string
const & host, std::string
const & service)
578 if (m_state != LISTENING) {
580 "asio::listen called from the wrong state");
588 ec = lib::error_code();
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>(
698 new_timer->async_wait(
704 lib::placeholders::_1
721 lib::asio::error_code
const & ec)
728 "asio handle_timer error: "+ec.message());
733 callback(lib::error_code());
744 lib::error_code & ec)
746 if (m_state != LISTENING) {
755 m_acceptor->async_accept(
756 tcon->get_raw_socket(),
757 tcon->get_strand()->wrap(lib::bind(
761 lib::placeholders::_1
765 m_acceptor->async_accept(
766 tcon->get_raw_socket(),
771 lib::placeholders::_1
806 lib::error_code ret_ec;
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()) {
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);
866 "starting async DNS resolve for "+host+
":"+port);
871 dns_timer = tcon->set_timer(
878 lib::placeholders::_1
883 m_resolver->async_resolve(
885 tcon->get_strand()->wrap(lib::bind(
891 lib::placeholders::_1,
892 lib::placeholders::_2
896 m_resolver->async_resolve(
904 lib::placeholders::_1,
905 lib::placeholders::_2
921 lib::error_code
const & ec)
923 lib::error_code ret_ec;
928 "asio handle_resolve_timeout timer cancelled");
939 m_resolver->cancel();
964 s <<
"Async DNS resolve successful. Results: ";
967 for (it = iterator; it != end; ++it) {
968 s << (*it).endpoint() <<
" ";
978 con_timer = tcon->set_timer(
986 lib::placeholders::_1
991 lib::asio::async_connect(
992 tcon->get_raw_socket(),
994 tcon->get_strand()->wrap(lib::bind(
1000 lib::placeholders::_1
1004 lib::asio::async_connect(
1005 tcon->get_raw_socket(),
1013 lib::placeholders::_1
1032 lib::error_code ret_ec;
1037 "asio handle_connect_timeout timer cancelled");
1048 tcon->cancel_socket_checked();
1062 con_timer->cancel();
1072 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1075 callback(lib::error_code());
1089 lib::error_code
init(transport_con_ptr tcon) {
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());
1126 io_service_ptr m_io_service;
1127 bool m_external_io_service;
1128 acceptor_ptr m_acceptor;
1129 resolver_ptr m_resolver;
1133 int m_listen_backlog;
1147 #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.
void write(level, std::string const &)
Write a string message to the given channel.
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.
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.
bool is_secure() const
Checks whether the endpoint creates secure connections.
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.
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 stopped() const
wraps the stopped method of the internal io_service object
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.
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
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.
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.
bool is_secure() const
Return whether or not the endpoint produces secure connections.
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)
Table::const_iterator iterator
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.
bool is_listening() const
Check if the endpoint is 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.
there was an error in the underlying transport library
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)
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.
void init_logging(alog_type *a, elog_type *e)
Initialize logging.
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.
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 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.