22 #ifndef NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_IMPL_HPP
23 #define NDN_CXX_TRANSPORT_DETAIL_STREAM_TRANSPORT_IMPL_HPP
27 #include <boost/asio/steady_timer.hpp>
28 #include <boost/asio/write.hpp>
41 template<
typename BaseTransport,
typename Protocol>
42 class StreamTransportImpl :
public std::enable_shared_from_this<StreamTransportImpl<BaseTransport, Protocol>>
56 connect(
const typename Protocol::endpoint& endpoint)
66 m_connectTimer.async_wait([
self = this->shared_from_this()] (
const auto& error) {
67 self->connectTimeoutHandler(error);
71 m_socket.async_connect(endpoint, [
self = this->shared_from_this()] (
const auto& error) {
72 self->connectHandler(error);
81 boost::system::error_code error;
165 [
this,
self = this->shared_from_this()] (
const auto& error,
size_t) {
167 if (error == boost::system::errc::operation_canceled) {
172 NDN_THROW(Transport::Error(error,
"error while writing data to socket"));
191 m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize,
194 [
this,
self = this->shared_from_this()] (
const auto& error,
size_t nBytesRecvd) {
196 if (error == boost::system::errc::operation_canceled) {
204 m_inputBufferSize += nBytesRecvd;
207 std::size_t offset = 0;
208 bool hasProcessedSome = processAllReceived(m_inputBuffer, offset, m_inputBufferSize);
215 if (offset != m_inputBufferSize) {
216 std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, m_inputBuffer);
217 m_inputBufferSize -= offset;
220 m_inputBufferSize = 0;
231 while (offset < nBytesAvailable) {
234 std::tie(isOk, element) =
Block::fromBuffer({buffer + offset, nBytesAvailable - offset});
238 m_transport.m_receiveCallback(element);
239 offset += element.
size();
249 size_t m_inputBufferSize = 0;
252 bool m_isConnecting =
false;
Represents a TLV element of the NDN packet format.
size_t size() const
Return the size of the encoded wire, i.e.
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset=0)
Try to parse Block from a wire buffer.
Implementation detail of a Boost.Asio-based stream-oriented transport.
Protocol::socket m_socket
bool processAllReceived(uint8_t *buffer, size_t &offset, size_t nBytesAvailable)
TransmissionQueue m_transmissionQueue
void send(const Block &block)
boost::asio::steady_timer m_connectTimer
std::queue< Block, std::list< Block > > TransmissionQueue
void connect(const typename Protocol::endpoint &endpoint)
StreamTransportImpl(BaseTransport &transport, boost::asio::io_service &ioService)
void connectHandler(const boost::system::error_code &error)
void connectTimeoutHandler(const boost::system::error_code &error)
BaseTransport & m_transport
boost::chrono::seconds seconds
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.