Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame^] | 22 | #ifndef NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_IMPL_HPP |
| 23 | #define NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_IMPL_HPP |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/transport/transport.hpp" |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 26 | |
Davide Pesavento | 0c31537 | 2019-04-25 14:50:42 -0400 | [diff] [blame] | 27 | #include <boost/asio/steady_timer.hpp> |
Davide Pesavento | 4d0d096 | 2017-12-19 22:23:14 -0500 | [diff] [blame] | 28 | #include <boost/asio/write.hpp> |
| 29 | |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 30 | #include <list> |
| 31 | |
| 32 | namespace ndn { |
Junxiao Shi | 70d76eb | 2018-12-18 13:15:43 -0700 | [diff] [blame] | 33 | namespace detail { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 35 | /** \brief Implementation detail of a Boost.Asio-based stream-oriented transport. |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 36 | * \tparam BaseTransport a subclass of Transport |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 37 | * \tparam Protocol a Boost.Asio stream-oriented protocol, e.g. boost::asio::ip::tcp |
| 38 | * or boost::asio::local::stream_protocol |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 39 | */ |
| 40 | template<typename BaseTransport, typename Protocol> |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 41 | class StreamTransportImpl : public std::enable_shared_from_this<StreamTransportImpl<BaseTransport, Protocol>> |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 42 | { |
| 43 | public: |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 44 | using Impl = StreamTransportImpl<BaseTransport, Protocol>; |
| 45 | using BlockSequence = std::list<Block>; |
| 46 | using TransmissionQueue = std::list<BlockSequence>; |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 47 | |
| 48 | StreamTransportImpl(BaseTransport& transport, boost::asio::io_service& ioService) |
| 49 | : m_transport(transport) |
| 50 | , m_socket(ioService) |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 51 | , m_connectTimer(ioService) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | connect(const typename Protocol::endpoint& endpoint) |
| 57 | { |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 58 | if (m_isConnecting) { |
| 59 | return; |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 60 | } |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 61 | m_isConnecting = true; |
| 62 | |
| 63 | // Wait at most 4 seconds to connect |
| 64 | /// @todo Decide whether this number should be configurable |
| 65 | m_connectTimer.expires_from_now(std::chrono::seconds(4)); |
| 66 | m_connectTimer.async_wait([self = this->shared_from_this()] (const auto& error) { |
| 67 | self->connectTimeoutHandler(error); |
| 68 | }); |
| 69 | |
| 70 | m_socket.open(); |
| 71 | m_socket.async_connect(endpoint, [self = this->shared_from_this()] (const auto& error) { |
| 72 | self->connectHandler(error); |
| 73 | }); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | close() |
| 78 | { |
| 79 | m_isConnecting = false; |
| 80 | |
| 81 | boost::system::error_code error; // to silently ignore all errors |
| 82 | m_connectTimer.cancel(error); |
| 83 | m_socket.cancel(error); |
| 84 | m_socket.close(error); |
| 85 | |
| 86 | m_transport.m_isConnected = false; |
| 87 | m_transport.m_isReceiving = false; |
| 88 | m_transmissionQueue.clear(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | pause() |
| 93 | { |
| 94 | if (m_isConnecting) |
| 95 | return; |
| 96 | |
| 97 | if (m_transport.m_isReceiving) { |
| 98 | m_transport.m_isReceiving = false; |
| 99 | m_socket.cancel(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | resume() |
| 105 | { |
| 106 | if (m_isConnecting) |
| 107 | return; |
| 108 | |
| 109 | if (!m_transport.m_isReceiving) { |
| 110 | m_transport.m_isReceiving = true; |
| 111 | m_inputBufferSize = 0; |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 112 | asyncReceive(); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | send(const Block& wire) |
| 118 | { |
| 119 | BlockSequence sequence; |
| 120 | sequence.push_back(wire); |
| 121 | send(std::move(sequence)); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | send(const Block& header, const Block& payload) |
| 126 | { |
| 127 | BlockSequence sequence; |
| 128 | sequence.push_back(header); |
| 129 | sequence.push_back(payload); |
| 130 | send(std::move(sequence)); |
| 131 | } |
| 132 | |
| 133 | protected: |
| 134 | void |
| 135 | connectHandler(const boost::system::error_code& error) |
| 136 | { |
| 137 | m_isConnecting = false; |
| 138 | m_connectTimer.cancel(); |
| 139 | |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 140 | if (error) { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 141 | m_transport.m_isConnected = false; |
| 142 | m_transport.close(); |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 143 | NDN_THROW(Transport::Error(error, "error while connecting to the forwarder")); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 144 | } |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 145 | |
| 146 | m_transport.m_isConnected = true; |
| 147 | |
| 148 | if (!m_transmissionQueue.empty()) { |
| 149 | resume(); |
| 150 | asyncWrite(); |
| 151 | } |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void |
| 155 | connectTimeoutHandler(const boost::system::error_code& error) |
| 156 | { |
| 157 | if (error) // e.g., cancelled timer |
| 158 | return; |
| 159 | |
| 160 | m_transport.close(); |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 161 | NDN_THROW(Transport::Error(error, "error while connecting to the forwarder")); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void |
| 165 | send(BlockSequence&& sequence) |
| 166 | { |
| 167 | m_transmissionQueue.emplace_back(sequence); |
| 168 | |
| 169 | if (m_transport.m_isConnected && m_transmissionQueue.size() == 1) { |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 170 | asyncWrite(); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // if not connected or there is transmission in progress (m_transmissionQueue.size() > 1), |
| 174 | // next write will be scheduled either in connectHandler or in asyncWriteHandler |
| 175 | } |
| 176 | |
| 177 | void |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 178 | asyncWrite() |
| 179 | { |
| 180 | BOOST_ASSERT(!m_transmissionQueue.empty()); |
| 181 | boost::asio::async_write(m_socket, m_transmissionQueue.front(), |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 182 | bind(&Impl::handleAsyncWrite, this->shared_from_this(), _1, |
| 183 | m_transmissionQueue.begin())); |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 187 | handleAsyncWrite(const boost::system::error_code& error, TransmissionQueue::iterator queueItem) |
| 188 | { |
| 189 | if (error) { |
| 190 | if (error == boost::system::errc::operation_canceled) { |
| 191 | // async receive has been explicitly cancelled (e.g., socket close) |
| 192 | return; |
| 193 | } |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 194 | m_transport.close(); |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 195 | NDN_THROW(Transport::Error(error, "error while writing data to socket")); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (!m_transport.m_isConnected) { |
| 199 | return; // queue has been already cleared |
| 200 | } |
| 201 | |
| 202 | m_transmissionQueue.erase(queueItem); |
| 203 | |
| 204 | if (!m_transmissionQueue.empty()) { |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 205 | asyncWrite(); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | void |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 210 | asyncReceive() |
| 211 | { |
| 212 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 213 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 214 | bind(&Impl::handleAsyncReceive, this->shared_from_this(), _1, _2)); |
| 215 | } |
| 216 | |
| 217 | void |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 218 | handleAsyncReceive(const boost::system::error_code& error, std::size_t nBytesRecvd) |
| 219 | { |
| 220 | if (error) { |
| 221 | if (error == boost::system::errc::operation_canceled) { |
| 222 | // async receive has been explicitly cancelled (e.g., socket close) |
| 223 | return; |
| 224 | } |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 225 | m_transport.close(); |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 226 | NDN_THROW(Transport::Error(error, "error while receiving data from socket")); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | m_inputBufferSize += nBytesRecvd; |
| 230 | // do magic |
| 231 | |
| 232 | std::size_t offset = 0; |
| 233 | bool hasProcessedSome = processAllReceived(m_inputBuffer, offset, m_inputBufferSize); |
| 234 | if (!hasProcessedSome && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) { |
| 235 | m_transport.close(); |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 236 | NDN_THROW(Transport::Error("input buffer full, but a valid TLV cannot be decoded")); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | if (offset > 0) { |
| 240 | if (offset != m_inputBufferSize) { |
| 241 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, m_inputBuffer); |
| 242 | m_inputBufferSize -= offset; |
| 243 | } |
| 244 | else { |
| 245 | m_inputBufferSize = 0; |
| 246 | } |
| 247 | } |
| 248 | |
Junxiao Shi | 8c56538 | 2016-07-25 23:04:49 +0000 | [diff] [blame] | 249 | asyncReceive(); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool |
| 253 | processAllReceived(uint8_t* buffer, size_t& offset, size_t nBytesAvailable) |
| 254 | { |
| 255 | while (offset < nBytesAvailable) { |
| 256 | bool isOk = false; |
| 257 | Block element; |
| 258 | std::tie(isOk, element) = Block::fromBuffer(buffer + offset, nBytesAvailable - offset); |
| 259 | if (!isOk) |
| 260 | return false; |
| 261 | |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 262 | m_transport.m_receiveCallback(element); |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 263 | offset += element.size(); |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | protected: |
| 269 | BaseTransport& m_transport; |
| 270 | |
| 271 | typename Protocol::socket m_socket; |
| 272 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 273 | size_t m_inputBufferSize = 0; |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 274 | |
| 275 | TransmissionQueue m_transmissionQueue; |
Davide Pesavento | 0c31537 | 2019-04-25 14:50:42 -0400 | [diff] [blame] | 276 | boost::asio::steady_timer m_connectTimer; |
Davide Pesavento | a421c80 | 2019-11-10 21:23:12 -0500 | [diff] [blame] | 277 | bool m_isConnecting = false; |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Junxiao Shi | 70d76eb | 2018-12-18 13:15:43 -0700 | [diff] [blame] | 280 | } // namespace detail |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 281 | } // namespace ndn |
| 282 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame^] | 283 | #endif // NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_IMPL_HPP |