30 #include <ndn-cxx/lp/fields.hpp>
40 , m_linkService(linkService)
44 std::tuple<bool, Block, lp::Packet>
47 BOOST_ASSERT(packet.has<lp::FragmentField>());
50 uint64_t fragIndex = 0;
51 uint64_t fragCount = 1;
52 if (packet.has<lp::FragIndexField>()) {
53 fragIndex = packet.get<lp::FragIndexField>();
55 if (packet.has<lp::FragCountField>()) {
56 fragCount = packet.get<lp::FragCountField>();
59 if (fragIndex >= fragCount) {
61 return {
false, {}, {}};
66 return {
false, {}, {}};
70 if (fragIndex == 0 && fragCount == 1) {
71 auto frag = packet.get<lp::FragmentField>();
72 Block netPkt({frag.first, frag.second});
73 return {
true, netPkt, packet};
77 if (!packet.has<lp::SequenceField>()) {
79 return {
false, {}, {}};
82 lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
83 Key key(remoteEndpoint, messageIdentifier);
86 PartialPacket& pp = m_partialPackets[key];
87 if (pp.fragCount == 0) {
88 pp.fragCount = fragCount;
89 pp.nReceivedFragments = 0;
90 pp.fragments.resize(fragCount);
93 if (fragCount != pp.fragCount) {
95 return {
false, {}, {}};
99 if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
101 return {
false, {}, {}};
104 pp.fragments[fragIndex] = packet;
105 ++pp.nReceivedFragments;
108 if (pp.nReceivedFragments == pp.fragCount) {
109 Block reassembled = doReassembly(key);
110 lp::Packet firstFrag(std::move(pp.fragments[0]));
111 m_partialPackets.erase(key);
112 return {
true, reassembled, firstFrag};
118 return {
false, {}, {}};
122 LpReassembler::doReassembly(
const Key& key)
124 PartialPacket& pp = m_partialPackets[key];
126 size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U,
127 [&] (
size_t sum,
const lp::Packet& pkt) ->
size_t {
128 auto [fragBegin, fragEnd] = pkt.get<lp::FragmentField>();
129 return sum + std::distance(fragBegin, fragEnd);
132 ndn::Buffer fragBuffer(payloadSize);
133 auto it = fragBuffer.begin();
134 for (
const lp::Packet& frag : pp.fragments) {
135 auto [fragBegin, fragEnd] = frag.get<lp::FragmentField>();
136 it = std::copy(fragBegin, fragEnd, it);
138 return Block(fragBuffer);
142 LpReassembler::timeoutPartialPacket(
const Key& key)
144 auto it = m_partialPackets.find(key);
145 if (it == m_partialPackets.end()) {
149 this->
beforeTimeout(std::get<0>(key), it->second.nReceivedFragments);
150 m_partialPackets.erase(it);
156 if (flh.
obj.getLinkService() ==
nullptr) {
157 os <<
"[id=0,local=unknown,remote=unknown] ";
160 os << FaceLogHelper<LinkService>(*flh.
obj.getLinkService());
For internal use by FaceLogging macros.
The upper half of a Face.
std::tuple< bool, Block, lp::Packet > receiveFragment(const EndpointId &remoteEndpoint, const lp::Packet &packet)
Adds received fragment to the buffer.
signal::Signal< LpReassembler, EndpointId, size_t > beforeTimeout
Notifies before a partial packet is dropped due to timeout.
LpReassembler(const Options &options, const LinkService *linkService=nullptr)
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Options that control the behavior of LpReassembler.
time::nanoseconds reassemblyTimeout
Timeout before a partially reassembled packet is dropped.
size_t nMaxFragments
Maximum number of fragments in a packet.