Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:08:24

0001 /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
0002  *
0003  * Distributed under the Boost Software License, Version 1.0. (See
0004  * accompanying file LICENSE.txt)
0005  */
0006 
0007 #ifndef BOOST_REDIS_CONNECTOR_HPP
0008 #define BOOST_REDIS_CONNECTOR_HPP
0009 
0010 #include <boost/redis/detail/helper.hpp>
0011 #include <boost/redis/error.hpp>
0012 #include <boost/asio/compose.hpp>
0013 #include <boost/asio/connect.hpp>
0014 #include <boost/asio/coroutine.hpp>
0015 #include <boost/asio/ip/tcp.hpp>
0016 #include <boost/asio/cancel_after.hpp>
0017 #include <string>
0018 #include <chrono>
0019 
0020 namespace boost::redis::detail
0021 {
0022 
0023 template <class Connector, class Stream>
0024 struct connect_op {
0025    Connector* ctor_ = nullptr;
0026    Stream* stream = nullptr;
0027    asio::ip::tcp::resolver::results_type const* res_ = nullptr;
0028    asio::coroutine coro{};
0029 
0030    template <class Self>
0031    void operator()( Self& self
0032                   , system::error_code const& ec = {}
0033                   , asio::ip::tcp::endpoint const& ep= {})
0034    {
0035       BOOST_ASIO_CORO_REENTER (coro)
0036       {
0037          BOOST_ASIO_CORO_YIELD
0038          asio::async_connect(*stream, *res_,
0039             [](system::error_code const&, auto const&) { return true; },
0040             asio::cancel_after(ctor_->timeout_, std::move(self)));
0041 
0042          ctor_->endpoint_ = ep;
0043 
0044          if (ec == asio::error::operation_aborted) {
0045             self.complete(redis::error::connect_timeout);
0046          } else {
0047             self.complete(ec);
0048          }
0049       }
0050    }
0051 };
0052 
0053 class connector {
0054 public:
0055    void set_config(config const& cfg)
0056       { timeout_ = cfg.connect_timeout; }
0057 
0058    template <class Stream, class CompletionToken>
0059    auto
0060    async_connect(
0061          Stream& stream,
0062          asio::ip::tcp::resolver::results_type const& res,
0063          CompletionToken&& token)
0064    {
0065       return asio::async_compose
0066          < CompletionToken
0067          , void(system::error_code)
0068          >(connect_op<connector, Stream>{this, &stream, &res}, token);
0069    }
0070 
0071    auto const& endpoint() const noexcept { return endpoint_;}
0072 
0073 private:
0074    template <class, class> friend struct connect_op;
0075 
0076    std::chrono::steady_clock::duration timeout_ = std::chrono::seconds{2};
0077    asio::ip::tcp::endpoint endpoint_;
0078 };
0079 
0080 } // boost::redis::detail
0081 
0082 #endif // BOOST_REDIS_CONNECTOR_HPP