Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
| 26 | #include "tcp-channel.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 27 | #include "core/global-io.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 29 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 31 | NFD_LOG_INIT("TcpChannel"); |
| 32 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 33 | using namespace boost::asio; |
| 34 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 35 | TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint) |
| 36 | : m_localEndpoint(localEndpoint) |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 37 | , m_isListening(false) |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 38 | { |
| 39 | this->setUri(FaceUri(localEndpoint)); |
| 40 | } |
| 41 | |
| 42 | TcpChannel::~TcpChannel() |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 47 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 48 | const ConnectFailedCallback& onAcceptFailed, |
| 49 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 51 | m_acceptor = make_shared<ip::tcp::acceptor>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 52 | m_acceptor->open(m_localEndpoint.protocol()); |
| 53 | m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 54 | if (m_localEndpoint.address().is_v6()) |
| 55 | { |
| 56 | m_acceptor->set_option(ip::v6_only(true)); |
| 57 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 58 | m_acceptor->bind(m_localEndpoint); |
| 59 | m_acceptor->listen(backlog); |
| 60 | |
| 61 | shared_ptr<ip::tcp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 62 | make_shared<ip::tcp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 63 | m_acceptor->async_accept(*clientSocket, |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 64 | bind(&TcpChannel::handleSuccessfulAccept, this, _1, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 65 | clientSocket, |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 66 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 67 | |
| 68 | m_isListening = true; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 72 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 73 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 74 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 75 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 76 | { |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 77 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 78 | if (i != m_channelFaces.end()) { |
| 79 | onFaceCreated(i->second); |
| 80 | return; |
| 81 | } |
| 82 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 83 | shared_ptr<ip::tcp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 84 | make_shared<ip::tcp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 85 | |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 86 | scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout, |
| 87 | bind(&TcpChannel::handleFailedConnect, this, clientSocket, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 89 | clientSocket->async_connect(remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 90 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 91 | clientSocket, connectTimeoutEvent, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 92 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 95 | void |
| 96 | TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 97 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 98 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 99 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 100 | { |
| 101 | shared_ptr<ip::tcp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 102 | make_shared<ip::tcp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 104 | ip::tcp::resolver::query query(remoteHost, remotePort); |
| 105 | shared_ptr<ip::tcp::resolver> resolver = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 106 | make_shared<ip::tcp::resolver>(ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 107 | |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 108 | scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout, |
| 109 | bind(&TcpChannel::handleFailedConnect, this, clientSocket, onConnectFailed)); |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 111 | resolver->async_resolve(query, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 112 | bind(&TcpChannel::handleEndpointResolution, this, _1, _2, |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 113 | clientSocket, connectTimeoutEvent, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 114 | onFaceCreated, onConnectFailed, |
| 115 | resolver)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 118 | size_t |
| 119 | TcpChannel::size() const |
| 120 | { |
| 121 | return m_channelFaces.size(); |
| 122 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 123 | |
| 124 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 125 | TcpChannel::createFace(const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 126 | const FaceCreatedCallback& onFaceCreated, |
| 127 | bool isOnDemand) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 129 | tcp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 131 | shared_ptr<Face> face; |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 132 | |
| 133 | ChannelFaceMap::iterator faceMapPos = m_channelFaces.find(remoteEndpoint); |
| 134 | if (faceMapPos == m_channelFaces.end()) |
| 135 | { |
| 136 | if (socket->local_endpoint().address().is_loopback()) |
| 137 | face = make_shared<TcpLocalFace>(socket, isOnDemand); |
| 138 | else |
| 139 | face = make_shared<TcpFace>(socket, isOnDemand); |
| 140 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 141 | face->onFail.connectSingleShot(bind(&TcpChannel::afterFaceFailed, this, remoteEndpoint)); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 142 | |
| 143 | m_channelFaces[remoteEndpoint] = face; |
| 144 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 145 | else |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 146 | { |
| 147 | // we've already created a a face for this endpoint, just reuse it |
| 148 | face = faceMapPos->second; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 149 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 150 | boost::system::error_code error; |
| 151 | socket->shutdown(ip::tcp::socket::shutdown_both, error); |
| 152 | socket->close(error); |
| 153 | } |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 154 | |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 155 | // Need to invoke the callback regardless of whether or not we have already created |
| 156 | // the face so that control responses and such can be sent. |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 157 | onFaceCreated(face); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 161 | TcpChannel::afterFaceFailed(tcp::Endpoint &remoteEndpoint) |
| 162 | { |
| 163 | NFD_LOG_DEBUG("afterFaceFailed: " << remoteEndpoint); |
| 164 | m_channelFaces.erase(remoteEndpoint); |
| 165 | } |
| 166 | |
| 167 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 168 | TcpChannel::handleSuccessfulAccept(const boost::system::error_code& error, |
| 169 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 170 | const FaceCreatedCallback& onFaceCreated, |
| 171 | const ConnectFailedCallback& onAcceptFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 172 | { |
| 173 | if (error) { |
| 174 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 175 | return; |
| 176 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 177 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 178 | << error.category().message(error.value())); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 179 | |
| 180 | if (static_cast<bool>(onAcceptFailed)) |
| 181 | onAcceptFailed("Connect to remote endpoint failed: " + |
| 182 | error.category().message(error.value())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 186 | // prepare accepting the next connection |
| 187 | shared_ptr<ip::tcp::socket> clientSocket = |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 188 | make_shared<ip::tcp::socket>(ref(getGlobalIoService())); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 189 | m_acceptor->async_accept(*clientSocket, |
| 190 | bind(&TcpChannel::handleSuccessfulAccept, this, _1, |
| 191 | clientSocket, |
| 192 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 194 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] " |
| 195 | "<< Connection from " << socket->remote_endpoint()); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 196 | createFace(socket, onFaceCreated, true); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void |
| 200 | TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error, |
| 201 | const shared_ptr<ip::tcp::socket>& socket, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 202 | const scheduler::EventId& connectTimeoutEvent, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 203 | const FaceCreatedCallback& onFaceCreated, |
| 204 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 205 | { |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 206 | scheduler::cancel(connectTimeoutEvent); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | 7465d5f | 2014-07-07 10:19:42 -0700 | [diff] [blame] | 208 | #if (BOOST_VERSION == 105400) |
| 209 | // To handle regression in Boost 1.54 |
| 210 | // https://svn.boost.org/trac/boost/ticket/8795 |
| 211 | boost::system::error_code anotherErrorCode; |
| 212 | socket->remote_endpoint(anotherErrorCode); |
| 213 | if (error || anotherErrorCode) { |
| 214 | #else |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 215 | if (error) { |
Alexander Afanasyev | 7465d5f | 2014-07-07 10:19:42 -0700 | [diff] [blame] | 216 | #endif |
| 217 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 218 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 219 | return; |
| 220 | |
| 221 | socket->close(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 222 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 223 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 224 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 225 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 226 | onConnectFailed("Connect to remote endpoint failed: " + |
| 227 | error.category().message(error.value())); |
| 228 | return; |
| 229 | } |
| 230 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 231 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] " |
| 232 | ">> Connection to " << socket->remote_endpoint()); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 233 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 234 | createFace(socket, onFaceCreated, false); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 238 | TcpChannel::handleFailedConnect(const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 239 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 240 | { |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 241 | NFD_LOG_DEBUG("Connect to remote endpoint timed out"); |
| 242 | onConnectFailed("Connect to remote endpoint timed out"); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 243 | socket->close(); // abort the connection |
| 244 | } |
| 245 | |
| 246 | void |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 247 | TcpChannel::handleEndpointResolution(const boost::system::error_code& error, |
| 248 | ip::tcp::resolver::iterator remoteEndpoint, |
| 249 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 250 | const scheduler::EventId& connectTimeoutEvent, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 251 | const FaceCreatedCallback& onFaceCreated, |
| 252 | const ConnectFailedCallback& onConnectFailed, |
| 253 | const shared_ptr<ip::tcp::resolver>& resolver) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 254 | { |
| 255 | if (error || |
| 256 | remoteEndpoint == ip::tcp::resolver::iterator()) |
| 257 | { |
| 258 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 259 | return; |
| 260 | |
| 261 | socket->close(); |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 262 | scheduler::cancel(connectTimeoutEvent); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 263 | |
| 264 | NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: " |
| 265 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 266 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 267 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 268 | error.category().message(error.value())); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | // got endpoint, now trying to connect (only try the first resolution option) |
| 273 | socket->async_connect(*remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 274 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
Alexander Afanasyev | 1886180 | 2014-06-13 17:49:03 -0700 | [diff] [blame] | 275 | socket, connectTimeoutEvent, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 276 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 279 | } // namespace nfd |