stream-transport-with-resolver-impl.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2024 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP
23 #define NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP
24 
26 
27 namespace ndn::detail {
28 
33 template<typename BaseTransport, typename Protocol>
34 class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol>
35 {
36 public:
37  StreamTransportWithResolverImpl(BaseTransport& transport, boost::asio::io_context& ioCtx)
38  : StreamTransportImpl<BaseTransport, Protocol>(transport, ioCtx)
39  {
40  }
41 
42  void
43  connect(std::string_view host, std::string_view port)
44  {
45  if (this->m_transport.getState() == Transport::State::CONNECTING) {
46  return;
47  }
48 
50  auto hostAndPort = std::string(host) + ':' + std::string(port);
51 
52  // Wait at most 4 seconds to connect
54  this->m_connectTimer.expires_after(std::chrono::seconds(4));
55  this->m_connectTimer.async_wait([self = this->shared_from_base(), hostAndPort] (const auto& ec) {
56  if (ec) // e.g., cancelled timer
57  return;
58 
59  self->m_transport.close();
60  NDN_THROW(Transport::Error(boost::system::errc::make_error_code(boost::system::errc::timed_out),
61  "could not connect to NDN forwarder at " + hostAndPort));
62  });
63 
64  auto resolver = make_shared<typename Protocol::resolver>(this->m_socket.get_executor());
65  resolver->async_resolve(host, port,
66  [self = this->shared_from_base(), hostAndPort, resolver] (auto&&... args) {
67  self->resolveHandler(hostAndPort, std::forward<decltype(args)>(args)...);
68  });
69  }
70 
71 protected:
72  void
73  resolveHandler(const std::string& hostAndPort,
74  const boost::system::error_code& error,
75  const typename Protocol::resolver::results_type& endpoints)
76  {
77  if (error) {
78  if (error == boost::asio::error::operation_aborted)
79  return;
80 
81  this->m_transport.close();
82  NDN_THROW(Transport::Error(error, "could not resolve " + hostAndPort));
83  }
84 
85  BOOST_ASSERT(!endpoints.empty()); // guaranteed by Asio if the resolve operation is successful
86 
87  this->m_endpoint = *endpoints.begin();
88  this->m_socket.async_connect(this->m_endpoint, [self = this->shared_from_base()] (const auto& ec) {
89  self->connectHandler(ec);
90  });
91  }
92 
93 private:
95 
96  shared_ptr<Impl>
97  shared_from_base()
98  {
99  return std::static_pointer_cast<Impl>(this->shared_from_this());
100  }
101 };
102 
103 } // namespace ndn::detail
104 
105 #endif // NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP
Implementation detail of a Boost.Asio-based stream-oriented transport.
boost::asio::steady_timer m_connectTimer
Implementation detail of a Boost.Asio-based stream-oriented transport with resolver support.
StreamTransportWithResolverImpl(BaseTransport &transport, boost::asio::io_context &ioCtx)
void connect(std::string_view host, std::string_view port)
void resolveHandler(const std::string &hostAndPort, const boost::system::error_code &error, const typename Protocol::resolver::results_type &endpoints)
#define NDN_THROW(e)
Definition: exception.hpp:56
Contains implementation details that are not part of the ndn-cxx public API.
::boost::chrono::seconds seconds
Definition: time.hpp:51