37 , reassemblyTimeout(time::milliseconds(500))
43 , m_linkService(linkService)
47 std::tuple<bool, Block, lp::Packet>
50 BOOST_ASSERT(packet.has<lp::FragmentField>());
52 static auto FALSE_RETURN = std::make_tuple(
false, Block(), lp::Packet());
55 uint64_t fragIndex = 0;
56 uint64_t fragCount = 1;
57 if (packet.has<lp::FragIndexField>()) {
58 fragIndex = packet.get<lp::FragIndexField>();
60 if (packet.has<lp::FragCountField>()) {
61 fragCount = packet.get<lp::FragCountField>();
64 if (fragIndex >= fragCount) {
75 if (fragIndex == 0 && fragCount == 1) {
76 ndn::Buffer::const_iterator fragBegin, fragEnd;
77 std::tie(fragBegin, fragEnd) = packet.get<lp::FragmentField>();
78 Block netPkt(&*fragBegin, std::distance(fragBegin, fragEnd));
79 return std::make_tuple(
true, netPkt, packet);
83 if (!packet.has<lp::SequenceField>()) {
87 lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
88 Key key = std::make_tuple(remoteEndpoint, messageIdentifier);
91 PartialPacket& pp = m_partialPackets[key];
92 if (pp.fragCount == 0) {
93 pp.fragCount = fragCount;
94 pp.nReceivedFragments = 0;
95 pp.fragments.resize(fragCount);
98 if (fragCount != pp.fragCount) {
104 if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
109 pp.fragments[fragIndex] = packet;
110 ++pp.nReceivedFragments;
113 if (pp.nReceivedFragments == pp.fragCount) {
114 Block reassembled = doReassembly(key);
115 lp::Packet firstFrag(std::move(pp.fragments[0]));
116 m_partialPackets.erase(key);
117 return std::make_tuple(
true, reassembled, firstFrag);
122 bind(&LpReassembler::timeoutPartialPacket,
this, key));
128 LpReassembler::doReassembly(
const Key& key)
130 PartialPacket& pp = m_partialPackets[key];
132 size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0,
133 [&] (
size_t sum,
const lp::Packet& pkt) ->
size_t {
134 ndn::Buffer::const_iterator fragBegin, fragEnd;
135 std::tie(fragBegin, fragEnd) = pkt.get<lp::FragmentField>();
136 return sum + std::distance(fragBegin, fragEnd);
139 ndn::Buffer fragBuffer(payloadSize);
142 for (
const lp::Packet& frag : pp.fragments) {
143 ndn::Buffer::const_iterator fragBegin, fragEnd;
144 std::tie(fragBegin, fragEnd) = frag.get<lp::FragmentField>();
145 it = std::copy(fragBegin, fragEnd, it);
148 return Block(&*(fragBuffer.cbegin()), std::distance(fragBuffer.cbegin(), fragBuffer.cend()));
152 LpReassembler::timeoutPartialPacket(
const Key& key)
154 auto it = m_partialPackets.find(key);
155 if (it == m_partialPackets.end()) {
159 this->
beforeTimeout(std::get<0>(key), it->second.nReceivedFragments);
160 m_partialPackets.erase(it);
164 operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh)
166 if (flh.obj.getLinkService() ==
nullptr) {
167 os <<
"[id=0,local=unknown,remote=unknown] ";
170 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
time::nanoseconds reassemblyTimeout
timeout before a partially reassembled packet is dropped
LpReassembler(const Options &options=Options(), const LinkService *linkService=nullptr)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
size_t nMaxFragments
maximum number of fragments in a packet
Table::const_iterator iterator
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Options that control the behavior of LpReassembler.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
signal::Signal< LpReassembler, Transport::EndpointId, size_t > beforeTimeout
signals before a partial packet is dropped due to timeout
uint64_t EndpointId
identifies an endpoint on the link
#define NFD_LOG_INIT(name)
std::tuple< bool, Block, lp::Packet > receiveFragment(Transport::EndpointId remoteEndpoint, const lp::Packet &packet)
adds received fragment to buffer
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event