Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 31 | #include "core/logger.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 35 | // forward declaration |
| 36 | template<class T, class U, class V> struct StreamFaceSenderImpl; |
| 37 | |
| 38 | template<class Protocol, class FaceBase = Face> |
| 39 | class StreamFace : public FaceBase |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 40 | { |
| 41 | public: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 42 | typedef Protocol protocol; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 44 | /** |
| 45 | * \brief Create instance of StreamFace |
| 46 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 47 | StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 48 | const shared_ptr<typename protocol::socket>& socket, |
| 49 | bool isOnDemand); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 51 | virtual |
| 52 | ~StreamFace(); |
| 53 | |
| 54 | // from Face |
| 55 | virtual void |
| 56 | sendInterest(const Interest& interest); |
| 57 | |
| 58 | virtual void |
| 59 | sendData(const Data& data); |
| 60 | |
| 61 | virtual void |
| 62 | close(); |
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: |
| 89 | uint8_t m_inputBuffer[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); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 131 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, 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 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 181 | this->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 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 189 | this->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 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 200 | NFD_LOG_INFO("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 201 | << ",uri:" << this->getRemoteUri() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 202 | << "] Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 203 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 204 | shutdownSocket(); |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 205 | this->fail("Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 206 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 208 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 209 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 210 | StreamFace<T, U>::processErrorCode(const boost::system::error_code& error) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 211 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 212 | if (error == boost::asio::error::operation_aborted || // when cancel() is called |
| 213 | error == boost::asio::error::shut_down) // after shutdown() is called |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 214 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 215 | |
| 216 | if (!m_socket->is_open()) |
| 217 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 218 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | if (error == boost::asio::error::eof) |
| 223 | { |
| 224 | NFD_LOG_INFO("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 225 | << ",uri:" << this->getRemoteUri() |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 226 | << "] Connection closed"); |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | NFD_LOG_WARN("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 231 | << ",uri:" << this->getRemoteUri() |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 232 | << "] Send or receive operation failed, closing face: " |
| 233 | << error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 236 | shutdownSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 237 | |
| 238 | if (error == boost::asio::error::eof) |
| 239 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 240 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 241 | } |
| 242 | else |
| 243 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 244 | this->fail("Send or receive operation failed, closing face: " + error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 248 | template<class T, class U> |
| 249 | inline void |
| 250 | StreamFace<T, U>::sendFromQueue() |
| 251 | { |
| 252 | const Block& block = this->m_sendQueue.front(); |
| 253 | boost::asio::async_write(*this->m_socket, boost::asio::buffer(block), |
| 254 | bind(&StreamFace<T, U>::handleSend, this, _1, _2)); |
| 255 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 256 | |
| 257 | template<class T, class U> |
| 258 | inline void |
| 259 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 260 | size_t nBytesSent) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 261 | { |
| 262 | if (error) |
| 263 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 264 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 265 | BOOST_ASSERT(!m_sendQueue.empty()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 266 | |
| 267 | NFD_LOG_TRACE("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 268 | << ",uri:" << this->getRemoteUri() |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 269 | << "] Successfully sent: " << nBytesSent << " bytes"); |
| 270 | this->getMutableCounters().getNOutBytes() += nBytesSent; |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 271 | |
| 272 | m_sendQueue.pop(); |
| 273 | if (!m_sendQueue.empty()) |
| 274 | sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | template<class T, class U> |
| 278 | inline void |
| 279 | StreamFace<T, U>::handleReceive(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 280 | size_t nBytesReceived) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 281 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 282 | if (error) |
| 283 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 284 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 285 | NFD_LOG_TRACE("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 286 | << ",uri:" << this->getRemoteUri() |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 287 | << "] Received: " << nBytesReceived << " bytes"); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 288 | this->getMutableCounters().getNInBytes() += nBytesReceived; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 289 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 290 | m_inputBufferSize += nBytesReceived; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 291 | |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 292 | size_t offset = 0; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 293 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 294 | bool isOk = true; |
| 295 | Block element; |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 296 | while (m_inputBufferSize - offset > 0) |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 297 | { |
| 298 | isOk = Block::fromBuffer(m_inputBuffer + offset, m_inputBufferSize - offset, element); |
| 299 | if (!isOk) |
| 300 | break; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 301 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 302 | offset += element.size(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 303 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 304 | BOOST_ASSERT(offset <= m_inputBufferSize); |
| 305 | |
| 306 | if (!this->decodeAndDispatchInput(element)) |
| 307 | { |
| 308 | NFD_LOG_WARN("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 309 | << ",uri:" << this->getRemoteUri() |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 310 | << "] Received unrecognized block of type [" |
| 311 | << element.type() << "]"); |
| 312 | // ignore unknown packet and proceed |
| 313 | } |
| 314 | } |
| 315 | if (!isOk && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
| 316 | { |
| 317 | NFD_LOG_WARN("[id:" << this->getId() |
Alexander Afanasyev | 29d1fab | 2014-07-07 19:27:16 -0700 | [diff] [blame] | 318 | << ",uri:" << this->getRemoteUri() |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 319 | << "] Failed to parse incoming packet or packet too large to process, " |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 320 | << "closing down the face"); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 321 | shutdownSocket(); |
| 322 | this->fail("Failed to parse incoming packet or packet too large to process, " |
| 323 | "closing down the face"); |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 324 | return; |
| 325 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 326 | |
| 327 | if (offset > 0) |
| 328 | { |
| 329 | if (offset != m_inputBufferSize) |
| 330 | { |
| 331 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 332 | m_inputBuffer); |
| 333 | m_inputBufferSize -= offset; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | m_inputBufferSize = 0; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 342 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 343 | bind(&StreamFace<T, U>::handleReceive, this, _1, _2)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 346 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 347 | inline void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 348 | StreamFace<T, U>::shutdownSocket() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 349 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 350 | // Cancel all outstanding operations and shutdown the socket |
| 351 | // so that no further sends or receives are possible. |
| 352 | // Use the non-throwing variants and ignore errors, if any. |
| 353 | boost::system::error_code error; |
| 354 | m_socket->cancel(error); |
| 355 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 356 | |
| 357 | boost::asio::io_service& io = m_socket->get_io_service(); |
| 358 | // ensure that the Face object is alive at least until all pending |
| 359 | // handlers are dispatched |
| 360 | io.post(bind(&StreamFace<T, U>::deferredClose, this, this->shared_from_this())); |
| 361 | |
| 362 | // Some bug or feature of Boost.Asio (see http://redmine.named-data.net/issues/1856): |
| 363 | // |
| 364 | // When shutdownSocket is called from within a socket event (e.g., from handleReceive), |
| 365 | // m_socket->shutdown() does not trigger the cancellation of the handleSend callback. |
| 366 | // Instead, handleSend is invoked as nothing bad happened. |
| 367 | // |
| 368 | // In order to prevent the assertion in handleSend from failing, we clear the queue |
| 369 | // and close the socket in deferredClose, i.e., after all callbacks scheduled up to |
| 370 | // this point have been executed. If more send operations are scheduled after this |
| 371 | // point, they will fail because the socket has been shutdown, and their callbacks |
| 372 | // will be invoked with error code == asio::error::shut_down. |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 375 | template<class T, class U> |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 376 | inline void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 377 | StreamFace<T, U>::deferredClose(const shared_ptr<Face>& face) |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 378 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 379 | NFD_LOG_DEBUG("[id:" << this->getId() |
| 380 | << ",uri:" << this->getRemoteUri() |
| 381 | << "] Clearing send queue"); |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 382 | |
| 383 | // clear send queue |
| 384 | std::queue<Block> emptyQueue; |
| 385 | std::swap(emptyQueue, m_sendQueue); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 386 | |
| 387 | // use the non-throwing variant and ignore errors, if any |
| 388 | boost::system::error_code error; |
| 389 | m_socket->close(error); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 390 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 391 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 392 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 393 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 394 | #endif // NFD_DAEMON_FACE_STREAM_FACE_HPP |