Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, 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 | |
| 58 | void |
| 59 | TcpTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) |
| 60 | { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 61 | // if persistency is changing from permanent to any other value |
| 62 | if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 63 | if (this->getState() == TransportState::DOWN) { |
| 64 | // non-permanent transport cannot be in DOWN state, so fail hard |
| 65 | this->setState(TransportState::FAILED); |
| 66 | doClose(); |
| 67 | } |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 71 | void |
| 72 | TcpTransport::handleError(const boost::system::error_code& error) |
| 73 | { |
| 74 | if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 75 | NFD_LOG_FACE_TRACE("TCP socket error: " << error.message()); |
| 76 | this->setState(TransportState::DOWN); |
| 77 | |
| 78 | // cancel all outstanding operations |
| 79 | boost::system::error_code error; |
| 80 | m_socket.cancel(error); |
| 81 | |
| 82 | // do this asynchronously because there could be some callbacks still pending |
| 83 | getGlobalIoService().post([this] { reconnect(); }); |
| 84 | } |
| 85 | else { |
| 86 | StreamTransport::handleError(error); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | TcpTransport::reconnect() |
| 92 | { |
| 93 | NFD_LOG_FACE_TRACE(__func__); |
| 94 | |
| 95 | if (getState() == TransportState::CLOSING || |
| 96 | getState() == TransportState::FAILED || |
| 97 | getState() == TransportState::CLOSED) { |
| 98 | // transport is shutting down, don't attempt to reconnect |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 103 | BOOST_ASSERT(getState() == TransportState::DOWN); |
| 104 | |
| 105 | // recreate the socket |
| 106 | m_socket = protocol::socket(m_socket.get_io_service()); |
| 107 | this->resetReceiveBuffer(); |
| 108 | this->resetSendQueue(); |
| 109 | |
| 110 | m_reconnectEvent = scheduler::schedule(m_nextReconnectWait, |
| 111 | [this] { handleReconnectTimeout(); }); |
| 112 | m_socket.async_connect(m_remoteEndpoint, |
| 113 | [this] (const boost::system::error_code& error) { handleReconnect(error); }); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | TcpTransport::handleReconnect(const boost::system::error_code& error) |
| 118 | { |
| 119 | if (getState() == TransportState::CLOSING || |
| 120 | getState() == TransportState::FAILED || |
| 121 | getState() == TransportState::CLOSED || |
| 122 | error == boost::asio::error::operation_aborted) { |
| 123 | // transport is shutting down, abort the reconnection attempt and ignore any errors |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (error) { |
| 128 | NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message()); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | m_reconnectEvent.cancel(); |
| 133 | m_nextReconnectWait = s_initialReconnectWait; |
| 134 | |
| 135 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 136 | NFD_LOG_FACE_TRACE("TCP connection reestablished"); |
| 137 | this->setState(TransportState::UP); |
| 138 | this->startReceive(); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | TcpTransport::handleReconnectTimeout() |
| 143 | { |
| 144 | // abort the reconnection attempt |
| 145 | boost::system::error_code error; |
| 146 | m_socket.close(error); |
| 147 | |
| 148 | // exponentially back off the reconnection timer |
| 149 | m_nextReconnectWait = |
| 150 | std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * s_reconnectWaitMultiplier), |
| 151 | s_maxReconnectWait); |
| 152 | |
| 153 | // do this asynchronously because there could be some callbacks still pending |
| 154 | getGlobalIoService().post([this] { reconnect(); }); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | TcpTransport::doClose() |
| 159 | { |
| 160 | m_reconnectEvent.cancel(); |
| 161 | StreamTransport::doClose(); |
| 162 | } |
| 163 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 164 | } // namespace face |
| 165 | } // namespace nfd |