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