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 | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 11 | using namespace boost::asio; |
| 12 | |
| 13 | TcpChannel::TcpChannel(io_service& ioService, |
| 14 | const tcp::Endpoint& localEndpoint) |
| 15 | : m_ioService(ioService) |
| 16 | , m_localEndpoint(localEndpoint) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | |
| 20 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 21 | TcpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
| 22 | const ConnectFailedCallback& onAcceptFailed, |
| 23 | int backlog/* = tcp::acceptor::max_connections*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 24 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 25 | m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(m_ioService)); |
| 26 | m_acceptor->open(m_localEndpoint.protocol()); |
| 27 | m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true)); |
| 28 | m_acceptor->bind(m_localEndpoint); |
| 29 | m_acceptor->listen(backlog); |
| 30 | |
| 31 | shared_ptr<ip::tcp::socket> clientSocket = |
| 32 | make_shared<ip::tcp::socket>(boost::ref(m_ioService)); |
| 33 | m_acceptor->async_accept(*clientSocket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 34 | bind(&TcpChannel::handleConnection, this, _1, |
| 35 | clientSocket, |
| 36 | onFaceCreated, onAcceptFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 40 | TcpChannel::connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 41 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 42 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 43 | const time::Duration& timeout/* = time::seconds(4)*/) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 45 | ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint); |
| 46 | if (i != m_channelFaces.end()) { |
| 47 | onFaceCreated(i->second); |
| 48 | return; |
| 49 | } |
| 50 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 51 | shared_ptr<ip::tcp::socket> clientSocket = |
| 52 | make_shared<ip::tcp::socket>(boost::ref(m_ioService)); |
| 53 | |
| 54 | shared_ptr<monotonic_deadline_timer> connectTimeoutTimer = |
| 55 | make_shared<monotonic_deadline_timer>(boost::ref(m_ioService)); |
| 56 | |
| 57 | // not sure if it works. This will bind to something... |
| 58 | // Do we need reuse here too? |
| 59 | clientSocket->bind(m_localEndpoint); |
| 60 | |
| 61 | clientSocket->async_connect(remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 62 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 63 | clientSocket, connectTimeoutTimer, |
| 64 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 65 | |
| 66 | connectTimeoutTimer->expires_from_now(timeout); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 67 | connectTimeoutTimer->async_wait(bind(&TcpChannel::handleFailedConnect, this, _1, |
| 68 | clientSocket, connectTimeoutTimer, |
| 69 | onConnectFailed)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 72 | void |
| 73 | TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 74 | const TcpChannel::FaceCreatedCallback& onFaceCreated, |
| 75 | const TcpChannel::ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 76 | const time::Duration& timeout/* = time::seconds(4)*/) |
| 77 | { |
| 78 | shared_ptr<ip::tcp::socket> clientSocket = |
| 79 | make_shared<ip::tcp::socket>(boost::ref(m_ioService)); |
| 80 | |
| 81 | shared_ptr<monotonic_deadline_timer> connectTimeoutTimer = |
| 82 | make_shared<monotonic_deadline_timer>(boost::ref(m_ioService)); |
| 83 | |
| 84 | // not sure if it works. This will bind to something... |
| 85 | // Do we need reuse here too? |
| 86 | clientSocket->bind(m_localEndpoint); |
| 87 | |
| 88 | ip::tcp::resolver::query query(remoteHost, remotePort); |
| 89 | shared_ptr<ip::tcp::resolver> resolver = |
| 90 | make_shared<ip::tcp::resolver>(boost::ref(m_ioService)); |
| 91 | |
| 92 | resolver->async_resolve(query, |
| 93 | bind(&TcpChannel::handleEndpointResoution, this, _1, _2, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 94 | clientSocket, connectTimeoutTimer, |
| 95 | onFaceCreated, onConnectFailed)); |
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 | |
| 103 | |
| 104 | void |
| 105 | TcpChannel::handleConnection(const boost::system::error_code& error, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 106 | const shared_ptr<ip::tcp::socket>& socket, |
| 107 | const FaceCreatedCallback& onFaceCreated, |
| 108 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 109 | { |
| 110 | if (error) { |
| 111 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 112 | return; |
| 113 | |
| 114 | /// \todo Log the error |
| 115 | onConnectFailed("Connect to remote endpoint failed: " + |
| 116 | error.category().message(error.value())); |
| 117 | return; |
| 118 | } |
| 119 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame^] | 120 | shared_ptr<TcpFace> face = make_shared<TcpFace>(boost::cref(socket)); |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 121 | onFaceCreated(face); |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 122 | |
| 123 | tcp::Endpoint remoteEndpoint = socket->remote_endpoint(); |
| 124 | m_channelFaces[remoteEndpoint] = face; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error, |
| 129 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 130 | const shared_ptr<monotonic_deadline_timer>& timer, |
| 131 | const FaceCreatedCallback& onFaceCreated, |
| 132 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 133 | { |
| 134 | timer->cancel(); |
| 135 | |
| 136 | if (error) { |
| 137 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 138 | return; |
| 139 | |
| 140 | socket->close(); |
| 141 | onConnectFailed("Connect to remote endpoint failed: " + |
| 142 | error.category().message(error.value())); |
| 143 | return; |
| 144 | } |
| 145 | |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 146 | handleConnection(error, socket, onFaceCreated, onConnectFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | TcpChannel::handleFailedConnect(const boost::system::error_code& error, |
| 151 | const shared_ptr<ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 152 | const shared_ptr<monotonic_deadline_timer>& timer, |
| 153 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 154 | { |
| 155 | if (error) { // e.g., cancelled |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | onConnectFailed("Connect to remote endpoint timed out: " + |
| 160 | error.category().message(error.value())); |
| 161 | socket->close(); // abort the connection |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | TcpChannel::handleEndpointResoution(const boost::system::error_code& error, |
| 166 | ip::tcp::resolver::iterator remoteEndpoint, |
| 167 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 168 | const shared_ptr<boost::asio::monotonic_deadline_timer>& timer, |
| 169 | const FaceCreatedCallback& onFaceCreated, |
| 170 | const ConnectFailedCallback& onConnectFailed) |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 171 | { |
| 172 | if (error || |
| 173 | remoteEndpoint == ip::tcp::resolver::iterator()) |
| 174 | { |
| 175 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 176 | return; |
| 177 | |
| 178 | socket->close(); |
| 179 | timer->cancel(); |
| 180 | onConnectFailed("Remote endpoint hostname or port cannot be resolved: " + |
| 181 | error.category().message(error.value())); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // got endpoint, now trying to connect (only try the first resolution option) |
| 186 | socket->async_connect(*remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 187 | bind(&TcpChannel::handleSuccessfulConnect, this, _1, |
| 188 | socket, timer, |
| 189 | onFaceCreated, onConnectFailed)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 193 | } // namespace nfd |