32 #include <boost/asio/ip/v6_only.hpp>
36 namespace ip = boost::asio::ip;
42 : m_localEndpoint(localEndpoint)
43 , 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(boost::asio::socket_base::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,
104 shared_ptr<Face> face;
105 boost::system::error_code ec;
109 if (onFaceCreationFailed) {
110 onFaceCreationFailed(500,
"Retrieve socket remote endpoint failed: " + ec.message());
115 auto it = m_channelFaces.find(remoteEndpoint);
116 if (it == m_channelFaces.end()) {
123 options.allowCongestionMarking = m_wantCongestionMarking;
136 auto linkService = make_unique<GenericLinkService>(options);
137 auto faceScope = m_determineFaceScope(socket.local_endpoint().address(),
138 socket.remote_endpoint().address());
139 auto transport = make_unique<TcpTransport>(std::move(socket), params.
persistency, faceScope);
140 face = make_shared<Face>(std::move(linkService), std::move(transport));
141 face->setChannel(weak_from_this());
143 m_channelFaces[remoteEndpoint] = face;
151 boost::system::error_code error;
152 socket.shutdown(boost::asio::socket_base::shutdown_both, error);
165 m_acceptor.async_accept([=] (
const boost::system::error_code& error, ip::tcp::socket socket) {
167 if (error != boost::asio::error::operation_aborted) {
170 onAcceptFailed(500,
"Accept failed: " + error.message());
178 params.
persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
179 createFace(std::move(socket), params, onFaceCreated, onAcceptFailed);
182 accept(onFaceCreated, onAcceptFailed);
187 TcpChannel::handleConnect(
const boost::system::error_code& error,
189 const shared_ptr<ip::tcp::socket>& socket,
190 const FaceParams& params,
191 const ndn::scheduler::EventId& connectTimeoutEvent,
195 connectTimeoutEvent.cancel();
198 if (error != boost::asio::error::operation_aborted) {
199 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
201 onConnectFailed(504,
"Connection failed: " + error.message());
207 createFace(std::move(*socket), params, onFaceCreated, onConnectFailed);
211 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
212 const shared_ptr<ip::tcp::socket>& socket,
218 boost::system::error_code error;
219 socket->close(error);
222 onConnectFailed(504,
"Connection timed out");
void setUri(const FaceUri &uri) noexcept
GenericLinkServiceOptions Options
Options for GenericLinkService.
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create a TCP channel for the specified localEndpoint.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::socket_base::max_listen_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.
bool isListening() const final
Returns whether the channel is listening.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
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.
std::function< ndn::nfd::FaceScope(const boost::asio::ip::address &local, const boost::asio::ip::address &remote)> DetermineFaceScopeFromAddress
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...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
boost::asio::ip::tcp::endpoint Endpoint
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
Parameters used to set Transport properties or LinkService options on a newly created face.
std::optional< uint64_t > defaultCongestionThreshold
std::optional< time::nanoseconds > baseCongestionMarkingInterval
ndn::nfd::FacePersistency persistency
boost::logic::tribool wantCongestionMarking
bool allowLocalFields
Enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy.