Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 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_TRANSPORT_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP |
| 23 | #define NDN_TRANSPORT_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP |
| 24 | |
| 25 | #include "stream-transport-impl.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
| 29 | /** \brief implementation detail of a Boost.Asio-based stream-oriented transport |
| 30 | * with resolver support |
| 31 | */ |
| 32 | template<typename BaseTransport, typename Protocol> |
| 33 | class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol> |
| 34 | { |
| 35 | public: |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 36 | using Impl = StreamTransportWithResolverImpl<BaseTransport,Protocol>; |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 37 | |
| 38 | StreamTransportWithResolverImpl(BaseTransport& transport, boost::asio::io_service& ioService) |
| 39 | : StreamTransportImpl<BaseTransport, Protocol>(transport, ioService) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | connect(const typename Protocol::resolver::query& query) |
| 45 | { |
| 46 | if (this->m_isConnecting) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | this->m_isConnecting = true; |
| 51 | |
| 52 | // Wait at most 4 seconds to connect |
| 53 | /// @todo Decide whether this number should be configurable |
| 54 | this->m_connectTimer.expires_from_now(boost::posix_time::seconds(4)); |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 55 | this->m_connectTimer.async_wait(bind(&Impl::connectTimeoutHandler, this->shared_from_this(), _1)); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 56 | |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 57 | auto resolver = make_shared<typename Protocol::resolver>(ref(this->m_socket.get_io_service())); |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 58 | resolver->async_resolve(query, bind(&Impl::resolveHandler, this->shared_from_base(), _1, _2, resolver)); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
| 62 | void |
| 63 | resolveHandler(const boost::system::error_code& error, |
| 64 | typename Protocol::resolver::iterator endpoint, |
| 65 | const shared_ptr<typename Protocol::resolver>&) |
| 66 | { |
| 67 | if (error) { |
| 68 | if (error == boost::system::errc::operation_canceled) |
| 69 | return; |
| 70 | |
| 71 | BOOST_THROW_EXCEPTION(Transport::Error(error, "Error during resolution of host or port")); |
| 72 | } |
| 73 | |
| 74 | typename Protocol::resolver::iterator end; |
| 75 | if (endpoint == end) { |
| 76 | this->m_transport.close(); |
Junxiao Shi | f9b880b | 2017-09-08 13:05:06 +0000 | [diff] [blame] | 77 | BOOST_THROW_EXCEPTION(Transport::Error(error, "Unable to resolve host or port")); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 80 | this->m_socket.async_connect(*endpoint, bind(&Impl::connectHandler, this->shared_from_this(), _1)); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | shared_ptr<Impl> |
| 85 | shared_from_base() |
| 86 | { |
| 87 | return static_pointer_cast<Impl>(this->shared_from_this()); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 88 | } |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | } // namespace ndn |
| 93 | |
| 94 | #endif // NDN_TRANSPORT_STREAM_TRANSPORT_WITH_RESOLVER_IMPL_HPP |