Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_STREAM_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_STREAM_FACE_HPP |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 28 | |
| 29 | #include "face.hpp" |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 30 | #include "local-face.hpp" |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 31 | #include "core/global-io.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 32 | #include "core/logger.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 34 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 36 | // forward declaration |
| 37 | template<class T, class U, class V> struct StreamFaceSenderImpl; |
| 38 | |
| 39 | template<class Protocol, class FaceBase = Face> |
| 40 | class StreamFace : public FaceBase |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 41 | { |
| 42 | public: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 43 | typedef Protocol protocol; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 45 | /** |
| 46 | * \brief Create instance of StreamFace |
| 47 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 48 | StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 49 | const shared_ptr<typename protocol::socket>& socket, |
| 50 | bool isOnDemand); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 51 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 52 | ~StreamFace() DECL_OVERRIDE; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 53 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 54 | // from FaceBase |
| 55 | void |
| 56 | sendInterest(const Interest& interest) DECL_OVERRIDE; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 57 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 58 | void |
| 59 | sendData(const Data& data) DECL_OVERRIDE; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 60 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 61 | void |
| 62 | close() DECL_OVERRIDE; |
Alexander Afanasyev | 93ce75e | 2014-02-18 19:45:34 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 64 | protected: |
| 65 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 66 | processErrorCode(const boost::system::error_code& error); |
| 67 | |
| 68 | void |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 69 | sendFromQueue(); |
| 70 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 71 | void |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 72 | handleSend(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 73 | size_t nBytesSent); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 74 | |
| 75 | void |
| 76 | handleReceive(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 77 | size_t nBytesReceived); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 79 | void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 80 | shutdownSocket(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 81 | |
| 82 | void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 83 | deferredClose(const shared_ptr<Face>& face); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 84 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 85 | protected: |
| 86 | shared_ptr<typename protocol::socket> m_socket; |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 87 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 88 | private: |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 89 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 90 | size_t m_inputBufferSize; |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 91 | std::queue<Block> m_sendQueue; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 92 | |
| 93 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Interest>; |
| 94 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Data>; |
| 95 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 96 | NFD_LOG_INCLASS_DECLARE(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 99 | // All inherited classes must use |
| 100 | // NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name"); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 102 | |
| 103 | /** \brief Class allowing validation of the StreamFace use |
| 104 | * |
| 105 | * For example, partial specialization based on boost::asio::ip::tcp should check |
| 106 | * that local endpoint is loopback |
| 107 | * |
| 108 | * @throws Face::Error if validation failed |
| 109 | */ |
| 110 | template<class Protocol, class U> |
| 111 | struct StreamFaceValidator |
| 112 | { |
| 113 | static void |
| 114 | validateSocket(typename Protocol::socket& socket) |
| 115 | { |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 120 | template<class T, class FaceBase> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 121 | inline |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 122 | StreamFace<T, FaceBase>::StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
| 123 | const shared_ptr<typename StreamFace::protocol::socket>& socket, |
| 124 | bool isOnDemand) |
| 125 | : FaceBase(remoteUri, localUri) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 126 | , m_socket(socket) |
Alexander Afanasyev | b9f6e43 | 2014-02-14 20:52:49 -0800 | [diff] [blame] | 127 | , m_inputBufferSize(0) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 129 | FaceBase::setOnDemand(isOnDemand); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 130 | StreamFaceValidator<T, FaceBase>::validateSocket(*socket); |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 131 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), 0, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 132 | bind(&StreamFace<T, FaceBase>::handleReceive, this, _1, _2)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 135 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 136 | inline |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 137 | StreamFace<T, U>::~StreamFace() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 138 | { |
| 139 | } |
| 140 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 141 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 142 | template<class Protocol, class FaceBase, class Packet> |
| 143 | struct StreamFaceSenderImpl |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 144 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 145 | static void |
| 146 | send(StreamFace<Protocol, FaceBase>& face, const Packet& packet) |
| 147 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 148 | bool wasQueueEmpty = face.m_sendQueue.empty(); |
| 149 | face.m_sendQueue.push(packet.wireEncode()); |
| 150 | |
| 151 | if (wasQueueEmpty) |
| 152 | face.sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 153 | } |
| 154 | }; |
| 155 | |
| 156 | // partial specialization (only classes can be partially specialized) |
| 157 | template<class Protocol, class Packet> |
| 158 | struct StreamFaceSenderImpl<Protocol, LocalFace, Packet> |
| 159 | { |
| 160 | static void |
| 161 | send(StreamFace<Protocol, LocalFace>& face, const Packet& packet) |
| 162 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 163 | bool wasQueueEmpty = face.m_sendQueue.empty(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 165 | if (!face.isEmptyFilteredLocalControlHeader(packet.getLocalControlHeader())) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 166 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 167 | face.m_sendQueue.push(face.filterAndEncodeLocalControlHeader(packet)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 168 | } |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 169 | face.m_sendQueue.push(packet.wireEncode()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 171 | if (wasQueueEmpty) |
| 172 | face.sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 173 | } |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | template<class T, class U> |
| 178 | inline void |
| 179 | StreamFace<T, U>::sendInterest(const Interest& interest) |
| 180 | { |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 181 | this->emitSignal(onSendInterest, interest); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 182 | StreamFaceSenderImpl<T, U, Interest>::send(*this, interest); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 185 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 186 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 187 | StreamFace<T, U>::sendData(const Data& data) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 188 | { |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 189 | this->emitSignal(onSendData, data); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 190 | StreamFaceSenderImpl<T, U, Data>::send(*this, data); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 193 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 194 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 195 | StreamFace<T, U>::close() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 196 | { |
| 197 | if (!m_socket->is_open()) |
| 198 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 199 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 200 | NFD_LOG_INFO("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
| 201 | << "] Closing face"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 202 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 203 | shutdownSocket(); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 204 | this->fail("Face closed"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 205 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 207 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 208 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 209 | StreamFace<T, U>::processErrorCode(const boost::system::error_code& error) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 210 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 211 | if (error == boost::asio::error::operation_aborted || // when cancel() is called |
| 212 | error == boost::asio::error::shut_down) // after shutdown() is called |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 213 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 214 | |
| 215 | if (!m_socket->is_open()) |
| 216 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 217 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 221 | if (error != boost::asio::error::eof) |
| 222 | NFD_LOG_WARN("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
| 223 | << "] Send or receive operation failed, closing face: " |
| 224 | << error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 225 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 226 | shutdownSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 227 | |
| 228 | if (error == boost::asio::error::eof) |
| 229 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 230 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 234 | this->fail("Send or receive operation failed: " + error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 238 | template<class T, class U> |
| 239 | inline void |
| 240 | StreamFace<T, U>::sendFromQueue() |
| 241 | { |
| 242 | const Block& block = this->m_sendQueue.front(); |
| 243 | boost::asio::async_write(*this->m_socket, boost::asio::buffer(block), |
| 244 | bind(&StreamFace<T, U>::handleSend, this, _1, _2)); |
| 245 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 246 | |
| 247 | template<class T, class U> |
| 248 | inline void |
| 249 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 250 | size_t nBytesSent) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 251 | { |
| 252 | if (error) |
| 253 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 254 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 255 | BOOST_ASSERT(!m_sendQueue.empty()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 256 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 257 | NFD_LOG_TRACE("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 258 | << "] Successfully sent: " << nBytesSent << " bytes"); |
| 259 | this->getMutableCounters().getNOutBytes() += nBytesSent; |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 260 | |
| 261 | m_sendQueue.pop(); |
| 262 | if (!m_sendQueue.empty()) |
| 263 | sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | template<class T, class U> |
| 267 | inline void |
| 268 | StreamFace<T, U>::handleReceive(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 269 | size_t nBytesReceived) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 270 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 271 | if (error) |
| 272 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 273 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 274 | NFD_LOG_TRACE("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 275 | << "] Received: " << nBytesReceived << " bytes"); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 276 | this->getMutableCounters().getNInBytes() += nBytesReceived; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 277 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 278 | m_inputBufferSize += nBytesReceived; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 279 | |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 280 | size_t offset = 0; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 281 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 282 | bool isOk = true; |
| 283 | Block element; |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 284 | while (m_inputBufferSize - offset > 0) |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 285 | { |
| 286 | isOk = Block::fromBuffer(m_inputBuffer + offset, m_inputBufferSize - offset, element); |
| 287 | if (!isOk) |
| 288 | break; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 289 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 290 | offset += element.size(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 291 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 292 | BOOST_ASSERT(offset <= m_inputBufferSize); |
| 293 | |
| 294 | if (!this->decodeAndDispatchInput(element)) |
| 295 | { |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 296 | NFD_LOG_WARN("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 297 | << "] Received unrecognized block of type [" |
| 298 | << element.type() << "]"); |
| 299 | // ignore unknown packet and proceed |
| 300 | } |
| 301 | } |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 302 | if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 303 | { |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 304 | NFD_LOG_WARN("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 305 | << "] Failed to parse incoming packet or packet too large to process, " |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 306 | << "closing face"); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 307 | shutdownSocket(); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 308 | this->fail("Failed to parse incoming packet or packet too large to process"); |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 309 | return; |
| 310 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 311 | |
| 312 | if (offset > 0) |
| 313 | { |
| 314 | if (offset != m_inputBufferSize) |
| 315 | { |
| 316 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 317 | m_inputBuffer); |
| 318 | m_inputBufferSize -= offset; |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | m_inputBufferSize = 0; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 327 | ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 328 | bind(&StreamFace<T, U>::handleReceive, this, _1, _2)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 331 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 332 | inline void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 333 | StreamFace<T, U>::shutdownSocket() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 334 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 335 | // Cancel all outstanding operations and shutdown the socket |
| 336 | // so that no further sends or receives are possible. |
| 337 | // Use the non-throwing variants and ignore errors, if any. |
| 338 | boost::system::error_code error; |
| 339 | m_socket->cancel(error); |
| 340 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 341 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 342 | // ensure that the Face object is alive at least until all pending |
| 343 | // handlers are dispatched |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 344 | getGlobalIoService().post(bind(&StreamFace<T, U>::deferredClose, |
| 345 | this, this->shared_from_this())); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 346 | |
| 347 | // Some bug or feature of Boost.Asio (see http://redmine.named-data.net/issues/1856): |
| 348 | // |
| 349 | // When shutdownSocket is called from within a socket event (e.g., from handleReceive), |
| 350 | // m_socket->shutdown() does not trigger the cancellation of the handleSend callback. |
| 351 | // Instead, handleSend is invoked as nothing bad happened. |
| 352 | // |
| 353 | // In order to prevent the assertion in handleSend from failing, we clear the queue |
| 354 | // and close the socket in deferredClose, i.e., after all callbacks scheduled up to |
| 355 | // this point have been executed. If more send operations are scheduled after this |
| 356 | // point, they will fail because the socket has been shutdown, and their callbacks |
| 357 | // will be invoked with error code == asio::error::shut_down. |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 360 | template<class T, class U> |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 361 | inline void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 362 | StreamFace<T, U>::deferredClose(const shared_ptr<Face>& face) |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 363 | { |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 364 | NFD_LOG_DEBUG("[id:" << this->getId() << ",uri:" << this->getRemoteUri() |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 365 | << "] Clearing send queue"); |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 366 | |
| 367 | // clear send queue |
| 368 | std::queue<Block> emptyQueue; |
| 369 | std::swap(emptyQueue, m_sendQueue); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 370 | |
| 371 | // use the non-throwing variant and ignore errors, if any |
| 372 | boost::system::error_code error; |
| 373 | m_socket->close(error); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 374 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 375 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 376 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 377 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 378 | #endif // NFD_DAEMON_FACE_STREAM_FACE_HPP |