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 | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 25 | #ifndef NFD_DAEMON_FACE_STREAM_FACE_HPP |
| 26 | #define NFD_DAEMON_FACE_STREAM_FACE_HPP |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 27 | |
| 28 | #include "face.hpp" |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 29 | #include "local-face.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 30 | #include "core/logger.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 32 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 34 | // forward declaration |
| 35 | template<class T, class U, class V> struct StreamFaceSenderImpl; |
| 36 | |
| 37 | template<class Protocol, class FaceBase = Face> |
| 38 | class StreamFace : public FaceBase |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 41 | typedef Protocol protocol; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 43 | /** |
| 44 | * \brief Create instance of StreamFace |
| 45 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 46 | StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 47 | const shared_ptr<typename protocol::socket>& socket, |
| 48 | bool isOnDemand); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 50 | virtual |
| 51 | ~StreamFace(); |
| 52 | |
| 53 | // from Face |
| 54 | virtual void |
| 55 | sendInterest(const Interest& interest); |
| 56 | |
| 57 | virtual void |
| 58 | sendData(const Data& data); |
| 59 | |
| 60 | virtual void |
| 61 | close(); |
Alexander Afanasyev | 93ce75e | 2014-02-18 19:45:34 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 63 | protected: |
| 64 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 65 | processErrorCode(const boost::system::error_code& error); |
| 66 | |
| 67 | void |
| 68 | handleSend(const boost::system::error_code& error, |
| 69 | const Block& header, const Block& payload); |
| 70 | void |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 71 | handleSend(const boost::system::error_code& error, |
| 72 | const Block& wire); |
| 73 | |
| 74 | void |
| 75 | handleReceive(const boost::system::error_code& error, |
| 76 | std::size_t bytes_recvd); |
| 77 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 78 | void |
| 79 | keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 80 | |
| 81 | void |
| 82 | closeSocket(); |
| 83 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 84 | protected: |
| 85 | shared_ptr<typename protocol::socket> m_socket; |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 86 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 87 | private: |
| 88 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
| 89 | std::size_t m_inputBufferSize; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 90 | |
| 91 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Interest>; |
| 92 | friend struct StreamFaceSenderImpl<Protocol, FaceBase, Data>; |
| 93 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 94 | NFD_LOG_INCLASS_DECLARE(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 97 | // All inherited classes must use |
| 98 | // NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name"); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 100 | |
| 101 | /** \brief Class allowing validation of the StreamFace use |
| 102 | * |
| 103 | * For example, partial specialization based on boost::asio::ip::tcp should check |
| 104 | * that local endpoint is loopback |
| 105 | * |
| 106 | * @throws Face::Error if validation failed |
| 107 | */ |
| 108 | template<class Protocol, class U> |
| 109 | struct StreamFaceValidator |
| 110 | { |
| 111 | static void |
| 112 | validateSocket(typename Protocol::socket& socket) |
| 113 | { |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 118 | template<class T, class FaceBase> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 119 | inline |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 120 | StreamFace<T, FaceBase>::StreamFace(const FaceUri& remoteUri, const FaceUri& localUri, |
| 121 | const shared_ptr<typename StreamFace::protocol::socket>& socket, |
| 122 | bool isOnDemand) |
| 123 | : FaceBase(remoteUri, localUri) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 124 | , m_socket(socket) |
Alexander Afanasyev | b9f6e43 | 2014-02-14 20:52:49 -0800 | [diff] [blame] | 125 | , m_inputBufferSize(0) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 126 | { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 127 | FaceBase::setOnDemand(isOnDemand); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 128 | StreamFaceValidator<T, FaceBase>::validateSocket(*socket); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 129 | 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] | 130 | bind(&StreamFace<T, FaceBase>::handleReceive, this, _1, _2)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 133 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 134 | inline |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 135 | StreamFace<T, U>::~StreamFace() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 136 | { |
| 137 | } |
| 138 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 139 | template<class Protocol, class FaceBase, class Packet> |
| 140 | struct StreamFaceSenderImpl |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 142 | static void |
| 143 | send(StreamFace<Protocol, FaceBase>& face, const Packet& packet) |
| 144 | { |
| 145 | face.m_socket->async_send(boost::asio::buffer(packet.wireEncode().wire(), |
| 146 | packet.wireEncode().size()), |
| 147 | bind(&StreamFace<Protocol, FaceBase>::handleSend, |
| 148 | &face, _1, packet.wireEncode())); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | // partial specialization (only classes can be partially specialized) |
| 153 | template<class Protocol, class Packet> |
| 154 | struct StreamFaceSenderImpl<Protocol, LocalFace, Packet> |
| 155 | { |
| 156 | static void |
| 157 | send(StreamFace<Protocol, LocalFace>& face, const Packet& packet) |
| 158 | { |
| 159 | using namespace boost::asio; |
| 160 | |
| 161 | if (face.isEmptyFilteredLocalControlHeader(packet.getLocalControlHeader())) |
| 162 | { |
| 163 | const Block& payload = packet.wireEncode(); |
| 164 | face.m_socket->async_send(buffer(payload.wire(), payload.size()), |
| 165 | bind(&StreamFace<Protocol, LocalFace>::handleSend, |
| 166 | &face, _1, packet.wireEncode())); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | Block header = face.filterAndEncodeLocalControlHeader(packet); |
| 171 | const Block& payload = packet.wireEncode(); |
| 172 | |
| 173 | std::vector<const_buffer> buffers; |
| 174 | buffers.reserve(2); |
| 175 | buffers.push_back(buffer(header.wire(), header.size())); |
| 176 | buffers.push_back(buffer(payload.wire(), payload.size())); |
| 177 | |
| 178 | face.m_socket->async_send(buffers, |
| 179 | bind(&StreamFace<Protocol, LocalFace>::handleSend, |
| 180 | &face, _1, header, payload)); |
| 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | |
| 186 | template<class T, class U> |
| 187 | inline void |
| 188 | StreamFace<T, U>::sendInterest(const Interest& interest) |
| 189 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 190 | this->onSendInterest(interest); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 191 | StreamFaceSenderImpl<T, U, Interest>::send(*this, interest); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 194 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 195 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 196 | StreamFace<T, U>::sendData(const Data& data) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 197 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 198 | this->onSendData(data); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 199 | StreamFaceSenderImpl<T, U, Data>::send(*this, data); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 202 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 203 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 204 | StreamFace<T, U>::close() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 205 | { |
| 206 | if (!m_socket->is_open()) |
| 207 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 208 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 209 | NFD_LOG_INFO("[id:" << this->getId() |
| 210 | << ",endpoint:" << m_socket->local_endpoint() |
| 211 | << "] Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 212 | |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 213 | closeSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 214 | this->onFail("Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 215 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 217 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 218 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 219 | StreamFace<T, U>::processErrorCode(const boost::system::error_code& error) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 220 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 221 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 222 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 223 | |
| 224 | if (!m_socket->is_open()) |
| 225 | { |
| 226 | this->onFail("Connection closed"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | if (error == boost::asio::error::eof) |
| 231 | { |
| 232 | NFD_LOG_INFO("[id:" << this->getId() |
| 233 | << ",endpoint:" << m_socket->local_endpoint() |
| 234 | << "] Connection closed"); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | NFD_LOG_WARN("[id:" << this->getId() |
| 239 | << ",endpoint:" << m_socket->local_endpoint() |
| 240 | << "] Send or receive operation failed, closing socket: " |
| 241 | << error.category().message(error.value())); |
| 242 | } |
| 243 | |
| 244 | closeSocket(); |
| 245 | |
| 246 | if (error == boost::asio::error::eof) |
| 247 | { |
| 248 | this->onFail("Connection closed"); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | this->onFail("Send or receive operation failed, closing socket: " + |
| 253 | error.category().message(error.value())); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | template<class T, class U> |
| 259 | inline void |
| 260 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
| 261 | const Block& wire) |
| 262 | { |
| 263 | if (error) |
| 264 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 265 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 266 | NFD_LOG_TRACE("[id:" << this->getId() |
| 267 | << ",endpoint:" << m_socket->local_endpoint() |
| 268 | << "] Successfully sent: " << wire.size() << " bytes"); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 271 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 272 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 273 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
| 274 | const Block& header, const Block& payload) |
| 275 | { |
| 276 | if (error) |
| 277 | return processErrorCode(error); |
| 278 | |
| 279 | NFD_LOG_TRACE("[id:" << this->getId() |
| 280 | << ",endpoint:" << m_socket->local_endpoint() |
| 281 | << "] Successfully sent: " << (header.size()+payload.size()) << " bytes"); |
| 282 | } |
| 283 | |
| 284 | template<class T, class U> |
| 285 | inline void |
| 286 | StreamFace<T, U>::handleReceive(const boost::system::error_code& error, |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 287 | std::size_t bytes_recvd) |
| 288 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 289 | if (error) |
| 290 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 291 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 292 | NFD_LOG_TRACE("[id:" << this->getId() |
| 293 | << ",endpoint:" << m_socket->local_endpoint() |
| 294 | << "] Received: " << bytes_recvd << " bytes"); |
| 295 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 296 | m_inputBufferSize += bytes_recvd; |
| 297 | // do magic |
| 298 | |
| 299 | std::size_t offset = 0; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 300 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 301 | bool isOk = true; |
| 302 | Block element; |
| 303 | while(m_inputBufferSize - offset > 0) |
| 304 | { |
| 305 | isOk = Block::fromBuffer(m_inputBuffer + offset, m_inputBufferSize - offset, element); |
| 306 | if (!isOk) |
| 307 | break; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 308 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 309 | offset += element.size(); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 310 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 311 | BOOST_ASSERT(offset <= m_inputBufferSize); |
| 312 | |
| 313 | if (!this->decodeAndDispatchInput(element)) |
| 314 | { |
| 315 | NFD_LOG_WARN("[id:" << this->getId() |
| 316 | << ",endpoint:" << m_socket->local_endpoint() |
| 317 | << "] Received unrecognized block of type [" |
| 318 | << element.type() << "]"); |
| 319 | // ignore unknown packet and proceed |
| 320 | } |
| 321 | } |
| 322 | if (!isOk && m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
| 323 | { |
| 324 | NFD_LOG_WARN("[id:" << this->getId() |
| 325 | << ",endpoint:" << m_socket->local_endpoint() |
| 326 | << "] Failed to parse incoming packet or it is too large to process, " |
| 327 | << "closing down the face"); |
| 328 | |
| 329 | closeSocket(); |
| 330 | this->onFail("Failed to parse incoming packet or it is too large to process, " |
| 331 | "closing down the face"); |
| 332 | return; |
| 333 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 334 | |
| 335 | if (offset > 0) |
| 336 | { |
| 337 | if (offset != m_inputBufferSize) |
| 338 | { |
| 339 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 340 | m_inputBuffer); |
| 341 | m_inputBufferSize -= offset; |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | m_inputBufferSize = 0; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 350 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 351 | bind(&StreamFace<T, U>::handleReceive, this, _1, _2)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 354 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 355 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 356 | StreamFace<T, U>::keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 357 | { |
| 358 | } |
| 359 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 360 | template<class T, class U> |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 361 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 362 | StreamFace<T, U>::closeSocket() |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 363 | { |
| 364 | boost::asio::io_service& io = m_socket->get_io_service(); |
| 365 | |
| 366 | // use the non-throwing variants and ignore errors, if any |
| 367 | boost::system::error_code error; |
| 368 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 369 | m_socket->close(error); |
| 370 | // after this, handlers will be called with an error code |
| 371 | |
| 372 | // ensure that the Face object is alive at least until all pending |
| 373 | // handlers are dispatched |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 374 | io.post(bind(&StreamFace<T, U>::keepFaceAliveUntilAllHandlersExecuted, |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 375 | this, this->shared_from_this())); |
| 376 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 377 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 378 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 379 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 380 | #endif // NFD_DAEMON_FACE_STREAM_FACE_HPP |