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