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 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame^] | 20 | StreamFace(const shared_ptr<typename protocol::socket>& socket); |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 21 | |
| 22 | protected: |
| 23 | void |
| 24 | handleSend(const boost::system::error_code& error, |
| 25 | const Block& wire); |
| 26 | |
| 27 | void |
| 28 | handleReceive(const boost::system::error_code& error, |
| 29 | std::size_t bytes_recvd); |
| 30 | |
| 31 | protected: |
| 32 | shared_ptr<typename protocol::socket> m_socket; |
| 33 | |
| 34 | private: |
| 35 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
| 36 | std::size_t m_inputBufferSize; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 39 | template <class T> |
| 40 | inline |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame^] | 41 | StreamFace<T>::StreamFace(const shared_ptr<typename StreamFace::protocol::socket>& socket) |
| 42 | : m_socket(socket) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 43 | { |
| 44 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
| 45 | bind(&StreamFace<T>::handleReceive, this, _1, _2)); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | template <class T> |
| 50 | inline void |
| 51 | StreamFace<T>::handleSend(const boost::system::error_code& error, |
| 52 | const Block& wire) |
| 53 | { |
| 54 | if (error) { |
| 55 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 56 | return; |
| 57 | |
| 58 | onFail("Send operation failed: " + error.category().message(error.value())); |
| 59 | m_socket->close(); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // do nothing (needed to retain validity of wire memory block |
| 64 | } |
| 65 | |
| 66 | template <class T> |
| 67 | inline void |
| 68 | StreamFace<T>::handleReceive(const boost::system::error_code& error, |
| 69 | std::size_t bytes_recvd) |
| 70 | { |
| 71 | if (error || bytes_recvd == 0) { |
| 72 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 73 | return; |
| 74 | |
| 75 | onFail("Receive operation failed: " + error.category().message(error.value())); |
| 76 | m_socket->close(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | m_inputBufferSize += bytes_recvd; |
| 81 | // do magic |
| 82 | |
| 83 | std::size_t offset = 0; |
| 84 | /// @todo Eliminate reliance on exceptions in this path |
| 85 | try { |
| 86 | Block element(m_inputBuffer + offset, m_inputBufferSize - offset); |
| 87 | offset += element.size(); |
| 88 | |
| 89 | /// @todo Ensure lazy field decoding process |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 90 | if (element.type() == tlv::Interest) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 91 | { |
| 92 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 93 | i->wireDecode(element); |
| 94 | onReceiveInterest(*i); |
| 95 | } |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 96 | else if (element.type() == tlv::Data) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 97 | { |
| 98 | shared_ptr<Data> d = make_shared<Data>(); |
| 99 | d->wireDecode(element); |
| 100 | onReceiveData(*d); |
| 101 | } |
| 102 | // @todo Add local header support |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 103 | // else if (element.type() == tlv::LocalHeader) |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 104 | // { |
| 105 | // shared_ptr<Interest> i = make_shared<Interest>(); |
| 106 | // i->wireDecode(element); |
| 107 | // } |
| 108 | else |
| 109 | { |
| 110 | /// @todo Add loggin |
| 111 | |
| 112 | // ignore unknown packet and proceed |
| 113 | } |
| 114 | } |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 115 | catch(const tlv::Error&) { |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 116 | if (m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0) |
| 117 | { |
| 118 | onFail("Received input is invalid or too large to process, closing down the face"); |
| 119 | m_socket->close(); |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (offset > 0) |
| 125 | { |
| 126 | if (offset != m_inputBufferSize) |
| 127 | { |
| 128 | std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, |
| 129 | m_inputBuffer); |
| 130 | m_inputBufferSize -= offset; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | m_inputBufferSize = 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize, |
| 139 | MAX_NDN_PACKET_SIZE - m_inputBufferSize), 0, |
| 140 | bind(&StreamFace<T>::handleReceive, this, _1, _2)); |
| 141 | } |
| 142 | |
| 143 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 144 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 145 | |
| 146 | #endif // NFD_FACE_STREAM_FACE_HPP |