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" |
| 11 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 12 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 13 | |
| 14 | template <class T> |
| 15 | class StreamFace : public Face |
| 16 | { |
| 17 | public: |
| 18 | typedef T protocol; |
| 19 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 20 | /** |
| 21 | * \brief Create instance of StreamFace |
| 22 | */ |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 23 | StreamFace(const shared_ptr<typename protocol::socket>& socket); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 25 | virtual |
| 26 | ~StreamFace(); |
| 27 | |
| 28 | // from Face |
| 29 | virtual void |
| 30 | sendInterest(const Interest& interest); |
| 31 | |
| 32 | virtual void |
| 33 | sendData(const Data& data); |
| 34 | |
| 35 | virtual void |
| 36 | close(); |
| 37 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 38 | protected: |
| 39 | void |
| 40 | handleSend(const boost::system::error_code& error, |
| 41 | const Block& wire); |
| 42 | |
| 43 | void |
| 44 | handleReceive(const boost::system::error_code& error, |
| 45 | std::size_t bytes_recvd); |
| 46 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 47 | void |
| 48 | keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 49 | |
| 50 | void |
| 51 | closeSocket(); |
| 52 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 53 | protected: |
| 54 | shared_ptr<typename protocol::socket> m_socket; |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 55 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 56 | private: |
| 57 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
| 58 | std::size_t m_inputBufferSize; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 60 | NFD_LOG_INCLASS_DECLARE(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 63 | // All inherited classes must use |
| 64 | // NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name"); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 66 | template <class T> |
| 67 | inline |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 68 | StreamFace<T>::StreamFace(const shared_ptr<typename StreamFace::protocol::socket>& socket) |
| 69 | : m_socket(socket) |
Alexander Afanasyev | b9f6e43 | 2014-02-14 20:52:49 -0800 | [diff] [blame] | 70 | , m_inputBufferSize(0) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 71 | { |
| 72 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
| 73 | bind(&StreamFace<T>::handleReceive, this, _1, _2)); |
| 74 | } |
| 75 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 76 | template <class T> |
| 77 | inline |
| 78 | StreamFace<T>::~StreamFace() |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | |
| 83 | template <class T> |
| 84 | inline void |
| 85 | StreamFace<T>::sendInterest(const Interest& interest) |
| 86 | { |
| 87 | m_socket->async_send(boost::asio::buffer(interest.wireEncode().wire(), |
| 88 | interest.wireEncode().size()), |
| 89 | bind(&StreamFace<T>::handleSend, this, _1, interest.wireEncode())); |
| 90 | |
| 91 | // anything else should be done here? |
| 92 | } |
| 93 | |
| 94 | template <class T> |
| 95 | inline void |
| 96 | StreamFace<T>::sendData(const Data& data) |
| 97 | { |
| 98 | m_socket->async_send(boost::asio::buffer(data.wireEncode().wire(), |
| 99 | data.wireEncode().size()), |
| 100 | bind(&StreamFace<T>::handleSend, this, _1, data.wireEncode())); |
| 101 | |
| 102 | // anything else should be done here? |
| 103 | } |
| 104 | |
| 105 | template <class T> |
| 106 | inline void |
| 107 | StreamFace<T>::close() |
| 108 | { |
| 109 | if (!m_socket->is_open()) |
| 110 | return; |
| 111 | |
| 112 | NFD_LOG_INFO("[id:" << this->getId() |
| 113 | << ",endpoint:" << m_socket->local_endpoint() |
| 114 | << "] Close connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 115 | |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 116 | closeSocket(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 117 | onFail("Close connection"); |
| 118 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 119 | |
| 120 | template <class T> |
| 121 | inline void |
| 122 | StreamFace<T>::handleSend(const boost::system::error_code& error, |
| 123 | const Block& wire) |
| 124 | { |
| 125 | if (error) { |
| 126 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 127 | return; |
| 128 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 129 | if (!m_socket->is_open()) |
| 130 | { |
| 131 | onFail("Connection closed"); |
| 132 | return; |
| 133 | } |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 135 | if (error == boost::asio::error::eof) |
| 136 | { |
| 137 | NFD_LOG_INFO("[id:" << this->getId() |
| 138 | << ",endpoint:" << m_socket->local_endpoint() |
| 139 | << "] Connection closed"); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | NFD_LOG_WARN("[id:" << this->getId() |
| 144 | << ",endpoint:" << m_socket->local_endpoint() |
| 145 | << "] Send operation failed, closing socket: " |
| 146 | << error.category().message(error.value())); |
| 147 | } |
| 148 | |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 149 | closeSocket(); |
| 150 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 151 | if (error == boost::asio::error::eof) |
| 152 | { |
| 153 | onFail("Connection closed"); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | onFail("Send operation failed, closing socket: " + |
| 158 | error.category().message(error.value())); |
| 159 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 163 | NFD_LOG_TRACE("[id:" << this->getId() |
| 164 | << ",endpoint:" << m_socket->local_endpoint() |
| 165 | << "] Successfully sent: " << wire.size() << " bytes"); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 166 | // do nothing (needed to retain validity of wire memory block |
| 167 | } |
| 168 | |
| 169 | template <class T> |
| 170 | inline void |
| 171 | StreamFace<T>::handleReceive(const boost::system::error_code& error, |
| 172 | std::size_t bytes_recvd) |
| 173 | { |
| 174 | if (error || bytes_recvd == 0) { |
| 175 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 176 | return; |
| 177 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 178 | // this should be unnecessary, but just in case |
| 179 | if (!m_socket->is_open()) |
| 180 | { |
| 181 | onFail("Connection closed"); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | if (error == boost::asio::error::eof) |
| 186 | { |
| 187 | NFD_LOG_INFO("[id:" << this->getId() |
| 188 | << ",endpoint:" << m_socket->local_endpoint() |
| 189 | << "] Connection closed"); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | NFD_LOG_WARN("[id:" << this->getId() |
| 194 | << ",endpoint:" << m_socket->local_endpoint() |
| 195 | << "] Receive operation failed: " |
| 196 | << error.category().message(error.value())); |
| 197 | } |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 198 | |
| 199 | closeSocket(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 200 | |
| 201 | if (error == boost::asio::error::eof) |
| 202 | { |
| 203 | onFail("Connection closed"); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | onFail("Receive operation failed, closing socket: " + |
| 208 | error.category().message(error.value())); |
| 209 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 213 | NFD_LOG_TRACE("[id:" << this->getId() |
| 214 | << ",endpoint:" << m_socket->local_endpoint() |
| 215 | << "] Received: " << bytes_recvd << " bytes"); |
| 216 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 217 | m_inputBufferSize += bytes_recvd; |
| 218 | // do magic |
| 219 | |
| 220 | std::size_t offset = 0; |
| 221 | /// @todo Eliminate reliance on exceptions in this path |
| 222 | try { |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 223 | while(m_inputBufferSize - offset > 0) |
| 224 | { |
| 225 | Block element(m_inputBuffer + offset, m_inputBufferSize - offset); |
| 226 | offset += element.size(); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 227 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 228 | BOOST_ASSERT(offset <= m_inputBufferSize); |
| 229 | |
| 230 | /// @todo Ensure lazy field decoding process |
| 231 | if (element.type() == tlv::Interest) |
| 232 | { |
| 233 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 234 | i->wireDecode(element); |
| 235 | onReceiveInterest(*i); |
| 236 | } |
| 237 | else if (element.type() == tlv::Data) |
| 238 | { |
| 239 | shared_ptr<Data> d = make_shared<Data>(); |
| 240 | d->wireDecode(element); |
| 241 | onReceiveData(*d); |
| 242 | } |
| 243 | // @todo Add local header support |
| 244 | // else if (element.type() == tlv::LocalHeader) |
| 245 | // { |
| 246 | // shared_ptr<Interest> i = make_shared<Interest>(); |
| 247 | // i->wireDecode(element); |
| 248 | // } |
| 249 | else |
| 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 |
| 256 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 259 | catch(const tlv::Error& e) { |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 260 | if (m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
| 261 | { |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 262 | NFD_LOG_WARN("[id:" << this->getId() |
| 263 | << ",endpoint:" << m_socket->local_endpoint() |
| 264 | << "] Received input is invalid or too large to process, " |
| 265 | << "closing down the face"); |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 266 | |
| 267 | closeSocket(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 268 | 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] | 269 | return; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (offset > 0) |
| 274 | { |
| 275 | if (offset != m_inputBufferSize) |
| 276 | { |
| 277 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 278 | m_inputBuffer); |
| 279 | m_inputBufferSize -= offset; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | m_inputBufferSize = 0; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 288 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 289 | bind(&StreamFace<T>::handleReceive, this, _1, _2)); |
| 290 | } |
| 291 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 292 | template <class T> |
| 293 | inline void |
| 294 | StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face) |
| 295 | { |
| 296 | } |
| 297 | |
Davide Pesavento | ba558e7 | 2014-02-17 18:38:19 +0100 | [diff] [blame] | 298 | template <class T> |
| 299 | inline void |
| 300 | StreamFace<T>::closeSocket() |
| 301 | { |
| 302 | boost::asio::io_service& io = m_socket->get_io_service(); |
| 303 | |
| 304 | // use the non-throwing variants and ignore errors, if any |
| 305 | boost::system::error_code error; |
| 306 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 307 | m_socket->close(error); |
| 308 | // after this, handlers will be called with an error code |
| 309 | |
| 310 | // ensure that the Face object is alive at least until all pending |
| 311 | // handlers are dispatched |
| 312 | io.post(bind(&StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted, |
| 313 | this, this->shared_from_this())); |
| 314 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 315 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 316 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 317 | |
| 318 | #endif // NFD_FACE_STREAM_FACE_HPP |