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 | |
Davide Pesavento | d2856a4 | 2015-09-19 21:29:50 +0200 | [diff] [blame^] | 33 | #include <queue> |
| 34 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 35 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 37 | // forward declaration |
| 38 | template<class T, class U, class V> struct StreamFaceSenderImpl; |
| 39 | |
| 40 | template<class Protocol, class FaceBase = Face> |
| 41 | class StreamFace : public FaceBase |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 44 | typedef Protocol protocol; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 45 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 46 | StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 47 | typename protocol::socket socket, bool isOnDemand); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 48 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 49 | // from FaceBase |
| 50 | void |
| 51 | sendInterest(const Interest& interest) DECL_OVERRIDE; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 52 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 53 | void |
| 54 | sendData(const Data& data) DECL_OVERRIDE; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 55 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 56 | void |
| 57 | close() DECL_OVERRIDE; |
Alexander Afanasyev | 93ce75e | 2014-02-18 19:45:34 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 59 | protected: |
| 60 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 61 | processErrorCode(const boost::system::error_code& error); |
| 62 | |
| 63 | void |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 64 | sendFromQueue(); |
| 65 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 66 | void |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 67 | handleSend(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 68 | size_t nBytesSent); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 69 | |
| 70 | void |
| 71 | handleReceive(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 72 | size_t nBytesReceived); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 74 | void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 75 | shutdownSocket(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 76 | |
| 77 | void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 78 | deferredClose(const shared_ptr<Face>& face); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 79 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 80 | protected: |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 81 | typename protocol::socket m_socket; |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 82 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 83 | NFD_LOG_INCLASS_DECLARE(); |
| 84 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 85 | private: |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 86 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 87 | size_t m_inputBufferSize; |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 88 | std::queue<Block> m_sendQueue; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 89 | |
| 90 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Interest>; |
| 91 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Data>; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 94 | // All inherited classes must use |
| 95 | // NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name"); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 97 | |
| 98 | /** \brief Class allowing validation of the StreamFace use |
| 99 | * |
| 100 | * For example, partial specialization based on boost::asio::ip::tcp should check |
| 101 | * that local endpoint is loopback |
| 102 | * |
| 103 | * @throws Face::Error if validation failed |
| 104 | */ |
| 105 | template<class Protocol, class U> |
| 106 | struct StreamFaceValidator |
| 107 | { |
| 108 | static void |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 109 | validateSocket(const typename Protocol::socket& socket) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 110 | { |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 115 | template<class T, class FaceBase> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 116 | inline |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 117 | StreamFace<T, FaceBase>::StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 118 | typename StreamFace::protocol::socket socket, bool isOnDemand) |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 119 | : FaceBase(remoteUri, localUri) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 120 | , m_socket(std::move(socket)) |
Alexander Afanasyev | b9f6e43 | 2014-02-14 20:52:49 -0800 | [diff] [blame] | 121 | , m_inputBufferSize(0) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 122 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 123 | NFD_LOG_FACE_INFO("Creating face"); |
| 124 | |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 125 | this->setPersistency(isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND : ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 126 | StreamFaceValidator<T, FaceBase>::validateSocket(m_socket); |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 127 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 128 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), |
| 129 | bind(&StreamFace<T, FaceBase>::handleReceive, this, |
| 130 | boost::asio::placeholders::error, |
| 131 | boost::asio::placeholders::bytes_transferred)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 134 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 135 | template<class Protocol, class FaceBase, class Packet> |
| 136 | struct StreamFaceSenderImpl |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 137 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 138 | static void |
| 139 | send(StreamFace<Protocol, FaceBase>& face, const Packet& packet) |
| 140 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 141 | bool wasQueueEmpty = face.m_sendQueue.empty(); |
| 142 | face.m_sendQueue.push(packet.wireEncode()); |
| 143 | |
| 144 | if (wasQueueEmpty) |
| 145 | face.sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
| 149 | // partial specialization (only classes can be partially specialized) |
| 150 | template<class Protocol, class Packet> |
| 151 | struct StreamFaceSenderImpl<Protocol, LocalFace, Packet> |
| 152 | { |
| 153 | static void |
| 154 | send(StreamFace<Protocol, LocalFace>& face, const Packet& packet) |
| 155 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 156 | bool wasQueueEmpty = face.m_sendQueue.empty(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 158 | if (!face.isEmptyFilteredLocalControlHeader(packet.getLocalControlHeader())) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 159 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 160 | face.m_sendQueue.push(face.filterAndEncodeLocalControlHeader(packet)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 161 | } |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 162 | face.m_sendQueue.push(packet.wireEncode()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 163 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 164 | if (wasQueueEmpty) |
| 165 | face.sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 166 | } |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | template<class T, class U> |
| 171 | inline void |
| 172 | StreamFace<T, U>::sendInterest(const Interest& interest) |
| 173 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 174 | NFD_LOG_FACE_TRACE(__func__); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 175 | this->emitSignal(onSendInterest, interest); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 176 | StreamFaceSenderImpl<T, U, Interest>::send(*this, interest); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 179 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 180 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 181 | StreamFace<T, U>::sendData(const Data& data) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 182 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 183 | NFD_LOG_FACE_TRACE(__func__); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 184 | this->emitSignal(onSendData, data); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 185 | StreamFaceSenderImpl<T, U, Data>::send(*this, data); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 188 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 189 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 190 | StreamFace<T, U>::close() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 191 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 192 | if (!m_socket.is_open()) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 193 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 194 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 195 | NFD_LOG_FACE_INFO("Closing face"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 196 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 197 | shutdownSocket(); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 198 | this->fail("Face closed"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 199 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 201 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 202 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 203 | StreamFace<T, U>::processErrorCode(const boost::system::error_code& error) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 204 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 205 | if (error == boost::asio::error::operation_aborted || // when cancel() is called |
| 206 | error == boost::asio::error::shut_down) // after shutdown() is called |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 207 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 208 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 209 | if (!m_socket.is_open()) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 210 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 211 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 215 | if (error != boost::asio::error::eof) |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 216 | NFD_LOG_FACE_WARN("Send or receive operation failed: " << error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 217 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 218 | shutdownSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 219 | |
| 220 | if (error == boost::asio::error::eof) |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 221 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 222 | else |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 223 | this->fail(error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 226 | template<class T, class U> |
| 227 | inline void |
| 228 | StreamFace<T, U>::sendFromQueue() |
| 229 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 230 | boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()), |
| 231 | bind(&StreamFace<T, U>::handleSend, this, |
| 232 | boost::asio::placeholders::error, |
| 233 | boost::asio::placeholders::bytes_transferred)); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 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 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 308 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 309 | ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), |
| 310 | bind(&StreamFace<T, U>::handleReceive, this, |
| 311 | boost::asio::placeholders::error, |
| 312 | boost::asio::placeholders::bytes_transferred)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 315 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 316 | inline void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 317 | StreamFace<T, U>::shutdownSocket() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 318 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 319 | NFD_LOG_FACE_TRACE(__func__); |
| 320 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 321 | // Cancel all outstanding operations and shutdown the socket |
| 322 | // so that no further sends or receives are possible. |
| 323 | // Use the non-throwing variants and ignore errors, if any. |
| 324 | boost::system::error_code error; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 325 | m_socket.cancel(error); |
| 326 | m_socket.shutdown(protocol::socket::shutdown_both, error); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 327 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 328 | // ensure that the Face object is alive at least until all pending |
| 329 | // handlers are dispatched |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 330 | getGlobalIoService().post(bind(&StreamFace<T, U>::deferredClose, |
| 331 | this, this->shared_from_this())); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 332 | |
| 333 | // Some bug or feature of Boost.Asio (see http://redmine.named-data.net/issues/1856): |
| 334 | // |
| 335 | // When shutdownSocket is called from within a socket event (e.g., from handleReceive), |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 336 | // m_socket.shutdown() does not trigger the cancellation of the handleSend callback. |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 337 | // Instead, handleSend is invoked as nothing bad happened. |
| 338 | // |
| 339 | // In order to prevent the assertion in handleSend from failing, we clear the queue |
| 340 | // and close the socket in deferredClose, i.e., after all callbacks scheduled up to |
| 341 | // this point have been executed. If more send operations are scheduled after this |
| 342 | // point, they will fail because the socket has been shutdown, and their callbacks |
| 343 | // will be invoked with error code == asio::error::shut_down. |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 346 | template<class T, class U> |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 347 | inline void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 348 | StreamFace<T, U>::deferredClose(const shared_ptr<Face>& face) |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 349 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 350 | NFD_LOG_FACE_TRACE(__func__); |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 351 | |
| 352 | // clear send queue |
| 353 | std::queue<Block> emptyQueue; |
| 354 | std::swap(emptyQueue, m_sendQueue); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 355 | |
| 356 | // use the non-throwing variant and ignore errors, if any |
| 357 | boost::system::error_code error; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 358 | m_socket.close(error); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 359 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 360 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 361 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 362 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 363 | #endif // NFD_DAEMON_FACE_STREAM_FACE_HPP |