tcp-transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "tcp-transport.hpp"
27 
28 namespace nfd {
29 namespace face {
30 
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;
36 
37 TcpTransport::TcpTransport(protocol::socket&& socket, ndn::nfd::FacePersistency persistency)
38  : StreamTransport(std::move(socket))
39  , m_remoteEndpoint(m_socket.remote_endpoint())
40  , m_nextReconnectWait(s_initialReconnectWait)
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 bool
59 TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
60 {
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) {
69  if (this->getState() == TransportState::DOWN) {
70  // non-permanent transport cannot be in DOWN state, so fail hard
72  doClose();
73  }
74  }
75 }
76 
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());
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 {
93  }
94 }
95 
96 void
97 TcpTransport::reconnect()
98 {
99  NFD_LOG_FACE_TRACE(__func__);
100 
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 {
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");
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
165 {
166  m_reconnectEvent.cancel();
168 }
169 
170 } // namespace face
171 } // namespace nfd
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:384
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:74
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:408
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:95
void setMtu(ssize_t mtu)
Definition: transport.hpp:438
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
STL namespace.
TcpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency)
the transport is being closed due to a failure
Implements Transport for stream-based protocols.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
the transport is closed, and can be safely deallocated
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:80
#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name)
Definition: logger.hpp:135
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:426
the transport is requested to be closed
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:151
void handleError(const boost::system::error_code &error) final
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:414
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
void doClose() final
performs Transport specific operations to close the transport
TransportState getState() const
Definition: transport.hpp:445
virtual void doClose() override
performs Transport specific operations to close the transport
void setState(TransportState newState)
set transport state
Definition: transport.cpp:174
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:396
the transport is up
void cancel()
cancels the event manually
Definition: scheduler.cpp:95
virtual void handleError(const boost::system::error_code &error)
the transport is down temporarily, and is being recovered
boost::asio::io_service & getGlobalIoService()
Definition: global-io.cpp:41