Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |
| 27 | #define NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |
| 28 | |
| 29 | #include "transport.hpp" |
| 30 | #include "core/global-io.hpp" |
| 31 | |
| 32 | #include <queue> |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace face { |
| 36 | |
| 37 | /** \brief Implements Transport for stream-based protocols. |
| 38 | * |
| 39 | * \tparam Protocol a stream-based protocol in Boost.Asio |
| 40 | */ |
| 41 | template<class Protocol> |
| 42 | class StreamTransport : public Transport |
| 43 | { |
| 44 | public: |
| 45 | typedef Protocol protocol; |
| 46 | |
| 47 | /** \brief Construct stream transport. |
| 48 | * |
| 49 | * \param socket Protocol-specific socket for the created transport |
| 50 | */ |
| 51 | explicit |
| 52 | StreamTransport(typename protocol::socket&& socket); |
| 53 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 54 | protected: |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 55 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 56 | doClose() override; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 57 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 58 | void |
| 59 | deferredClose(); |
| 60 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 61 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 62 | doSend(Transport::Packet&& packet) override; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 63 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 64 | void |
| 65 | sendFromQueue(); |
| 66 | |
| 67 | void |
| 68 | handleSend(const boost::system::error_code& error, |
| 69 | size_t nBytesSent); |
| 70 | |
| 71 | void |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 72 | startReceive(); |
| 73 | |
| 74 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 75 | handleReceive(const boost::system::error_code& error, |
| 76 | size_t nBytesReceived); |
| 77 | |
| 78 | void |
| 79 | processErrorCode(const boost::system::error_code& error); |
| 80 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 81 | virtual void |
| 82 | handleError(const boost::system::error_code& error); |
| 83 | |
| 84 | void |
| 85 | resetReceiveBuffer(); |
| 86 | |
| 87 | void |
| 88 | resetSendQueue(); |
| 89 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 90 | protected: |
| 91 | typename protocol::socket m_socket; |
| 92 | |
| 93 | NFD_LOG_INCLASS_DECLARE(); |
| 94 | |
| 95 | private: |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 96 | uint8_t m_receiveBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
| 97 | size_t m_receiveBufferSize; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 98 | std::queue<Block> m_sendQueue; |
| 99 | }; |
| 100 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 101 | |
| 102 | template<class T> |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 103 | StreamTransport<T>::StreamTransport(typename StreamTransport::protocol::socket&& socket) |
| 104 | : m_socket(std::move(socket)) |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 105 | , m_receiveBufferSize(0) |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 106 | { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 107 | startReceive(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 111 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 112 | StreamTransport<T>::doClose() |
| 113 | { |
| 114 | NFD_LOG_FACE_TRACE(__func__); |
| 115 | |
| 116 | if (m_socket.is_open()) { |
| 117 | // Cancel all outstanding operations and shutdown the socket |
| 118 | // so that no further sends or receives are possible. |
| 119 | // Use the non-throwing variants and ignore errors, if any. |
| 120 | boost::system::error_code error; |
| 121 | m_socket.cancel(error); |
| 122 | m_socket.shutdown(protocol::socket::shutdown_both, error); |
| 123 | } |
| 124 | |
| 125 | // Ensure that the Transport stays alive at least until |
| 126 | // all pending handlers are dispatched |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 127 | getGlobalIoService().post([this] { deferredClose(); }); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 128 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 129 | // Some bug or feature of Boost.Asio (see https://redmine.named-data.net/issues/1856): |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 130 | // |
| 131 | // When doClose is called from a socket event handler (e.g., from handleReceive), |
| 132 | // m_socket.shutdown() does not trigger the cancellation of the handleSend callback. |
| 133 | // Instead, handleSend is invoked as nothing bad happened. |
| 134 | // |
| 135 | // In order to prevent the assertion in handleSend from failing, we clear the queue |
| 136 | // and close the socket in deferredClose, i.e., after all callbacks scheduled up to |
| 137 | // this point have been executed. If more send operations are scheduled after this |
| 138 | // point, they will fail because the socket has been shutdown, and their callbacks |
| 139 | // will be invoked with error code == asio::error::shut_down. |
| 140 | } |
| 141 | |
| 142 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 143 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 144 | StreamTransport<T>::deferredClose() |
| 145 | { |
| 146 | NFD_LOG_FACE_TRACE(__func__); |
| 147 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 148 | resetSendQueue(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 149 | |
| 150 | // use the non-throwing variant and ignore errors, if any |
| 151 | boost::system::error_code error; |
| 152 | m_socket.close(error); |
| 153 | |
| 154 | this->setState(TransportState::CLOSED); |
| 155 | } |
| 156 | |
| 157 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 158 | void |
| 159 | StreamTransport<T>::doSend(Transport::Packet&& packet) |
| 160 | { |
| 161 | NFD_LOG_FACE_TRACE(__func__); |
| 162 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 163 | if (getState() != TransportState::UP) |
| 164 | return; |
| 165 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 166 | bool wasQueueEmpty = m_sendQueue.empty(); |
| 167 | m_sendQueue.push(packet.packet); |
| 168 | |
| 169 | if (wasQueueEmpty) |
| 170 | sendFromQueue(); |
| 171 | } |
| 172 | |
| 173 | template<class T> |
| 174 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 175 | StreamTransport<T>::sendFromQueue() |
| 176 | { |
| 177 | boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()), |
| 178 | bind(&StreamTransport<T>::handleSend, this, |
| 179 | boost::asio::placeholders::error, |
| 180 | boost::asio::placeholders::bytes_transferred)); |
| 181 | } |
| 182 | |
| 183 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 184 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 185 | StreamTransport<T>::handleSend(const boost::system::error_code& error, |
| 186 | size_t nBytesSent) |
| 187 | { |
| 188 | if (error) |
| 189 | return processErrorCode(error); |
| 190 | |
| 191 | NFD_LOG_FACE_TRACE("Successfully sent: " << nBytesSent << " bytes"); |
| 192 | |
| 193 | BOOST_ASSERT(!m_sendQueue.empty()); |
| 194 | m_sendQueue.pop(); |
| 195 | |
| 196 | if (!m_sendQueue.empty()) |
| 197 | sendFromQueue(); |
| 198 | } |
| 199 | |
| 200 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 201 | void |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 202 | StreamTransport<T>::startReceive() |
| 203 | { |
| 204 | BOOST_ASSERT(getState() == TransportState::UP); |
| 205 | |
| 206 | m_socket.async_receive(boost::asio::buffer(m_receiveBuffer + m_receiveBufferSize, |
| 207 | ndn::MAX_NDN_PACKET_SIZE - m_receiveBufferSize), |
| 208 | bind(&StreamTransport<T>::handleReceive, this, |
| 209 | boost::asio::placeholders::error, |
| 210 | boost::asio::placeholders::bytes_transferred)); |
| 211 | } |
| 212 | |
| 213 | template<class T> |
| 214 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 215 | StreamTransport<T>::handleReceive(const boost::system::error_code& error, |
| 216 | size_t nBytesReceived) |
| 217 | { |
| 218 | if (error) |
| 219 | return processErrorCode(error); |
| 220 | |
| 221 | NFD_LOG_FACE_TRACE("Received: " << nBytesReceived << " bytes"); |
| 222 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 223 | m_receiveBufferSize += nBytesReceived; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 224 | size_t offset = 0; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 225 | bool isOk = true; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 226 | while (m_receiveBufferSize - offset > 0) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 227 | Block element; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 228 | std::tie(isOk, element) = Block::fromBuffer(m_receiveBuffer + offset, m_receiveBufferSize - offset); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 229 | if (!isOk) |
| 230 | break; |
| 231 | |
| 232 | offset += element.size(); |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 233 | BOOST_ASSERT(offset <= m_receiveBufferSize); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 234 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 235 | this->receive(Transport::Packet(std::move(element))); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 238 | if (!isOk && m_receiveBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 239 | NFD_LOG_FACE_ERROR("Failed to parse incoming packet or packet too large to process"); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 240 | this->setState(TransportState::FAILED); |
| 241 | doClose(); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (offset > 0) { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 246 | if (offset != m_receiveBufferSize) { |
| 247 | std::copy(m_receiveBuffer + offset, m_receiveBuffer + m_receiveBufferSize, m_receiveBuffer); |
| 248 | m_receiveBufferSize -= offset; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 249 | } |
| 250 | else { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 251 | m_receiveBufferSize = 0; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 255 | startReceive(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 259 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 260 | StreamTransport<T>::processErrorCode(const boost::system::error_code& error) |
| 261 | { |
| 262 | NFD_LOG_FACE_TRACE(__func__); |
| 263 | |
| 264 | if (getState() == TransportState::CLOSING || |
| 265 | getState() == TransportState::FAILED || |
| 266 | getState() == TransportState::CLOSED || |
| 267 | error == boost::asio::error::operation_aborted || // when cancel() is called |
| 268 | error == boost::asio::error::shut_down) // after shutdown() is called |
| 269 | // transport is shutting down, ignore any errors |
| 270 | return; |
| 271 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 272 | handleError(error); |
| 273 | } |
| 274 | |
| 275 | template<class T> |
| 276 | void |
| 277 | StreamTransport<T>::handleError(const boost::system::error_code& error) |
| 278 | { |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 279 | if (error != boost::asio::error::eof) |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame^] | 280 | NFD_LOG_FACE_ERROR("Send or receive operation failed: " << error.message()); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 281 | |
| 282 | this->setState(TransportState::FAILED); |
| 283 | doClose(); |
| 284 | } |
| 285 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 286 | template<class T> |
| 287 | void |
| 288 | StreamTransport<T>::resetReceiveBuffer() |
| 289 | { |
| 290 | m_receiveBufferSize = 0; |
| 291 | } |
| 292 | |
| 293 | template<class T> |
| 294 | void |
| 295 | StreamTransport<T>::resetSendQueue() |
| 296 | { |
| 297 | std::queue<Block> emptyQueue; |
| 298 | std::swap(emptyQueue, m_sendQueue); |
| 299 | } |
| 300 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 301 | } // namespace face |
| 302 | } // namespace nfd |
| 303 | |
| 304 | #endif // NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |