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