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);
88 handleConnectTimeout(remoteEndpoint, clientSocket, onConnectFailed);
92 clientSocket->async_connect(remoteEndpoint, [=] (
const auto& e) {
93 this->handleConnect(e, remoteEndpoint, clientSocket, 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, [=] (
const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
158 TcpChannel::handleAccept(
const boost::system::error_code& error,
163 if (error != boost::asio::error::operation_aborted) {
166 onAcceptFailed(500,
"Accept failed: " + error.message());
175 createFace(std::move(m_socket), params, onFaceCreated);
178 accept(onFaceCreated, onAcceptFailed);
182 TcpChannel::handleConnect(
const boost::system::error_code& error,
184 const shared_ptr<ip::tcp::socket>& socket,
185 const FaceParams& params,
186 const scheduler::EventId& connectTimeoutEvent,
193 if (error != boost::asio::error::operation_aborted) {
194 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
196 onConnectFailed(504,
"Connection failed: " + error.message());
202 createFace(std::move(*socket), params, onFaceCreated);
206 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
207 const shared_ptr<ip::tcp::socket>& socket,
213 boost::system::error_code error;
214 socket->close(error);
217 onConnectFailed(504,
"Connection timed out");
optional< uint64_t > defaultCongestionThreshold
void setUri(const FaceUri &uri)
bool isListening() const override
Returns whether the channel is listening.
boost::logic::tribool wantCongestionMarking
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
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
ndn::nfd::FacePersistency persistency
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
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
void cancel(EventId eventId)
Cancel a scheduled event.
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create TCP channel for the local endpoint.
optional< time::nanoseconds > baseCongestionMarkingInterval
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.
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.
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
#define NFD_LOG_INIT(name)
Class implementing TCP-based channel to create faces.
Parameters used to set Transport properties or LinkService options on a newly created face.