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