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