Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "ndnlp-slicer.hpp" |
| 8 | |
| 9 | #include <ndn-cpp-dev/encoding/encoding-buffer.hpp> |
| 10 | |
| 11 | namespace nfd { |
| 12 | namespace ndnlp { |
| 13 | |
| 14 | Slicer::Slicer(size_t mtu) |
| 15 | : m_mtu(mtu) |
| 16 | { |
| 17 | this->estimateOverhead(); |
| 18 | } |
| 19 | |
| 20 | Slicer::~Slicer() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | template<bool T> |
| 25 | static inline size_t |
| 26 | Slicer_encodeFragment(ndn::EncodingImpl<T>& blk, |
| 27 | uint64_t seq, uint16_t fragIndex, uint16_t fragCount, |
| 28 | const uint8_t* payload, size_t payloadSize) |
| 29 | { |
| 30 | size_t totalLength = 0; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 32 | // NdnlpPayload |
| 33 | size_t payloadLength = blk.prependByteArray(payload, payloadSize); |
| 34 | totalLength += payloadLength; |
| 35 | totalLength += blk.prependVarNumber(payloadLength); |
| 36 | totalLength += blk.prependVarNumber(tlv::NdnlpPayload); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 38 | bool needFragIndexAndCount = fragCount > 1; |
| 39 | if (needFragIndexAndCount) { |
| 40 | // NdnlpFragCount |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 41 | size_t fragCountLength = blk.prependNonNegativeInteger(fragCount); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 42 | totalLength += fragCountLength; |
| 43 | totalLength += blk.prependVarNumber(fragCountLength); |
| 44 | totalLength += blk.prependVarNumber(tlv::NdnlpFragCount); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 46 | // NdnlpFragIndex |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 47 | size_t fragIndexLength = blk.prependNonNegativeInteger(fragIndex); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 48 | totalLength += fragIndexLength; |
| 49 | totalLength += blk.prependVarNumber(fragIndexLength); |
| 50 | totalLength += blk.prependVarNumber(tlv::NdnlpFragIndex); |
| 51 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 53 | // NdnlpSequence |
| 54 | uint64_t sequenceBE = htobe64(seq); |
| 55 | size_t sequenceLength = blk.prependByteArray( |
| 56 | reinterpret_cast<uint8_t*>(&sequenceBE), sizeof(sequenceBE)); |
| 57 | totalLength += sequenceLength; |
| 58 | totalLength += blk.prependVarNumber(sequenceLength); |
| 59 | totalLength += blk.prependVarNumber(tlv::NdnlpSequence); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 60 | |
| 61 | // NdnlpData |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 62 | totalLength += blk.prependVarNumber(totalLength); |
| 63 | totalLength += blk.prependVarNumber(tlv::NdnlpData); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 65 | return totalLength; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | Slicer::estimateOverhead() |
| 70 | { |
| 71 | ndn::EncodingEstimator estimator; |
| 72 | size_t estimatedSize = Slicer_encodeFragment(estimator, |
| 73 | 0, 0, 2, 0, m_mtu); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 75 | size_t overhead = estimatedSize - m_mtu; |
| 76 | m_maxPayload = m_mtu - overhead; |
| 77 | } |
| 78 | |
| 79 | PacketArray |
| 80 | Slicer::slice(const Block& block) |
| 81 | { |
| 82 | BOOST_ASSERT(block.hasWire()); |
| 83 | const uint8_t* networkPacket = block.wire(); |
| 84 | size_t networkPacketSize = block.size(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 86 | uint16_t fragCount = static_cast<uint16_t>( |
| 87 | (networkPacketSize / m_maxPayload) + |
| 88 | (networkPacketSize % m_maxPayload == 0 ? 0 : 1) |
| 89 | ); |
| 90 | PacketArray pa = make_shared<std::vector<Block> >(); |
| 91 | pa->reserve(fragCount); |
| 92 | SequenceBlock seqBlock = m_seqgen.nextBlock(fragCount); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 94 | for (uint16_t fragIndex = 0; fragIndex < fragCount; ++fragIndex) { |
| 95 | size_t payloadOffset = fragIndex * m_maxPayload; |
| 96 | const uint8_t* payload = networkPacket + payloadOffset; |
| 97 | size_t payloadSize = std::min(m_maxPayload, networkPacketSize - payloadOffset); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 99 | ndn::EncodingBuffer buffer(m_mtu, 0); |
| 100 | size_t pktSize = Slicer_encodeFragment(buffer, |
| 101 | seqBlock[fragIndex], fragIndex, fragCount, payload, payloadSize); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 103 | BOOST_ASSERT(pktSize <= m_mtu); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 104 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 105 | pa->push_back(buffer.block()); |
| 106 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 108 | return pa; |
| 109 | } |
| 110 | |
| 111 | } // namespace ndnlp |
| 112 | } // namespace nfd |