1 #ifndef SIMPLE_WEB_ASIO_COMPATIBILITY_HPP 2 #define SIMPLE_WEB_ASIO_COMPATIBILITY_HPP 3 4 #include <memory> 5 6 #ifdef ASIO_STANDALONE 7 #include <asio.hpp> 8 #include <asio/steady_timer.hpp> 9 namespace SimpleWeb { 10 namespace error = asio::error; 11 using error_code = std::error_code; 12 using errc = std::errc; 13 using system_error = std::system_error; 14 namespace make_error_code = std; 15 } // namespace SimpleWeb 16 #else 17 #include <boost/asio.hpp> 18 #include <boost/asio/steady_timer.hpp> 19 namespace SimpleWeb { 20 namespace asio = boost::asio; 21 namespace error = asio::error; 22 using error_code = boost::system::error_code; 23 namespace errc = boost::system::errc; 24 using system_error = boost::system::system_error; 25 namespace make_error_code = boost::system::errc; 26 } // namespace SimpleWeb 27 #endif 28 29 namespace SimpleWeb { 30 #if(ASIO_STANDALONE && ASIO_VERSION >= 101300) || BOOST_ASIO_VERSION >= 101300 31 using io_context = asio::io_context; 32 using resolver_results = asio::ip::tcp::resolver::results_type; 33 using async_connect_endpoint = asio::ip::tcp::endpoint; 34 35 #if(ASIO_STANDALONE && ASIO_VERSION >= 101800) || BOOST_ASIO_VERSION >= 101800 36 using strand = asio::strand<asio::any_io_executor>; 37 #else 38 using strand = asio::strand<asio::executor>; 39 #endif 40 41 template <typename handler_type> post(io_context & context,handler_type && handler)42 inline void post(io_context &context, handler_type &&handler) { 43 asio::post(context, std::forward<handler_type>(handler)); 44 } restart(io_context & context)45 inline void restart(io_context &context) noexcept { 46 context.restart(); 47 } make_address(const std::string & str)48 inline asio::ip::address make_address(const std::string &str) noexcept { 49 return asio::ip::make_address(str); 50 } 51 template <typename socket_type, typename duration_type> make_steady_timer(socket_type & socket,std::chrono::duration<duration_type> duration)52 inline std::unique_ptr<asio::steady_timer> make_steady_timer(socket_type &socket, std::chrono::duration<duration_type> duration) { 53 return std::unique_ptr<asio::steady_timer>(new asio::steady_timer(socket.get_executor(), duration)); 54 } 55 template <typename handler_type> async_resolve(asio::ip::tcp::resolver & resolver,const std::pair<std::string,std::string> & host_port,handler_type && handler)56 inline void async_resolve(asio::ip::tcp::resolver &resolver, const std::pair<std::string, std::string> &host_port, handler_type &&handler) { 57 resolver.async_resolve(host_port.first, host_port.second, std::forward<handler_type>(handler)); 58 } make_work_guard(io_context & context)59 inline asio::executor_work_guard<io_context::executor_type> make_work_guard(io_context &context) { 60 return asio::make_work_guard(context); 61 } 62 template <typename socket_type> get_executor(socket_type & socket)63 inline asio::basic_socket<asio::ip::tcp>::executor_type get_executor(socket_type &socket) { 64 return socket.get_executor(); 65 } 66 template <typename execution_context, typename handler_type> bind_executor(strand & strand,handler_type && handler)67 inline asio::executor_binder<typename asio::decay<handler_type>::type, typename execution_context::executor_type> bind_executor(strand &strand, handler_type &&handler) { 68 return asio::bind_executor(strand, std::forward<handler_type>(handler)); 69 } 70 #else 71 using io_context = asio::io_service; 72 using resolver_results = asio::ip::tcp::resolver::iterator; 73 using async_connect_endpoint = asio::ip::tcp::resolver::iterator; 74 using strand = asio::io_service::strand; 75 76 template <typename handler_type> 77 inline void post(io_context &context, handler_type &&handler) { 78 context.post(std::forward<handler_type>(handler)); 79 } 80 template <typename handler_type> 81 inline void post(strand &strand, handler_type &&handler) { 82 strand.post(std::forward<handler_type>(handler)); 83 } 84 inline void restart(io_context &context) noexcept { 85 context.reset(); 86 } 87 inline asio::ip::address make_address(const std::string &str) noexcept { 88 return asio::ip::address::from_string(str); 89 } 90 template <typename socket_type, typename duration_type> 91 inline std::unique_ptr<asio::steady_timer> make_steady_timer(socket_type &socket, std::chrono::duration<duration_type> duration) { 92 return std::unique_ptr<asio::steady_timer>(new asio::steady_timer(socket.get_io_service(), duration)); 93 } 94 template <typename handler_type> 95 inline void async_resolve(asio::ip::tcp::resolver &resolver, const std::pair<std::string, std::string> &host_port, handler_type &&handler) { 96 resolver.async_resolve(asio::ip::tcp::resolver::query(host_port.first, host_port.second), std::forward<handler_type>(handler)); 97 } 98 inline io_context::work make_work_guard(io_context &context) { 99 return io_context::work(context); 100 } 101 template <typename socket_type> 102 inline io_context &get_executor(socket_type &socket) { 103 return socket.get_io_service(); 104 } 105 template <typename handler_type> 106 inline asio::detail::wrapped_handler<strand, handler_type, asio::detail::is_continuation_if_running> bind_executor(strand &strand, handler_type &&handler) { 107 return strand.wrap(std::forward<handler_type>(handler)); 108 } 109 #endif 110 } // namespace SimpleWeb 111 112 #endif /* SIMPLE_WEB_ASIO_COMPATIBILITY_HPP */ 113