Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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/>. |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_DATAGRAM_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_DATAGRAM_FACE_HPP |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 28 | |
| 29 | #include "face.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 30 | #include "core/logger.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 34 | class Unicast {}; |
| 35 | class Multicast {}; |
| 36 | |
| 37 | template<class Protocol, class Type = Unicast> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 38 | class DatagramFace : public Face |
| 39 | { |
| 40 | public: |
Davide Pesavento | d8d4d98 | 2014-03-21 18:47:58 +0100 | [diff] [blame] | 41 | typedef Protocol protocol; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 42 | |
| 43 | /** \brief Construct datagram face |
| 44 | * |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 45 | * \param socket Protocol-specific socket for the created face |
| 46 | * \param isOnDemand If true, the face can be closed after it remains |
Davide Pesavento | d8d4d98 | 2014-03-21 18:47:58 +0100 | [diff] [blame] | 47 | * unused for a certain amount of time |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 48 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 49 | DatagramFace(const FaceUri& remoteUri, const FaceUri& localUri, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 50 | const shared_ptr<typename protocol::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 51 | bool isOnDemand); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 52 | |
| 53 | virtual |
| 54 | ~DatagramFace(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 55 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 56 | // from Face |
| 57 | virtual void |
| 58 | sendInterest(const Interest& interest); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 59 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 60 | virtual void |
| 61 | sendData(const Data& data); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 62 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 63 | virtual void |
| 64 | close(); |
| 65 | |
| 66 | void |
| 67 | handleSend(const boost::system::error_code& error, |
| 68 | const Block& wire); |
| 69 | |
| 70 | void |
| 71 | handleReceive(const boost::system::error_code& error, |
| 72 | size_t nBytesReceived); |
| 73 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 74 | /** |
| 75 | * \brief Set m_hasBeenUsedRecently to false |
| 76 | */ |
| 77 | void |
| 78 | resetRecentUsage(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 79 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 80 | bool |
| 81 | hasBeenUsedRecently() const; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 82 | |
| 83 | void |
| 84 | setOnDemand(bool isOnDemand); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 85 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 86 | protected: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 87 | void |
| 88 | receiveDatagram(const uint8_t* buffer, |
| 89 | size_t nBytesReceived, |
| 90 | const boost::system::error_code& error); |
| 91 | |
| 92 | void |
| 93 | keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 94 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 95 | void |
| 96 | closeSocket(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 97 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 98 | protected: |
| 99 | shared_ptr<typename protocol::socket> m_socket; |
| 100 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 101 | bool m_hasBeenUsedRecently; |
| 102 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 103 | NFD_LOG_INCLASS_DECLARE(); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 106 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 107 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 108 | inline |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 109 | DatagramFace<T, U>::DatagramFace(const FaceUri& remoteUri, const FaceUri& localUri, |
| 110 | const shared_ptr<typename DatagramFace::protocol::socket>& socket, |
| 111 | bool isOnDemand) |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 112 | : Face(remoteUri, localUri) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 113 | , m_socket(socket) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 114 | { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 115 | setOnDemand(isOnDemand); |
| 116 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 117 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 118 | bind(&DatagramFace<T, U>::handleReceive, this, _1, _2)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 121 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 122 | inline |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 123 | DatagramFace<T, U>::~DatagramFace() |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 124 | { |
| 125 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 126 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 127 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 128 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 129 | DatagramFace<T, U>::sendInterest(const Interest& interest) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 130 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 131 | this->onSendInterest(interest); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 132 | m_socket->async_send(boost::asio::buffer(interest.wireEncode().wire(), |
| 133 | interest.wireEncode().size()), |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 134 | bind(&DatagramFace<T, U>::handleSend, this, _1, interest.wireEncode())); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 135 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 136 | // anything else should be done here? |
| 137 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 138 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 139 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 140 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 141 | DatagramFace<T, U>::sendData(const Data& data) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 142 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 143 | this->onSendData(data); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 144 | m_socket->async_send(boost::asio::buffer(data.wireEncode().wire(), |
| 145 | data.wireEncode().size()), |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 146 | bind(&DatagramFace<T, U>::handleSend, this, _1, data.wireEncode())); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 147 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 148 | // anything else should be done here? |
| 149 | } |
| 150 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 151 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 152 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 153 | DatagramFace<T, U>::handleSend(const boost::system::error_code& error, |
| 154 | const Block& wire) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 155 | { |
| 156 | if (error != 0) { |
| 157 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 158 | return; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 159 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 160 | if (!m_socket->is_open()) |
| 161 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 162 | fail("Tunnel closed"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 163 | return; |
| 164 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 165 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 166 | NFD_LOG_WARN("[id:" << this->getId() |
| 167 | << ",endpoint:" << m_socket->local_endpoint() |
| 168 | << "] Send operation failed, closing socket: " |
| 169 | << error.category().message(error.value())); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 170 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 171 | closeSocket(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 172 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 173 | if (error == boost::asio::error::eof) |
| 174 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 175 | fail("Tunnel closed"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 179 | fail("Send operation failed, closing socket: " + |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 180 | error.category().message(error.value())); |
| 181 | } |
| 182 | return; |
| 183 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 184 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 185 | NFD_LOG_TRACE("[id:" << this->getId() |
| 186 | << ",endpoint:" << m_socket->local_endpoint() |
| 187 | << "] Successfully sent: " << wire.size() << " bytes"); |
| 188 | // do nothing (needed to retain validity of wire memory block |
| 189 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 190 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 191 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 192 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 193 | DatagramFace<T, U>::close() |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 194 | { |
| 195 | if (!m_socket->is_open()) |
| 196 | return; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 197 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 198 | NFD_LOG_INFO("[id:" << this->getId() |
| 199 | << ",endpoint:" << m_socket->local_endpoint() |
| 200 | << "] Close tunnel"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 201 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 202 | closeSocket(); |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 203 | fail("Close tunnel"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 206 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 207 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 208 | DatagramFace<T, U>::handleReceive(const boost::system::error_code& error, |
| 209 | size_t nBytesReceived) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 210 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 211 | NFD_LOG_DEBUG("handleReceive: " << nBytesReceived); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 212 | receiveDatagram(m_inputBuffer, nBytesReceived, error); |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 213 | if (m_socket->is_open()) |
| 214 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 215 | bind(&DatagramFace<T, U>::handleReceive, this, _1, _2)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 218 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 219 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 220 | DatagramFace<T, U>::receiveDatagram(const uint8_t* buffer, |
| 221 | size_t nBytesReceived, |
| 222 | const boost::system::error_code& error) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 223 | { |
| 224 | if (error != 0 || nBytesReceived == 0) { |
| 225 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 226 | return; |
| 227 | |
| 228 | // this should be unnecessary, but just in case |
| 229 | if (!m_socket->is_open()) |
| 230 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 231 | fail("Tunnel closed"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | NFD_LOG_WARN("[id:" << this->getId() |
| 236 | << ",endpoint:" << m_socket->local_endpoint() |
| 237 | << "] Receive operation failed: " |
| 238 | << error.category().message(error.value())); |
| 239 | |
| 240 | closeSocket(); |
| 241 | |
| 242 | if (error == boost::asio::error::eof) |
| 243 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 244 | fail("Tunnel closed"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 245 | } |
| 246 | else |
| 247 | { |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 248 | fail("Receive operation failed, closing socket: " + |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 249 | error.category().message(error.value())); |
| 250 | } |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | NFD_LOG_TRACE("[id:" << this->getId() |
| 255 | << ",endpoint:" << m_socket->local_endpoint() |
| 256 | << "] Received: " << nBytesReceived << " bytes"); |
| 257 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 258 | Block element; |
| 259 | bool isOk = Block::fromBuffer(buffer, nBytesReceived, element); |
| 260 | if (!isOk) |
| 261 | { |
| 262 | NFD_LOG_WARN("[id:" << this->getId() |
| 263 | << ",endpoint:" << m_socket->local_endpoint() |
| 264 | << "] Failed to parse incoming packet"); |
| 265 | // This message won't extend the face lifetime |
| 266 | return; |
| 267 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 268 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 269 | if (element.size() != nBytesReceived) |
| 270 | { |
| 271 | NFD_LOG_WARN("[id:" << this->getId() |
| 272 | << ",endpoint:" << m_socket->local_endpoint() |
| 273 | << "] Received datagram size and decoded " |
| 274 | << "element size don't match"); |
| 275 | // This message won't extend the face lifetime |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | if (!this->decodeAndDispatchInput(element)) |
| 280 | { |
| 281 | NFD_LOG_WARN("[id:" << this->getId() |
| 282 | << ",endpoint:" << m_socket->local_endpoint() |
| 283 | << "] Received unrecognized block of type [" |
| 284 | << element.type() << "]"); |
| 285 | // This message won't extend the face lifetime |
| 286 | return; |
| 287 | } |
| 288 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 289 | m_hasBeenUsedRecently = true; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 293 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 294 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 295 | DatagramFace<T, U>::keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 296 | { |
| 297 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 298 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 299 | template<class T, class U> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 300 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 301 | DatagramFace<T, U>::closeSocket() |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 302 | { |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 303 | NFD_LOG_DEBUG("closeSocket " << m_socket->local_endpoint()); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 304 | boost::asio::io_service& io = m_socket->get_io_service(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 305 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 306 | // use the non-throwing variants and ignore errors, if any |
| 307 | boost::system::error_code error; |
| 308 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 309 | m_socket->close(error); |
| 310 | // after this, handlers will be called with an error code |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 311 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 312 | // ensure that the Face object is alive at least until all pending |
| 313 | // handlers are dispatched |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 314 | io.post(bind(&DatagramFace<T, U>::keepFaceAliveUntilAllHandlersExecuted, |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 315 | this, this->shared_from_this())); |
| 316 | } |
| 317 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 318 | template<class T, class U> |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 319 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 320 | DatagramFace<T, U>::setOnDemand(bool isOnDemand) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 321 | { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 322 | Face::setOnDemand(isOnDemand); |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 325 | template<class T, class U> |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 326 | inline void |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 327 | DatagramFace<T, U>::resetRecentUsage() |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 328 | { |
| 329 | m_hasBeenUsedRecently = false; |
| 330 | } |
| 331 | |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 332 | template<class T, class U> |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 333 | inline bool |
Alexander Afanasyev | cd591e1 | 2014-06-18 16:35:16 -0700 | [diff] [blame] | 334 | DatagramFace<T, U>::hasBeenUsedRecently() const |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 335 | { |
| 336 | return m_hasBeenUsedRecently; |
| 337 | } |
| 338 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 339 | } // namespace nfd |
| 340 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 341 | #endif // NFD_DAEMON_FACE_DATAGRAM_FACE_HPP |