Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "tcp-channel.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 8 | #include "core/global-io.hpp" |
| 9 | #include "core/face-uri.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 13 | NFD_LOG_INIT("TcpChannel"); |
| 14 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 15 | using namespace boost::asio; |
| 16 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 17 | TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint) |
| 18 | : m_localEndpoint(localEndpoint) |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame^] | 19 | , m_isListening(false) |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 20 | { |
| 21 | this->setUri(FaceUri(localEndpoint)); |
| 22 | } |
| 23 | |
| 24 | TcpChannel::~TcpChannel() |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | { |
| 26 | } |
| 27 | |
| 28 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 29 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 30 | const ConnectFailedCallback& onAcceptFailed, |
| 31 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 32 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 33 | m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 34 | m_acceptor->open(m_localEndpoint.protocol()); |
| 35 | m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true)); |
Alexander Afanasyev | eee71fd | 2014-03-13 17:40:33 -0700 | [diff] [blame] | 36 | if (m_localEndpoint.address().is_v6()) |
| 37 | { |
| 38 | m_acceptor->set_option(ip::v6_only(true)); |
| 39 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 40 | m_acceptor->bind(m_localEndpoint); |
| 41 | m_acceptor->listen(backlog); |
| 42 | |
| 43 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 44 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 45 | m_acceptor->async_accept(*clientSocket, |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 46 | bind(&TcpChannel::handleSuccessfulAccept, this, _1, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 47 | clientSocket, |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 48 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame^] | 49 | |
| 50 | m_isListening = true; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 54 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 55 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 56 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 57 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 59 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 60 | if (i != m_channelFaces.end()) { |
| 61 | onFaceCreated(i->second); |
| 62 | return; |
| 63 | } |
| 64 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 65 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 66 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 68 | shared_ptr<ndn::monotonic_deadline_timer> connectTimeoutTimer = |
| 69 | make_shared<ndn::monotonic_deadline_timer>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 71 | clientSocket->async_connect(remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 72 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 73 | clientSocket, connectTimeoutTimer, |
| 74 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 75 | |
| 76 | connectTimeoutTimer->expires_from_now(timeout); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 77 | connectTimeoutTimer->async_wait(bind(&TcpChannel::handleFailedConnect, this, _1, |
| 78 | clientSocket, connectTimeoutTimer, |
| 79 | onConnectFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 82 | void |
| 83 | TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 84 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 85 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 86 | const time::seconds& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 87 | { |
| 88 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 89 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 91 | shared_ptr<ndn::monotonic_deadline_timer> connectTimeoutTimer = |
| 92 | make_shared<ndn::monotonic_deadline_timer>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 94 | ip::tcp::resolver::query query(remoteHost, remotePort); |
| 95 | shared_ptr<ip::tcp::resolver> resolver = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 96 | make_shared<ip::tcp::resolver>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 97 | |
| 98 | resolver->async_resolve(query, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 99 | bind(&TcpChannel::handleEndpointResolution, this, _1, _2, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 100 | clientSocket, connectTimeoutTimer, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 101 | onFaceCreated, onConnectFailed, |
| 102 | resolver)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 103 | |
| 104 | connectTimeoutTimer->expires_from_now(timeout); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 105 | connectTimeoutTimer->async_wait(bind(&TcpChannel::handleFailedConnect, this, _1, |
| 106 | clientSocket, connectTimeoutTimer, |
| 107 | onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 110 | size_t |
| 111 | TcpChannel::size() const |
| 112 | { |
| 113 | return m_channelFaces.size(); |
| 114 | } |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 115 | |
| 116 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 117 | TcpChannel::createFace(const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 118 | const FaceCreatedCallback& onFaceCreated, |
| 119 | bool isOnDemand) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 120 | { |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 121 | tcp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 123 | shared_ptr<Face> face; |
Alexander Afanasyev | bfd31de | 2014-02-26 13:20:08 -0800 | [diff] [blame] | 124 | if (socket->local_endpoint().address().is_loopback()) |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 125 | face = make_shared<TcpLocalFace>(boost::cref(socket), isOnDemand); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 126 | else |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 127 | face = make_shared<TcpFace>(boost::cref(socket), isOnDemand); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 129 | face->onFail += bind(&TcpChannel::afterFaceFailed, this, remoteEndpoint); |
| 130 | |
| 131 | onFaceCreated(face); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 132 | m_channelFaces[remoteEndpoint] = face; |
| 133 | } |
| 134 | |
| 135 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 136 | TcpChannel::afterFaceFailed(tcp::Endpoint &remoteEndpoint) |
| 137 | { |
| 138 | NFD_LOG_DEBUG("afterFaceFailed: " << remoteEndpoint); |
| 139 | m_channelFaces.erase(remoteEndpoint); |
| 140 | } |
| 141 | |
| 142 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 143 | TcpChannel::handleSuccessfulAccept(const boost::system::error_code& error, |
| 144 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 145 | const FaceCreatedCallback& onFaceCreated, |
| 146 | const ConnectFailedCallback& onAcceptFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 147 | { |
| 148 | if (error) { |
| 149 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 150 | return; |
| 151 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 152 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 153 | << error.category().message(error.value())); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 154 | |
| 155 | if (static_cast<bool>(onAcceptFailed)) |
| 156 | onAcceptFailed("Connect to remote endpoint failed: " + |
| 157 | error.category().message(error.value())); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 161 | // prepare accepting the next connection |
| 162 | shared_ptr<ip::tcp::socket> clientSocket = |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 163 | make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService())); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 164 | m_acceptor->async_accept(*clientSocket, |
| 165 | bind(&TcpChannel::handleSuccessfulAccept, this, _1, |
| 166 | clientSocket, |
| 167 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 169 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] " |
| 170 | "<< Connection from " << socket->remote_endpoint()); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 171 | createFace(socket, onFaceCreated, true); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
| 175 | TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error, |
| 176 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 177 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 178 | const FaceCreatedCallback& onFaceCreated, |
| 179 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 180 | { |
| 181 | timer->cancel(); |
| 182 | |
| 183 | if (error) { |
| 184 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 185 | return; |
| 186 | |
| 187 | socket->close(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 188 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 189 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 190 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 192 | onConnectFailed("Connect to remote endpoint failed: " + |
| 193 | error.category().message(error.value())); |
| 194 | return; |
| 195 | } |
| 196 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 197 | NFD_LOG_DEBUG("[" << m_localEndpoint << "] " |
| 198 | ">> Connection to " << socket->remote_endpoint()); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 200 | createFace(socket, onFaceCreated, false); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void |
| 204 | TcpChannel::handleFailedConnect(const boost::system::error_code& error, |
| 205 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 206 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 207 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 208 | { |
| 209 | if (error) { // e.g., cancelled |
| 210 | return; |
| 211 | } |
| 212 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 213 | NFD_LOG_DEBUG("Connect to remote endpoint timed out: " |
| 214 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 215 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 216 | onConnectFailed("Connect to remote endpoint timed out: " + |
| 217 | error.category().message(error.value())); |
| 218 | socket->close(); // abort the connection |
| 219 | } |
| 220 | |
| 221 | void |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 222 | TcpChannel::handleEndpointResolution(const boost::system::error_code& error, |
| 223 | ip::tcp::resolver::iterator remoteEndpoint, |
| 224 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 225 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 226 | const FaceCreatedCallback& onFaceCreated, |
| 227 | const ConnectFailedCallback& onConnectFailed, |
| 228 | const shared_ptr<ip::tcp::resolver>& resolver) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 229 | { |
| 230 | if (error || |
| 231 | remoteEndpoint == ip::tcp::resolver::iterator()) |
| 232 | { |
| 233 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 234 | return; |
| 235 | |
| 236 | socket->close(); |
| 237 | timer->cancel(); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 238 | |
| 239 | NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: " |
| 240 | << error.category().message(error.value())); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 241 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 242 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 243 | error.category().message(error.value())); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | // got endpoint, now trying to connect (only try the first resolution option) |
| 248 | socket->async_connect(*remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 249 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 250 | socket, timer, |
| 251 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 254 | } // namespace nfd |