36 namespace ip = boost::asio::ip;
40 : m_localEndpoint(localEndpoint)
43 , m_wantCongestionMarking(wantCongestionMarking)
44 , m_determineFaceScope(
std::move(determineFaceScope))
60 m_acceptor.open(m_localEndpoint.protocol());
61 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
62 if (m_localEndpoint.address().is_v6()) {
63 m_acceptor.set_option(ip::v6_only(
true));
65 m_acceptor.bind(m_localEndpoint);
66 m_acceptor.listen(backlog);
68 accept(onFaceCreated, onAcceptFailed);
77 time::nanoseconds timeout)
79 auto it = m_channelFaces.find(remoteEndpoint);
80 if (it != m_channelFaces.end()) {
82 onFaceCreated(it->second);
87 auto timeoutEvent =
scheduler::schedule(timeout, bind(&TcpChannel::handleConnectTimeout,
this,
88 remoteEndpoint, clientSocket, onConnectFailed));
91 clientSocket->async_connect(remoteEndpoint,
92 bind(&TcpChannel::handleConnect,
this,
93 boost::asio::placeholders::error, remoteEndpoint, clientSocket,
94 params, timeoutEvent, onFaceCreated, onConnectFailed));
98 TcpChannel::createFace(ip::tcp::socket&& socket,
102 shared_ptr<Face>
face;
105 auto it = m_channelFaces.find(remoteEndpoint);
106 if (it == m_channelFaces.end()) {
126 auto linkService = make_unique<GenericLinkService>(options);
127 auto faceScope = m_determineFaceScope(socket.local_endpoint().address(),
128 socket.remote_endpoint().address());
129 auto transport = make_unique<TcpTransport>(std::move(socket), params.
persistency, faceScope);
130 face = make_shared<Face>(std::move(linkService), std::move(transport));
132 m_channelFaces[remoteEndpoint] =
face;
140 boost::system::error_code error;
141 socket.shutdown(ip::tcp::socket::shutdown_both, error);
154 m_acceptor.async_accept(m_socket, bind(&TcpChannel::handleAccept,
this,
155 boost::asio::placeholders::error,
156 onFaceCreated, onAcceptFailed));
160 TcpChannel::handleAccept(
const boost::system::error_code& error,
165 if (error != boost::asio::error::operation_aborted) {
168 onAcceptFailed(500,
"Accept failed: " + error.message());
177 createFace(std::move(m_socket), params, onFaceCreated);
180 accept(onFaceCreated, onAcceptFailed);
184 TcpChannel::handleConnect(
const boost::system::error_code& error,
186 const shared_ptr<ip::tcp::socket>& socket,
187 const FaceParams& params,
194 #if (BOOST_VERSION == 105400) 197 boost::system::error_code anotherErrorCode;
198 socket->remote_endpoint(anotherErrorCode);
199 if (error || anotherErrorCode) {
203 if (error != boost::asio::error::operation_aborted) {
204 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
206 onConnectFailed(504,
"Connection failed: " + error.message());
212 createFace(std::move(*socket), params, onFaceCreated);
216 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
217 const shared_ptr<ip::tcp::socket>& socket,
223 boost::system::error_code error;
224 socket->close(error);
227 onConnectFailed(504,
"Connection timed out");
optional< time::nanoseconds > baseCongestionMarkingInterval
void setUri(const FaceUri &uri)
bool isListening() const override
Returns whether the channel is listening.
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy ...
void cancel(const EventId &eventId)
Cancel a scheduled event.
size_t defaultCongestionThreshold
default congestion threshold in bytes
std::function< ndn::nfd::FaceScope(const boost::asio::ip::address &local, const boost::asio::ip::address &remote)> DetermineFaceScopeFromAddress
LpReliability::Options reliabilityOptions
options for reliability
detail::SimulatorIo & getGlobalIoService()
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::ip::tcp::acceptor::max_connections)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
std::function< void(const shared_ptr< Face > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
bool isEnabled
enables link-layer reliability
optional< uint64_t > defaultCongestionThreshold
std::shared_ptr< ns3::EventId > EventId
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Parameters used to set Transport properties or LinkService options on a newly created face...
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create TCP channel for the local endpoint.
bool allowCongestionMarking
enables send queue congestion detection and marking
Copyright (c) 2011-2015 Regents of the University of California.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
time::nanoseconds baseCongestionMarkingInterval
starting value for congestion marking interval
boost::asio::ip::tcp::endpoint Endpoint
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
ndn::nfd::FacePersistency persistency
represents the underlying protocol and address used by a Face
Options that control the behavior of GenericLinkService.
void connect(const tcp::Endpoint &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, time::nanoseconds timeout=8_s)
Create a face by establishing a TCP connection to remoteEndpoint.
boost::logic::tribool wantCongestionMarking
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
#define NFD_LOG_INIT(name)