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