Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "tcp-transport.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | namespace face { |
| 30 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 31 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamTransport, TcpTransport::protocol, "TcpTransport"); |
| 32 | |
| 33 | time::milliseconds TcpTransport::s_initialReconnectWait = time::seconds(1); |
| 34 | time::milliseconds TcpTransport::s_maxReconnectWait = time::minutes(5); |
| 35 | float TcpTransport::s_reconnectWaitMultiplier = 2.0f; |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 36 | |
| 37 | TcpTransport::TcpTransport(protocol::socket&& socket, ndn::nfd::FacePersistency persistency) |
| 38 | : StreamTransport(std::move(socket)) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 39 | , m_remoteEndpoint(m_socket.remote_endpoint()) |
| 40 | , m_nextReconnectWait(s_initialReconnectWait) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 41 | { |
| 42 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 43 | this->setRemoteUri(FaceUri(m_socket.remote_endpoint())); |
| 44 | |
| 45 | if (m_socket.local_endpoint().address().is_loopback() && |
| 46 | m_socket.remote_endpoint().address().is_loopback()) |
| 47 | this->setScope(ndn::nfd::FACE_SCOPE_LOCAL); |
| 48 | else |
| 49 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 50 | |
| 51 | this->setPersistency(persistency); |
| 52 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 53 | this->setMtu(MTU_UNLIMITED); |
| 54 | |
| 55 | NFD_LOG_FACE_INFO("Creating transport"); |
| 56 | } |
| 57 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame^] | 58 | bool |
| 59 | TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 60 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame^] | 61 | return true; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | TcpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) |
| 66 | { |
| 67 | // if persistency was changed from permanent to any other value |
| 68 | if (oldPersistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 69 | if (this->getState() == TransportState::DOWN) { |
| 70 | // non-permanent transport cannot be in DOWN state, so fail hard |
| 71 | this->setState(TransportState::FAILED); |
| 72 | doClose(); |
| 73 | } |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 77 | void |
| 78 | TcpTransport::handleError(const boost::system::error_code& error) |
| 79 | { |
| 80 | if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 81 | NFD_LOG_FACE_TRACE("TCP socket error: " << error.message()); |
| 82 | this->setState(TransportState::DOWN); |
| 83 | |
| 84 | // cancel all outstanding operations |
| 85 | boost::system::error_code error; |
| 86 | m_socket.cancel(error); |
| 87 | |
| 88 | // do this asynchronously because there could be some callbacks still pending |
| 89 | getGlobalIoService().post([this] { reconnect(); }); |
| 90 | } |
| 91 | else { |
| 92 | StreamTransport::handleError(error); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | TcpTransport::reconnect() |
| 98 | { |
| 99 | NFD_LOG_FACE_TRACE(__func__); |
| 100 | |
| 101 | if (getState() == TransportState::CLOSING || |
| 102 | getState() == TransportState::FAILED || |
| 103 | getState() == TransportState::CLOSED) { |
| 104 | // transport is shutting down, don't attempt to reconnect |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 109 | BOOST_ASSERT(getState() == TransportState::DOWN); |
| 110 | |
| 111 | // recreate the socket |
| 112 | m_socket = protocol::socket(m_socket.get_io_service()); |
| 113 | this->resetReceiveBuffer(); |
| 114 | this->resetSendQueue(); |
| 115 | |
| 116 | m_reconnectEvent = scheduler::schedule(m_nextReconnectWait, |
| 117 | [this] { handleReconnectTimeout(); }); |
| 118 | m_socket.async_connect(m_remoteEndpoint, |
| 119 | [this] (const boost::system::error_code& error) { handleReconnect(error); }); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | TcpTransport::handleReconnect(const boost::system::error_code& error) |
| 124 | { |
| 125 | if (getState() == TransportState::CLOSING || |
| 126 | getState() == TransportState::FAILED || |
| 127 | getState() == TransportState::CLOSED || |
| 128 | error == boost::asio::error::operation_aborted) { |
| 129 | // transport is shutting down, abort the reconnection attempt and ignore any errors |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | if (error) { |
| 134 | NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message()); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | m_reconnectEvent.cancel(); |
| 139 | m_nextReconnectWait = s_initialReconnectWait; |
| 140 | |
| 141 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 142 | NFD_LOG_FACE_TRACE("TCP connection reestablished"); |
| 143 | this->setState(TransportState::UP); |
| 144 | this->startReceive(); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | TcpTransport::handleReconnectTimeout() |
| 149 | { |
| 150 | // abort the reconnection attempt |
| 151 | boost::system::error_code error; |
| 152 | m_socket.close(error); |
| 153 | |
| 154 | // exponentially back off the reconnection timer |
| 155 | m_nextReconnectWait = |
| 156 | std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * s_reconnectWaitMultiplier), |
| 157 | s_maxReconnectWait); |
| 158 | |
| 159 | // do this asynchronously because there could be some callbacks still pending |
| 160 | getGlobalIoService().post([this] { reconnect(); }); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | TcpTransport::doClose() |
| 165 | { |
| 166 | m_reconnectEvent.cancel(); |
| 167 | StreamTransport::doClose(); |
| 168 | } |
| 169 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 170 | } // namespace face |
| 171 | } // namespace nfd |