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