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); |
| 49 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 50 | protected: |
| 51 | shared_ptr<typename protocol::socket> m_socket; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 52 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 53 | private: |
| 54 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
| 55 | std::size_t m_inputBufferSize; |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 57 | #ifdef _DEBUG |
| 58 | typename protocol::endpoint m_localEndpoint; |
| 59 | #endif |
| 60 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 61 | NFD_LOG_INCLASS_DECLARE(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 64 | // All inherited classes must use |
| 65 | // NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(StreamFace, <specialization-parameter>, "Name"); |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 67 | template <class T> |
| 68 | inline |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 69 | StreamFace<T>::StreamFace(const shared_ptr<typename StreamFace::protocol::socket>& socket) |
| 70 | : m_socket(socket) |
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"); |
| 115 | |
| 116 | |
| 117 | boost::asio::io_service& io = m_socket->get_io_service(); |
| 118 | m_socket->close(); |
| 119 | // after this, handleSend/handleReceive will be called with set error code |
| 120 | |
| 121 | // ensure that if that Face object is alive at least until all pending |
| 122 | // methods are dispatched |
| 123 | io.post(bind(&StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted, |
| 124 | this, this->shared_from_this())); |
| 125 | |
| 126 | onFail("Close connection"); |
| 127 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 128 | |
| 129 | template <class T> |
| 130 | inline void |
| 131 | StreamFace<T>::handleSend(const boost::system::error_code& error, |
| 132 | const Block& wire) |
| 133 | { |
| 134 | if (error) { |
| 135 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 136 | return; |
| 137 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 138 | if (!m_socket->is_open()) |
| 139 | { |
| 140 | onFail("Connection closed"); |
| 141 | return; |
| 142 | } |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 144 | if (error == boost::asio::error::eof) |
| 145 | { |
| 146 | NFD_LOG_INFO("[id:" << this->getId() |
| 147 | << ",endpoint:" << m_socket->local_endpoint() |
| 148 | << "] Connection closed"); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | NFD_LOG_WARN("[id:" << this->getId() |
| 153 | << ",endpoint:" << m_socket->local_endpoint() |
| 154 | << "] Send operation failed, closing socket: " |
| 155 | << error.category().message(error.value())); |
| 156 | } |
| 157 | |
| 158 | boost::asio::io_service& io = m_socket->get_io_service(); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 159 | m_socket->close(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 160 | |
| 161 | // ensure that if that Face object is alive at least until all pending |
| 162 | // methods are dispatched |
| 163 | io.post(bind(&StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted, |
| 164 | this, this->shared_from_this())); |
| 165 | |
| 166 | if (error == boost::asio::error::eof) |
| 167 | { |
| 168 | onFail("Connection closed"); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | onFail("Send operation failed, closing socket: " + |
| 173 | error.category().message(error.value())); |
| 174 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 178 | NFD_LOG_TRACE("[id:" << this->getId() |
| 179 | << ",endpoint:" << m_socket->local_endpoint() |
| 180 | << "] Successfully sent: " << wire.size() << " bytes"); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 181 | // do nothing (needed to retain validity of wire memory block |
| 182 | } |
| 183 | |
| 184 | template <class T> |
| 185 | inline void |
| 186 | StreamFace<T>::handleReceive(const boost::system::error_code& error, |
| 187 | std::size_t bytes_recvd) |
| 188 | { |
| 189 | if (error || bytes_recvd == 0) { |
| 190 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 191 | return; |
| 192 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 193 | // this should be unnecessary, but just in case |
| 194 | if (!m_socket->is_open()) |
| 195 | { |
| 196 | onFail("Connection closed"); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | if (error == boost::asio::error::eof) |
| 201 | { |
| 202 | NFD_LOG_INFO("[id:" << this->getId() |
| 203 | << ",endpoint:" << m_socket->local_endpoint() |
| 204 | << "] Connection closed"); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | NFD_LOG_WARN("[id:" << this->getId() |
| 209 | << ",endpoint:" << m_socket->local_endpoint() |
| 210 | << "] Receive operation failed: " |
| 211 | << error.category().message(error.value())); |
| 212 | } |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 214 | boost::asio::io_service& io = m_socket->get_io_service(); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 215 | m_socket->close(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 216 | |
| 217 | // ensure that if that Face object is alive at least until all pending |
| 218 | // methods are dispatched |
| 219 | io.post(bind(&StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted, |
| 220 | this, this->shared_from_this())); |
| 221 | |
| 222 | if (error == boost::asio::error::eof) |
| 223 | { |
| 224 | onFail("Connection closed"); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | onFail("Receive operation failed, closing socket: " + |
| 229 | error.category().message(error.value())); |
| 230 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 234 | NFD_LOG_TRACE("[id:" << this->getId() |
| 235 | << ",endpoint:" << m_socket->local_endpoint() |
| 236 | << "] Received: " << bytes_recvd << " bytes"); |
| 237 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 238 | m_inputBufferSize += bytes_recvd; |
| 239 | // do magic |
| 240 | |
| 241 | std::size_t offset = 0; |
| 242 | /// @todo Eliminate reliance on exceptions in this path |
| 243 | try { |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 244 | while(m_inputBufferSize - offset > 0) |
| 245 | { |
| 246 | Block element(m_inputBuffer + offset, m_inputBufferSize - offset); |
| 247 | offset += element.size(); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 248 | |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 249 | BOOST_ASSERT(offset <= m_inputBufferSize); |
| 250 | |
| 251 | /// @todo Ensure lazy field decoding process |
| 252 | if (element.type() == tlv::Interest) |
| 253 | { |
| 254 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 255 | i->wireDecode(element); |
| 256 | onReceiveInterest(*i); |
| 257 | } |
| 258 | else if (element.type() == tlv::Data) |
| 259 | { |
| 260 | shared_ptr<Data> d = make_shared<Data>(); |
| 261 | d->wireDecode(element); |
| 262 | onReceiveData(*d); |
| 263 | } |
| 264 | // @todo Add local header support |
| 265 | // else if (element.type() == tlv::LocalHeader) |
| 266 | // { |
| 267 | // shared_ptr<Interest> i = make_shared<Interest>(); |
| 268 | // i->wireDecode(element); |
| 269 | // } |
| 270 | else |
| 271 | { |
| 272 | NFD_LOG_WARN("[id:" << this->getId() |
| 273 | << ",endpoint:" << m_socket->local_endpoint() |
| 274 | << "] Received unrecognized block of type [" |
| 275 | << element.type() << "]"); |
| 276 | // ignore unknown packet and proceed |
| 277 | } |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 280 | catch(const tlv::Error& e) { |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 281 | if (m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
| 282 | { |
Alexander Afanasyev | 3958b01 | 2014-01-31 15:06:13 -0800 | [diff] [blame] | 283 | NFD_LOG_WARN("[id:" << this->getId() |
| 284 | << ",endpoint:" << m_socket->local_endpoint() |
| 285 | << "] Received input is invalid or too large to process, " |
| 286 | << "closing down the face"); |
| 287 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 288 | boost::asio::io_service& io = m_socket->get_io_service(); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 289 | m_socket->close(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 290 | |
| 291 | // ensure that if that Face object is alive at least until all pending |
| 292 | // methods are dispatched |
| 293 | io.post(bind(&StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted, |
| 294 | this, this->shared_from_this())); |
| 295 | |
| 296 | 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] | 297 | return; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (offset > 0) |
| 302 | { |
| 303 | if (offset != m_inputBufferSize) |
| 304 | { |
| 305 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 306 | m_inputBuffer); |
| 307 | m_inputBufferSize -= offset; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | m_inputBufferSize = 0; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 316 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 317 | bind(&StreamFace<T>::handleReceive, this, _1, _2)); |
| 318 | } |
| 319 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame^] | 320 | template <class T> |
| 321 | inline void |
| 322 | StreamFace<T>::keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face) |
| 323 | { |
| 324 | } |
| 325 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 326 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 327 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 328 | |
| 329 | #endif // NFD_FACE_STREAM_FACE_HPP |