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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include <list> |
| 28 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 29 | namespace ndn { |
| 30 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 31 | template<class BaseTransport, class Protocol> |
| 32 | class StreamTransportImpl |
| 33 | { |
| 34 | public: |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 35 | typedef StreamTransportImpl<BaseTransport,Protocol> Impl; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 37 | typedef std::list<Block> BlockSequence; |
| 38 | typedef std::list<BlockSequence> TransmissionQueue; |
| 39 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 40 | StreamTransportImpl(BaseTransport& transport, boost::asio::io_service& ioService) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 41 | : m_transport(transport) |
| 42 | , m_socket(ioService) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 43 | , m_inputBufferSize(0) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 44 | , m_connectionInProgress(false) |
| 45 | , m_connectTimer(ioService) |
| 46 | { |
| 47 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 49 | void |
| 50 | connectHandler(const boost::system::error_code& error) |
| 51 | { |
| 52 | m_connectionInProgress = false; |
| 53 | m_connectTimer.cancel(); |
| 54 | |
| 55 | if (!error) |
| 56 | { |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 57 | resume(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 58 | m_transport.m_isConnected = true; |
| 59 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 60 | if (!m_transmissionQueue.empty()) { |
| 61 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 62 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 63 | m_transmissionQueue.begin())); |
| 64 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 65 | } |
| 66 | else |
| 67 | { |
| 68 | // may need to throw exception |
| 69 | m_transport.m_isConnected = false; |
| 70 | m_transport.close(); |
| 71 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | connectTimeoutHandler(const boost::system::error_code& error) |
| 77 | { |
| 78 | if (error) // e.g., cancelled timer |
| 79 | return; |
| 80 | |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 81 | m_transport.close(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 82 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 83 | } |
| 84 | |
| 85 | void |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 86 | connect(const typename Protocol::endpoint& endpoint) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 87 | { |
| 88 | if (!m_connectionInProgress) { |
| 89 | m_connectionInProgress = true; |
| 90 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 91 | // Wait at most 4 seconds to connect |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 92 | /// @todo Decide whether this number should be configurable |
| 93 | m_connectTimer.expires_from_now(boost::posix_time::seconds(4)); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 94 | m_connectTimer.async_wait(bind(&Impl::connectTimeoutHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 95 | |
| 96 | m_socket.open(); |
| 97 | m_socket.async_connect(endpoint, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 98 | bind(&Impl::connectHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 101 | |
| 102 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 103 | close() |
| 104 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 105 | m_connectionInProgress = false; |
| 106 | |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 107 | boost::system::error_code error; // to silently ignore all errors |
| 108 | m_connectTimer.cancel(error); |
| 109 | m_socket.cancel(error); |
| 110 | m_socket.close(error); |
| 111 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 112 | m_transport.m_isConnected = false; |
Alexander Afanasyev | 6e0c5a5 | 2014-03-18 16:18:58 -0700 | [diff] [blame] | 113 | m_transport.m_isExpectingData = false; |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 114 | m_transmissionQueue.clear(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 118 | pause() |
| 119 | { |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 120 | if (m_connectionInProgress) |
| 121 | return; |
| 122 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 123 | if (m_transport.m_isExpectingData) |
| 124 | { |
| 125 | m_transport.m_isExpectingData = false; |
| 126 | m_socket.cancel(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | resume() |
| 132 | { |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 133 | if (m_connectionInProgress) |
| 134 | return; |
| 135 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 136 | if (!m_transport.m_isExpectingData) |
| 137 | { |
| 138 | m_transport.m_isExpectingData = true; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 139 | m_inputBufferSize = 0; |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 140 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
| 141 | bind(&Impl::handleAsyncReceive, this, _1, _2)); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 146 | send(const Block& wire) |
| 147 | { |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 148 | BlockSequence sequence; |
| 149 | sequence.push_back(wire); |
| 150 | m_transmissionQueue.push_back(sequence); |
| 151 | |
| 152 | if (m_transport.m_isConnected && m_transmissionQueue.size() == 1) { |
| 153 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 154 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 155 | m_transmissionQueue.begin())); |
| 156 | } |
| 157 | |
| 158 | // if not connected or there is transmission in progress (m_transmissionQueue.size() > 1), |
| 159 | // next write will be scheduled either in connectHandler or in asyncWriteHandler |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void |
| 163 | send(const Block& header, const Block& payload) |
| 164 | { |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 165 | BlockSequence sequence; |
| 166 | sequence.push_back(header); |
| 167 | sequence.push_back(payload); |
| 168 | m_transmissionQueue.push_back(sequence); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 169 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 170 | if (m_transport.m_isConnected && m_transmissionQueue.size() == 1) { |
| 171 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 172 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 173 | m_transmissionQueue.begin())); |
| 174 | } |
| 175 | |
| 176 | // if not connected or there is transmission in progress (m_transmissionQueue.size() > 1), |
| 177 | // next write will be scheduled either in connectHandler or in asyncWriteHandler |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | handleAsyncWrite(const boost::system::error_code& error, |
| 182 | TransmissionQueue::iterator queueItem) |
| 183 | { |
| 184 | if (error) |
| 185 | { |
| 186 | if (error == boost::system::errc::operation_canceled) { |
| 187 | // async receive has been explicitly cancelled (e.g., socket close) |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | m_transport.close(); |
| 192 | throw Transport::Error(error, "error while sending data to socket"); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 193 | } |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 194 | |
| 195 | m_transmissionQueue.erase(queueItem); |
| 196 | |
| 197 | if (!m_transmissionQueue.empty()) { |
| 198 | boost::asio::async_write(m_socket, *m_transmissionQueue.begin(), |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 199 | bind(&Impl::handleAsyncWrite, this, _1, |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 200 | m_transmissionQueue.begin())); |
| 201 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 202 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 204 | bool |
| 205 | processAll(uint8_t* buffer, size_t& offset, size_t nBytesAvailable) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 206 | { |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 207 | while (offset < nBytesAvailable) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 208 | { |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 209 | Block element; |
| 210 | bool ok = Block::fromBuffer(buffer + offset, nBytesAvailable - offset, element); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 211 | if (!ok) |
| 212 | return false; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 214 | m_transport.receive(element); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 215 | offset += element.size(); |
| 216 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 217 | return true; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 218 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 219 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 220 | void |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 221 | handleAsyncReceive(const boost::system::error_code& error, std::size_t nBytesRecvd) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 222 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 223 | if (error) |
| 224 | { |
| 225 | if (error == boost::system::errc::operation_canceled) { |
| 226 | // async receive has been explicitly cancelled (e.g., socket close) |
| 227 | return; |
| 228 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 230 | m_transport.close(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 231 | throw Transport::Error(error, "error while receiving data from socket"); |
| 232 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 233 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 234 | m_inputBufferSize += nBytesRecvd; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 235 | // do magic |
| 236 | |
| 237 | std::size_t offset = 0; |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 238 | bool hasProcessedSome = processAll(m_inputBuffer, offset, m_inputBufferSize); |
| 239 | if (!hasProcessedSome && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 240 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 241 | m_transport.close(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 242 | throw Transport::Error(boost::system::error_code(), |
| 243 | "input buffer full, but a valid TLV cannot be decoded"); |
| 244 | } |
| 245 | |
| 246 | if (offset > 0) |
| 247 | { |
| 248 | if (offset != m_inputBufferSize) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 249 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 250 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 251 | m_inputBuffer); |
| 252 | m_inputBufferSize -= offset; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 253 | } |
| 254 | else |
| 255 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 256 | m_inputBufferSize = 0; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 260 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 261 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 262 | bind(&Impl::handleAsyncReceive, this, _1, _2)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 265 | protected: |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 266 | BaseTransport& m_transport; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 267 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 268 | typename Protocol::socket m_socket; |
| 269 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 270 | size_t m_inputBufferSize; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 271 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 272 | TransmissionQueue m_transmissionQueue; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 273 | bool m_connectionInProgress; |
| 274 | |
| 275 | boost::asio::deadline_timer m_connectTimer; |
| 276 | }; |
| 277 | |
| 278 | |
| 279 | template<class BaseTransport, class Protocol> |
| 280 | class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol> |
| 281 | { |
| 282 | public: |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 283 | typedef StreamTransportWithResolverImpl<BaseTransport,Protocol> Impl; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 284 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 285 | StreamTransportWithResolverImpl(BaseTransport& transport, boost::asio::io_service& ioService) |
| 286 | : StreamTransportImpl<BaseTransport, Protocol>(transport, ioService) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 287 | { |
| 288 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 289 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 290 | void |
| 291 | resolveHandler(const boost::system::error_code& error, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 292 | typename Protocol::resolver::iterator endpoint, |
| 293 | const shared_ptr<typename Protocol::resolver>&) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 294 | { |
| 295 | if (error) |
| 296 | { |
| 297 | if (error == boost::system::errc::operation_canceled) |
| 298 | return; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 299 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 300 | throw Transport::Error(error, "Error during resolution of host or port"); |
| 301 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 302 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 303 | typename Protocol::resolver::iterator end; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 304 | if (endpoint == end) |
| 305 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 306 | this->m_transport.close(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 307 | throw Transport::Error(error, "Unable to resolve because host or port"); |
| 308 | } |
| 309 | |
| 310 | this->m_socket.async_connect(*endpoint, |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 311 | bind(&Impl::connectHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 312 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 313 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 314 | void |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 315 | connect(const typename Protocol::resolver::query& query) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 316 | { |
| 317 | if (!this->m_connectionInProgress) { |
| 318 | this->m_connectionInProgress = true; |
| 319 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 320 | // Wait at most 4 seconds to connect |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 321 | /// @todo Decide whether this number should be configurable |
| 322 | this->m_connectTimer.expires_from_now(boost::posix_time::seconds(4)); |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 323 | this->m_connectTimer.async_wait(bind(&Impl::connectTimeoutHandler, this, _1)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 324 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 325 | // typename boost::asio::ip::basic_resolver< Protocol > resolver; |
| 326 | shared_ptr<typename Protocol::resolver> resolver = |
| 327 | make_shared<typename Protocol::resolver>(ref(this->m_socket.get_io_service())); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 328 | |
Alexander Afanasyev | 49bb1fb | 2014-07-21 12:54:01 -0700 | [diff] [blame] | 329 | resolver->async_resolve(query, bind(&Impl::resolveHandler, this, _1, _2, resolver)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | }; |
| 333 | |
| 334 | |
| 335 | } // namespace ndn |
| 336 | |
| 337 | #endif // NDN_TRANSPORT_STREAM_TRANSPORT_HPP |