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" |
| 8 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 9 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 11 | NFD_LOG_INIT("TcpChannel"); |
| 12 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 13 | using namespace boost::asio; |
| 14 | |
| 15 | TcpChannel::TcpChannel(io_service& ioService, |
| 16 | const tcp::Endpoint& localEndpoint) |
| 17 | : m_ioService(ioService) |
| 18 | , m_localEndpoint(localEndpoint) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 19 | { |
| 20 | } |
| 21 | |
| 22 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 23 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 24 | const ConnectFailedCallback& onAcceptFailed, |
| 25 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 26 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 27 | m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(m_ioService)); |
| 28 | m_acceptor->open(m_localEndpoint.protocol()); |
| 29 | m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true)); |
| 30 | m_acceptor->bind(m_localEndpoint); |
| 31 | m_acceptor->listen(backlog); |
| 32 | |
| 33 | shared_ptr<ip::tcp::socket> clientSocket = |
| 34 | make_shared<ip::tcp::socket>(boost::ref(m_ioService)); |
| 35 | m_acceptor->async_accept(*clientSocket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 36 | bind(&TcpChannel::handleConnection, this, _1, |
| 37 | clientSocket, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 38 | onFaceCreated, onAcceptFailed, |
| 39 | true)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 43 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 44 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 45 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 46 | const time::Duration& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 47 | { |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 48 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 49 | if (i != m_channelFaces.end()) { |
| 50 | onFaceCreated(i->second); |
| 51 | return; |
| 52 | } |
| 53 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 54 | shared_ptr<ip::tcp::socket> clientSocket = |
| 55 | make_shared<ip::tcp::socket>(boost::ref(m_ioService)); |
| 56 | |
| 57 | shared_ptr<monotonic_deadline_timer> connectTimeoutTimer = |
| 58 | make_shared<monotonic_deadline_timer>(boost::ref(m_ioService)); |
| 59 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 60 | clientSocket->open(m_localEndpoint.protocol()); |
| 61 | |
| 62 | // The following does not work and CCNx does not bind the local |
| 63 | // socket to a fixed port number for TCP connections |
| 64 | |
| 65 | // clientSocket->set_option(ip::tcp::socket::reuse_address(true)); |
| 66 | // clientSocket->bind(m_localEndpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 67 | |
| 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 | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 83 | const time::Duration& timeout/* = time::seconds(4)*/) |
| 84 | { |
| 85 | shared_ptr<ip::tcp::socket> clientSocket = |
| 86 | make_shared<ip::tcp::socket>(boost::ref(m_ioService)); |
| 87 | |
| 88 | shared_ptr<monotonic_deadline_timer> connectTimeoutTimer = |
| 89 | make_shared<monotonic_deadline_timer>(boost::ref(m_ioService)); |
| 90 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 91 | clientSocket->open(m_localEndpoint.protocol()); |
| 92 | |
| 93 | // The following does not work and CCNx does not bind the local |
| 94 | // socket to a fixed port number for TCP connections |
| 95 | |
| 96 | // clientSocket->set_option(ip::tcp::socket::reuse_address(true)); |
| 97 | // clientSocket->bind(m_localEndpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 98 | |
| 99 | ip::tcp::resolver::query query(remoteHost, remotePort); |
| 100 | shared_ptr<ip::tcp::resolver> resolver = |
| 101 | make_shared<ip::tcp::resolver>(boost::ref(m_ioService)); |
| 102 | |
| 103 | resolver->async_resolve(query, |
| 104 | bind(&TcpChannel::handleEndpointResoution, this, _1, _2, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 105 | clientSocket, connectTimeoutTimer, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 106 | onFaceCreated, onConnectFailed, |
| 107 | resolver)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 108 | |
| 109 | connectTimeoutTimer->expires_from_now(timeout); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 110 | connectTimeoutTimer->async_wait(bind(&TcpChannel::handleFailedConnect, this, _1, |
| 111 | clientSocket, connectTimeoutTimer, |
| 112 | onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | TcpChannel::handleConnection(const boost::system::error_code& error, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 118 | const shared_ptr<ip::tcp::socket>& socket, |
| 119 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 120 | const ConnectFailedCallback& onConnectFailed, |
| 121 | bool remoteConnection) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 122 | { |
| 123 | if (error) { |
| 124 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 125 | return; |
| 126 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 127 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 128 | << error.category().message(error.value())); |
| 129 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 130 | onConnectFailed("Connect to remote endpoint failed: " + |
| 131 | error.category().message(error.value())); |
| 132 | return; |
| 133 | } |
| 134 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 135 | if (remoteConnection) |
| 136 | { |
| 137 | NFD_LOG_DEBUG("[" << socket->local_endpoint() << "] " |
| 138 | "<< Connection from " << socket->remote_endpoint()); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | NFD_LOG_DEBUG("[" << socket->local_endpoint() << "] " |
| 143 | ">> Connection to " << socket->remote_endpoint()); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * \todo Remove FaceId from here |
| 148 | */ |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 149 | shared_ptr<TcpFace> face = make_shared<TcpFace>(boost::cref(socket)); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 150 | onFaceCreated(face); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 151 | |
| 152 | tcp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
| 153 | m_channelFaces[remoteEndpoint] = face; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
| 157 | TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error, |
| 158 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 159 | const shared_ptr<monotonic_deadline_timer>& timer, |
| 160 | const FaceCreatedCallback& onFaceCreated, |
| 161 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 162 | { |
| 163 | timer->cancel(); |
| 164 | |
| 165 | if (error) { |
| 166 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 167 | return; |
| 168 | |
| 169 | socket->close(); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 170 | |
| 171 | NFD_LOG_DEBUG("Connect to remote endpoint failed: " |
| 172 | << error.category().message(error.value())); |
| 173 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 174 | onConnectFailed("Connect to remote endpoint failed: " + |
| 175 | error.category().message(error.value())); |
| 176 | return; |
| 177 | } |
| 178 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 179 | handleConnection(error, socket, onFaceCreated, onConnectFailed, false); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
| 183 | TcpChannel::handleFailedConnect(const boost::system::error_code& error, |
| 184 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 185 | const shared_ptr<monotonic_deadline_timer>& timer, |
| 186 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 187 | { |
| 188 | if (error) { // e.g., cancelled |
| 189 | return; |
| 190 | } |
| 191 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 192 | NFD_LOG_DEBUG("Connect to remote endpoint timed out: " |
| 193 | << error.category().message(error.value())); |
| 194 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 195 | onConnectFailed("Connect to remote endpoint timed out: " + |
| 196 | error.category().message(error.value())); |
| 197 | socket->close(); // abort the connection |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | TcpChannel::handleEndpointResoution(const boost::system::error_code& error, |
| 202 | ip::tcp::resolver::iterator remoteEndpoint, |
| 203 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 204 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer, |
| 205 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 206 | const ConnectFailedCallback& onConnectFailed, |
| 207 | const shared_ptr<ip::tcp::resolver>& resolver) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 208 | { |
| 209 | if (error || |
| 210 | remoteEndpoint == ip::tcp::resolver::iterator()) |
| 211 | { |
| 212 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 213 | return; |
| 214 | |
| 215 | socket->close(); |
| 216 | timer->cancel(); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 217 | |
| 218 | NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: " |
| 219 | << error.category().message(error.value())); |
| 220 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 221 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 222 | error.category().message(error.value())); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // got endpoint, now trying to connect (only try the first resolution option) |
| 227 | socket->async_connect(*remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 228 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 229 | socket, timer, |
| 230 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 234 | } // namespace nfd |