28#ifndef WEBSOCKETPP_TRANSPORT_ASIO_HPP
29#define WEBSOCKETPP_TRANSPORT_ASIO_HPP
52template <
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)
94 , m_listen_backlog(lib::
asio::socket_base::max_connections)
96 , m_state(UNINITIALIZED)
108 if (m_state != UNINITIALIZED && !m_external_io_service) {
116#ifdef _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;
169 return socket_type::is_secure();
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>(*m_io_service);
197 ec = lib::error_code();
228#ifdef _WEBSOCKETPP_CPP11_MEMORY_
229 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
231 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
234 if( !ec ) service.release();
235 m_external_io_service =
false;
250#ifdef _WEBSOCKETPP_CPP11_MEMORY_
251 lib::unique_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
253 lib::auto_ptr<lib::asio::io_service> service(
new lib::asio::io_service());
258 m_external_io_service =
false;
272 m_tcp_pre_init_handler = h;
301 m_tcp_post_init_handler = h;
324 m_listen_backlog = backlog;
342 m_reuse_addr =
value;
357 return *m_io_service;
375 return m_acceptor->local_endpoint(ec);
377 ec = lib::asio::error::make_error_code(lib::asio::error::bad_descriptor);
378 return lib::asio::ip::tcp::endpoint();
390 void listen(lib::asio::ip::tcp::endpoint
const & ep, lib::error_code & ec)
392 if (m_state != READY) {
394 "asio::listen called from the wrong state");
402 lib::asio::error_code bec;
404 m_acceptor->open(ep.protocol(),bec);
406 m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
409 m_acceptor->bind(ep,bec);
412 m_acceptor->listen(m_listen_backlog,bec);
415 if (m_acceptor->is_open()) {
422 ec = lib::error_code();
432 void listen(lib::asio::ip::tcp::endpoint
const & ep) {
452 template <
typename InternetProtocol>
454 lib::error_code & ec)
456 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
473 template <
typename InternetProtocol>
476 lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
493 listen(lib::asio::ip::tcp::v6(), port, ec);
509 listen(lib::asio::ip::tcp::v6(), port);
528 void listen(std::string
const & host, std::string
const & service,
529 lib::error_code & ec)
531 using lib::asio::ip::tcp;
532 tcp::resolver
r(*m_io_service);
533 tcp::resolver::query query(host, service);
534 tcp::resolver::iterator endpoint_iterator =
r.resolve(query);
535 tcp::resolver::iterator end;
536 if (endpoint_iterator == end) {
538 "asio::listen could not resolve the supplied host or service");
542 listen(*endpoint_iterator,ec);
561 void listen(std::string
const & host, std::string
const & service)
577 if (m_state != LISTENING) {
579 "asio::listen called from the wrong state");
587 ec = lib::error_code();
608 return (m_state == LISTENING);
613 return m_io_service->run();
621 return m_io_service->run_one();
626 m_io_service->stop();
631 return m_io_service->poll();
636 return m_io_service->poll_one();
641 m_io_service->reset();
646 return m_io_service->stopped();
662 m_work = lib::make_shared<lib::asio::io_service::work>(*m_io_service);
690 timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
695 new_timer->async_wait(
701 lib::placeholders::_1
718 lib::asio::error_code
const & ec)
721 if (ec == lib::asio::error::operation_aborted) {
725 "asio handle_timer error: "+ec.message());
730 callback(lib::error_code());
741 lib::error_code & ec)
743 if (m_state != LISTENING) {
751 if (config::enable_multithreading) {
752 m_acceptor->async_accept(
753 tcon->get_raw_socket(),
754 tcon->get_strand()->wrap(lib::bind(
758 lib::placeholders::_1
762 m_acceptor->async_accept(
763 tcon->get_raw_socket(),
768 lib::placeholders::_1
803 lib::error_code ret_ec;
808 if (asio_ec == lib::asio::errc::operation_canceled) {
822 using namespace lib::asio::ip;
826 m_resolver = lib::make_shared<lib::asio::ip::tcp::resolver>(*m_io_service);
831 std::string proxy = tcon->get_proxy();
836 host = u->get_host();
837 port = u->get_port_str();
841 uri_ptr pu = lib::make_shared<uri>(proxy);
843 if (!pu->get_valid()) {
848 ec = tcon->proxy_init(u->get_authority());
854 host = pu->get_host();
855 port = pu->get_port_str();
858 tcp::resolver::query query(host,port);
862 "starting async DNS resolve for "+host+
":"+port);
867 dns_timer = tcon->set_timer(
868 config::timeout_dns_resolve,
874 lib::placeholders::_1
878 if (config::enable_multithreading) {
879 m_resolver->async_resolve(
881 tcon->get_strand()->wrap(lib::bind(
887 lib::placeholders::_1,
888 lib::placeholders::_2
892 m_resolver->async_resolve(
900 lib::placeholders::_1,
901 lib::placeholders::_2
917 lib::error_code
const & ec)
919 lib::error_code ret_ec;
924 "asio handle_resolve_timeout timer cancelled");
935 m_resolver->cancel();
941 lib::asio::ip::tcp::resolver::iterator iterator)
943 if (ec == lib::asio::error::operation_aborted ||
960 s <<
"Async DNS resolve successful. Results: ";
962 lib::asio::ip::tcp::resolver::iterator it, end;
963 for (it = iterator; it != end; ++it) {
964 s << (*it).endpoint() <<
" ";
974 con_timer = tcon->set_timer(
975 config::timeout_connect,
982 lib::placeholders::_1
986 if (config::enable_multithreading) {
987 lib::asio::async_connect(
988 tcon->get_raw_socket(),
990 tcon->get_strand()->wrap(lib::bind(
996 lib::placeholders::_1
1000 lib::asio::async_connect(
1001 tcon->get_raw_socket(),
1009 lib::placeholders::_1
1028 lib::error_code ret_ec;
1033 "asio handle_connect_timeout timer cancelled");
1044 tcon->cancel_socket_checked();
1051 if (ec == lib::asio::error::operation_aborted ||
1058 con_timer->cancel();
1068 "Async connect to "+tcon->get_remote_endpoint()+
" successful.");
1071 callback(lib::error_code());
1094 ec = tcon->init_asio(m_io_service);
1095 if (ec) {
return ec;}
1097 tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1098 tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1100 return lib::error_code();
1104 template <
typename error_type>
1105 void log_err(
log::level l,
char const * msg, error_type
const & ec) {
1106 std::stringstream
s;
1107 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1108 m_elog->write(
l,
s.str());
1123 bool m_external_io_service;
1129 int m_listen_backlog;
Asio based endpoint transport component.
std::size_t run_one()
wraps the run_one method of the internal io_service object
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
config::concurrency_type concurrency_type
Type of the concurrency policy.
asio::connection< config > transport_con_type
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
void handle_connect(transport_con_ptr tcon, timer_ptr con_timer, connect_handler callback, lib::asio::error_code const &ec)
void init_asio()
Initialize asio transport with internal io_service.
void init_logging(alog_type *a, elog_type *e)
Initialize logging.
bool is_secure() const
Return whether or not the endpoint produces secure connections.
void init_asio(io_service_ptr ptr)
initialize asio transport with external io_service
lib::asio::ip::tcp::endpoint get_local_endpoint(lib::asio::error_code &ec)
Get local TCP endpoint.
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
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.
void stop()
wraps the stop method of the internal io_service object
std::size_t run()
wraps the run method of the internal io_service object
config::socket_type socket_type
Type of the socket policy.
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
config::elog_type elog_type
Type of the error logging policy.
transport_con_type::ptr transport_con_ptr
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
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 init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_service (exception free)
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
void listen(uint16_t port)
Set up endpoint for listening on a port.
void handle_accept(accept_handler callback, lib::asio::error_code const &asio_ec)
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)
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
lib::asio::io_service & get_io_service()
Retrieve a reference to the endpoint's io_service.
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)
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.
endpoint< config > type
Type of this endpoint transport component.
lib::shared_ptr< lib::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
void listen(lib::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
void handle_resolve_timeout(timer_ptr, connect_handler callback, lib::error_code const &ec)
DNS resolution timeout handler.
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
lib::shared_ptr< lib::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
void reset()
wraps the reset 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.
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
lib::shared_ptr< lib::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
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)
std::size_t poll_one()
wraps the poll_one method of the internal io_service object
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 listen(uint16_t port, lib::error_code &ec)
Set up endpoint for listening on a port (exception free)
config::alog_type alog_type
Type of the access logging policy.
bool is_listening() const
Check if the endpoint is listening.
void stop_listening()
Stop listening.
@ async_accept_not_listening
@ operation_canceled
The requested operation was canceled.
@ invalid_state
The connection was in the wrong state for this operation.
lib::error_code make_error_code(error::value e)
boost::posix_time::time_duration milliseconds(long duration)
uint32_t level
Type of a channel package.
@ proxy_invalid
Invalid Proxy URI.
@ invalid_host_service
Invalid host or service.
lib::function< void(connection_hdl)> tcp_init_handler
@ operation_aborted
Operation aborted.
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
Namespace for the WebSocket++ project.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
schedule config_dir_name data_dir_name p2p_port http_port file_size name host(p2p_endpoint)) FC_REFLECT(tn_node_def
static level const devel
Development messages (warning: very chatty)
static level const devel
Low level debugging information (warning: very chatty)
static level const library