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;
157 [
this,
self = this->shared_from_this()] (
const auto& error,
size_t) {
159 if (error == boost::system::errc::operation_canceled) {
164 NDN_THROW(Transport::Error(error,
"error while writing data to socket"));
183 m_socket.async_receive(boost::asio::buffer(m_inputBuffer + m_inputBufferSize,
186 [
this,
self = this->shared_from_this()] (
const auto& error,
size_t nBytesRecvd) {
188 if (error == boost::system::errc::operation_canceled) {
196 m_inputBufferSize += nBytesRecvd;
199 std::size_t offset = 0;
200 bool hasProcessedSome = processAllReceived(m_inputBuffer, offset, m_inputBufferSize);
207 if (offset != m_inputBufferSize) {
208 std::copy(m_inputBuffer + offset, m_inputBuffer + m_inputBufferSize, m_inputBuffer);
209 m_inputBufferSize -= offset;
212 m_inputBufferSize = 0;
223 while (offset < nBytesAvailable) {
226 std::tie(isOk, element) =
Block::fromBuffer({buffer + offset, nBytesAvailable - offset});
230 m_transport.m_receiveCallback(element);
231 offset += element.
size();
241 size_t m_inputBufferSize = 0;
Represents a TLV element of the NDN packet format.
size_t size() const
Return the size of the encoded wire, i.e., of the whole TLV.
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.