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