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 | |
| 25 | #include "../common.hpp" |
| 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 | |
| 31 | const size_t MAX_LENGTH = 9000; |
| 32 | |
| 33 | template<class BaseTransport, class Protocol> |
| 34 | class StreamTransportImpl |
| 35 | { |
| 36 | public: |
| 37 | typedef BaseTransport base_transport; |
| 38 | typedef Protocol protocol; |
| 39 | typedef StreamTransportImpl<BaseTransport,Protocol> impl; |
| 40 | |
| 41 | StreamTransportImpl(base_transport& transport, boost::asio::io_service& ioService) |
| 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 | |
| 61 | for (std::list<Block>::iterator i = m_sendQueue.begin(); i != m_sendQueue.end(); ++i) |
| 62 | m_socket.async_send(boost::asio::buffer(i->wire(), i->size()), |
| 63 | bind(&impl::handle_async_send, this, _1, *i)); |
| 64 | |
| 65 | for (std::list< std::pair<Block,Block> >::iterator i = m_sendPairQueue.begin(); |
| 66 | i != m_sendPairQueue.end(); ++i) |
| 67 | { |
| 68 | std::vector<boost::asio::const_buffer> buffer; |
| 69 | buffer.reserve(2); |
| 70 | buffer.push_back(boost::asio::buffer(i->first.wire(), i->first.size())); |
| 71 | buffer.push_back(boost::asio::buffer(i->second.wire(), i->second.size())); |
| 72 | m_socket.async_send(buffer, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 73 | bind(&impl::handle_async_send2, this, _1, i->first, i->second)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 74 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 76 | m_sendQueue.clear(); |
| 77 | m_sendPairQueue.clear(); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | // may need to throw exception |
| 82 | m_transport.m_isConnected = false; |
| 83 | m_transport.close(); |
| 84 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | connectTimeoutHandler(const boost::system::error_code& error) |
| 90 | { |
| 91 | if (error) // e.g., cancelled timer |
| 92 | return; |
| 93 | |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame^] | 94 | m_transport.close(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 95 | throw Transport::Error(error, "error while connecting to the forwarder"); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | connect(const typename protocol::endpoint& endpoint) |
| 100 | { |
| 101 | if (!m_connectionInProgress) { |
| 102 | m_connectionInProgress = true; |
| 103 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 104 | // Wait at most 4 time::seconds to connect |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 105 | /// @todo Decide whether this number should be configurable |
| 106 | m_connectTimer.expires_from_now(boost::posix_time::seconds(4)); |
| 107 | m_connectTimer.async_wait(bind(&impl::connectTimeoutHandler, this, _1)); |
| 108 | |
| 109 | m_socket.open(); |
| 110 | m_socket.async_connect(endpoint, |
| 111 | bind(&impl::connectHandler, this, _1)); |
| 112 | } |
| 113 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 114 | |
| 115 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 116 | close() |
| 117 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame^] | 118 | m_connectionInProgress = false; |
| 119 | |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 120 | boost::system::error_code error; // to silently ignore all errors |
| 121 | m_connectTimer.cancel(error); |
| 122 | m_socket.cancel(error); |
| 123 | m_socket.close(error); |
| 124 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 125 | m_transport.m_isConnected = false; |
Alexander Afanasyev | 6e0c5a5 | 2014-03-18 16:18:58 -0700 | [diff] [blame] | 126 | m_transport.m_isExpectingData = false; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 127 | m_sendQueue.clear(); |
| 128 | m_sendPairQueue.clear(); |
| 129 | } |
| 130 | |
| 131 | void |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 132 | pause() |
| 133 | { |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 134 | if (m_connectionInProgress) |
| 135 | return; |
| 136 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 137 | if (m_transport.m_isExpectingData) |
| 138 | { |
| 139 | m_transport.m_isExpectingData = false; |
| 140 | m_socket.cancel(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | resume() |
| 146 | { |
Alexander Afanasyev | 6507fb1 | 2014-04-28 23:18:56 -0700 | [diff] [blame] | 147 | if (m_connectionInProgress) |
| 148 | return; |
| 149 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 150 | if (!m_transport.m_isExpectingData) |
| 151 | { |
| 152 | m_transport.m_isExpectingData = true; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 153 | m_inputBufferSize = 0; |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 154 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer, MAX_LENGTH), 0, |
| 155 | bind(&impl::handle_async_receive, this, _1, _2)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 160 | send(const Block& wire) |
| 161 | { |
| 162 | if (!m_transport.m_isConnected) |
| 163 | m_sendQueue.push_back(wire); |
| 164 | else |
| 165 | m_socket.async_send(boost::asio::buffer(wire.wire(), wire.size()), |
| 166 | bind(&impl::handle_async_send, this, _1, wire)); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | send(const Block& header, const Block& payload) |
| 171 | { |
| 172 | if (!m_transport.m_isConnected) |
| 173 | { |
| 174 | m_sendPairQueue.push_back(std::make_pair(header, payload)); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | std::vector<boost::asio::const_buffer> buffers; |
| 179 | buffers.reserve(2); |
| 180 | buffers.push_back(boost::asio::buffer(header.wire(), header.size())); |
| 181 | buffers.push_back(boost::asio::buffer(payload.wire(), payload.size())); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 183 | m_socket.async_send(buffers, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 184 | bind(&impl::handle_async_send2, this, _1, header, payload)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 187 | |
| 188 | inline bool |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 189 | processAll(uint8_t* buffer, size_t& offset, size_t availableSize) |
| 190 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 191 | Block element; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 192 | while(offset < availableSize) |
| 193 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 194 | bool ok = Block::fromBuffer(buffer + offset, availableSize - offset, element); |
| 195 | if (!ok) |
| 196 | return false; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 197 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 198 | m_transport.receive(element); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 199 | offset += element.size(); |
| 200 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 201 | return true; |
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 | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 204 | void |
| 205 | handle_async_receive(const boost::system::error_code& error, std::size_t bytes_recvd) |
| 206 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 207 | if (error) |
| 208 | { |
| 209 | if (error == boost::system::errc::operation_canceled) { |
| 210 | // async receive has been explicitly cancelled (e.g., socket close) |
| 211 | return; |
| 212 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 213 | |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame^] | 214 | m_transport.close(); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 215 | throw Transport::Error(error, "error while receiving data from socket"); |
| 216 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 217 | |
| 218 | m_inputBufferSize += bytes_recvd; |
| 219 | // do magic |
| 220 | |
| 221 | std::size_t offset = 0; |
| 222 | bool ok = processAll(m_inputBuffer, offset, m_inputBufferSize); |
| 223 | if (!ok && m_inputBufferSize == MAX_LENGTH && offset == 0) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 224 | { |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame^] | 225 | m_transport.close(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 226 | throw Transport::Error(boost::system::error_code(), |
| 227 | "input buffer full, but a valid TLV cannot be decoded"); |
| 228 | } |
| 229 | |
| 230 | if (offset > 0) |
| 231 | { |
| 232 | if (offset != m_inputBufferSize) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 233 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 234 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 235 | m_inputBuffer); |
| 236 | m_inputBufferSize -= offset; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 237 | } |
| 238 | else |
| 239 | { |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 240 | m_inputBufferSize = 0; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 244 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 245 | MAX_LENGTH - m_inputBufferSize), 0, |
| 246 | bind(&impl::handle_async_receive, this, _1, _2)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void |
| 250 | handle_async_send(const boost::system::error_code& error, const Block& wire) |
| 251 | { |
| 252 | // pass (needed to keep data block alive during the send) |
| 253 | } |
| 254 | |
| 255 | void |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 256 | handle_async_send2(const boost::system::error_code& error, |
| 257 | const Block& header, const Block& payload) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 258 | { |
| 259 | // pass (needed to keep data blocks alive during the send) |
| 260 | } |
| 261 | |
| 262 | protected: |
| 263 | base_transport& m_transport; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 264 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 265 | typename protocol::socket m_socket; |
| 266 | uint8_t m_inputBuffer[MAX_LENGTH]; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 267 | size_t m_inputBufferSize; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 268 | |
| 269 | std::list< Block > m_sendQueue; |
| 270 | std::list< std::pair<Block, Block> > m_sendPairQueue; |
| 271 | bool m_connectionInProgress; |
| 272 | |
| 273 | boost::asio::deadline_timer m_connectTimer; |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | template<class BaseTransport, class Protocol> |
| 278 | class StreamTransportWithResolverImpl : public StreamTransportImpl<BaseTransport, Protocol> |
| 279 | { |
| 280 | public: |
| 281 | typedef BaseTransport base_transport; |
| 282 | typedef Protocol protocol; |
| 283 | typedef StreamTransportWithResolverImpl<BaseTransport,Protocol> impl; |
| 284 | |
| 285 | StreamTransportWithResolverImpl(base_transport& transport, boost::asio::io_service& ioService) |
| 286 | : StreamTransportImpl<base_transport, protocol>(transport, ioService) |
| 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, |
| 292 | typename protocol::resolver::iterator endpoint, |
| 293 | const shared_ptr<typename protocol::resolver>&) |
| 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 | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 303 | typename protocol::resolver::iterator end; |
| 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, |
| 311 | bind(&impl::connectHandler, this, _1)); |
| 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 |
| 315 | connect(const typename protocol::resolver::query& query) |
| 316 | { |
| 317 | if (!this->m_connectionInProgress) { |
| 318 | this->m_connectionInProgress = true; |
| 319 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 320 | // Wait at most 4 time::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)); |
| 323 | this->m_connectTimer.async_wait(bind(&impl::connectTimeoutHandler, this, _1)); |
| 324 | |
| 325 | // typename boost::asio::ip::basic_resolver< protocol > resolver; |
| 326 | shared_ptr<typename protocol::resolver> resolver = |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 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 | |
| 329 | resolver->async_resolve(query, bind(&impl::resolveHandler, this, _1, _2, resolver)); |
| 330 | } |
| 331 | } |
| 332 | }; |
| 333 | |
| 334 | |
| 335 | } // namespace ndn |
| 336 | |
| 337 | #endif // NDN_TRANSPORT_STREAM_TRANSPORT_HPP |