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