NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
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 
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())
48  else
50 
51  this->setPersistency(persistency);
53  this->setMtu(MTU_UNLIMITED);
54 
55  NFD_LOG_FACE_INFO("Creating transport");
56 }
57 
58 bool
60 {
61  return true;
62 }
63 
64 void
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 {
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 
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 {
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 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 doClose() override
performs Transport specific operations to close the transport
void cancel()
cancels the event manually
Definition: scheduler.cpp:95
void doClose() final
performs Transport specific operations to close the transport
TcpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name)
Definition: logger.hpp:46
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:95
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:396
virtual void handleError(const boost::system::error_code &error)
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
STL namespace.
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:426
void setMtu(ssize_t mtu)
Definition: transport.hpp:438
Implements Transport for stream-based protocols.
the transport is being closed due to a failure
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:408
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
the transport is closed, and can be safely deallocated
void post(const std::function< void()> &callback)
Definition: global-io.cpp:34
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:384
void handleError(const boost::system::error_code &error) final
the transport is being closed gracefully, either by the peer or by a call to close() ...
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:43
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
Definition: scheduler.cpp:47
TransportState getState() const
Definition: transport.hpp:445
void setState(TransportState newState)
set transport state
Definition: transport.cpp:174
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
the transport is up and can transmit packets
the transport is temporarily down, and is being recovered
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:414