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 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 169 | this->onSendInterest(interest); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 170 | StreamFaceSenderImpl<T, U, Interest>::send(*this, interest); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 173 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 174 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 175 | StreamFace<T, U>::sendData(const Data& data) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 177 | this->onSendData(data); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 178 | StreamFaceSenderImpl<T, U, Data>::send(*this, data); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 181 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 182 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 183 | StreamFace<T, U>::close() |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 184 | { |
| 185 | if (!m_socket->is_open()) |
| 186 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 188 | NFD_LOG_INFO("[id:" << this->getId() |
| 189 | << ",endpoint:" << m_socket->local_endpoint() |
| 190 | << "] Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 191 | |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 192 | closeSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 193 | this->onFail("Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 194 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 195 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 196 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 197 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 198 | StreamFace<T, U>::processErrorCode(const boost::system::error_code& error) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 199 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 200 | 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] | 201 | return; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 202 | |
| 203 | if (!m_socket->is_open()) |
| 204 | { |
| 205 | this->onFail("Connection closed"); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | if (error == boost::asio::error::eof) |
| 210 | { |
| 211 | NFD_LOG_INFO("[id:" << this->getId() |
| 212 | << ",endpoint:" << m_socket->local_endpoint() |
| 213 | << "] Connection closed"); |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | NFD_LOG_WARN("[id:" << this->getId() |
| 218 | << ",endpoint:" << m_socket->local_endpoint() |
| 219 | << "] Send or receive operation failed, closing socket: " |
| 220 | << error.category().message(error.value())); |
| 221 | } |
| 222 | |
| 223 | closeSocket(); |
| 224 | |
| 225 | if (error == boost::asio::error::eof) |
| 226 | { |
| 227 | this->onFail("Connection closed"); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | this->onFail("Send or receive operation failed, closing socket: " + |
| 232 | error.category().message(error.value())); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | template<class T, class U> |
| 238 | inline void |
| 239 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
| 240 | const Block& wire) |
| 241 | { |
| 242 | if (error) |
| 243 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 244 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 245 | NFD_LOG_TRACE("[id:" << this->getId() |
| 246 | << ",endpoint:" << m_socket->local_endpoint() |
| 247 | << "] Successfully sent: " << wire.size() << " bytes"); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 250 | template<class T, class U> |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 251 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 252 | StreamFace<T, U>::handleSend(const boost::system::error_code& error, |
| 253 | const Block& header, const Block& payload) |
| 254 | { |
| 255 | if (error) |
| 256 | return processErrorCode(error); |
| 257 | |
| 258 | NFD_LOG_TRACE("[id:" << this->getId() |
| 259 | << ",endpoint:" << m_socket->local_endpoint() |
| 260 | << "] Successfully sent: " << (header.size()+payload.size()) << " bytes"); |
| 261 | } |
| 262 | |
| 263 | template<class T, class U> |
| 264 | inline void |
| 265 | StreamFace<T, U>::handleReceive(const boost::system::error_code& error, |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 266 | std::size_t bytes_recvd) |
| 267 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 268 | if (error) |
| 269 | return processErrorCode(error); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 270 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 271 | NFD_LOG_TRACE("[id:" << this->getId() |
| 272 | << ",endpoint:" << m_socket->local_endpoint() |
| 273 | << "] Received: " << bytes_recvd << " bytes"); |
| 274 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 275 | m_inputBufferSize += bytes_recvd; |
| 276 | // do magic |
| 277 | |
| 278 | std::size_t offset = 0; |
| 279 | /// @todo Eliminate reliance on exceptions in this path |
| 280 | try { |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 281 | while(m_inputBufferSize - offset > 0) |
| 282 | { |
| 283 | Block element(m_inputBuffer + offset, m_inputBufferSize - offset); |
| 284 | offset += element.size(); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 285 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 286 | BOOST_ASSERT(offset <= m_inputBufferSize); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 287 | |
| 288 | if (!this->decodeAndDispatchInput(element)) |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 289 | { |
| 290 | NFD_LOG_WARN("[id:" << this->getId() |
| 291 | << ",endpoint:" << m_socket->local_endpoint() |
| 292 | << "] Received unrecognized block of type [" |
| 293 | << element.type() << "]"); |
| 294 | // ignore unknown packet and proceed |
| 295 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 298 | catch(const tlv::Error& e) { |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 299 | if (m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
| 300 | { |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 301 | NFD_LOG_WARN("[id:" << this->getId() |
| 302 | << ",endpoint:" << m_socket->local_endpoint() |
| 303 | << "] Received input is invalid or too large to process, " |
| 304 | << "closing down the face"); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 305 | |
| 306 | closeSocket(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 307 | 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] | 308 | return; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (offset > 0) |
| 313 | { |
| 314 | if (offset != m_inputBufferSize) |
| 315 | { |
| 316 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 317 | m_inputBuffer); |
| 318 | m_inputBufferSize -= offset; |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | m_inputBufferSize = 0; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 327 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 328 | bind(&StreamFace<T, U>::handleReceive, this, _1, _2)); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 331 | template<class T, class U> |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 332 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 333 | StreamFace<T, U>::keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face) |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 334 | { |
| 335 | } |
| 336 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 337 | template<class T, class U> |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 338 | inline void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 339 | StreamFace<T, U>::closeSocket() |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 340 | { |
| 341 | boost::asio::io_service& io = m_socket->get_io_service(); |
| 342 | |
| 343 | // use the non-throwing variants and ignore errors, if any |
| 344 | boost::system::error_code error; |
| 345 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 346 | m_socket->close(error); |
| 347 | // after this, handlers will be called with an error code |
| 348 | |
| 349 | // ensure that the Face object is alive at least until all pending |
| 350 | // handlers are dispatched |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 351 | io.post(bind(&StreamFace<T, U>::keepFaceAliveUntilAllHandlersExecuted, |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 352 | this, this->shared_from_this())); |
| 353 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 354 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 355 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 356 | |
| 357 | #endif // NFD_FACE_STREAM_FACE_HPP |