37 namespace ip = boost::asio::ip;
41 : m_localEndpoint(localEndpoint)
44 , m_wantCongestionMarking(wantCongestionMarking)
45 , m_determineFaceScope(
std::move(determineFaceScope))
47 setUri(FaceUri(m_localEndpoint));
61 m_acceptor.open(m_localEndpoint.protocol());
62 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
63 if (m_localEndpoint.address().is_v6()) {
64 m_acceptor.set_option(ip::v6_only(
true));
66 m_acceptor.bind(m_localEndpoint);
67 m_acceptor.listen(backlog);
69 accept(onFaceCreated, onAcceptFailed);
78 time::nanoseconds timeout)
80 auto it = m_channelFaces.find(remoteEndpoint);
81 if (it != m_channelFaces.end()) {
83 onFaceCreated(it->second);
88 auto timeoutEvent =
getScheduler().schedule(timeout, [=] {
89 handleConnectTimeout(remoteEndpoint, clientSocket, onConnectFailed);
93 clientSocket->async_connect(remoteEndpoint, [=] (
const auto& e) {
94 this->handleConnect(e, remoteEndpoint, clientSocket, params, timeoutEvent, onFaceCreated, onConnectFailed);
99 TcpChannel::createFace(ip::tcp::socket&& socket,
103 shared_ptr<Face> face;
106 auto it = m_channelFaces.find(remoteEndpoint);
107 if (it == m_channelFaces.end()) {
127 auto linkService = make_unique<GenericLinkService>(options);
128 auto faceScope = m_determineFaceScope(socket.local_endpoint().address(),
129 socket.remote_endpoint().address());
130 auto transport = make_unique<TcpTransport>(std::move(socket), params.
persistency, faceScope);
131 face = make_shared<Face>(std::move(linkService), std::move(transport));
132 face->setChannel(shared_from_this());
134 m_channelFaces[remoteEndpoint] = face;
142 boost::system::error_code error;
143 socket.shutdown(ip::tcp::socket::shutdown_both, error);
156 m_acceptor.async_accept(m_socket, [=] (
const auto& e) { this->handleAccept(e, 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());
176 params.
persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
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,
188 const scheduler::EventId& connectTimeoutEvent,
192 connectTimeoutEvent.cancel();
195 if (error != boost::asio::error::operation_aborted) {
196 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
198 onConnectFailed(504,
"Connection failed: " + error.message());
204 createFace(std::move(*socket), params, onFaceCreated);
208 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
209 const shared_ptr<ip::tcp::socket>& socket,
215 boost::system::error_code error;
216 socket->close(error);
219 onConnectFailed(504,
"Connection timed out");
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create TCP channel for the local endpoint.
bool isEnabled
enables link-layer reliability
std::function< ndn::nfd::FaceScope(const boost::asio::ip::address &local, const boost::asio::ip::address &remote)> DetermineFaceScopeFromAddress
boost::logic::tribool wantCongestionMarking
optional< uint64_t > defaultCongestionThreshold
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.
Parameters used to set Transport properties or LinkService options on a newly created face...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
bool isListening() const override
Returns whether the channel is listening.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
optional< time::nanoseconds > baseCongestionMarkingInterval
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
boost::asio::ip::tcp::endpoint Endpoint
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...
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.
LpReliability::Options reliabilityOptions
options for reliability
ndn::nfd::FacePersistency persistency
void setUri(const FaceUri &uri)
Options that control the behavior of GenericLinkService.
#define NFD_LOG_INIT(name)
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
time::nanoseconds baseCongestionMarkingInterval
starting value for congestion marking interval
size_t defaultCongestionThreshold
default congestion threshold in bytes
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy ...
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
bool allowCongestionMarking
enables send queue congestion detection and marking
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.