28 #include <ndn-cxx/encoding/encoding-buffer.hpp> 36 this->estimateOverhead();
45 Slicer::encodeFragment(ndn::EncodingImpl<T>& blk,
46 uint64_t seq, uint16_t fragIndex, uint16_t fragCount,
47 const uint8_t* payload,
size_t payloadSize)
49 size_t totalLength = 0;
52 size_t payloadLength = blk.prependByteArray(payload, payloadSize);
53 totalLength += payloadLength;
54 totalLength += blk.prependVarNumber(payloadLength);
57 bool needFragIndexAndCount = fragCount > 1;
58 if (needFragIndexAndCount) {
60 size_t fragCountLength = blk.prependNonNegativeInteger(fragCount);
61 totalLength += fragCountLength;
62 totalLength += blk.prependVarNumber(fragCountLength);
66 size_t fragIndexLength = blk.prependNonNegativeInteger(fragIndex);
67 totalLength += fragIndexLength;
68 totalLength += blk.prependVarNumber(fragIndexLength);
73 uint64_t sequenceBE = htobe64(seq);
74 size_t sequenceLength = blk.prependByteArray(
75 reinterpret_cast<uint8_t*>(&sequenceBE),
sizeof(sequenceBE));
76 totalLength += sequenceLength;
77 totalLength += blk.prependVarNumber(sequenceLength);
81 totalLength += blk.prependVarNumber(totalLength);
88 Slicer::estimateOverhead()
92 size_t estimatedSize = this->encodeFragment(estimator,
93 std::numeric_limits<uint64_t>::max(),
94 std::numeric_limits<uint16_t>::max() - 1,
95 std::numeric_limits<uint16_t>::max(),
98 size_t overhead = estimatedSize - m_mtu;
99 m_maxPayload = m_mtu - overhead;
106 const uint8_t* networkPacket = block.
wire();
107 size_t networkPacketSize = block.
size();
109 uint16_t fragCount =
static_cast<uint16_t
>(
110 (networkPacketSize / m_maxPayload) +
111 (networkPacketSize % m_maxPayload == 0 ? 0 : 1)
113 PacketArray pa = make_shared<std::vector<Block>>();
114 pa->reserve(fragCount);
117 for (uint16_t fragIndex = 0; fragIndex < fragCount; ++fragIndex) {
118 size_t payloadOffset = fragIndex * m_maxPayload;
119 const uint8_t* payload = networkPacket + payloadOffset;
120 size_t payloadSize = std::min(m_maxPayload, networkPacketSize - payloadOffset);
123 size_t pktSize = this->encodeFragment(buffer,
124 seqBlock[fragIndex], fragIndex, fragCount, payload, payloadSize);
126 BOOST_VERIFY(pktSize <= m_mtu);
128 pa->push_back(buffer.block());
PacketArray slice(const Block &block)
EncodingImpl< EstimatorTag > EncodingEstimator
Class representing a wire element of NDN-TLV packet format.
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
represents a block of sequence numbers
shared_ptr< std::vector< Block > > PacketArray
SequenceBlock nextBlock(size_t count)
generates a block of consecutive sequence numbers
const uint8_t * wire() const
bool hasWire() const
Check if the Block has fully encoded wire.