Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 2 | /** |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_TRANSPORT_STREAM_TRANSPORT_HPP |
| 23 | #define NDN_TRANSPORT_STREAM_TRANSPORT_HPP |
| 24 | |
Junxiao Shi | 71355d5 | 2014-12-11 09:20:44 -0700 | [diff] [blame] | 25 | #include "transport.hpp" |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame^] | 27 | #include <boost/asio.hpp> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 28 | #include <list> |
| 29 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 32 | template<class BaseTransport, class Protocol> |
| 33 | class StreamTransportImpl |
| 34 | { |
| 35 | public: |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 36 | typedef StreamTransportImpl<BaseTransport,Protocol> Impl; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 38 | typedef std::list<Block> BlockSequence; |
| 39 | typedef std::list<BlockSequence> TransmissionQueue; |
| 40 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 41 | StreamTransportImpl(BaseTransport& transport, boost::asio::io_service& ioService) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 42 | : m_transport(transport) |
| 43 | , m_socket(ioService) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 44 | , m_inputBufferSize(0) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 45 | , m_connectionInProgress(false) |
| 46 | , m_connectTimer(ioService) |
| 47 | { |
| 48 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 50 | void |
| 51 | connectHandler(const boost::system::error_code& error) |
| 52 | { |
| 53 | m_connectionInProgress = false; |
| 54 | m_connectTimer.cancel(); |
| 55 | |
| 56 | if (!error) |
| 57 | { |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 58 | resume(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 59 | m_transport.m_isConnected = true; |
| 60 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 61 | if (!m_transmissionQueue.empty()) { |
| 62 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 63 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 64 | m_transmissionQueue.begin())); |
| 65 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
| 69 | // may need to throw exception |
| 70 | m_transport.m_isConnected = false; |
| 71 | m_transport.close(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 72 | BOOST_THROW_EXCEPTION(Transport::Error(error, "error while connecting to the forwarder")); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | connectTimeoutHandler(const boost::system::error_code& error) |
| 78 | { |
| 79 | if (error) // e.g., cancelled timer |
| 80 | return; |
| 81 | |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 82 | m_transport.close(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 83 | BOOST_THROW_EXCEPTION(Transport::Error(error, "error while connecting to the forwarder")); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 87 | connect(const typename Protocol::endpoint& endpoint) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 88 | { |
| 89 | if (!m_connectionInProgress) { |
| 90 | m_connectionInProgress = true; |
| 91 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 92 | // Wait at most 4 seconds to connect |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 93 | /// @todo Decide whether this number should be configurable |
| 94 | m_connectTimer.expires_from_now(boost::posix_time::seconds(4)); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 95 | m_connectTimer.async_wait(bind(&Impl::connectTimeoutHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 96 | |
| 97 | m_socket.open(); |
| 98 | m_socket.async_connect(endpoint, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 99 | bind(&Impl::connectHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 102 | |
| 103 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 104 | close() |
| 105 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 106 | m_connectionInProgress = false; |
| 107 | |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 108 | boost::system::error_code error; // to silently ignore all errors |
| 109 | m_connectTimer.cancel(error); |
| 110 | m_socket.cancel(error); |
| 111 | m_socket.close(error); |
| 112 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 113 | m_transport.m_isConnected = false; |
Alexander Afanasyev | 6e0c5a5 | 2014-03-18 16:18:58 -0700 | [diff] [blame] | 114 | m_transport.m_isExpectingData = false; |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 115 | m_transmissionQueue.clear(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 119 | pause() |
| 120 | { |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 121 | if (m_connectionInProgress) |
| 122 | return; |
| 123 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 124 | if (m_transport.m_isExpectingData) |
| 125 | { |
| 126 | m_transport.m_isExpectingData = false; |
| 127 | m_socket.cancel(); |
| 128 | } |
| 129 | } |
| 130 | |
Alexander Afanasyev | 9d158f0 | 2015-02-17 21:30:19 -0800 | [diff] [blame] | 131 | /** |
| 132 | * @warning Must not be called directly or indirectly from within handleAsyncReceive invocation |
| 133 | */ |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 134 | void |
| 135 | resume() |
| 136 | { |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 137 | if (m_connectionInProgress) |
| 138 | return; |
| 139 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 140 | if (!m_transport.m_isExpectingData) |
| 141 | { |
| 142 | m_transport.m_isExpectingData = true; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 143 | m_inputBufferSize = 0; |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 144 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
| 145 | bind(&Impl::handleAsyncReceive, this, _1, _2)); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 150 | send(const Block& wire) |
| 151 | { |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 152 | BlockSequence sequence; |
| 153 | sequence.push_back(wire); |
| 154 | m_transmissionQueue.push_back(sequence); |
| 155 | |
| 156 | if (m_transport.m_isConnected && m_transmissionQueue.size() == 1) { |
| 157 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 158 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 159 | m_transmissionQueue.begin())); |
| 160 | } |
| 161 | |
| 162 | // if not connected or there is transmission in progress (m_transmissionQueue.size() > 1), |
| 163 | // next write will be scheduled either in connectHandler or in asyncWriteHandler |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void |
| 167 | send(const Block& header, const Block& payload) |
| 168 | { |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 169 | BlockSequence sequence; |
| 170 | sequence.push_back(header); |
| 171 | sequence.push_back(payload); |
| 172 | m_transmissionQueue.push_back(sequence); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 174 | if (m_transport.m_isConnected && m_transmissionQueue.size() == 1) { |
| 175 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 176 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 177 | m_transmissionQueue.begin())); |
| 178 | } |
| 179 | |
| 180 | // if not connected or there is transmission in progress (m_transmissionQueue.size() > 1), |
| 181 | // next write will be scheduled either in connectHandler or in asyncWriteHandler |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | handleAsyncWrite(const boost::system::error_code& error, |
| 186 | TransmissionQueue::iterator queueItem) |
| 187 | { |
| 188 | if (error) |
| 189 | { |
| 190 | if (error == boost::system::errc::operation_canceled) { |
| 191 | // async receive has been explicitly cancelled (e.g., socket close) |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | m_transport.close(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 196 | BOOST_THROW_EXCEPTION(Transport::Error(error, "error while sending data to socket")); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 197 | } |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | fba1ac6 | 2015-08-26 15:19:13 -0700 | [diff] [blame] | 199 | if (!m_transport.m_isConnected) { |
| 200 | return; // queue has been already cleared |
| 201 | } |
| 202 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 203 | m_transmissionQueue.erase(queueItem); |
| 204 | |
| 205 | if (!m_transmissionQueue.empty()) { |
| 206 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 207 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 208 | m_transmissionQueue.begin())); |
| 209 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 210 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 211 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 212 | bool |
| 213 | processAll(uint8_t* buffer, size_t& offset, size_t nBytesAvailable) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 214 | { |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 215 | while (offset < nBytesAvailable) { |
| 216 | bool isOk = false; |
| 217 | Block element; |
| 218 | std::tie(isOk, element) = Block::fromBuffer(buffer + offset, nBytesAvailable - offset); |
| 219 | if (!isOk) |
| 220 | return false; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 221 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 222 | m_transport.receive(element); |
| 223 | offset += element.size(); |
| 224 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 225 | return true; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 226 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 227 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 228 | void |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 229 | handleAsyncReceive(const boost::system::error_code& error, std::size_t nBytesRecvd) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 230 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 231 | if (error) |
| 232 | { |
| 233 | if (error == boost::system::errc::operation_canceled) { |
| 234 | // async receive has been explicitly cancelled (e.g., socket close) |
| 235 | return; |
| 236 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 237 | |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 238 | m_transport.close(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 239 | BOOST_THROW_EXCEPTION(Transport::Error(error, "error while receiving data from socket")); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 240 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 241 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 242 | m_inputBufferSize += nBytesRecvd; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 243 | // do magic |
| 244 | |
| 245 | std::size_t offset = 0; |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 246 | bool hasProcessedSome = processAll(m_inputBuffer, offset, m_inputBufferSize); |
| 247 | if (!hasProcessedSome && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 248 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 249 | m_transport.close(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 250 | BOOST_THROW_EXCEPTION(Transport::Error(boost::system::error_code(), |
| 251 | "input buffer full, but a valid TLV cannot be " |
| 252 | "decoded")); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | if (offset > 0) |
| 256 | { |
| 257 | if (offset != m_inputBufferSize) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 258 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 259 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 260 | m_inputBuffer); |
| 261 | m_inputBufferSize -= offset; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 262 | } |
| 263 | else |
| 264 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 265 | m_inputBufferSize = 0; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 269 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 270 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 271 | bind(&Impl::handleAsyncReceive, this, _1, _2)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 274 | protected: |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 275 | BaseTransport& m_transport; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 276 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 277 | typename Protocol::socket m_socket; |
| 278 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 279 | size_t m_inputBufferSize; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 280 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 281 | TransmissionQueue m_transmissionQueue; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 282 | bool m_connectionInProgress; |
| 283 | |
| 284 | boost::asio::deadline_timer m_connectTimer; |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | template<class BaseTransport, class Protocol> |
| 289 | class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol> |
| 290 | { |
| 291 | public: |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 292 | typedef StreamTransportWithResolverImpl<BaseTransport,Protocol> Impl; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 293 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 294 | StreamTransportWithResolverImpl(BaseTransport& transport, boost::asio::io_service& ioService) |
| 295 | : StreamTransportImpl<BaseTransport, Protocol>(transport, ioService) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 296 | { |
| 297 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 298 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 299 | void |
| 300 | resolveHandler(const boost::system::error_code& error, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 301 | typename Protocol::resolver::iterator endpoint, |
| 302 | const shared_ptr<typename Protocol::resolver>&) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 303 | { |
| 304 | if (error) |
| 305 | { |
| 306 | if (error == boost::system::errc::operation_canceled) |
| 307 | return; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 308 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 309 | BOOST_THROW_EXCEPTION(Transport::Error(error, "Error during resolution of host or port")); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 310 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 311 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 312 | typename Protocol::resolver::iterator end; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 313 | if (endpoint == end) |
| 314 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 315 | this->m_transport.close(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 316 | BOOST_THROW_EXCEPTION(Transport::Error(error, "Unable to resolve because host or port")); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | this->m_socket.async_connect(*endpoint, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 320 | bind(&Impl::connectHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 321 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 322 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 323 | void |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 324 | connect(const typename Protocol::resolver::query& query) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 325 | { |
| 326 | if (!this->m_connectionInProgress) { |
| 327 | this->m_connectionInProgress = true; |
| 328 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 329 | // Wait at most 4 seconds to connect |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 330 | /// @todo Decide whether this number should be configurable |
| 331 | this->m_connectTimer.expires_from_now(boost::posix_time::seconds(4)); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 332 | this->m_connectTimer.async_wait(bind(&Impl::connectTimeoutHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 333 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 334 | // typename boost::asio::ip::basic_resolver< Protocol > resolver; |
| 335 | shared_ptr<typename Protocol::resolver> resolver = |
| 336 | make_shared<typename Protocol::resolver>(ref(this->m_socket.get_io_service())); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 337 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 338 | resolver->async_resolve(query, bind(&Impl::resolveHandler, this, _1, _2, resolver)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | }; |
| 342 | |
| 343 | |
| 344 | } // namespace ndn |
| 345 | |
| 346 | #endif // NDN_TRANSPORT_STREAM_TRANSPORT_HPP |