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)
120 boost::system::error_code error;
122 m_socket.shutdown(protocol::socket::shutdown_both, error);
151 boost::system::error_code error;
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));
193 BOOST_ASSERT(!m_sendQueue.empty());
196 if (!m_sendQueue.empty())
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)
223 m_receiveBufferSize += nBytesReceived;
226 while (m_receiveBufferSize - offset > 0) {
228 std::tie(isOk, element) =
Block::fromBuffer(m_receiveBuffer + offset, m_receiveBufferSize - offset);
232 offset += element.
size();
233 BOOST_ASSERT(offset <= m_receiveBufferSize);
246 if (offset != m_receiveBufferSize) {
247 std::copy(m_receiveBuffer + offset, m_receiveBuffer + m_receiveBufferSize, m_receiveBuffer);
248 m_receiveBufferSize -= offset;
251 m_receiveBufferSize = 0;
268 error == boost::asio::error::shut_down)
293 m_receiveBufferSize = 0;
300 std::queue<Block> emptyQueue;
301 std::swap(emptyQueue, m_sendQueue);
307 #endif // NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP void doClose() override
performs Transport specific operations to close the transport
void processErrorCode(const boost::system::error_code &error)
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
virtual void handleError(const boost::system::error_code &error)
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
stores a packet along with the remote endpoint
detail::SimulatorIo & getGlobalIoService()
Represents a TLV element of NDN packet format.
Implements Transport for stream-based protocols.
protocol::socket m_socket
the transport is being closed due to a failure
NFD_LOG_INCLASS_DECLARE()
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Copyright (c) 2011-2015 Regents of the University of California.
the transport is closed, and can be safely deallocated
void post(const std::function< void()> &callback)
StreamTransport(typename protocol::socket &&socket)
Construct stream transport.
void doSend(Transport::Packet &&packet) override
performs Transport specific operations to send a packet
the transport is being closed gracefully, either by the peer or by a call to close() ...
void handleReceive(const boost::system::error_code &error, size_t nBytesReceived)
void resetReceiveBuffer()
void handleSend(const boost::system::error_code &error, size_t nBytesSent)
TransportState getState() const
void setState(TransportState newState)
set transport state
Catch-all error for socket component errors that don't fit in other categories.
the transport is up and can transmit packets
void receive(Packet &&packet)
receive a link-layer packet
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size