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 
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())
48  else
50 
51  this->setPersistency(persistency);
53  this->setMtu(MTU_UNLIMITED);
54 
55  NFD_LOG_FACE_INFO("Creating transport");
56 }
57 
58 void
60 {
61  // if persistency is changing from permanent to any other value
63  if (this->getState() == TransportState::DOWN) {
64  // non-permanent transport cannot be in DOWN state, so fail hard
66  doClose();
67  }
68  }
69 }
70 
71 void
72 TcpTransport::handleError(const boost::system::error_code& error)
73 {
75  NFD_LOG_FACE_TRACE("TCP socket error: " << error.message());
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 {
87  }
88 }
89 
90 void
91 TcpTransport::reconnect()
92 {
93  NFD_LOG_FACE_TRACE(__func__);
94 
98  // transport is shutting down, don't attempt to reconnect
99  return;
100  }
101 
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 {
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");
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
159 {
160  m_reconnectEvent.cancel();
162 }
163 
164 } // namespace face
165 } // namespace nfd
virtual 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:74
#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name)
Definition: logger.hpp:46
void setPersistency(ndn::nfd::FacePersistency persistency)
changes face persistency setting
Definition: transport.cpp:131
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:374
virtual void handleError(const boost::system::error_code &error)
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
STL namespace.
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:404
void setMtu(ssize_t mtu)
Definition: transport.hpp:416
Implements Transport for stream-based protocols.
the transport is being closed due to a failure
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:386
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:80
TransportState getState() const
Definition: transport.hpp:423
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:362
void handleError(const boost::system::error_code &error) final
the transport is requested to be closed
void beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) final
invoked before persistency is changed
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:392
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
void setState(TransportState newState)
set transport state
Definition: transport.cpp:150
the transport is up
the transport is down temporarily, and is being recovered