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 | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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: |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 55 | virtual 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 | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 61 | virtual 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 |
| 127 | getGlobalIoService().post(bind(&StreamTransport<T>::deferredClose, this)); |
| 128 | |
| 129 | // Some bug or feature of Boost.Asio (see http://redmine.named-data.net/issues/1856): |
| 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 | |
| 225 | size_t offset = 0; |
| 226 | |
| 227 | bool isOk = true; |
| 228 | Block element; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 229 | while (m_receiveBufferSize - offset > 0) { |
| 230 | std::tie(isOk, element) = Block::fromBuffer(m_receiveBuffer + offset, m_receiveBufferSize - offset); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 231 | if (!isOk) |
| 232 | break; |
| 233 | |
| 234 | offset += element.size(); |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 235 | BOOST_ASSERT(offset <= m_receiveBufferSize); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 236 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 237 | this->receive(Transport::Packet(std::move(element))); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 240 | if (!isOk && m_receiveBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) { |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 241 | NFD_LOG_FACE_WARN("Failed to parse incoming packet or packet too large to process"); |
| 242 | this->setState(TransportState::FAILED); |
| 243 | doClose(); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (offset > 0) { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 248 | if (offset != m_receiveBufferSize) { |
| 249 | std::copy(m_receiveBuffer + offset, m_receiveBuffer + m_receiveBufferSize, m_receiveBuffer); |
| 250 | m_receiveBufferSize -= offset; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 251 | } |
| 252 | else { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 253 | m_receiveBufferSize = 0; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 257 | startReceive(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 261 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 262 | StreamTransport<T>::processErrorCode(const boost::system::error_code& error) |
| 263 | { |
| 264 | NFD_LOG_FACE_TRACE(__func__); |
| 265 | |
| 266 | if (getState() == TransportState::CLOSING || |
| 267 | getState() == TransportState::FAILED || |
| 268 | getState() == TransportState::CLOSED || |
| 269 | error == boost::asio::error::operation_aborted || // when cancel() is called |
| 270 | error == boost::asio::error::shut_down) // after shutdown() is called |
| 271 | // transport is shutting down, ignore any errors |
| 272 | return; |
| 273 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 274 | handleError(error); |
| 275 | } |
| 276 | |
| 277 | template<class T> |
| 278 | void |
| 279 | StreamTransport<T>::handleError(const boost::system::error_code& error) |
| 280 | { |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 281 | if (error != boost::asio::error::eof) |
| 282 | NFD_LOG_FACE_WARN("Send or receive operation failed: " << error.message()); |
| 283 | |
| 284 | this->setState(TransportState::FAILED); |
| 285 | doClose(); |
| 286 | } |
| 287 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 288 | template<class T> |
| 289 | void |
| 290 | StreamTransport<T>::resetReceiveBuffer() |
| 291 | { |
| 292 | m_receiveBufferSize = 0; |
| 293 | } |
| 294 | |
| 295 | template<class T> |
| 296 | void |
| 297 | StreamTransport<T>::resetSendQueue() |
| 298 | { |
| 299 | std::queue<Block> emptyQueue; |
| 300 | std::swap(emptyQueue, m_sendQueue); |
| 301 | } |
| 302 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 303 | } // namespace face |
| 304 | } // namespace nfd |
| 305 | |
| 306 | #endif // NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |