26 #ifndef NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP 27 #define NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP 41 template<
class Protocol>
68 handleSend(
const boost::system::error_code& error,
76 size_t nBytesReceived);
82 handleError(
const boost::system::error_code& error);
97 size_t m_receiveBufferSize;
98 std::queue<Block> m_sendQueue;
105 , m_receiveBufferSize(0)
116 if (m_socket.is_open()) {
120 boost::system::error_code error;
121 m_socket.cancel(error);
122 m_socket.shutdown(protocol::socket::shutdown_both, error);
151 boost::system::error_code error;
152 m_socket.close(error);
154 this->setState(TransportState::CLOSED);
163 if (getState() != TransportState::UP)
166 bool wasQueueEmpty = m_sendQueue.empty();
167 m_sendQueue.push(packet.packet);
177 boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()),
179 boost::asio::placeholders::error,
180 boost::asio::placeholders::bytes_transferred));
189 return processErrorCode(error);
193 BOOST_ASSERT(!m_sendQueue.empty());
196 if (!m_sendQueue.empty())
204 BOOST_ASSERT(getState() == TransportState::UP);
206 m_socket.async_receive(boost::asio::buffer(m_receiveBuffer + m_receiveBufferSize,
209 boost::asio::placeholders::error,
210 boost::asio::placeholders::bytes_transferred));
216 size_t nBytesReceived)
219 return processErrorCode(error);
223 m_receiveBufferSize += nBytesReceived;
229 while (m_receiveBufferSize - offset > 0) {
230 std::tie(isOk, element) = Block::fromBuffer(m_receiveBuffer + offset, m_receiveBufferSize - offset);
234 offset += element.
size();
235 BOOST_ASSERT(offset <= m_receiveBufferSize);
241 NFD_LOG_FACE_WARN(
"Failed to parse incoming packet or packet too large to process");
242 this->setState(TransportState::FAILED);
248 if (offset != m_receiveBufferSize) {
249 std::copy(m_receiveBuffer + offset, m_receiveBuffer + m_receiveBufferSize, m_receiveBuffer);
250 m_receiveBufferSize -= offset;
253 m_receiveBufferSize = 0;
266 if (getState() == TransportState::CLOSING ||
267 getState() == TransportState::FAILED ||
268 getState() == TransportState::CLOSED ||
269 error == boost::asio::error::operation_aborted ||
270 error == boost::asio::error::shut_down)
281 if (error != boost::asio::error::eof)
284 this->setState(TransportState::FAILED);
292 m_receiveBufferSize = 0;
299 std::queue<Block> emptyQueue;
300 std::swap(emptyQueue, m_sendQueue);
306 #endif // NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP virtual void doClose() override
performs Transport specific operations to close the transport
void processErrorCode(const boost::system::error_code &error)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
virtual void handleError(const boost::system::error_code &error)
stores a packet along with the remote endpoint
detail::SimulatorIo & getGlobalIoService()
Class representing a wire element of NDN-TLV packet format.
Implements Transport for stream-based protocols.
protocol::socket m_socket
NFD_LOG_INCLASS_DECLARE()
Copyright (c) 2011-2015 Regents of the University of California.
void post(const std::function< void()> &callback)
StreamTransport(typename protocol::socket &&socket)
Construct stream transport.
virtual void doSend(Transport::Packet &&packet) override
performs Transport specific operations to send a packet
void handleReceive(const boost::system::error_code &error, size_t nBytesReceived)
void resetReceiveBuffer()
void handleSend(const boost::system::error_code &error, size_t nBytesSent)
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size