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-2022 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 {
28 namespace detail {
29 
33 template<typename BaseTransport, typename Protocol>
34 class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol>
35 {
36 public:
37  StreamTransportWithResolverImpl(BaseTransport& transport, boost::asio::io_service& ioService)
38  : StreamTransportImpl<BaseTransport, Protocol>(transport, ioService)
39  {
40  }
41 
42  void
43  connect(const typename Protocol::resolver::query& query)
44  {
45  if (this->m_transport.getState() == Transport::State::CONNECTING) {
46  return;
47  }
49 
50  // Wait at most 4 seconds to connect
52  this->m_connectTimer.expires_from_now(std::chrono::seconds(4));
53  this->m_connectTimer.async_wait([self = this->shared_from_base()] (const auto& ec) {
54  self->connectTimeoutHandler(ec);
55  });
56 
57  auto resolver = make_shared<typename Protocol::resolver>(this->m_socket
58 #if BOOST_VERSION >= 107000
59  .get_executor()
60 #else
61  .get_io_service()
62 #endif
63  );
64  resolver->async_resolve(query, [self = this->shared_from_base(), resolver] (auto&&... args) {
65  self->resolveHandler(std::forward<decltype(args)>(args)..., resolver);
66  });
67  }
68 
69 protected:
70  void
71  resolveHandler(const boost::system::error_code& error,
72  typename Protocol::resolver::iterator endpoint,
73  const shared_ptr<typename Protocol::resolver>&)
74  {
75  if (error) {
76  if (error == boost::system::errc::operation_canceled)
77  return;
78 
79  NDN_THROW(Transport::Error(error, "Error during resolution of host or port"));
80  }
81 
82  typename Protocol::resolver::iterator end;
83  if (endpoint == end) {
84  this->m_transport.close();
85  NDN_THROW(Transport::Error(error, "Unable to resolve host or port"));
86  }
87 
88  this->m_socket.async_connect(*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 static_pointer_cast<Impl>(this->shared_from_this());
100  }
101 };
102 
103 } // namespace detail
104 } // namespace ndn
105 
106 #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_service &ioService)
void resolveHandler(const boost::system::error_code &error, typename Protocol::resolver::iterator endpoint, const shared_ptr< typename Protocol::resolver > &)
void connect(const typename Protocol::resolver::query &query)
#define NDN_THROW(e)
Definition: exception.hpp:61
boost::chrono::seconds seconds
Definition: time.hpp:47
Definition: data.cpp:25