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 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 44 | StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 45 | typename protocol::socket socket, bool isOnDemand); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 46 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 47 | // from FaceBase |
| 48 | void |
| 49 | sendInterest(const Interest& interest) DECL_OVERRIDE; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 50 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 51 | void |
| 52 | sendData(const Data& data) 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 | void |
| 55 | close() DECL_OVERRIDE; |
Alexander Afanasyev | 93ce75e | 2014-02-18 19:45:34 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 57 | protected: |
| 58 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 59 | processErrorCode(const boost::system::error_code& error); |
| 60 | |
| 61 | void |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 62 | sendFromQueue(); |
| 63 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 64 | void |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 65 | handleSend(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 66 | size_t nBytesSent); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 67 | |
| 68 | void |
| 69 | handleReceive(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 70 | size_t nBytesReceived); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 72 | void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 73 | shutdownSocket(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 74 | |
| 75 | void |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 76 | deferredClose(const shared_ptr<Face>& face); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 77 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 78 | protected: |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 79 | typename protocol::socket m_socket; |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 80 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 81 | NFD_LOG_INCLASS_DECLARE(); |
| 82 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 83 | private: |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 84 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 85 | size_t m_inputBufferSize; |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 86 | std::queue<Block> m_sendQueue; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 87 | |
| 88 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Interest>; |
| 89 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Data>; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 92 | // All inherited classes must use |
| 93 | // NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name"); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 95 | |
| 96 | /** \brief Class allowing validation of the StreamFace use |
| 97 | * |
| 98 | * For example, partial specialization based on boost::asio::ip::tcp should check |
| 99 | * that local endpoint is loopback |
| 100 | * |
| 101 | * @throws Face::Error if validation failed |
| 102 | */ |
| 103 | template<class Protocol, class U> |
| 104 | struct StreamFaceValidator |
| 105 | { |
| 106 | static void |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 107 | validateSocket(const typename Protocol::socket& socket) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 108 | { |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 113 | template<class T, class FaceBase> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 114 | inline |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 115 | StreamFace<T, FaceBase>::StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 116 | typename StreamFace::protocol::socket socket, bool isOnDemand) |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 117 | : FaceBase(remoteUri, localUri) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 118 | , m_socket(std::move(socket)) |
Alexander Afanasyev | b9f6e43 | 2014-02-14 20:52:49 -0800 | [diff] [blame] | 119 | , m_inputBufferSize(0) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 120 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 121 | NFD_LOG_FACE_INFO("Creating face"); |
| 122 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 123 | this->setOnDemand(isOnDemand); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 124 | StreamFaceValidator<T, FaceBase>::validateSocket(m_socket); |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 125 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 126 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE), |
| 127 | bind(&StreamFace<T, FaceBase>::handleReceive, this, |
| 128 | boost::asio::placeholders::error, |
| 129 | boost::asio::placeholders::bytes_transferred)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 132 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 133 | template<class Protocol, class FaceBase, class Packet> |
| 134 | struct StreamFaceSenderImpl |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 135 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 136 | static void |
| 137 | send(StreamFace<Protocol, FaceBase>& face, const Packet& packet) |
| 138 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 139 | bool wasQueueEmpty = face.m_sendQueue.empty(); |
| 140 | face.m_sendQueue.push(packet.wireEncode()); |
| 141 | |
| 142 | if (wasQueueEmpty) |
| 143 | face.sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
| 147 | // partial specialization (only classes can be partially specialized) |
| 148 | template<class Protocol, class Packet> |
| 149 | struct StreamFaceSenderImpl<Protocol, LocalFace, Packet> |
| 150 | { |
| 151 | static void |
| 152 | send(StreamFace<Protocol, LocalFace>& face, const Packet& packet) |
| 153 | { |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 154 | bool wasQueueEmpty = face.m_sendQueue.empty(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 156 | if (!face.isEmptyFilteredLocalControlHeader(packet.getLocalControlHeader())) |
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 | face.m_sendQueue.push(face.filterAndEncodeLocalControlHeader(packet)); |
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(packet.wireEncode()); |
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 | if (wasQueueEmpty) |
| 163 | face.sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 164 | } |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | template<class T, class U> |
| 169 | inline void |
| 170 | StreamFace<T, U>::sendInterest(const Interest& interest) |
| 171 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 172 | NFD_LOG_FACE_TRACE(__func__); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 173 | this->emitSignal(onSendInterest, interest); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 174 | StreamFaceSenderImpl<T, U, Interest>::send(*this, interest); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 177 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 178 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 179 | StreamFace<T, U>::sendData(const Data& data) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 180 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 181 | NFD_LOG_FACE_TRACE(__func__); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 182 | this->emitSignal(onSendData, data); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 183 | StreamFaceSenderImpl<T, U, Data>::send(*this, data); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 186 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 187 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 188 | StreamFace<T, U>::close() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 189 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 190 | if (!m_socket.is_open()) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 191 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 192 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 193 | NFD_LOG_FACE_INFO("Closing face"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 194 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 195 | shutdownSocket(); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 196 | this->fail("Face closed"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 197 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 198 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 199 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 200 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 201 | StreamFace<T, U>::processErrorCode(const boost::system::error_code& error) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 202 | { |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 203 | if (error == boost::asio::error::operation_aborted || // when cancel() is called |
| 204 | error == boost::asio::error::shut_down) // after shutdown() is called |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 205 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 206 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 207 | if (!m_socket.is_open()) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 208 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 209 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 213 | if (error != boost::asio::error::eof) |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 214 | NFD_LOG_FACE_WARN("Send or receive operation failed: " << error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 215 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 216 | shutdownSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 217 | |
| 218 | if (error == boost::asio::error::eof) |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 219 | this->fail("Connection closed"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 220 | else |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 221 | this->fail(error.message()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 224 | template<class T, class U> |
| 225 | inline void |
| 226 | StreamFace<T, U>::sendFromQueue() |
| 227 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 228 | boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()), |
| 229 | bind(&StreamFace<T, U>::handleSend, this, |
| 230 | boost::asio::placeholders::error, |
| 231 | boost::asio::placeholders::bytes_transferred)); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 232 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 233 | |
| 234 | template<class T, class U> |
| 235 | inline void |
| 236 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 237 | size_t nBytesSent) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 238 | { |
| 239 | if (error) |
| 240 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 241 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 242 | BOOST_ASSERT(!m_sendQueue.empty()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 243 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 244 | NFD_LOG_FACE_TRACE("Successfully sent: " << nBytesSent << " bytes"); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 245 | this->getMutableCounters().getNOutBytes() += nBytesSent; |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 246 | |
| 247 | m_sendQueue.pop(); |
| 248 | if (!m_sendQueue.empty()) |
| 249 | sendFromQueue(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | template<class T, class U> |
| 253 | inline void |
| 254 | StreamFace<T, U>::handleReceive(const boost::system::error_code& error, |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 255 | size_t nBytesReceived) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 256 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 257 | if (error) |
| 258 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 259 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 260 | NFD_LOG_FACE_TRACE("Received: " << nBytesReceived << " bytes"); |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 261 | this->getMutableCounters().getNInBytes() += nBytesReceived; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 262 | |
Alexander Afanasyev | e5966b7 | 2014-07-20 23:39:50 -0700 | [diff] [blame] | 263 | m_inputBufferSize += nBytesReceived; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 264 | |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 265 | size_t offset = 0; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 266 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 267 | bool isOk = true; |
| 268 | Block element; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 269 | while (m_inputBufferSize - offset > 0) { |
| 270 | std::tie(isOk, element) = Block::fromBuffer(m_inputBuffer + offset, m_inputBufferSize - offset); |
| 271 | if (!isOk) |
| 272 | break; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 273 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 274 | offset += element.size(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 275 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 276 | BOOST_ASSERT(offset <= m_inputBufferSize); |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 277 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 278 | if (!this->decodeAndDispatchInput(element)) { |
| 279 | NFD_LOG_FACE_WARN("Received unrecognized TLV block of type " << element.type()); |
| 280 | // ignore unknown packet and proceed |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 281 | } |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 284 | if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 285 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 286 | 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] | 287 | shutdownSocket(); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 288 | 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] | 289 | return; |
| 290 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 291 | |
| 292 | if (offset > 0) |
| 293 | { |
| 294 | if (offset != m_inputBufferSize) |
| 295 | { |
| 296 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 297 | m_inputBuffer); |
| 298 | m_inputBufferSize -= offset; |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | m_inputBufferSize = 0; |
| 303 | } |
| 304 | } |
| 305 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 306 | m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 307 | ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize), |
| 308 | bind(&StreamFace<T, U>::handleReceive, this, |
| 309 | boost::asio::placeholders::error, |
| 310 | boost::asio::placeholders::bytes_transferred)); |
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; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 323 | m_socket.cancel(error); |
| 324 | m_socket.shutdown(protocol::socket::shutdown_both, error); |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 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), |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 334 | // m_socket.shutdown() does not trigger the cancellation of the handleSend callback. |
Davide Pesavento | 3f5655f | 2014-08-30 21:38:59 +0200 | [diff] [blame] | 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; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 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 |