blob: d08c1d582a3214a61e2df086aaadf1b60d09898a [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi5dd26c32014-07-20 23:15:14 -070024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_STREAM_FACE_HPP
27#define NFD_DAEMON_FACE_STREAM_FACE_HPP
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080028
29#include "face.hpp"
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080030#include "local-face.hpp"
Davide Pesavento66ff0982015-01-29 22:39:00 +010031#include "core/global-io.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080034
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080035// forward declaration
36template<class T, class U, class V> struct StreamFaceSenderImpl;
37
38template<class Protocol, class FaceBase = Face>
39class StreamFace : public FaceBase
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080040{
41public:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080042 typedef Protocol protocol;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080043
Junxiao Shi79494162014-04-02 18:25:11 -070044 StreamFace(const FaceUri& remoteUri, const FaceUri& localUri,
Davide Pesavento292e5e12015-03-13 02:08:33 +010045 typename protocol::socket socket, bool isOnDemand);
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080046
Davide Pesavento66ff0982015-01-29 22:39:00 +010047 // from FaceBase
48 void
49 sendInterest(const Interest& interest) DECL_OVERRIDE;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080050
Davide Pesavento66ff0982015-01-29 22:39:00 +010051 void
52 sendData(const Data& data) DECL_OVERRIDE;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080053
Davide Pesavento66ff0982015-01-29 22:39:00 +010054 void
55 close() DECL_OVERRIDE;
Alexander Afanasyev93ce75e2014-02-18 19:45:34 -080056
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080057protected:
58 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080059 processErrorCode(const boost::system::error_code& error);
60
61 void
Alexander Afanasyeve5966b72014-07-20 23:39:50 -070062 sendFromQueue();
63
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080064 void
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080065 handleSend(const boost::system::error_code& error,
Junxiao Shi5dd26c32014-07-20 23:15:14 -070066 size_t nBytesSent);
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080067
68 void
69 handleReceive(const boost::system::error_code& error,
Junxiao Shi5dd26c32014-07-20 23:15:14 -070070 size_t nBytesReceived);
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080071
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080072 void
Davide Pesavento3f5655f2014-08-30 21:38:59 +020073 shutdownSocket();
Davide Pesaventoba558e72014-02-17 18:38:19 +010074
75 void
Davide Pesavento3f5655f2014-08-30 21:38:59 +020076 deferredClose(const shared_ptr<Face>& face);
Davide Pesaventoba558e72014-02-17 18:38:19 +010077
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080078protected:
Davide Pesavento292e5e12015-03-13 02:08:33 +010079 typename protocol::socket m_socket;
Davide Pesaventoba558e72014-02-17 18:38:19 +010080
Davide Pesaventobe40fb12015-02-23 21:09:34 +010081 NFD_LOG_INCLASS_DECLARE();
82
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080083private:
Junxiao Shi39cd6332014-11-06 21:53:18 -070084 uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE];
Junxiao Shi5dd26c32014-07-20 23:15:14 -070085 size_t m_inputBufferSize;
Alexander Afanasyeve5966b72014-07-20 23:39:50 -070086 std::queue<Block> m_sendQueue;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080087
88 friend struct StreamFaceSenderImpl<Protocol, FaceBase, Interest>;
89 friend struct StreamFaceSenderImpl<Protocol, FaceBase, Data>;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080090};
91
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080092// All inherited classes must use
93// NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name");
Alexander Afanasyev3958b012014-01-31 15:06:13 -080094
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080095
96/** \brief Class allowing validation of the StreamFace use
97 *
98 * For example, partial specialization based on boost::asio::ip::tcp should check
99 * that local endpoint is loopback
100 *
101 * @throws Face::Error if validation failed
102 */
103template<class Protocol, class U>
104struct StreamFaceValidator
105{
106 static void
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100107 validateSocket(const typename Protocol::socket& socket)
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800108 {
109 }
110};
111
112
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000113template<class T, class FaceBase>
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800114inline
Junxiao Shi79494162014-04-02 18:25:11 -0700115StreamFace<T, FaceBase>::StreamFace(const FaceUri& remoteUri, const FaceUri& localUri,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100116 typename StreamFace::protocol::socket socket, bool isOnDemand)
Junxiao Shi79494162014-04-02 18:25:11 -0700117 : FaceBase(remoteUri, localUri)
Davide Pesavento292e5e12015-03-13 02:08:33 +0100118 , m_socket(std::move(socket))
Alexander Afanasyevb9f6e432014-02-14 20:52:49 -0800119 , m_inputBufferSize(0)
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800120{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100121 NFD_LOG_FACE_INFO("Creating face");
122
Yukai Tu731f0d72015-07-04 11:14:44 +0800123 this->setPersistency(isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND : ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100124 StreamFaceValidator<T, FaceBase>::validateSocket(m_socket);
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100125
Davide Pesavento292e5e12015-03-13 02:08:33 +0100126 m_socket.async_receive(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
127 bind(&StreamFace<T, FaceBase>::handleReceive, this,
128 boost::asio::placeholders::error,
129 boost::asio::placeholders::bytes_transferred));
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800130}
131
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200132
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800133template<class Protocol, class FaceBase, class Packet>
134struct StreamFaceSenderImpl
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800135{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800136 static void
137 send(StreamFace<Protocol, FaceBase>& face, const Packet& packet)
138 {
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700139 bool wasQueueEmpty = face.m_sendQueue.empty();
140 face.m_sendQueue.push(packet.wireEncode());
141
142 if (wasQueueEmpty)
143 face.sendFromQueue();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800144 }
145};
146
147// partial specialization (only classes can be partially specialized)
148template<class Protocol, class Packet>
149struct StreamFaceSenderImpl<Protocol, LocalFace, Packet>
150{
151 static void
152 send(StreamFace<Protocol, LocalFace>& face, const Packet& packet)
153 {
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700154 bool wasQueueEmpty = face.m_sendQueue.empty();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800155
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700156 if (!face.isEmptyFilteredLocalControlHeader(packet.getLocalControlHeader()))
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800157 {
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700158 face.m_sendQueue.push(face.filterAndEncodeLocalControlHeader(packet));
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800159 }
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700160 face.m_sendQueue.push(packet.wireEncode());
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800161
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700162 if (wasQueueEmpty)
163 face.sendFromQueue();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800164 }
165};
166
167
168template<class T, class U>
169inline void
170StreamFace<T, U>::sendInterest(const Interest& interest)
171{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100172 NFD_LOG_FACE_TRACE(__func__);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700173 this->emitSignal(onSendInterest, interest);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800174 StreamFaceSenderImpl<T, U, Interest>::send(*this, interest);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800175}
176
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800177template<class T, class U>
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800178inline void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800179StreamFace<T, U>::sendData(const Data& data)
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800180{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100181 NFD_LOG_FACE_TRACE(__func__);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700182 this->emitSignal(onSendData, data);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800183 StreamFaceSenderImpl<T, U, Data>::send(*this, data);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800184}
185
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800186template<class T, class U>
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800187inline void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800188StreamFace<T, U>::close()
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800189{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100190 if (!m_socket.is_open())
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800191 return;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800192
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100193 NFD_LOG_FACE_INFO("Closing face");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800194
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200195 shutdownSocket();
Davide Pesavento66ff0982015-01-29 22:39:00 +0100196 this->fail("Face closed");
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800197}
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800198
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800199template<class T, class U>
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800200inline void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800201StreamFace<T, U>::processErrorCode(const boost::system::error_code& error)
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800202{
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200203 if (error == boost::asio::error::operation_aborted || // when cancel() is called
204 error == boost::asio::error::shut_down) // after shutdown() is called
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800205 return;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800206
Davide Pesavento292e5e12015-03-13 02:08:33 +0100207 if (!m_socket.is_open())
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800208 {
Junxiao Shi08d07a72014-06-09 23:17:57 -0700209 this->fail("Connection closed");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800210 return;
211 }
212
Davide Pesavento66ff0982015-01-29 22:39:00 +0100213 if (error != boost::asio::error::eof)
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100214 NFD_LOG_FACE_WARN("Send or receive operation failed: " << error.message());
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800215
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200216 shutdownSocket();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800217
218 if (error == boost::asio::error::eof)
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100219 this->fail("Connection closed");
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800220 else
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100221 this->fail(error.message());
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800222}
223
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200224template<class T, class U>
225inline void
226StreamFace<T, U>::sendFromQueue()
227{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100228 boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()),
229 bind(&StreamFace<T, U>::handleSend, this,
230 boost::asio::placeholders::error,
231 boost::asio::placeholders::bytes_transferred));
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200232}
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800233
234template<class T, class U>
235inline void
236StreamFace<T, U>::handleSend(const boost::system::error_code& error,
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700237 size_t nBytesSent)
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800238{
239 if (error)
240 return processErrorCode(error);
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800241
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700242 BOOST_ASSERT(!m_sendQueue.empty());
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800243
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100244 NFD_LOG_FACE_TRACE("Successfully sent: " << nBytesSent << " bytes");
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700245 this->getMutableCounters().getNOutBytes() += nBytesSent;
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700246
247 m_sendQueue.pop();
248 if (!m_sendQueue.empty())
249 sendFromQueue();
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800250}
251
252template<class T, class U>
253inline void
254StreamFace<T, U>::handleReceive(const boost::system::error_code& error,
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700255 size_t nBytesReceived)
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800256{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800257 if (error)
258 return processErrorCode(error);
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800259
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100260 NFD_LOG_FACE_TRACE("Received: " << nBytesReceived << " bytes");
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700261 this->getMutableCounters().getNInBytes() += nBytesReceived;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800262
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700263 m_inputBufferSize += nBytesReceived;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800264
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700265 size_t offset = 0;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800266
Alexander Afanasyev5a8d8d82014-03-21 14:08:41 -0700267 bool isOk = true;
268 Block element;
Junxiao Shi78926c92015-02-28 22:56:06 -0700269 while (m_inputBufferSize - offset > 0) {
270 std::tie(isOk, element) = Block::fromBuffer(m_inputBuffer + offset, m_inputBufferSize - offset);
271 if (!isOk)
272 break;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800273
Junxiao Shi78926c92015-02-28 22:56:06 -0700274 offset += element.size();
Davide Pesaventoba558e72014-02-17 18:38:19 +0100275
Junxiao Shi78926c92015-02-28 22:56:06 -0700276 BOOST_ASSERT(offset <= m_inputBufferSize);
Alexander Afanasyev5a8d8d82014-03-21 14:08:41 -0700277
Junxiao Shi78926c92015-02-28 22:56:06 -0700278 if (!this->decodeAndDispatchInput(element)) {
279 NFD_LOG_FACE_WARN("Received unrecognized TLV block of type " << element.type());
280 // ignore unknown packet and proceed
Alexander Afanasyev5a8d8d82014-03-21 14:08:41 -0700281 }
Junxiao Shi78926c92015-02-28 22:56:06 -0700282 }
283
Junxiao Shi39cd6332014-11-06 21:53:18 -0700284 if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0)
Alexander Afanasyev5a8d8d82014-03-21 14:08:41 -0700285 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100286 NFD_LOG_FACE_WARN("Failed to parse incoming packet or packet too large to process");
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200287 shutdownSocket();
Davide Pesavento66ff0982015-01-29 22:39:00 +0100288 this->fail("Failed to parse incoming packet or packet too large to process");
Alexander Afanasyev5a8d8d82014-03-21 14:08:41 -0700289 return;
290 }
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800291
292 if (offset > 0)
293 {
294 if (offset != m_inputBufferSize)
295 {
296 std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize,
297 m_inputBuffer);
298 m_inputBufferSize -= offset;
299 }
300 else
301 {
302 m_inputBufferSize = 0;
303 }
304 }
305
Davide Pesavento292e5e12015-03-13 02:08:33 +0100306 m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize,
307 ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize),
308 bind(&StreamFace<T, U>::handleReceive, this,
309 boost::asio::placeholders::error,
310 boost::asio::placeholders::bytes_transferred));
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800311}
312
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800313template<class T, class U>
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800314inline void
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200315StreamFace<T, U>::shutdownSocket()
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800316{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100317 NFD_LOG_FACE_TRACE(__func__);
318
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200319 // Cancel all outstanding operations and shutdown the socket
320 // so that no further sends or receives are possible.
321 // Use the non-throwing variants and ignore errors, if any.
322 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100323 m_socket.cancel(error);
324 m_socket.shutdown(protocol::socket::shutdown_both, error);
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200325
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200326 // ensure that the Face object is alive at least until all pending
327 // handlers are dispatched
Davide Pesavento66ff0982015-01-29 22:39:00 +0100328 getGlobalIoService().post(bind(&StreamFace<T, U>::deferredClose,
329 this, this->shared_from_this()));
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200330
331 // Some bug or feature of Boost.Asio (see http://redmine.named-data.net/issues/1856):
332 //
333 // When shutdownSocket is called from within a socket event (e.g., from handleReceive),
Davide Pesavento292e5e12015-03-13 02:08:33 +0100334 // m_socket.shutdown() does not trigger the cancellation of the handleSend callback.
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200335 // Instead, handleSend is invoked as nothing bad happened.
336 //
337 // In order to prevent the assertion in handleSend from failing, we clear the queue
338 // and close the socket in deferredClose, i.e., after all callbacks scheduled up to
339 // this point have been executed. If more send operations are scheduled after this
340 // point, they will fail because the socket has been shutdown, and their callbacks
341 // will be invoked with error code == asio::error::shut_down.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800342}
343
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800344template<class T, class U>
Davide Pesaventoba558e72014-02-17 18:38:19 +0100345inline void
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200346StreamFace<T, U>::deferredClose(const shared_ptr<Face>& face)
Davide Pesaventoba558e72014-02-17 18:38:19 +0100347{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100348 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyeve5966b72014-07-20 23:39:50 -0700349
350 // clear send queue
351 std::queue<Block> emptyQueue;
352 std::swap(emptyQueue, m_sendQueue);
Davide Pesavento3f5655f2014-08-30 21:38:59 +0200353
354 // use the non-throwing variant and ignore errors, if any
355 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100356 m_socket.close(error);
Davide Pesaventoba558e72014-02-17 18:38:19 +0100357}
Alexander Afanasyevd32cb962014-01-28 12:43:47 -0800358
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800359} // namespace nfd
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800360
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700361#endif // NFD_DAEMON_FACE_STREAM_FACE_HPP