37 namespace ip = boost::asio::ip;
41 : m_localEndpoint(localEndpoint)
44 , m_wantCongestionMarking(wantCongestionMarking)
45 , m_determineFaceScope(std::
move(determineFaceScope))
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());
133 m_channelFaces[remoteEndpoint] = face;
141 boost::system::error_code error;
142 socket.shutdown(ip::tcp::socket::shutdown_both, error);
155 m_acceptor.async_accept(m_socket, [=] (
const auto& e) { this->handleAccept(e, onFaceCreated, onAcceptFailed); });
159 TcpChannel::handleAccept(
const boost::system::error_code& error,
164 if (error != boost::asio::error::operation_aborted) {
167 onAcceptFailed(500,
"Accept failed: " + error.message());
176 createFace(
std::move(m_socket), params, onFaceCreated);
179 accept(onFaceCreated, onAcceptFailed);
183 TcpChannel::handleConnect(
const boost::system::error_code& error,
185 const shared_ptr<ip::tcp::socket>& socket,
186 const FaceParams& params,
191 connectTimeoutEvent.
cancel();
194 if (error != boost::asio::error::operation_aborted) {
195 NFD_LOG_CHAN_DEBUG(
"Connection to " << remoteEndpoint <<
" failed: " << error.message());
197 onConnectFailed(504,
"Connection failed: " + error.message());
203 createFace(
std::move(*socket), params, onFaceCreated);
207 TcpChannel::handleConnectTimeout(
const tcp::Endpoint& remoteEndpoint,
208 const shared_ptr<ip::tcp::socket>& socket,
214 boost::system::error_code error;
215 socket->close(error);
218 onConnectFailed(504,
"Connection timed out");