File indexing completed on 2025-01-18 09:51:19
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_REDIS_RESOLVER_HPP
0008 #define BOOST_REDIS_RESOLVER_HPP
0009
0010 #include <boost/redis/config.hpp>
0011 #include <boost/redis/detail/helper.hpp>
0012 #include <boost/redis/error.hpp>
0013 #include <boost/asio/compose.hpp>
0014 #include <boost/asio/coroutine.hpp>
0015 #include <boost/asio/experimental/parallel_group.hpp>
0016 #include <boost/asio/ip/tcp.hpp>
0017 #include <boost/asio/steady_timer.hpp>
0018 #include <string>
0019 #include <chrono>
0020
0021 namespace boost::redis::detail
0022 {
0023
0024 template <class Resolver>
0025 struct resolve_op {
0026 Resolver* resv_ = nullptr;
0027 asio::coroutine coro{};
0028
0029 template <class Self>
0030 void operator()( Self& self
0031 , std::array<std::size_t, 2> order = {}
0032 , system::error_code ec1 = {}
0033 , asio::ip::tcp::resolver::results_type res = {}
0034 , system::error_code ec2 = {})
0035 {
0036 BOOST_ASIO_CORO_REENTER (coro)
0037 {
0038 resv_->timer_.expires_after(resv_->timeout_);
0039
0040 BOOST_ASIO_CORO_YIELD
0041 asio::experimental::make_parallel_group(
0042 [this](auto token)
0043 {
0044 return resv_->resv_.async_resolve(resv_->addr_.host, resv_->addr_.port, token);
0045 },
0046 [this](auto token) { return resv_->timer_.async_wait(token);}
0047 ).async_wait(
0048 asio::experimental::wait_for_one(),
0049 std::move(self));
0050
0051 if (is_cancelled(self)) {
0052 self.complete(asio::error::operation_aborted);
0053 return;
0054 }
0055
0056 switch (order[0]) {
0057 case 0: {
0058
0059 resv_->results_ = res;
0060 self.complete(ec1);
0061 } break;
0062
0063 case 1: {
0064 if (ec2) {
0065
0066
0067 self.complete(ec2);
0068 } else {
0069
0070
0071 self.complete(error::resolve_timeout);
0072 }
0073 } break;
0074
0075 default: BOOST_ASSERT(false);
0076 }
0077 }
0078 }
0079 };
0080
0081 template <class Executor>
0082 class resolver {
0083 public:
0084 using timer_type =
0085 asio::basic_waitable_timer<
0086 std::chrono::steady_clock,
0087 asio::wait_traits<std::chrono::steady_clock>,
0088 Executor>;
0089
0090 resolver(Executor ex) : resv_{ex} , timer_{ex} {}
0091
0092 template <class CompletionToken>
0093 auto async_resolve(CompletionToken&& token)
0094 {
0095 return asio::async_compose
0096 < CompletionToken
0097 , void(system::error_code)
0098 >(resolve_op<resolver>{this}, token, resv_);
0099 }
0100
0101 std::size_t cancel(operation op)
0102 {
0103 switch (op) {
0104 case operation::resolve:
0105 case operation::all:
0106 resv_.cancel();
0107 timer_.cancel();
0108 break;
0109 default: ;
0110 }
0111
0112 return 0;
0113 }
0114
0115 auto const& results() const noexcept
0116 { return results_;}
0117
0118 void set_config(config const& cfg)
0119 {
0120 addr_ = cfg.addr;
0121 timeout_ = cfg.resolve_timeout;
0122 }
0123
0124 private:
0125 using resolver_type = asio::ip::basic_resolver<asio::ip::tcp, Executor>;
0126 template <class> friend struct resolve_op;
0127
0128 resolver_type resv_;
0129 timer_type timer_;
0130 address addr_;
0131 std::chrono::steady_clock::duration timeout_;
0132 asio::ip::tcp::resolver::results_type results_;
0133 };
0134
0135 }
0136
0137 #endif