32 #include <boost/asio/ip/v6_only.hpp>
38 namespace ip = boost::asio::ip;
42 : m_localEndpoint(localEndpoint)
45 , m_wantCongestionMarking(wantCongestionMarking)
46 , m_determineFaceScope(std::move(determineFaceScope))
48 setUri(FaceUri(m_localEndpoint));
62 m_acceptor.open(m_localEndpoint.protocol());
63 m_acceptor.set_option(ip::tcp::acceptor::reuse_address(
true));
64 if (m_localEndpoint.address().is_v6()) {
65 m_acceptor.set_option(ip::v6_only(
true));
67 m_acceptor.bind(m_localEndpoint);
68 m_acceptor.listen(backlog);
70 accept(onFaceCreated, onAcceptFailed);
79 time::nanoseconds timeout)
81 auto it = m_channelFaces.find(remoteEndpoint);
82 if (it != m_channelFaces.end()) {
84 onFaceCreated(it->second);
89 auto timeoutEvent =
getScheduler().schedule(timeout, [=] {
90 handleConnectTimeout(remoteEndpoint, clientSocket, onConnectFailed);
94 clientSocket->async_connect(remoteEndpoint, [=] (
const auto& e) {
95 this->handleConnect(e, remoteEndpoint, clientSocket, params, timeoutEvent, onFaceCreated, onConnectFailed);
100 TcpChannel::createFace(ip::tcp::socket&& socket,
105 shared_ptr<Face> face;
106 boost::system::error_code ec;
110 if (onFaceCreationFailed) {
111 onFaceCreationFailed(500,
"Retrieve socket remote endpoint failed: " + ec.message());
116 auto it = m_channelFaces.find(remoteEndpoint);
117 if (it == m_channelFaces.end()) {
118 GenericLinkService::Options options;
124 options.allowCongestionMarking = m_wantCongestionMarking;
137 auto linkService = make_unique<GenericLinkService>(options);
138 auto faceScope = m_determineFaceScope(socket.local_endpoint().address(),
139 socket.remote_endpoint().address());
140 auto transport = make_unique<TcpTransport>(std::move(socket), params.
persistency, faceScope);
141 face = make_shared<Face>(std::move(linkService), std::move(transport));
142 face->setChannel(weak_from_this());
144 m_channelFaces[remoteEndpoint] = face;
152 boost::system::error_code error;
153 socket.shutdown(ip::tcp::socket::shutdown_both, error);
166 m_acceptor.async_accept(m_socket, [=] (
const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
170 TcpChannel::handleAccept(
const boost::system::error_code& error,
175 if (error != boost::asio::error::operation_aborted) {
178 onAcceptFailed(500,
"Accept failed: " + error.message());
186 params.
persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
187 createFace(std::move(m_socket), params, onFaceCreated, onAcceptFailed);
190 accept(onFaceCreated, onAcceptFailed);
194 TcpChannel::handleConnect(
const boost::system::error_code& error,
196 const shared_ptr<ip::tcp::socket>& socket,
197 const FaceParams& params,
198 const scheduler::EventId& connectTimeoutEvent,
202 connectTimeoutEvent.cancel();
205 if (error != boost::asio::error::operation_aborted) {
206 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
208 onConnectFailed(504,
"Connection failed: " + error.message());
214 createFace(std::move(*socket), params, onFaceCreated, onConnectFailed);
218 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
219 const shared_ptr<ip::tcp::socket>& socket,
225 boost::system::error_code error;
226 socket->close(error);
229 onConnectFailed(504,
"Connection timed out");
void setUri(const FaceUri &uri) noexcept
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...
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create TCP channel for the local endpoint.
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
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Scheduler & getScheduler()
Returns the global Scheduler 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